Merge branch 'main' of https://github.com/Xargana/blahaj-srv
This commit is contained in:
commit
e05db3ba4d
5 changed files with 313 additions and 42 deletions
|
|
@ -3,6 +3,19 @@ const ping = require("ping");
|
|||
const pm2 = require("pm2");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const axios = require("axios");
|
||||
const admin = require("firebase-admin");
|
||||
|
||||
// Initialize Firebase Admin SDK
|
||||
try {
|
||||
const serviceAccount = require("../../firebase-service-account.json");
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount)
|
||||
});
|
||||
console.log("Firebase Admin SDK initialized successfully");
|
||||
} catch (error) {
|
||||
console.error("Error initializing Firebase Admin SDK:", error);
|
||||
}
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
|
|
@ -30,6 +43,10 @@ function ensureLogDirectories() {
|
|||
}
|
||||
}
|
||||
|
||||
// Track previous states for notifications
|
||||
let previousServersStatus = {};
|
||||
let previousPM2Status = {};
|
||||
|
||||
let serversStatus = {};
|
||||
REMOTE_SERVERS.forEach(server => {
|
||||
serversStatus[server.name] = {
|
||||
|
|
@ -37,11 +54,41 @@ REMOTE_SERVERS.forEach(server => {
|
|||
lastChecked: null,
|
||||
responseTime: null,
|
||||
};
|
||||
// Initialize previous status
|
||||
previousServersStatus[server.name] = false;
|
||||
});
|
||||
|
||||
// Add PM2 services status object
|
||||
let pm2ServicesStatus = {};
|
||||
|
||||
// Function to send FCM notification
|
||||
async function sendFCMNotification(message, topic) {
|
||||
try {
|
||||
if (!admin.apps.length) {
|
||||
console.warn("Firebase Admin not initialized, skipping notification");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the message object according to Firebase Admin SDK format
|
||||
const fcmMessage = {
|
||||
topic: topic,
|
||||
notification: {
|
||||
title: 'Server Status Alert',
|
||||
body: message
|
||||
},
|
||||
data: {
|
||||
type: 'server_status',
|
||||
timestamp: Date.now().toString()
|
||||
}
|
||||
};
|
||||
|
||||
await admin.messaging().send(fcmMessage);
|
||||
console.log(`Notification sent: ${message}`);
|
||||
} catch (error) {
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function checkServers() {
|
||||
try {
|
||||
ensureLogDirectories();
|
||||
|
|
@ -51,13 +98,37 @@ async function checkServers() {
|
|||
const res = await ping.promise.probe(server.host, {
|
||||
timeout: 4, // Set a timeout of 4 seconds
|
||||
});
|
||||
serversStatus[server.name].online = res.alive;
|
||||
|
||||
// Get previous status before updating
|
||||
const wasOnline = previousServersStatus[server.name];
|
||||
const isNowOnline = res.alive;
|
||||
|
||||
// Update status
|
||||
serversStatus[server.name].online = isNowOnline;
|
||||
serversStatus[server.name].responseTime = res.time;
|
||||
|
||||
// Send notifications for status changes
|
||||
if (wasOnline === false && isNowOnline) {
|
||||
await sendFCMNotification(`Server ${server.name} is back online`, 'service_online');
|
||||
} else if (wasOnline === true && !isNowOnline) {
|
||||
await sendFCMNotification(`Server ${server.name} is offline`, 'service_offline');
|
||||
}
|
||||
|
||||
// Update previous status
|
||||
previousServersStatus[server.name] = isNowOnline;
|
||||
|
||||
} catch (error) {
|
||||
console.error(`Error pinging ${server.host}:`, error);
|
||||
serversStatus[server.name].online = false;
|
||||
serversStatus[server.name].responseTime = null;
|
||||
|
||||
// Check if status changed from online to offline
|
||||
if (previousServersStatus[server.name] === true) {
|
||||
await sendFCMNotification(`Server ${server.name} is unreachable`, 'service_offline');
|
||||
previousServersStatus[server.name] = false;
|
||||
}
|
||||
}
|
||||
|
||||
serversStatus[server.name].lastChecked = new Date().toISOString();
|
||||
|
||||
// Log server status to the appropriate folder
|
||||
|
|
@ -85,47 +156,73 @@ async function checkServers() {
|
|||
}
|
||||
|
||||
async function checkPM2Services() {
|
||||
return new Promise((resolve, reject) => {
|
||||
pm2.connect(function(err) {
|
||||
if (err) {
|
||||
console.error('Error connecting to PM2:', err);
|
||||
pm2.disconnect();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
pm2.list((err, list) => {
|
||||
if (err) {
|
||||
console.error('Error getting PM2 process list:', err);
|
||||
pm2.disconnect();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// Update PM2 services status
|
||||
list.forEach(process => {
|
||||
// Calculate uptime correctly - pm_uptime is a timestamp, not a duration
|
||||
const uptimeMs = process.pm2_env.pm_uptime ?
|
||||
Date.now() - process.pm2_env.pm_uptime :
|
||||
null;
|
||||
|
||||
pm2ServicesStatus[process.name] = {
|
||||
name: process.name,
|
||||
id: process.pm_id,
|
||||
status: process.pm2_env.status,
|
||||
cpu: process.monit ? process.monit.cpu : null,
|
||||
memory: process.monit ? process.monit.memory : null,
|
||||
uptime: uptimeMs, // Store the uptime in milliseconds
|
||||
restarts: process.pm2_env.restart_time,
|
||||
lastChecked: new Date().toISOString()
|
||||
};
|
||||
});
|
||||
|
||||
pm2.disconnect();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
pm2.connect(function(err) {
|
||||
if (err) {
|
||||
console.error('Error connecting to PM2:', err);
|
||||
pm2.disconnect();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
pm2.list(async (err, list) => {
|
||||
if (err) {
|
||||
console.error('Error getting PM2 process list:', err);
|
||||
pm2.disconnect();
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Process each PM2 service sequentially with proper async handling
|
||||
for (const process of list) {
|
||||
const uptimeMs = process.pm2_env.pm_uptime ?
|
||||
Date.now() - process.pm2_env.pm_uptime :
|
||||
null;
|
||||
|
||||
const processName = process.name;
|
||||
const isNowOnline = process.pm2_env.status === 'online';
|
||||
|
||||
// Check if we've seen this process before
|
||||
if (previousPM2Status[processName] === undefined) {
|
||||
// First time seeing this process - initialize and don't send notification
|
||||
previousPM2Status[processName] = isNowOnline;
|
||||
console.log(`Initializing PM2 service status for ${processName}: ${isNowOnline ? 'online' : 'offline'}`);
|
||||
}
|
||||
// Check if status changed
|
||||
else if (previousPM2Status[processName] === false && isNowOnline) {
|
||||
await sendFCMNotification(`PM2 service ${processName} is back online`, 'service_online');
|
||||
console.log(`PM2 service ${processName} changed from offline to online`);
|
||||
}
|
||||
else if (previousPM2Status[processName] === true && !isNowOnline) {
|
||||
await sendFCMNotification(`PM2 service ${processName} is offline (status: ${process.pm2_env.status})`, 'service_offline');
|
||||
console.log(`PM2 service ${processName} changed from online to ${process.pm2_env.status}`);
|
||||
}
|
||||
|
||||
// Update previous status
|
||||
previousPM2Status[processName] = isNowOnline;
|
||||
|
||||
// Update status object
|
||||
pm2ServicesStatus[processName] = {
|
||||
name: processName,
|
||||
id: process.pm_id,
|
||||
status: process.pm2_env.status,
|
||||
cpu: process.monit ? process.monit.cpu : null,
|
||||
memory: process.monit ? process.monit.memory : null,
|
||||
uptime: uptimeMs,
|
||||
restarts: process.pm2_env.restart_time,
|
||||
lastChecked: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error processing PM2 services:', error);
|
||||
}
|
||||
|
||||
pm2.disconnect();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function checkAll() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue