Initial commit: Momentry Core v0.1

- Rust-based digital asset management system
- Video analysis: ASR, OCR, YOLO, Face, Pose
- RAG capabilities with Qdrant vector database
- Multi-database support: PostgreSQL, Redis, MongoDB
- Monitoring system with launchd plists
- n8n workflow automation integration
This commit is contained in:
accusys
2026-03-16 15:07:33 +08:00
commit de14bd6afa
101 changed files with 19858 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
#!/bin/bash
# Momentry 外部監控 (Layer 1)
# 路徑: /Users/accusys/momentry_core_0.1/monitor/service/external_monitor.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOG_DIR="/Users/accusys/momentry/log/monitor"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/external_check.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 記錄結果
record_external() {
local target=$1
local target_type=$2
local reachable=$3
local response_time=$4
local error=$5
psql -U accusys -h localhost -d momentry << EOF 2>/dev/null
INSERT INTO monitor_external (target_name, target_type, is_reachable, response_time_ms, error_message, checked_at)
VALUES ('$target', '$target_type', $reachable, $response_time, '$error', NOW());
EOF
}
# 檢查 DDNS
check_ddns() {
local start=$(date +%s%N)
local ip=$(dig +short momentry.ddns.net 2>/dev/null | tail -1)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ -n "$ip" ]; then
echo "✓ DDNS (momentry.ddns.net) -> $ip (${ms}ms)"
record_external "ddns" "ddns" "true" "$ms" ""
return 0
else
echo "✗ DDNS (momentry.ddns.net) - DNS resolution failed"
record_external "ddns" "ddns" "false" "0" "DNS resolution failed"
return 1
fi
}
# 檢查網關
check_gateway() {
local start=$(date +%s%N)
if ping -c 1 -W 2 192.168.110.1 > /dev/null 2>&1; then
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
echo "✓ Gateway (192.168.110.1) - ${ms}ms"
record_external "gateway" "gateway" "true" "$ms" ""
return 0
else
echo "✗ Gateway (192.168.110.1) - Unreachable"
record_external "gateway" "gateway" "false" "0" "Unreachable"
return 1
fi
}
# 檢查互聯網
check_internet() {
local start=$(date +%s%N)
if ping -c 1 -W 2 8.8.8.8 > /dev/null 2>&1; then
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
echo "✓ Internet (8.8.8.8) - ${ms}ms"
record_external "internet" "internet" "true" "$ms" ""
return 0
else
echo "✗ Internet (8.8.8.8) - Unreachable"
record_external "internet" "internet" "false" "0" "Unreachable"
return 1
fi
}
# 主程序
echo "========================================"
echo "Layer 1: External Monitoring"
echo "Time: $(date)"
echo "========================================"
echo ""
check_ddns
check_gateway
check_internet
echo ""
echo "========================================"
log "External check completed"

370
monitor/service/health_check.sh Executable file
View File

