Files
k8s-manifests/n8n/health-check-code-node.js
T
chemavxandClaude Fable 5 e00c520a26 Decomisión de openclaw
- Elimina manifests openclaw/ y argocd/application-openclaw.yaml
- backup-system: fuera del backup diario, crown-jewels, expected y retain;
  fuera el mount hostPath del cronjob y la fila de RESTORE.md
- monitoring: eliminado panel openclaw del dashboard homelab-overview
- n8n: openclaw y polymarket fuera del health-check (espejo del workflow vivo)
- cluster-wide/namespaces.yaml: fuera openclaw + añadidos separadores '---'
  que faltaban (el archivo era un único doc YAML donde ganaba el último ns)

Cluster ya limpio: app ArgoCD, namespace, PVC/PV, RBAC cluster-scoped y
monitor de Uptime Kuma. Backup final: ~/decommissioned/openclaw-final-2026-07-18.tar.gz

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-18 14:57:46 +00:00

107 lines
4.0 KiB
JavaScript

// n8n Code node — Health Check
// Usa https de Node.js (NODE_FUNCTION_ALLOW_BUILTIN=https)
// Patrón async IIFE: compatible con n8n 2.x task runner
const https = require('https');
return await (async () => {
// ── Configuración ────────────────────────────────────────────────────────
const TELEGRAM_TOKEN = process.env.TELEGRAM_BOT_TOKEN /* rotado: leer de env, no hardcodear */;
const TELEGRAM_CHAT = '5138407666';
const RESTART_WEBHOOK = 'https://n8n.chemavx.xyz/webhook/uptime-kuma-restart';
const SERVICES = [
{ name: 'n8n', url: 'https://n8n.chemavx.xyz' },
{ name: 'vaultwarden', url: 'https://vaultwarden.chemavx.xyz' },
{ name: 'grafana', url: 'https://grafana.chemavx.xyz' },
{ name: 'uptime', url: 'https://uptime.chemavx.xyz' },
{ name: 'auth', url: 'https://auth.chemavx.xyz' },
{ name: 'home', url: 'https://home.chemavx.xyz' },
{ name: 'git', url: 'https://git.chemavx.xyz' },
{ name: 'argocd', url: 'https://argocd.chemavx.xyz' },
{ name: 'ollama', url: 'https://ollama.chemavx.xyz/api/tags' },
];
// ── Helpers ──────────────────────────────────────────────────────────────
const checkService = (service) => {
return new Promise((resolve) => {
const req = https.get(service.url, (res) => {
resolve({ name: service.name, url: service.url, ok: res.statusCode < 400, status: res.statusCode });
res.resume();
});
req.on('error', (err) => {
resolve({ name: service.name, url: service.url, ok: false, status: 0, reason: err.message });
});
req.setTimeout(10000, () => {
req.destroy();
resolve({ name: service.name, url: service.url, ok: false, status: 0, reason: 'timeout' });
});
});
};
const httpsPost = (urlStr, body) => {
return new Promise((resolve) => {
const data = JSON.stringify(body);
const u = new URL(urlStr);
const options = {
hostname: u.hostname,
path: u.pathname + u.search,
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) },
};
const req = https.request(options, (res) => {
res.resume();
resolve();
});
req.on('error', () => resolve());
req.setTimeout(10000, () => { req.destroy(); resolve(); });
req.write(data);
req.end();
});
};
const sendTelegram = (text) => {
return httpsPost(
'https://api.telegram.org/bot' + TELEGRAM_TOKEN + '/sendMessage',
{ chat_id: TELEGRAM_CHAT, text, parse_mode: 'Markdown' }
);
};
const triggerRestart = (failedServices) => {
return httpsPost(RESTART_WEBHOOK, { failed: failedServices });
};
// ── Main ─────────────────────────────────────────────────────────────────
const results = await Promise.all(SERVICES.map(checkService));
const failed = results.filter(r => !r.ok);
const passed = results.filter(r => r.ok);
if (failed.length > 0) {
const lines = failed
.map(s => '• *' + s.name + '*: ' + (s.reason || ('HTTP ' + s.status)) + ' (' + s.url + ')')
.join('\n');
const message = '⚠️ *Health Check Failed*\n\n' + lines + '\n\n_Auto-restart triggered_';
await sendTelegram(message);
await triggerRestart(failed);
}
// ── Return ───────────────────────────────────────────────────────────────
return [{
json: {
checked: results.length,
passed: passed.length,
failed: failed.length,
allOk: failed.length === 0,
results,
},
}];
})();