HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/server/nodejs/v16.9.0/lib/node_modules/pm2/node_modules/pm2-sysmonit/src/pm2.js
const fs = require('fs')
const path = require('path')
const pidusage = require('pidusage')

class PM2Monitoring {
  constructor() {
    this.pm2_monitoring = { cpu: 0, mem: 0 }
    this.pm2_agent_monitoring = { cpu: 0, mem: 0 }
  }

  startCollection() {
    setInterval(() => {
      this.monitorPM2Agent()
      this.monitorPM2()
    }, 900)
  }

  getDefaultPM2Home() {
    var PM2_ROOT_PATH;

    if (process.env.PM2_HOME)
      PM2_ROOT_PATH = process.env.PM2_HOME;
    else if (process.env.HOME && !process.env.HOMEPATH)
      PM2_ROOT_PATH = path.resolve(process.env.HOME, '.pm2');
    else if (process.env.HOME || process.env.HOMEPATH)
      PM2_ROOT_PATH = path.resolve(process.env.HOMEDRIVE, process.env.HOME || process.env.HOMEPATH, '.pm2');
    else {
      console.error('[PM2][Initialization] Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!');
      console.error('[PM2][Initialization] Defaulting to /etc/.pm2');
      PM2_ROOT_PATH = path.resolve('/etc', '.pm2');
    }

    return PM2_ROOT_PATH;
  }


  report() {
    return {
      pm2: this.pm2_monitoring,
      agent: this.pm2_agent_monitoring
    }
  }

  monitorPM2() {
    let pm2_pid_file = path.join(this.getDefaultPM2Home(), 'pm2.pid')

    fs.readFile(pm2_pid_file, (err, pm2_pid) => {
      if (err) return console.error(`Could not read ${pm2_pid_file}`)
      if (!pm2_pid) return console.error(`PID is null`)

      pm2_pid = parseInt(pm2_pid)

      pidusage(pm2_pid, (err, stats) => {
        if (err) return console.error(err)
        this.pm2_monitoring = {
          cpu: stats.cpu.toFixed(1),
          mem: (stats.memory / 1024 / 1024).toFixed(1)
        }
      })
    })
  }

  monitorPM2Agent() {
    let pm2_agent_pid_file = path.join(this.getDefaultPM2Home(), 'agent.pid')

    fs.readFile(pm2_agent_pid_file, (err, pm2_agent_pid) => {
      if (err) return
      if (!pm2_agent_pid) return

      pidusage(pm2_agent_pid, (err, stats) => {
        if (err) return
        this.pm2_agent_monitoring = {
          cpu: stats.cpu.toFixed(1),
          mem: (stats.memory / 1024 / 1024).toFixed(1)
        }
      })
    })
  }

}

module.exports = PM2Monitoring