UNPKG

pm2-slack-monitor

Version:

A CLI tool to monitor PM2 processes and send error logs to individual Slack channels via webhook.

80 lines (68 loc) 2.14 kB
const fs = require('fs'); const path = require('path'); const os = require('os'); const CONFIG_DIR = path.join(os.homedir(), '.my-monitor'); const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json'); function ensureConfigFile() { if (!fs.existsSync(CONFIG_DIR)) { fs.mkdirSync(CONFIG_DIR, { recursive: true }); } if (!fs.existsSync(CONFIG_FILE)) { fs.writeFileSync(CONFIG_FILE, JSON.stringify({ processes: {} }, null, 2)); } } function readConfig() { ensureConfigFile(); return JSON.parse(fs.readFileSync(CONFIG_FILE)); } function writeConfig(config) { ensureConfigFile(); fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2)); } function addProcess(name, webhook) { const config = readConfig(); if (config.processes[name]) { console.error(`❌ Process "${name}" already exists. Use 'update' command to modify webhook.`); process.exit(1); } config.processes[name] = webhook; writeConfig(config); console.log(`✅ Process "${name}" added with Slack webhook.`); } function updateProcess(name, webhook) { const config = readConfig(); if (!config.processes[name]) { console.error(`❌ Process "${name}" does not exist. Use 'add' to register it first.`); process.exit(1); } config.processes[name] = webhook; writeConfig(config); console.log(`🔄 Webhook for process "${name}" has been updated.`); } function removeProcess(name) { const config = readConfig(); if (config.processes[name]) { delete config.processes[name]; writeConfig(config); console.log(`❌ Process "${name}" removed from monitoring.`); } else { console.log(`⚠️ Process "${name}" not found.`); } } function getProcessesWithWebhooks() { return readConfig().processes; } function getProcesses() { return Object.keys(readConfig().processes); } function getWebhookForProcess(name) { return readConfig().processes[name]; } module.exports = { addProcess, updateProcess, removeProcess, getProcesses, getProcessesWithWebhooks, getWebhookForProcess };