Files
ljwx-k8s/verify-mysql-monitoring.sh
2026-02-20 22:50:45 +08:00

97 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# 验证 MySQL 监控配置
# 包括 MySQL、MySQL Exporter、ServiceMonitor 和 Grafana Dashboard
set -e
echo "======================================"
echo " MySQL 监控验证脚本"
echo "======================================"
echo ""
# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 检查函数
check_status() {
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ $1${NC}"
return 0
else
echo -e "${RED}✗ $1${NC}"
return 1
fi
}
echo "1. 检查 MySQL 数据库状态..."
kubectl get pod mysql-infra-0 -n infra --no-headers 2>/dev/null | grep -q "Running"
check_status "MySQL Pod 运行正常"
kubectl get svc mysql-infra -n infra --no-headers 2>/dev/null | grep -q "mysql-infra"
check_status "MySQL Service 已创建"
echo ""
echo "2. 检查 MySQL Exporter 状态..."
kubectl get pod -n monitoring -l app=mysql-exporter --no-headers 2>/dev/null | grep -q "Running"
check_status "MySQL Exporter Pod 运行正常"
kubectl get svc mysql-exporter -n monitoring --no-headers 2>/dev/null | grep -q "mysql-exporter"
check_status "MySQL Exporter Service 已创建"
kubectl get servicemonitor mysql-exporter -n monitoring --no-headers 2>/dev/null | grep -q "mysql-exporter"
check_status "ServiceMonitor 已创建"
echo ""
echo "3. 检查 Metrics 端点..."
POD_NAME=$(kubectl get pod -n monitoring -l app=mysql-exporter -o jsonpath='{.items[0].metadata.name}')
if [ -n "$POD_NAME" ]; then
METRICS=$(kubectl exec -n monitoring "$POD_NAME" -- wget -q -O- http://localhost:9104/metrics 2>/dev/null)
echo "$METRICS" | grep -q "mysql_up"
check_status "mysql_up 指标可用"
echo "$METRICS" | grep -q "mysql_global_status_threads_connected"
check_status "mysql_global_status_threads_connected 指标可用"
echo "$METRICS" | grep -q "mysql_global_status_queries"
check_status "mysql_global_status_queries 指标可用"
echo "$METRICS" | grep -q "mysql_global_status_innodb"
check_status "InnoDB 指标可用"
fi
echo ""
echo "4. 检查 Grafana Dashboard..."
kubectl get configmap grafana-dashboard-mysql -n monitoring --no-headers 2>/dev/null | grep -q "grafana-dashboard-mysql"
check_status "MySQL Dashboard ConfigMap 已创建"
echo ""
echo "5. 验证 Prometheus Target..."
echo -e "${YELLOW}提示: 请访问 Prometheus UI 检查 mysql-exporter target 状态${NC}"
echo " kubectl port-forward -n monitoring svc/prometheus-operated 9090:9090"
echo " 然后访问: http://localhost:9090/targets"
echo ""
echo "6. 访问 Grafana Dashboard..."
echo -e "${YELLOW}提示: 请访问 Grafana 查看 MySQL 监控面板${NC}"
echo " kubectl port-forward -n monitoring svc/grafana 30300:80"
echo " 然后访问: http://localhost:30300"
echo " Dashboard: MySQL Database Monitoring (UID: mysql-monitoring)"
echo ""
echo "======================================"
echo " 验证完成"
echo "======================================"
echo ""
# 显示当前指标快照
echo "当前 MySQL 指标快照:"
echo "---"
kubectl exec -n monitoring "$POD_NAME" -- wget -q -O- http://localhost:9104/metrics 2>/dev/null | grep -E "^mysql_up|^mysql_global_status_threads_connected|^mysql_global_status_queries |^mysql_global_status_innodb_buffer_pool_bytes" | head -10
echo "---"
echo ""
echo -e "${GREEN}MySQL 监控配置验证完成!${NC}"