SPB Git

Repository Public

monitor.ts 3.7 KB 60 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Social Engine & Remote Navigator8 * File             : services/scheduler/src/monitor.ts9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-social11 * Project Website  : https://social.spboucher.ai12 * Created          : 2026-12-0113 * Last Modified    : 2026-12-0114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// One-shot health probe (cron). Exits non-zero and records a critical alert when something is down. Prints no secret.43import { statfsSync } from 'node:fs';44import { loadConfig } from '../../control-api/src/config.ts';45import { Db } from '../../control-api/src/db.ts';46const cfg = loadConfig(); const db = new Db(cfg.databaseUrl, 1); const problems: string[] = [];47const probe = async (name: string, url: string) => { try { const r = await fetch(url, { signal: AbortSignal.timeout(8000) }); if (!r.ok) problems.push(`${name}: HTTP ${r.status}`); } catch (e) { problems.push(`${name}: ${String((e as Error).message)}`); } };48await probe('control-api', `${cfg.controlInternalUrl}/health/ready`);49if (process.env.MLS_FIXTURE_PORT) await probe('fixtures', `http://127.0.0.1:${process.env.MLS_FIXTURE_PORT}/login`);50await probe('navigator-gateway', `http://127.0.0.1:${process.env.MLS_GATEWAY_PORT ?? 8881}/health/ready`);51try { const st = statfsSync(cfg.dataDir); const freeGb = (st.bavail * st.bsize) / 1e9; if (freeGb < Number(process.env.MLS_MIN_FREE_GB ?? 15)) problems.push(`disque: ${freeGb.toFixed(1)} Go libres`); } catch (e) { problems.push(`disque: ${String((e as Error).message)}`); }52try {53  const b = await db.one("SELECT max(at) AS at FROM audit_events WHERE action='backup.ok'");54  if (process.env.MLS_EXPECT_BACKUP === '1' && (!b?.at || Date.now() - new Date(b.at).getTime() > 26 * 3600_000)) problems.push('sauvegarde: aucune depuis 26 h');55  await db.query("UPDATE alerts SET status='resolved', updated_at=now() WHERE status='open' AND kind='monitor' AND $1", [problems.length === 0]);56  if (problems.length) await db.query("INSERT INTO alerts(severity, kind, message, dedupe_key) VALUES ('critical','monitor',$1,'monitor') ON CONFLICT (dedupe_key) WHERE status='open' AND dedupe_key IS NOT NULL DO UPDATE SET message=excluded.message, updated_at=now()", [problems.join(' ; ').slice(0, 900)]);57} catch (e) { problems.push(`base: ${String((e as Error).message)}`); }58console.log(JSON.stringify({ at: new Date().toISOString(), ok: problems.length === 0, problems }));59await db.end().catch(() => {}); process.exit(problems.length ? 1 : 0);60
Files main
Menu

Theme