index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const tx2 = require('tx2')
  2. const SystemInfos = require('./src/SystemInfos.js')
  3. const PM2Infos = require('./src/pm2.js')
  4. class SysMonit {
  5. constructor() {
  6. this.sysinfos = new SystemInfos()
  7. this.report = {}
  8. this.pass = 0
  9. this.pm2infos = new PM2Infos()
  10. this.pm2_report = {}
  11. }
  12. start() {
  13. this.sysinfos.startCollection()
  14. this.report = this.sysinfos.report()
  15. this.pm2infos.startCollection()
  16. this.pm2_report = this.pm2infos.report()
  17. this.bindActions()
  18. setInterval(() => {
  19. if (this.pass++ < 4)
  20. this.bindMetrics()
  21. this.report = this.sysinfos.report()
  22. this.pm2_report = this.pm2infos.report()
  23. this.processContinuousMetrics()
  24. if (process.env.VERBOSE) {
  25. console.log(JSON.stringify(this.report, '', 2))
  26. console.log(JSON.stringify(this.pm2_report, '', 2))
  27. }
  28. }, 1000)
  29. }
  30. bindActions() {
  31. tx2.action('info', (cb) => {
  32. cb(this.sysinfos.report())
  33. })
  34. }
  35. processContinuousMetrics() {
  36. let most_used_disk = this.report.storage.filesystems.reduce((p, v) => {
  37. return (p.use < v.use ? p : v)
  38. })
  39. tx2.metric(`Disk Usage`, '%', () => most_used_disk.use)
  40. tx2.metric(`Disk Size`, 'gb', () => (most_used_disk.size / 1024 / 1024 / 1024).toFixed(2))
  41. let tx5 = 0, rx5 = 0
  42. Object.keys(this.report.network).forEach(iface => {
  43. tx5 += this.report.network[iface].tx_5
  44. rx5 += this.report.network[iface].rx_5
  45. })
  46. tx2.metric(`Total TX`, 'mb/s', () => tx5)
  47. tx2.metric(`Total RX`, 'mb/s', () => rx5)
  48. }
  49. bindMetrics() {
  50. tx2.metric('PM2 CPU Usage', '%', () => this.pm2_report.pm2.cpu)
  51. tx2.metric('PM2 Memory Usage', 'mb', () => this.pm2_report.pm2.mem)
  52. tx2.metric('PM2 Agent CPU Usage', '%', () => this.pm2_report.agent.cpu)
  53. tx2.metric('PM2 Agent Memory Usage', 'mb', () => this.pm2_report.agent.mem)
  54. /**
  55. * From Sysinfo
  56. */
  57. tx2.metric('CPU Usage', '%', () => this.report.cpu.load)
  58. tx2.metric('CPUs Usage', () => this.report.cpu.loads)
  59. tx2.metric('CPU Temperature', '°C', () => this.report.cpu.temperature)
  60. tx2.metric('RAM Total', 'gb', () => this.report.mem.total)
  61. tx2.metric('RAM Free', 'gb', () => this.report.mem.free)
  62. tx2.metric('RAM Active', 'gb', () => this.report.mem.active)
  63. tx2.metric('RAM Available', 'gb', () => this.report.mem.available)
  64. tx2.metric('RAM Usage', '%', () => this.report.mem.usage)
  65. tx2.metric('FD Opened', () => this.report.fd.opened)
  66. tx2.metric('Disk Writes', 'mb/s', () => this.report.storage.io.read)
  67. tx2.metric('Disk Reads', 'mb/s', () => this.report.storage.io.write)
  68. this.report.storage.filesystems.forEach((fss, i) => {
  69. if (!fss.fs) return
  70. tx2.metric(`fs:use:${fss.fs}`, '%', () => this.report.storage.filesystems[i].use)
  71. tx2.metric(`fs:size:${fss.fs}`, 'gb', () => (this.report.storage.filesystems[i].size / 1024 / 1024 / 1024).toFixed(2))
  72. })
  73. Object.keys(this.report.network).forEach(iface => {
  74. tx2.metric(`net:tx_5:${iface}`, 'mb/s', () => this.report.network[iface].tx_5)
  75. tx2.metric(`net:rx_5:${iface}`, 'mb/s', () => this.report.network[iface].rx_5)
  76. tx2.metric(`net:rx_errors_60:${iface}`, '/min', () => this.report.network[iface].rx_errors_60)
  77. tx2.metric(`net:tx_errors_60:${iface}`, '/min', () => this.report.network[iface].tx_errors_60)
  78. tx2.metric(`net:rx_dropped_60:${iface}`, '/min', () => this.report.network[iface].rx_dropped_60)
  79. tx2.metric(`net:tx_dropped_60:${iface}`, '/min', () => this.report.network[iface].tx_dropped_60)
  80. })
  81. if (this.report.graphics.memTotal) {
  82. tx2.metric('graphics:mem:total', 'mb', () => this.report.graphics.memTotal)
  83. tx2.metric('graphics:mem:used', 'mb', () => this.report.graphics.memUsed)
  84. tx2.metric('graphics:temp', '°C', () => this.report.graphics.temperature)
  85. }
  86. //tx2.transpose('report', () => this.report)
  87. }
  88. }
  89. if (require.main === module) {
  90. let sys = new SysMonit()
  91. sys.start()
  92. }