// UATU const CronJob = require('cron').CronJob; const pm2 = require('pm2'); const logger = require('log4js').getLogger('WATCHER'); const nodeFree = require('node-free'); // load env variables from file require('dotenv').config(); logger.level = 'trace'; function formatBytes(bytes, decimals = 2) { if (bytes === 0) return '0 Bytes'; const k = 1024; const dm = decimals < 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) } ${ sizes[i]}`; } async function logMemory() { pm2.list((err, processDescriptionList) => { if (err) logger.error(err); else for (const item of processDescriptionList) { // logger.debug(JSON.stringify(item)); const { pid, name, monit } = item; const { memory, cpu } = monit; if (name !== 'watcher') logger.info(`${name} :: PID:${pid} :: MEMORY:${formatBytes(memory)} :: CPU:${cpu}`); } }); logger.info(`Total:${formatBytes(nodeFree.total())} :: Used:${formatBytes(nodeFree.used())} :: Free:${formatBytes(nodeFree.free())}`); } async function run() { pm2.connect(() => { logMemory(); }); new CronJob('*/5 * * * *', async () => { await logMemory(); }, null, true); logger.info('Watcher Launched'); } process.once('uncaughtException', function caught(err) { logger.error('Uncaught', err); }); run();