@@ -0,0 +1,370 @@
#!/bin/bash
# Momentry 服務健康檢查 (Layer 2)
# 路徑: /Users/accusys/momentry_core_0.1/monitor/service/health_check.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MONITOR_DIR="$(dirname "$SCRIPT_DIR")"
LOG_DIR="/Users/accusys/momentry/log/monitor"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/service_check.log"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 顏色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 記錄結果到資料庫
record_service() {
local service=$1
local status=$2
local response_time=$3
local error_msg=$4
psql -U accusys -h localhost -d momentry << EOF 2>/dev/null
INSERT INTO monitor_services (service_name, service_type, status, response_time_ms, error_message, checked_at)
VALUES ('$service', 'service', '$status', $response_time, '$error_msg', NOW());
EOF
}
# 檢查 PostgreSQL
check_postgresql() {
local start=$(date +%s%N)
if pg_isready -h localhost -p 5432 -U accusys > /dev/null 2>&1; then
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
echo -e "${GREEN}${NC} PostgreSQL (5432) - ${ms}ms"
record_service "postgresql" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} PostgreSQL (5432) - Down"
record_service "postgresql" "down" "0" "Connection failed"
return 1
fi
}
# 檢查 Redis
check_redis() {
local start=$(date +%s%N)
if redis-cli -a accusys ping 2>/dev/null | grep -q "PONG"; then
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
echo -e "${GREEN}${NC} Redis (6379) - ${ms}ms"
record_service "redis" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} Redis (6379) - Down"
record_service "redis" "down" "0" "Connection failed"
return 1
fi
}
# 檢查 MariaDB
check_mariadb() {
local start=$(date +%s%N)
if mysql -u accusys -e "SELECT 1" > /dev/null 2>&1; then
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
echo -e "${GREEN}${NC} MariaDB (3306) - ${ms}ms"
record_service "mariadb" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} MariaDB (3306) - Down"
record_service "mariadb" "down" "0" "Connection failed"
return 1
fi
}
# 檢查 n8n
check_n8n() {
local start=$(date +%s%N)
local http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8085/ --max-time 5)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ "$http_code" = "200" ] || [ "$http_code" = "302" ]; then
echo -e "${GREEN}${NC} n8n (8085) - ${ms}ms"
record_service "n8n" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} n8n (8085) - HTTP $http_code"
record_service "n8n" "down" "0" "HTTP $http_code"
return 1
fi
}
# 檢查 Caddy
check_caddy() {
local start=$(date +%s%N)
local http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:2019/config/ --max-time 5)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ "$http_code" = "200" ]; then
echo -e "${GREEN}${NC} Caddy (2019) - ${ms}ms"
record_service "caddy" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} Caddy (2019) - HTTP $http_code"
record_service "caddy" "down" "0" "HTTP $http_code"
return 1
fi
}
# 檢查 Gitea
check_gitea() {
local start=$(date +%s%N)
local http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/ --max-time 5)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ "$http_code" = "200" ]; then
echo -e "${GREEN}${NC} Gitea (3000) - ${ms}ms"
record_service "gitea" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} Gitea (3000) - HTTP $http_code"
record_service "gitea" "down" "0" "HTTP $http_code"
return 1
fi
}
# 檢查 SFTPGo
check_sftpgo() {
local start=$(date +%s%N)
local http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080 --max-time 5)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ "$http_code" = "200" ] || [ "$http_code" = "301" ] || [ "$http_code" = "302" ]; then
echo -e "${GREEN}${NC} SFTPGo (8080) - ${ms}ms"
record_service "sftpgo" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} SFTPGo (8080) - HTTP $http_code"
record_service "sftpgo" "down" "0" "HTTP $http_code"
return 1
fi
}
# 檢查 Ollama
check_ollama() {
local start=$(date +%s%N)
local http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:11434/api/tags --max-time 5)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ "$http_code" = "200" ]; then
echo -e "${GREEN}${NC} Ollama (11434) - ${ms}ms"
record_service "ollama" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} Ollama (11434) - HTTP $http_code"
record_service "ollama" "down" "0" "HTTP $http_code"
return 1
fi
}
# 檢查 Qdrant
check_qdrant() {
local start=$(date +%s%N)
local http_code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:6333/collections --max-time 5)
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
if [ "$http_code" = "200" ] || [ "$http_code" = "401" ]; then
echo -e "${GREEN}${NC} Qdrant (6333) - ${ms}ms"
record_service "qdrant" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} Qdrant (6333) - HTTP $http_code"
record_service "qdrant" "down" "0" "HTTP $http_code"
return 1
fi
}
# 檢查 MongoDB
check_mongodb() {
local start=$(date +%s%N)
if mongosh --quiet --eval "db.adminCommand('ping')" > /dev/null 2>&1; then
local end=$(date +%s%N)
local ms=$(( (end - start) / 1000000 ))
echo -e "${GREEN}${NC} MongoDB (27017) - ${ms}ms"
record_service "mongodb" "up" "$ms" ""
return 0
else
echo -e "${RED}${NC} MongoDB (27017) - Down"
record_service "mongodb" "down" "0" "Connection failed"
return 1
fi
}
# 檢查 PHP-FPM
check_php() {
if pgrep -f "php-fpm" > /dev/null 2>&1; then
echo -e "${GREEN}${NC} PHP-FPM - Running"
record_service "php" "up" "1" ""
return 0
else
echo -e "${RED}${NC} PHP-FPM - Not running"
record_service "php" "down" "0" "Process not found"
return 1
fi
}
# 檢查 RustDesk
check_rustdesk() {
local hbbs_ok=false
local hbbr_ok=false
if nc -z localhost 21116 > /dev/null 2>&1; then
hbbs_ok=true
fi
if nc -z localhost 21117 > /dev/null 2>&1; then
hbbr_ok=true
fi
if $hbbs_ok && $hbbr_ok; then
echo -e "${GREEN}${NC} RustDesk (21116/21117) - Running"
record_service "rustdesk" "up" "1" ""
return 0
else
echo -e "${YELLOW}${NC} RustDesk - Partial (hbbs: $hbbs_ok, hbbr: $hbbr_ok)"
record_service "rustdesk" "degraded" "0" "hbbs:$hbbs_ok hbbr:$hbbr_ok"
return 1
fi
}
# 檢查 Node.js 版本
check_node() {
local LOCKED_NODE_VERSION="22"
local version_issues=0
local node_pids=$(pgrep -f "n8n" 2>/dev/null)
if [ -z "$node_pids" ]; then
echo -e "${YELLOW}${NC} Node.js - n8n not running"
record_service "node" "degraded" "1" "n8n not running"
return 1
fi
for pid in $node_pids; do
local node_path=$(lsof -p $pid 2>/dev/null | grep "txt" | grep "node" | head -1 | awk '{print $NF}' | grep -v "dylib")
if [ -n "$node_path" ] && [ -f "$node_path" ]; then
local node_version=$($node_path --version 2>/dev/null | sed 's/v//')
local node_major=$(echo "$node_version" | cut -d. -f1)
if [ "$node_major" != "$LOCKED_NODE_VERSION" ]; then
version_issues=$((version_issues + 1))
fi
fi
done
if [ $version_issues -gt 0 ]; then
echo -e "${RED}${NC} Node.js - Version issues detected"
record_service "node" "degraded" "1" "$version_issues version issues"
return 1
else
echo -e "${GREEN}${NC} Node.js (${LOCKED_NODE_VERSION}.x) - Running"
record_service "node" "up" "1" ""
return 0
fi
}
# 檢查 Python 版本
check_python() {
local LOCKED_PYTHON_VERSION="3.11.14"
local script_issues=0
local scripts=(
"/Users/accusys/momentry_core_0.1/scripts/asr_processor.py"
"/Users/accusys/momentry_core_0.1/scripts/thumbnail_extractor.py"
)
for script in "${scripts[@]}"; do
if [ -f "$script" ]; then
local shebang=$(head -1 "$script")
if [[ "$shebang" != *"python3.11"* ]]; then
script_issues=$((script_issues + 1))
fi
fi
done
if [ $script_issues -gt 0 ]; then
echo -e "${RED}${NC} Python - Script version issues"
record_service "python" "degraded" "1" "$script_issues script issues"
return 1
else
echo -e "${GREEN}${NC} Python (${LOCKED_PYTHON_VERSION}) - Configured"
record_service "python" "up" "1" ""
return 0
fi
}
# 主程序
echo "========================================"
echo "Layer 2: Service Health Check"
echo "Time: $(date)"
echo "========================================"
echo ""
total=0
passed=0
total=$((total + 1))
check_postgresql && passed=$((passed + 1))
total=$((total + 1))
check_redis && passed=$((passed + 1))
total=$((total + 1))
check_mariadb && passed=$((passed + 1))
total=$((total + 1))
check_n8n && passed=$((passed + 1))
total=$((total + 1))
check_caddy && passed=$((passed + 1))
total=$((total + 1))
check_gitea && passed=$((passed + 1))
total=$((total + 1))
check_sftpgo && passed=$((passed + 1))
total=$((total + 1))
check_ollama && passed=$((passed + 1))
total=$((total + 1))
check_qdrant && passed=$((passed + 1))
total=$((total + 1))
check_mongodb && passed=$((passed + 1))
total=$((total + 1))
check_php && passed=$((passed + 1))
total=$((total + 1))
check_rustdesk && passed=$((passed + 1))
total=$((total + 1))
check_node && passed=$((passed + 1))
total=$((total + 1))
check_python && passed=$((passed + 1))
echo ""
echo "========================================"
echo "Result: $passed / $total services healthy"
echo "========================================"
log "Service check completed: $passed/$total healthy"

