fix: check-alert v3 修复钉钉推送断档 P0 假阳性
根因:sqlite_exec 吞掉 stderr(2>/dev/null),查询失败返回空串, 0 默认为 0 导致误报 修复:业务层取数显式调用 sqlite3 + 检查退出码 + 正则校验, 查询失败跳过检测并 warn 日志
This commit is contained in:
1 parent
7ab75364b1
commit
20fa48fb34
17 files changed
+1747
-26
No files matched your search
@@ -0,0 +1,248 @@
|
||||
#!/usr/bin/env python3
|
||||
# ============================================================================
|
||||
# OA Monitor · Python HTTP API(纯标准库,无第三方依赖)
|
||||
# 唯一职责:读 SQLite → 吐 JSON
|
||||
# 只读,绝不写 SQLite;所有 POST/PUT/DELETE 返回 405
|
||||
# ============================================================================
|
||||
import http.server, json, sqlite3, os, sys, urllib.parse
|
||||
from datetime import datetime, timezone
|
||||
|
||||
DB_PATH = "/opt/oa-monitor/data/oa-monitor.db"
|
||||
DASHBOARD_DIR = "/opt/oa-monitor/dashboard"
|
||||
|
||||
def get_db():
|
||||
return sqlite3.connect(DB_PATH, timeout=5)
|
||||
|
||||
def ts_to_iso(ts):
|
||||
if not ts: return None
|
||||
return datetime.fromtimestamp(int(ts)).strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
def fetch_all(sql, params=()):
|
||||
db = get_db()
|
||||
db.row_factory = sqlite3.Row
|
||||
try:
|
||||
rows = db.execute(sql, params).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
def fetch_one(sql, params=()):
|
||||
rows = fetch_all(sql, params)
|
||||
return rows[0] if rows else None
|
||||
|
||||
class MonitorHandler(http.server.BaseHTTPRequestHandler):
|
||||
# ---- CORS + JSON 头 ----
|
||||
def _send_json(self, data, code=200):
|
||||
body = json.dumps(data, ensure_ascii=False, default=str).encode()
|
||||
self.send_response(code)
|
||||
self.send_header('Content-Type', 'application/json; charset=utf-8')
|
||||
self.send_header('Content-Length', str(len(body)))
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
|
||||
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_OPTIONS(self):
|
||||
self._send_json({})
|
||||
|
||||
# ---- 路由 ----
|
||||
def do_GET(self):
|
||||
parsed = urllib.parse.urlparse(self.path)
|
||||
path = parsed.path.rstrip('/')
|
||||
qs = urllib.parse.parse_qs(parsed.query)
|
||||
|
||||
try:
|
||||
if path.startswith('/api/'):
|
||||
self._route_api(path, qs)
|
||||
else:
|
||||
self._route_static(path)
|
||||
except Exception as e:
|
||||
self._send_json({"error": str(e)}, 500)
|
||||
|
||||
def _route_api(self, path, qs):
|
||||
if path == '/api/health':
|
||||
self._health()
|
||||
elif path == '/api/overview':
|
||||
self._overview()
|
||||
elif path == '/api/db/recent':
|
||||
self._db_recent(qs)
|
||||
elif path == '/api/db/long_queries':
|
||||
self._db_long_queries(qs)
|
||||
elif path == '/api/http/recent':
|
||||
self._http_recent(qs)
|
||||
elif path == '/api/business/recent':
|
||||
self._biz_recent(qs)
|
||||
elif path == '/api/infra/recent':
|
||||
self._infra_recent(qs)
|
||||
elif path == '/api/alerts':
|
||||
self._alerts(qs)
|
||||
else:
|
||||
self._send_json({"error": "not found", "path": path}, 404)
|
||||
|
||||
# ---- 静态文件服务 ----
|
||||
def _route_static(self, path):
|
||||
# 安全:禁止 .. 和 /etc 等
|
||||
if '..' in path or path.startswith('/etc'):
|
||||
self._send_json({"error": "forbidden"}, 403); return
|
||||
|
||||
# 根路径 → index.html
|
||||
if path == '' or path == '/':
|
||||
path = '/index.html'
|
||||
|
||||
filepath = os.path.join(DASHBOARD_DIR, path.lstrip('/'))
|
||||
# 双重安全
|
||||
if not filepath.startswith(DASHBOARD_DIR):
|
||||
self._send_json({"error": "forbidden"}, 403); return
|
||||
|
||||
if not os.path.isfile(filepath):
|
||||
self._send_json({"error": "not found", "path": path}, 404); return
|
||||
|
||||
with open(filepath, 'rb') as f:
|
||||
content = f.read()
|
||||
|
||||
ext = os.path.splitext(filepath)[1].lower()
|
||||
mime = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.js': 'application/javascript; charset=utf-8',
|
||||
'.json': 'application/json; charset=utf-8',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.png': 'image/png',
|
||||
'.ico': 'image/x-icon',
|
||||
}.get(ext, 'application/octet-stream')
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', mime)
|
||||
self.send_header('Content-Length', str(len(content)))
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header('Cache-Control', 'no-cache')
|
||||
self.end_headers()
|
||||
self.wfile.write(content)
|
||||
|
||||
def do_POST(self): self._send_json({"error": "read-only"}, 405)
|
||||
def do_PUT(self): self._send_json({"error": "read-only"}, 405)
|
||||
def do_DELETE(self): self._send_json({"error": "read-only"}, 405)
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
pass # 静默日志
|
||||
|
||||
# ---- Endpoint 实现 ----
|
||||
def _health(self):
|
||||
db_ok = os.path.exists(DB_PATH)
|
||||
db_size = os.path.getsize(DB_PATH) if db_ok else 0
|
||||
# 最新写入时间
|
||||
last = fetch_one("SELECT MAX(ts) AS last_ts FROM db_metrics")
|
||||
self._send_json({
|
||||
"service": "oa-monitor-api",
|
||||
"db_exists": db_ok,
|
||||
"db_size_bytes": db_size,
|
||||
"latest_data_ts": ts_to_iso(last['last_ts'] if last else None),
|
||||
"now": ts_to_iso(int(datetime.now().timestamp()))
|
||||
})
|
||||
|
||||
def _overview(self):
|
||||
# 各层最新一条
|
||||
db = fetch_one("SELECT * FROM db_metrics ORDER BY ts DESC LIMIT 1")
|
||||
http_all = fetch_all("SELECT endpoint, status_code, resp_time_ms, healthy, ts FROM http_metrics WHERE ts > ? ORDER BY ts DESC", ((datetime.now().timestamp() - 120),))
|
||||
biz = fetch_one("SELECT * FROM business_metrics ORDER BY ts DESC LIMIT 1")
|
||||
infra = fetch_one("SELECT * FROM infra_metrics ORDER BY ts DESC LIMIT 1")
|
||||
|
||||
# 活跃告警
|
||||
active_alerts = fetch_all("SELECT * FROM alerts WHERE ack = 0 ORDER BY ts DESC")
|
||||
|
||||
# HTTP 端点最新状态聚合
|
||||
http_summary = []
|
||||
for row in http_all:
|
||||
http_summary.append({
|
||||
"endpoint": row["endpoint"],
|
||||
"status_code": row["status_code"],
|
||||
"resp_time_ms": row["resp_time_ms"],
|
||||
"healthy": row["healthy"],
|
||||
"ts": ts_to_iso(row["ts"])
|
||||
})
|
||||
|
||||
# DB 层最近长查询
|
||||
long_q = fetch_all("SELECT * FROM db_processlist WHERE time_sec >= 30 ORDER BY time_sec DESC LIMIT 5")
|
||||
|
||||
self._send_json({
|
||||
"db_latest": {
|
||||
**({} if not db else db),
|
||||
"ts_iso": ts_to_iso(db["ts"]) if db else None
|
||||
},
|
||||
"http_summary": http_summary,
|
||||
"biz_latest": {
|
||||
**({} if not biz else biz),
|
||||
"ts_iso": ts_to_iso(biz["ts"]) if biz else None
|
||||
},
|
||||
"infra_latest": {
|
||||
**({} if not infra else infra),
|
||||
"ts_iso": ts_to_iso(infra["ts"]) if infra else None
|
||||
},
|
||||
"active_alerts": [{**a, "ts_iso": ts_to_iso(a["ts"])} for a in active_alerts],
|
||||
"long_queries": [{**q, "ts_iso": ts_to_iso(q["ts"])} for q in long_q]
|
||||
})
|
||||
|
||||
def _db_recent(self, qs):
|
||||
minutes = int(qs.get('minutes', [30])[0])
|
||||
since = int(datetime.now().timestamp()) - minutes * 60
|
||||
rows = fetch_all(
|
||||
"SELECT * FROM db_metrics WHERE ts >= ? ORDER BY ts ASC",
|
||||
(since,)
|
||||
)
|
||||
self._send_json([{**r, "ts_iso": ts_to_iso(r["ts"])} for r in rows])
|
||||
|
||||
def _db_long_queries(self, qs):
|
||||
since_min = int(qs.get('since_minutes', [60])[0])
|
||||
since = int(datetime.now().timestamp()) - since_min * 60
|
||||
rows = fetch_all(
|
||||
"SELECT * FROM db_processlist WHERE ts >= ? ORDER BY time_sec DESC LIMIT 100",
|
||||
(since,)
|
||||
)
|
||||
self._send_json([{**r, "ts_iso": ts_to_iso(r["ts"])} for r in rows])
|
||||
|
||||
def _http_recent(self, qs):
|
||||
minutes = int(qs.get('minutes', [30])[0])
|
||||
since = int(datetime.now().timestamp()) - minutes * 60
|
||||
rows = fetch_all(
|
||||
"SELECT * FROM http_metrics WHERE ts >= ? ORDER BY ts ASC",
|
||||
(since,)
|
||||
)
|
||||
self._send_json([{**r, "ts_iso": ts_to_iso(r["ts"])} for r in rows])
|
||||
|
||||
def _biz_recent(self, qs):
|
||||
hours = int(qs.get('hours', [24])[0])
|
||||
since = int(datetime.now().timestamp()) - hours * 3600
|
||||
rows = fetch_all(
|
||||
"SELECT * FROM business_metrics WHERE ts >= ? ORDER BY ts ASC",
|
||||
(since,)
|
||||
)
|
||||
self._send_json([{**r, "ts_iso": ts_to_iso(r["ts"])} for r in rows])
|
||||
|
||||
def _infra_recent(self, qs):
|
||||
minutes = int(qs.get('minutes', [30])[0])
|
||||
since = int(datetime.now().timestamp()) - minutes * 60
|
||||
rows = fetch_all(
|
||||
"SELECT * FROM infra_metrics WHERE ts >= ? ORDER BY ts ASC",
|
||||
(since,)
|
||||
)
|
||||
self._send_json([{**r, "ts_iso": ts_to_iso(r["ts"])} for r in rows])
|
||||
|
||||
def _alerts(self, qs):
|
||||
limit = int(qs.get('limit', [100])[0])
|
||||
only_unack = qs.get('unack', ['0'])[0]
|
||||
sql = "SELECT * FROM alerts"
|
||||
params = []
|
||||
if only_unack == '1':
|
||||
sql += " WHERE ack = 0"
|
||||
sql += " ORDER BY ts DESC LIMIT ?"
|
||||
params.append(limit)
|
||||
rows = fetch_all(sql, tuple(params))
|
||||
self._send_json([{**r, "ts_iso": ts_to_iso(r["ts"])} for r in rows])
|
||||
|
||||
# ---- 启动 ----
|
||||
if __name__ == '__main__':
|
||||
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8455
|
||||
server = http.server.HTTPServer(('0.0.0.0', port), MonitorHandler)
|
||||
print(f"OA Monitor API listening on :{port} (DB={DB_PATH})", flush=True)
|
||||
server.serve_forever()
|
||||
Reference in new issue
Block a user