Merge branch 'main' of https://github.com/Xargana/blahaj-srv
This commit is contained in:
commit
32bf902f83
|
@ -1,6 +1,6 @@
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const ping = require("ping");
|
const ping = require("ping");
|
||||||
const pm2 = require("pm2");
|
const { execSync } = require("child_process");
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
@ -45,54 +45,90 @@ async function checkServers() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkPM2Services() {
|
function checkPM2Services() {
|
||||||
return new Promise((resolve, reject) => {
|
try {
|
||||||
pm2.connect(function(err) {
|
// Use execSync to ensure we get the output immediately
|
||||||
if (err) {
|
const output = execSync('pm2 jlist', { encoding: 'utf8' });
|
||||||
console.error('Error connecting to PM2:', err);
|
|
||||||
pm2.disconnect();
|
try {
|
||||||
resolve();
|
const processList = JSON.parse(output);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pm2.list((err, list) => {
|
// Clear previous status
|
||||||
if (err) {
|
pm2ServicesStatus = {};
|
||||||
console.error('Error getting PM2 process list:', err);
|
|
||||||
pm2.disconnect();
|
processList.forEach(process => {
|
||||||
resolve();
|
pm2ServicesStatus[process.name] = {
|
||||||
return;
|
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: process.pm2_env.pm_uptime ?
|
||||||
|
Math.floor((Date.now() - process.pm2_env.pm_uptime) / 1000) :
|
||||||
|
null,
|
||||||
|
restarts: process.pm2_env.restart_time,
|
||||||
|
lastChecked: new Date().toISOString()
|
||||||
|
};
|
||||||
|
});
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error(`Error parsing PM2 output: ${parseError.message}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error executing pm2 jlist: ${error.message}`);
|
||||||
|
|
||||||
|
// Try an alternative approach using pm2 list
|
||||||
|
try {
|
||||||
|
const output = execSync('pm2 list --format json', { encoding: 'utf8' });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const processList = JSON.parse(output);
|
||||||
|
|
||||||
// Update PM2 services status
|
// Clear previous status
|
||||||
list.forEach(process => {
|
pm2ServicesStatus = {};
|
||||||
|
|
||||||
|
processList.forEach(process => {
|
||||||
pm2ServicesStatus[process.name] = {
|
pm2ServicesStatus[process.name] = {
|
||||||
name: process.name,
|
name: process.name,
|
||||||
id: process.pm_id,
|
id: process.pm_id,
|
||||||
status: process.pm2_env.status,
|
status: process.status || 'unknown',
|
||||||
cpu: process.monit ? process.monit.cpu : null,
|
cpu: process.cpu || null,
|
||||||
memory: process.monit ? process.monit.memory : null,
|
memory: process.memory || null,
|
||||||
uptime: process.pm2_env.pm_uptime ?
|
uptime: null, // Not available in this format
|
||||||
Date.now() - process.pm2_env.pm_uptime :
|
restarts: process.restart || 0,
|
||||||
null,
|
|
||||||
restarts: process.pm2_env.restart_time,
|
|
||||||
lastChecked: new Date().toISOString()
|
lastChecked: new Date().toISOString()
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error(`Error parsing PM2 list output: ${parseError.message}`);
|
||||||
|
}
|
||||||
|
} catch (fallbackError) {
|
||||||
|
console.error(`Error with fallback PM2 command: ${fallbackError.message}`);
|
||||||
|
|
||||||
|
// Last resort: just check if processes are running using ps
|
||||||
|
try {
|
||||||
|
const output = execSync('ps aux | grep pm2', { encoding: 'utf8' });
|
||||||
|
const lines = output.split('\n').filter(line =>
|
||||||
|
line.includes('PM2') && !line.includes('grep')
|
||||||
|
);
|
||||||
|
|
||||||
pm2.disconnect();
|
pm2ServicesStatus = {
|
||||||
resolve();
|
'pm2-daemon': {
|
||||||
});
|
name: 'pm2-daemon',
|
||||||
});
|
status: lines.length > 0 ? 'online' : 'stopped',
|
||||||
});
|
lastChecked: new Date().toISOString()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} catch (psError) {
|
||||||
|
console.error(`Error checking processes: ${psError.message}`);
|
||||||
|
pm2ServicesStatus = { error: 'Unable to check PM2 processes' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkAll() {
|
function checkAll() {
|
||||||
try {
|
checkServers();
|
||||||
await checkServers();
|
checkPM2Services();
|
||||||
await checkPM2Services();
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error in checkAll function:", error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial check with error handling
|
// Initial check with error handling
|
||||||
|
|
Loading…
Reference in a new issue