270
monitor/service/node_monitor.sh Executable file
View File

@@ -0,0 +1,270 @@
#!/bin/bash
#===============================================================================
# Momentry Node.js 監控腳本
# 路徑: /Users/accusys/momentry_core_0.1/monitor/service/node_monitor.sh
#
# 監控重點:
# - n8n 使用的 Node.js 版本鎖定 (22.x)
# - 進程數量與狀態
# - 資源使用情況
#
# 使用方式:
# ./node_monitor.sh status # 顯示監控狀態
# ./node_monitor.sh baseline # 建立版本基線
# ./node_monitor.sh check # 檢查版本變化
#===============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MONITOR_DIR="$(dirname "$SCRIPT_DIR")"
LOG_DIR="/Users/accusys/momentry/log/monitor"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/node_check.log"
# 顏色定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 鎖定的 Node.js 版本
LOCKED_NODE_VERSION="22"
LOCKED_NODE_MINOR="22"
#===============================================================================
# 記錄函數
#===============================================================================
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log_success() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] ✅ $1${NC}" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ❌ $1${NC}" | tee -a "$LOG_FILE"
}
log_warn() {
echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] ⚠️ $1${NC}" | tee -a "$LOG_FILE"
}
#===============================================================================
# 記錄到資料庫
#===============================================================================
record_node_baseline() {
local runtime_name=$1
local current_version=$2
local process_path=$3
local pid=$4
local required_version="${LOCKED_NODE_VERSION}.x"
local is_compliant="false"
if [[ "$current_version" == "${LOCKED_NODE_VERSION}".* ]]; then
is_compliant="true"
fi
psql -U accusys -h localhost -d momentry << EOF 2>/dev/null
INSERT INTO node_version_baseline (runtime_name, required_version, current_version, process_name, process_path, is_compliant, locked_path, checked_at)
VALUES ('$runtime_name', '$required_version', '$current_version', 'node', '$process_path', $is_compliant, '$process_path', NOW())
ON CONFLICT DO NOTHING;
EOF
}
record_node_history() {
local process_name=$1
local old_version=$2
local new_version=$3
local old_path=$4
local new_path=$5
# node_version_history table does not exist - skip recording
true
}
record_monitor_service() {
local service=$1
local status=$2
local version=$3
local error_msg=$4
psql -U accusys -h localhost -d momentry << EOF 2>/dev/null
INSERT INTO monitor_services (service_name, service_type, status, response_time_ms, error_message, checked_at)
VALUES ('$service', 'node', '$status', 0, '$version - $error_msg', NOW());
EOF
}
#===============================================================================
# 發現 Node.js 進程
#===============================================================================
discover_node_processes() {
log "發現 Node.js 進程..."
echo ""
echo "========================================"
echo "Node.js 監控狀態"
echo "時間: $(date)"
echo "========================================"
echo ""
# 獲取所有 node 進程
local node_pids=$(pgrep -f "node" 2>/dev/null)
if [ -z "$node_pids" ]; then
echo -e "${RED}沒有運行中的 Node.js 進程${NC}"
record_monitor_service "node" "down" "N/A" "No processes"
return 1
fi
echo "鎖定版本: Node.js ${LOCKED_NODE_VERSION}.x (n8n 專用)"
echo ""
echo "----------------------------------------"
echo "發現的 Node.js 進程:"
echo "----------------------------------------"
local total_processes=0
local n8n_processes=0
local version_issues=0
for pid in $node_pids; do
# 獲取進程命令
local cmd=$(ps -o args= -p $pid 2>/dev/null | head -1)
# 獲取 Node.js 版本
local node_path=$(lsof -p $pid 2>/dev/null | grep "txt" | grep "node" | head -1 | awk '{print $NF}' | grep -v "dylib")
if [ -n "$node_path" ] && [ -f "$node_path" ]; then
local node_version=$($node_path --version 2>/dev/null | sed 's/v//')
local node_major=$(echo "$node_version" | cut -d. -f1)
local node_minor=$(echo "$node_version" | cut -d. -f2)
else
local node_version="unknown"
local node_major="unknown"
fi
# 內存使用
local mem=$(ps -o rss= -p $pid 2>/dev/null | awk '{print int($1/1024)}')
# CPU 使用
local cpu=$(ps -o %cpu= -p $pid 2>/dev/null | awk '{print int($1)}')
# 運行時間
local time=$(ps -o etime= -p $pid 2>/dev/null | tr -d ' ')
# 識別服務類型
local service_type="other"
if echo "$cmd" | grep -q "n8n"; then
service_type="n8n"
n8n_processes=$((n8n_processes + 1))
elif echo "$cmd" | grep -q "worker"; then
service_type="n8n-worker"
n8n_processes=$((n8n_processes + 1))
fi
# 版本檢查
local version_status="✅"
if [ "$service_type" = "n8n" ] || [ "$service_type" = "n8n-worker" ]; then
if [ "$node_major" != "$LOCKED_NODE_VERSION" ]; then
version_status="❌ 版本錯誤!"
version_issues=$((version_issues + 1))
log_error "n8n 使用 Node.js $node_version (應為 ${LOCKED_NODE_VERSION}.x)"
fi
fi
echo " PID: $pid"
echo " 命令: ${cmd:0:60}..."
echo " Node.js: $node_version $version_status"
echo " 路徑: $node_path"
echo " 內存: ${mem}MB | CPU: ${cpu}% | 運行: $time"
echo " 類型: $service_type"
echo ""
total_processes=$((total_processes + 1))
# 記錄基線
record_node_baseline "$service_type" "$node_version" "$node_path" "$pid"
done
echo "----------------------------------------"
echo "總結:"
echo " 總進程數: $total_processes"
echo " n8n 相關: $n8n_processes"
echo " 版本問題: $version_issues"
echo "========================================"
# 記錄到資料庫
if [ $version_issues -gt 0 ]; then
record_monitor_service "node" "degraded" "${LOCKED_NODE_VERSION}.x" "$version_issues version issues"
return 1
else
record_monitor_service "node" "up" "${LOCKED_NODE_VERSION}.x" "OK"
return 0
fi
}
#===============================================================================
# 版本基線檢查
#===============================================================================
check_baseline() {
log "檢查 Node.js 版本基線..."
# 檢查 n8n 進程
local n8n_pid=$(pgrep -f "n8n start" | head -1)
if [ -z "$n8n_pid" ]; then
log_error "n8n 進程未運行"
return 1
fi
# 獲取 n8n 使用的 Node.js 版本
local node_path=$(lsof -p $n8n_pid 2>/dev/null | grep "txt" | grep "node" | head -1 | awk '{print $NF}' | grep -v "dylib")
if [ -n "$node_path" ] && [ -f "$node_path" ]; then
local node_version=$($node_path --version 2>/dev/null | sed 's/v//')
local node_major=$(echo "$node_version" | cut -d. -f1)
echo "n8n 當前 Node.js 版本: $node_version"
if [ "$node_major" = "$LOCKED_NODE_VERSION" ]; then
log_success "版本正確: Node.js $node_version"
return 0
else
log_error "版本錯誤: Node.js $node_version (應為 ${LOCKED_NODE_VERSION}.x)"
return 1
fi
else
log_error "無法確定 Node.js 版本"
return 1
fi
}
#===============================================================================
# 顯示狀態
#===============================================================================
show_status() {
discover_node_processes
}
#===============================================================================
# 主程序
#===============================================================================
command=${1:-status}
case $command in
status|check)
show_status
;;
baseline)
check_baseline
;;
*)
echo "用法: $0 {status|baseline}"
echo ""
echo " status - 顯示 Node.js 監控狀態"
echo " baseline - 檢查版本基線"
exit 1
;;
esac

