Files
hzdyjs/退学、休学、复学、毕业/health_check.sh
T
2025-12-25 18:01:27 +08:00

360 lines
8.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ===========================================
# 学生状态变更处理服务健康检查脚本
# 用于监控服务运行状态和数据库连接
# ===========================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/data.conf"
CONTROL_SCRIPT="${SCRIPT_DIR}/control_scripts.sh"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Load configuration
if [[ ! -f "$CONFIG_FILE" ]]; then
echo -e "${RED}Error: Configuration file $CONFIG_FILE not found!${NC}"
exit 1
fi
source "$CONFIG_FILE"
# Functions
log() {
local level="$1"
local message="$2"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "${timestamp} [$level] $message"
}
success() {
echo -e "${GREEN}✓ $1${NC}"
}
error() {
echo -e "${RED}✗ $1${NC}"
}
warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
info() {
echo -e "${BLUE}ℹ $1${NC}"
}
# Check database connection
check_database() {
info "Checking database connection..."
local db_test
if db_test=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "SELECT 1 as test;" 2>/dev/null); then
if echo "$db_test" | grep -q "test"; then
success "Database connection successful"
return 0
else
error "Database query failed"
return 1
fi
else
error "Database connection failed"
return 1
fi
}
# Check service status
check_service() {
local service_name="$1"
local service_desc="$2"
info "Checking $service_desc..."
if [[ ! -x "$CONTROL_SCRIPT" ]]; then
error "Control script not found or not executable: $CONTROL_SCRIPT"
return 1
fi
local status_output
status_output=$("$CONTROL_SCRIPT" status "$service_name" 2>/dev/null)
if echo "$status_output" | grep -q "is running"; then
success "$service_desc is running"
# Extract PID if available
local pid
pid=$(echo "$status_output" | grep -o "PID: [0-9]*" | cut -d' ' -f2)
if [[ -n "$pid" ]]; then
info " Process ID: $pid"
fi
return 0
else
error "$service_desc is not running"
return 1
fi
}
# Check log files
check_logs() {
info "Checking log files..."
if [[ ! -d "$LOG_DIR" ]]; then
warning "Log directory does not exist: $LOG_DIR"
return 1
fi
# Count log files
local log_count
log_count=$(find "$LOG_DIR" -name "*.log" -type f | wc -l)
if [[ $log_count -gt 0 ]]; then
success "Found $log_count log files"
# Check for recent errors
local recent_errors
recent_errors=$(find "$LOG_DIR" -name "*.log" -type f -mmin -60 -exec grep -l "ERROR" {} \; | wc -l)
if [[ $recent_errors -gt 0 ]]; then
warning "Found $recent_errors log files with recent errors (last 60 minutes)"
else
info "No recent errors found in log files"
fi
return 0
else
warning "No log files found"
return 1
fi
}
# Check pending records
check_pending_records() {
info "Checking pending records in database..."
local pending_changes pending_resume
# Check changes (XIU_XUE, TUI_XUE, BI_YE)
pending_changes=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "
SELECT COUNT(*) as count FROM xg_student_status_change
WHERE process_status = 0
AND change_type IN ('XIU_XUE', 'TUI_XUE', 'BI_YE')
AND deleted = 0;
" 2>/dev/null | tail -n1)
# Check resume (FU_XUE)
pending_resume=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "
SELECT COUNT(*) as count FROM xg_student_status_change
WHERE process_status = 0
AND change_type = 'FU_XUE'
AND deleted = 0;
" 2>/dev/null | tail -n1)
# Remove potential header
pending_changes=$(echo "$pending_changes" | grep -E '^[0-9]+$')
pending_resume=$(echo "$pending_resume" | grep -E '^[0-9]+$')
if [[ -z "$pending_changes" ]]; then
pending_changes="0"
fi
if [[ -z "$pending_resume" ]]; then
pending_resume="0"
fi
local total_pending=$((pending_changes + pending_resume))
if [[ $total_pending -gt 0 ]]; then
info "Found $total_pending pending records ($pending_changes changes, $pending_resume resume)"
if [[ $pending_changes -gt 0 ]]; then
warning " $pending_changes records waiting for changes processing"
fi
if [[ $pending_resume -gt 0 ]]; then
warning " $pending_resume records waiting for resume processing"
fi
else
success "No pending records found"
fi
return 0
}
# Check disk space
check_disk_space() {
info "Checking disk space..."
local log_disk_usage
log_disk_usage=$(df "$LOG_DIR" 2>/dev/null | tail -n1 | awk '{print $5}' | sed 's/%//')
if [[ -z "$log_disk_usage" ]]; then
warning "Cannot determine log directory disk usage"
return 1
fi
if [[ $log_disk_usage -gt 90 ]]; then
error "Log directory disk usage is ${log_disk_usage}% - consider cleaning up old logs"
return 1
elif [[ $log_disk_usage -gt 75 ]]; then
warning "Log directory disk usage is ${log_disk_usage}%"
else
success "Log directory disk usage is ${log_disk_usage}%"
fi
return 0
}
# Generate health report
generate_report() {
local report_file="${LOG_DIR}/health_check_$(date +%Y%m%d_%H%M%S).log"
{
echo "=== 学生状态变更处理服务健康检查报告 ==="
echo "检查时间: $(date)"
echo "检查主机: $(hostname)"
echo ""
echo "=== 数据库连接检查 ==="
if check_database; then
echo "状态: 正常"
else
echo "状态: 异常"
fi
echo ""
echo "=== 服务状态检查 ==="
echo "学生状态变更处理服务 (休学/退学/毕业):"
if check_service "changes" "学生状态变更处理服务"; then
echo "状态: 运行中"
else
echo "状态: 未运行"
fi
echo "学生复学处理服务:"
if check_service "resume" "学生复学处理服务"; then
echo "状态: 运行中"
else
echo "状态: 未运行"
fi
echo ""
echo "=== 待处理记录检查 ==="
check_pending_records
echo ""
echo "=== 日志文件检查 ==="
check_logs
echo ""
echo "=== 磁盘空间检查 ==="
check_disk_space
echo ""
echo "=== 配置信息 ==="
echo "数据库主机: $DB_HOST:$DB_PORT"
echo "数据库名称: $DB_NAME"
echo "日志目录: $LOG_DIR"
echo "脚本目录: $SCRIPT_DIR"
echo ""
echo "报告生成完成"
} > "$report_file" 2>/dev/null
if [[ -f "$report_file" ]]; then
success "Health check report generated: $report_file"
else
error "Failed to generate health check report"
fi
}
# Main function
main() {
local generate_report="${1:-false}"
echo ""
echo "========================================"
echo " 学生状态变更处理服务健康检查"
echo "========================================"
echo ""
# Perform all checks
check_database
echo ""
check_service "changes" "学生状态变更处理服务 (休学/退学/毕业)"
check_service "resume" "学生复学处理服务"
echo ""
check_pending_records
echo ""
check_logs
echo ""
check_disk_space
echo ""
# Generate report if requested
if [[ "$generate_report" == "report" ]]; then
generate_report
fi
echo ""
echo "========================================"
success "健康检查完成"
echo ""
if [[ "$generate_report" == "report" ]]; then
echo "详细报告已保存到日志目录"
fi
echo "使用方法:"
echo " $0 # 执行健康检查"
echo " $0 report # 执行检查并生成报告"
echo ""
}
# Show usage
show_usage() {
cat << EOF
Usage: $0 [report]
执行学生状态变更处理服务的健康检查。
Options:
report 生成详细的健康检查报告
检查内容:
- 数据库连接状态
- 服务运行状态
- 待处理记录数量
- 日志文件状态
- 磁盘空间使用情况
EOF
}
# Parse arguments
case "${1:-}" in
report)
main "report"
;;
-h|--help)
show_usage
;;
"")
main
;;
*)
error "Unknown option: $1"
show_usage
exit 1
;;
esac