本文目录导读:

Git 钩子脚本可以通过在代码提交或合并前运行自定义检查来监控告警策略相关的配置,以下是几种常见的实现方式:
准备工作
创建钩子脚本
# 进入项目仓库 cd your-repo # 创建 pre-commit 钩子(或其他钩子) vim .git/hooks/pre-commit chmod +x .git/hooks/pre-commit
检查告警策略的示例脚本
#!/bin/bash
set -e
# 告警策略检查脚本
echo "🔍 正在检查告警策略配置..."
# 定义检查规则
ERROR_COUNT=0
WARNING_COUNT=0
# 1. 检查 YAML/JSON 格式的告警策略文件
check_alerting_files() {
local files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(yaml|yml|json)$' || true)
for file in $files; do
# 检查文件语法
if [[ $file == *.yaml ]] || [[ $file == *.yml ]]; then
if ! python3 -c "import yaml; yaml.safe_load(open('$file'))" 2>/dev/null; then
echo "❌ YAML语法错误: $file"
((ERROR_COUNT++))
fi
elif [[ $file == *.json ]]; then
if ! python3 -c "import json; json.load(open('$file'))" 2>/dev/null; then
echo "❌ JSON语法错误: $file"
((ERROR_COUNT++))
fi
fi
done
}
# 2. 检查告警阈值配置
check_alert_thresholds() {
local alert_files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '(alert|alarm|threshold)' || true)
for file in $alert_files; do
if [[ -f "$file" ]]; then
# 检查阈值是否在合理范围内
local threshold=$(grep -oP '"threshold":\s*\K[0-9.]+' "$file" || true)
if [[ -n "$threshold" ]]; then
if (( $(echo "$threshold > 10000" | bc -l) )); then
echo "⚠️ 警告: $file 中的告警阈值过高: $threshold"
((WARNING_COUNT++))
fi
fi
# 检查是否配置了通知渠道
if ! grep -q '"notify\|notification\|webhook' "$file" 2>/dev/null; then
echo "⚠️ 警告: $file 缺少通知渠道配置"
((WARNING_COUNT++))
fi
fi
done
}
# 3. 检查是否引用了不存在的服务或指标
check_service_references() {
local changed_files=$(git diff --cached --name-only --diff-filter=ACM)
for file in $changed_files; do
if grep -qP '(service|metric)_(name|id):' "$file" 2>/dev/null; then
local refs=$(grep -oP '(service|metric)_(name|id):\s*\K\w+' "$file")
for ref in $refs; do
# 检查引用的服务或指标是否存在于配置文件中
if ! grep -q "$ref" "config/services.yaml" 2>/dev/null && \
! grep -q "$ref" "config/metrics.yaml" 2>/dev/null; then
if [[ "$file" != "config/services.yaml" ]] && \
[[ "$file" != "config/metrics.yaml" ]]; then
echo "⚠️ 警告: $file 引用了不存在的服务/指标: $ref"
((WARNING_COUNT++))
fi
fi
done
fi
done
}
# 4. 检查重复的告警规则
check_duplicate_rules() {
local files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '(alert|rule)' || true)
for file in $files; do
if [[ -f "$file" ]]; then
# 检查是否有重复的告警名称
local rule_names=$(grep -oP '(alert|rule)[_ ]?name:\s*\K\w+' "$file" 2>/dev/null)
local duplicates=$(echo "$rule_names" | sort | uniq -d)
if [[ -n "$duplicates" ]]; then
echo "❌ 发现重复的告警规则名称:"
echo "$duplicates" | while read -r dup; do
echo " - $file 中的 '$dup'"
((ERROR_COUNT++))
done
fi
fi
done
}
# 5. 执行所有检查
check_alerting_files
check_alert_thresholds
check_service_references
check_duplicate_rules
# 输出检查结果
echo ""
if [[ $ERROR_COUNT -gt 0 ]] || [[ $WARNING_COUNT -gt 0 ]]; then
echo "📊 检查结果:"
echo " - 错误: $ERROR_COUNT"
echo " - 警告: $WARNING_COUNT"
if [[ $ERROR_COUNT -gt 0 ]]; then
echo "❌ 存在严重错误,提交被拒绝"
exit 1
fi
fi
echo "✅ 告警策略检查通过"
exit 0
特定运维场景的检查脚本
Prometheus Alertmanager 配置检查
#!/bin/bash
# pre-commit 钩子 - 检查 Prometheus 告警规则
echo "检查 Prometheus 告警规则..."
# 使用 promtool 验证
for file in $(git diff --cached --name-only | grep 'rules/.*\.yml'); do
if ! promtool check rules "$file" 2>/dev/null; then
echo "❌ Prometheus 规则语法错误: $file"
exit 1
fi
# 检查告警表达式语法
if ! promtool test rules "$file" 2>/dev/null; then
echo "❌ Prometheus 规则测试失败: $file"
exit 1
fi
done
云平台告警策略检查
#!/bin/bash
# 检查云平台告警策略
check_cloud_alerts() {
local files=$(git diff --cached --name-only | grep -E '(alarm|alert|monitor)')
for file in $files; do
echo "检查: $file"
# 检查必要的字段
required_fields=("metric" "threshold" "period" "evaluation_count")
for field in "${required_fields[@]}"; do
if ! grep -q "\"$field\":" "$file" 2>/dev/null && \
! grep -q "$field:" "$file" 2>/dev/null; then
echo "⚠️ 缺少必需字段: $field"
fi
done
# 检查 Actions 配置
if grep -q '"actions"' "$file" 2>/dev/null; then
if ! grep -q '"notify"' "$file" 2>/dev/null; then
echo "⚠️ 告警未配置通知方式"
fi
fi
done
}
集成测试框架
#!/bin/bash
# pre-commit 钩子 - 运行告警策略测试
# 运行专门的测试脚本
if [[ -f "tests/test_alerts.py" ]]; then
python3 -m pytest tests/test_alerts.py -v --tb=short
if [[ $? -ne 0 ]]; then
echo "❌ 告警策略测试失败"
exit 1
fi
fi
# 运行告警模拟测试
if command -v alert_simulator &> /dev/null; then
for file in $(git diff --cached --name-only | grep -E 'alert|alarm|rule'); do
if ! alert_simulator simulate "$file"; then
echo "❌ 告警模拟测试失败: $file"
exit 1
fi
done
fi
最佳实践建议
配置管理
# 检查配置版本兼容性
check_version_compatibility() {
local files=$(git diff --cached --name-only | grep 'alert')
for file in $files; do
local api_version=$(grep -oP 'apiVersion:\s*\K\w+' "$file" 2>/dev/null)
if [[ -n "$api_version" ]] && [[ "$api_version" != "v1" ]] && [[ "$api_version" != "v2" ]]; then
echo "⚠️ 不支持的 API 版本: $api_version"
fi
done
}
注意事项
- 性能考虑:钩子脚本不应过于复杂,避免影响开发体验
- 错误处理:提供清晰的错误信息和修复建议
- 可跳过检查:对于紧急情况,提供
--no-verify选项 - 日志记录:记录检查结果便于审计
- 版本控制:将钩子脚本加入版本库,使用
git hooks管理工具
通过以上方式,可以在代码提交前自动检查告警策略的配置问题,及时发现和修复潜在风险。