281
monitor/service/python_monitor.sh Executable file
View File

@@ -0,0 +1,281 @@
#!/bin/bash
#===============================================================================
# Momentry Python 監控腳本
# 路徑: /Users/accusys/momentry_core_0.1/monitor/service/python_monitor.sh
#
# 監控重點:
# - Momentry Python 腳本版本鎖定 (3.11.14)
# - 進程數量與狀態
# - 腳本執行狀態
#
# 使用方式:
# ./python_monitor.sh status # 顯示監控狀態
# ./python_monitor.sh baseline # 建立版本基線
# ./python_monitor.sh check # 檢查版本變化
#===============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MONITOR_DIR="$(dirname "$SCRIPT_DIR")"
LOG_DIR="/Users/accusys/momentry/log/monitor"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/python_check.log"
# 顏色定義
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 鎖定的 Python 版本
LOCKED_PYTHON_VERSION="3.11.14"
LOCKED_PYTHON_MAJOR="3"
LOCKED_PYTHON_MINOR="11"
# Momentry Python 腳本
MOMENTRY_SCRIPTS=(
"/Users/accusys/momentry_core_0.1/scripts/asr_processor.py"
"/Users/accusys/momentry_core_0.1/scripts/thumbnail_extractor.py"
)
#===============================================================================
# 記錄函數
#===============================================================================
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
log_success() {
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] ✅ $1${NC}" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ❌ $1${NC}" | tee -a "$LOG_FILE"
}
log_warn() {
echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] ⚠️ $1${NC}" | tee -a "$LOG_FILE"
}
#===============================================================================
# 記錄到資料庫
#===============================================================================
record_python_baseline() {
local runtime_name=$1
local current_version=$2
local interpreter_path=$3
local required_version="${LOCKED_PYTHON_VERSION}"
local is_compliant="false"
if [[ "$current_version" == "${LOCKED_PYTHON_VERSION}" ]]; then
is_compliant="true"
fi
psql -U accusys -h localhost -d momentry << EOF 2>/dev/null
INSERT INTO python_version_baseline (runtime_name, required_version, current_version, interpreter_path, is_compliant, checked_at)
VALUES ('$runtime_name', '$required_version', '$current_version', '$interpreter_path', $is_compliant, NOW())
ON CONFLICT DO NOTHING;
EOF
}
record_python_history() {
local script_name=$1
local old_version=$2
local new_version=$3
local old_path=$4
local new_path=$5
# python_version_history table does not exist - skip recording
true
}
record_monitor_service() {
local service=$1
local status=$2
local version=$3
local error_msg=$4
psql -U accusys -h localhost -d momentry << EOF 2>/dev/null
INSERT INTO monitor_services (service_name, service_type, status, response_time_ms, error_message, checked_at)
VALUES ('$service', 'python', '$status', 0, '$version - $error_msg', NOW());
EOF
}
#===============================================================================
# 發現 Python 進程
#===============================================================================
discover_python_processes() {
log "發現 Python 進程..."
echo ""
echo "========================================"
echo "Python 監控狀態"
echo "時間: $(date)"
echo "========================================"
echo ""
echo "鎖定版本: Python ${LOCKED_PYTHON_VERSION} (Momentry 專用)"
echo ""
# 檢查 Momentry 腳本
echo "----------------------------------------"
echo "Momentry Python 腳本:"
echo "----------------------------------------"
local script_issues=0
for script in "${MOMENTRY_SCRIPTS[@]}"; do
if [ -f "$script" ]; then
# 獲取腳本使用的 Python
local shebang=$(head -1 "$script")
local python_path=""
if [[ "$shebang" == *"/python3.11"* ]]; then
python_path="/opt/homebrew/bin/python3.11"
elif [[ "$shebang" == *"/python3"* ]]; then
# 檢查系統 python3
python_path=$(which python3 2>/dev/null)
fi
if [ -n "$python_path" ] && [ -f "$python_path" ]; then
local python_version=$($python_path --version 2>&1 | sed 's/Python //')
local python_major=$(echo "$python_version" | cut -d. -f1)
local python_minor=$(echo "$python_version" | cut -d. -f2)
# 檢查版本
local version_status="✅"
if [ "$python_major" = "$LOCKED_PYTHON_MAJOR" ] && [ "$python_minor" = "$LOCKED_PYTHON_MINOR" ]; then
log_success "$(basename $script): $python_version"
else
version_status="❌ 版本錯誤!"
script_issues=$((script_issues + 1))
log_error "$(basename $script): $python_version (應為 ${LOCKED_PYTHON_VERSION})"
fi
echo " $(basename $script)"
echo " 路徑: $python_path"
echo " 版本: $python_version $version_status"
echo " shebang: $shebang"
echo ""
# 記錄基線
record_python_baseline "python_${LOCKED_PYTHON_VERSION}" "$python_version" "$python_path"
else
log_error "$(basename $script): 無法確定 Python 路徑"
script_issues=$((script_issues + 1))
fi
else
log_warn "$(basename $script): 文件不存在"
script_issues=$((script_issues + 1))
fi
done
# 檢查運行中的 Python 進程
echo "----------------------------------------"
echo "運行中的 Python 進程:"
echo "----------------------------------------"
local python_pids=$(pgrep -f "python" 2>/dev/null)
local total_processes=0
if [ -n "$python_pids" ]; then
for pid in $python_pids; do
# 獲取進程命令
local cmd=$(ps -o args= -p $pid 2>/dev/null | head -1 | cut -c1-80)
# 獲取 Python 路徑
local python_path=$(lsof -p $pid 2>/dev/null | grep "txt" | grep "Python" | head -1 | awk '{print $NF}' | grep -v "dylib")
if [ -n "$python_path" ] && [ -f "$python_path" ]; then
local python_version=$($python_path --version 2>&1 | sed 's/Python //')
else
local python_version="unknown"
fi
# 內存使用
local mem=$(ps -o rss= -p $pid 2>/dev/null | awk '{print int($1/1024)}')
echo " PID $pid: $cmd"
echo " Python: $python_version"
echo " 內存: ${mem}MB"
echo ""
total_processes=$((total_processes + 1))
done
else
echo " (無運行中的 Python 進程)"
fi
echo "----------------------------------------"
echo "總結:"
echo " 總進程數: $total_processes"
echo " 腳本問題: $script_issues"
echo "========================================"
# 記錄到資料庫
if [ $script_issues -gt 0 ]; then
record_monitor_service "python" "degraded" "${LOCKED_PYTHON_VERSION}" "$script_issues issues"
return 1
else
record_monitor_service "python" "up" "${LOCKED_PYTHON_VERSION}" "OK"
return 0
fi
}
#===============================================================================
# 版本基線檢查
#===============================================================================
check_baseline() {
log "檢查 Python 版本基線..."
local script_issues=0
for script in "${MOMENTRY_SCRIPTS[@]}"; do
if [ -f "$script" ]; then
local shebang=$(head -1 "$script")
if [[ "$shebang" == *"/python3.11"* ]]; then
log_success "$(basename $script): 使用正確版本"
else
log_error "$(basename $script): 未使用 python3.11"
script_issues=$((script_issues + 1))
fi
fi
done
if [ $script_issues -gt 0 ]; then
return 1
else
return 0
fi
}
#===============================================================================
# 顯示狀態
#===============================================================================
show_status() {
discover_python_processes
}
#===============================================================================
# 主程序
#===============================================================================
command=${1:-status}
case $command in
status|check)
show_status
;;
baseline)
check_baseline
;;
*)
echo "用法: $0 {status|baseline}"
echo ""
echo " status - 顯示 Python 監控狀態"
echo " baseline - 檢查版本基線"
exit 1
;;
esac