Files
oa-monitor/bin/init-db.sh
T
gsadmin 90be249a72 fix: 修复 check-alert.sh 假阳性 + collect-business.sh 窗口对齐
- check-alert.sh 新增 Step0 数据新鲜度前置检查:business_metrics 超 30min 过期 →
  先告警采集脚本挂了,跳过业务层检测,防止读旧数据乱报推送断档
- collect-business.sh msg_log_hourly 窗口从 1h 改为 15min(和 crontab 频率对齐)
- CURR_HOUR 用 10# 算术强制十进制,防 08/09 被解析成 octal
2026-10-28 18:38:13 +08:00

85 lines
2.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.
#!/usr/bin/env bash
# ============================================================================
# OA Monitor · SQLite 数据库初始化
# 只建表,不删数据;幂等可重复执行
# ============================================================================
set -u
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/../lib/common.sh"
log_info "初始化 SQLite 数据库:${MONITOR_DB}"
sqlite_exec "CREATE TABLE IF NOT EXISTS db_metrics (
ts INTEGER PRIMARY KEY,
threads_running INTEGER,
threads_connected INTEGER,
threads_idle INTEGER,
uptime_sec INTEGER,
qps_read INTEGER,
qps_write INTEGER,
slow_queries INTEGER -- 近 30s 慢查询数(>30s)
);"
sqlite_exec "CREATE TABLE IF NOT EXISTS db_processlist (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER,
pid INTEGER,
user TEXT,
state TEXT,
time_sec INTEGER,
info TEXT
);"
sqlite_exec "CREATE INDEX IF NOT EXISTS idx_db_proc_ts ON db_processlist(ts);"
sqlite_exec "CREATE INDEX IF NOT EXISTS idx_db_proc_time ON db_processlist(time_sec);"
sqlite_exec "CREATE TABLE IF NOT EXISTS http_metrics (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER,
endpoint TEXT,
status_code INTEGER,
resp_time_ms INTEGER,
healthy INTEGER -- 0/1
);"
sqlite_exec "CREATE INDEX IF NOT EXISTS idx_http_ts_ep ON http_metrics(ts, endpoint);"
sqlite_exec "CREATE TABLE IF NOT EXISTS business_metrics (
ts INTEGER PRIMARY KEY,
msg_log_hourly INTEGER, -- 本小时 ecology_message_log 增量
msg_log_fail_rate REAL, -- 失败率
ongoing_requests INTEGER, -- 进行中流程数
overdue_tasks INTEGER -- 超时待办数
);"
sqlite_exec "CREATE TABLE IF NOT EXISTS infra_metrics (
ts INTEGER PRIMARY KEY,
docker_daemon_ok INTEGER, -- 0/1
docker_container_count INTEGER,
docker_root_write_mb REAL, -- 1 分钟 Docker data-root 写入量 MB
disk_usage_pct REAL,
inode_usage_pct REAL,
load_avg REAL
);"
sqlite_exec "CREATE TABLE IF NOT EXISTS alerts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts INTEGER,
level TEXT, -- P0/P1/P2
category TEXT, -- db/http/business/infra
title TEXT,
detail TEXT,
ack INTEGER DEFAULT 0, -- 0=未确认 1=已确认
resolved INTEGER DEFAULT 0 -- 0=未恢复 1=已恢复
);"
sqlite_exec "CREATE INDEX IF NOT EXISTS idx_alerts_ts ON alerts(ts);"
sqlite_exec "CREATE INDEX IF NOT EXISTS idx_alerts_ack ON alerts(ack, resolved);"
sqlite_exec "CREATE TABLE IF NOT EXISTS alert_cooldown (
alert_key TEXT PRIMARY KEY,
last_alert_ts INTEGER
);"
log_info "SQLite schema 初始化完成"