pm2-perfmonitor 2.5.1 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # pm2-perfmonitor
2
2
 
3
- A pm2 module for performance monitor.
3
+ A pm2 module for **zombie process** and **CPU overload** detection.
4
4
 
5
5
  # Features
6
6
 
package/lib/app.js CHANGED
@@ -6,6 +6,7 @@ const {
6
6
  parseParamToArray,
7
7
  parseParamToNumber,
8
8
  parseBool,
9
+ safeToFixed,
9
10
  sleepAsync,
10
11
  getSysCpuUsageByPid,
11
12
  } = require('./utils')
@@ -79,6 +80,15 @@ const cpuOverloadHistory = new Map()
79
80
  const cpuOverloadRestartHistory = new Map()
80
81
  const cpuOverloadRestartFailedHistory = new Map()
81
82
 
83
+ /**
84
+ * @type { Map<number, number> }
85
+ */
86
+ const cpuUsageFromPM2 = new Map()
87
+ /**
88
+ * @type { Map<number, number> }
89
+ */
90
+ const cpuUsageFromSys = new Map()
91
+
82
92
  let isProcessCheckerRunning = false
83
93
 
84
94
  /**
@@ -248,6 +258,28 @@ const processChecker = async () => {
248
258
 
249
259
  const apps = await listAppsAsync()
250
260
 
261
+ cpuUsageFromPM2.clear()
262
+ cpuUsageFromSys.clear()
263
+
264
+ const cpus = apps.map((v) => v.monit?.cpu)
265
+
266
+ const hasAbnormalMonitoringData = cpus.every((v) => typeof v !== 'number')
267
+
268
+ if (hasAbnormalMonitoringData) {
269
+ const details = apps.map((v) => {
270
+ const { name, pid, pm_id, monit } = v
271
+
272
+ return {
273
+ name,
274
+ pid,
275
+ pm_id,
276
+ monit,
277
+ }
278
+ })
279
+
280
+ logger('warn', 'Abnormal monitoring data exists: ', details)
281
+ }
282
+
251
283
  for (const app of apps) {
252
284
  const { name, pid, pm_id, monit, pm2_env } = app
253
285
 
@@ -276,6 +308,9 @@ const processChecker = async () => {
276
308
  const appCpuUsage =
277
309
  typeof sysCpuUsage === 'number' ? sysCpuUsage : pm2CpuUsage
278
310
 
311
+ cpuUsageFromPM2.set(pm_id, pm2CpuUsage)
312
+ cpuUsageFromSys.set(pm_id, sysCpuUsage)
313
+
279
314
  const cpuHistory = setZombieCpuHistory(pm_id, appCpuUsage)
280
315
  const cpuHistory2 = setCpuOverloadHistory(pm_id, appCpuUsage)
281
316
 
@@ -511,6 +546,36 @@ const runModule = () => {
511
546
  return res.join(', ')
512
547
  },
513
548
  })
549
+
550
+ Probe.metric({
551
+ name: 'CPU (pm2)',
552
+ value: () => {
553
+ const res = []
554
+
555
+ for (const [k, v] of cpuUsageFromPM2) {
556
+ res.push([k, v])
557
+ }
558
+
559
+ if (!res.length) return 'N/A'
560
+
561
+ return res.map((v) => `[${v[0]}]:${v[1]}%`).join(';')
562
+ },
563
+ })
564
+
565
+ Probe.metric({
566
+ name: 'CPU (sys)',
567
+ value: () => {
568
+ const res = []
569
+
570
+ for (const [k, v] of cpuUsageFromSys) {
571
+ res.push([k, v])
572
+ }
573
+
574
+ if (!res.length) return 'N/A'
575
+
576
+ return res.map((v) => `[${v[0]}]:${v[1]}%`).join(';')
577
+ },
578
+ })
514
579
  }
515
580
 
516
581
  runModule()
package/lib/utils.js CHANGED
@@ -30,6 +30,20 @@ const parseBool = (value, defaultVal = false) => {
30
30
  return defaultVal
31
31
  }
32
32
 
33
+ /**
34
+ * 保留小数
35
+ * @param { number } value
36
+ * @param { number} [fractionDigits]
37
+ * @returns { number }
38
+ */
39
+ const safeToFixed = (value, fractionDigits = 2) => {
40
+ if (typeof value === 'number') {
41
+ return Number(value.toFixed(fractionDigits))
42
+ }
43
+
44
+ return value
45
+ }
46
+
33
47
  /**
34
48
  * @param { number} duration - sleep duration (ms)
35
49
  */
@@ -47,7 +61,7 @@ const sleepAsync = (duration = 0) => {
47
61
  const getSysCpuUsageByPid = async (pid) => {
48
62
  try {
49
63
  const stats = await pidusage(pid)
50
- return stats.cpu
64
+ return safeToFixed(stats.cpu, 2)
51
65
  } catch (err) {
52
66
  console.error('Call pidusage error:', err)
53
67
  }
@@ -57,6 +71,7 @@ module.exports = {
57
71
  parseParamToArray,
58
72
  parseParamToNumber,
59
73
  parseBool,
74
+ safeToFixed,
60
75
  sleepAsync,
61
76
  getSysCpuUsageByPid,
62
77
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pm2-perfmonitor",
3
- "version": "2.5.1",
4
- "description": "A pm2 module for performance monitoring. Automatically detect zombie processes and restart it",
3
+ "version": "2.6.1",
4
+ "description": "A pm2 module for zombie process and CPU overload detection.",
5
5
  "author": {
6
6
  "name": "elenh",
7
7
  "email": "yisiwings@163.com"
@@ -31,7 +31,8 @@
31
31
  "keywords": [
32
32
  "pm2",
33
33
  "monitor",
34
- "zombie process"
34
+ "zombie process",
35
+ "cpu overload"
35
36
  ],
36
37
  "apps": [
37
38
  {