-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.ts
129 lines (113 loc) · 3.78 KB
/
system.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// JAUL: system.ts
/** @hidden */
const moment = require("moment")
/** @hidden */
const os = require("os")
/** @hidden */
const path = require("path")
/** @hidden */
let lastCpuLoad = null
/** Options for [[getInfo]] */
interface GetInfoOptions {
/** If false, labels won't be added to the output (%, MB, etc). Default is true. */
labels: boolean
}
/** Stats about the system, returned by [[getInfo]]. */
interface SystemMetrics {
/** System uptime as human readable string. */
uptime: string
/** System hostname. */
hostname: string
/** Process title. */
title: string
/** System platform name. */
platform: string
/** Total memory available. */
memoryTotal: any
/** Current memory usage. */
memoryUsage: any
/** CPU load average. */
loadAvg: any
/** Number of CPU cores. */
cpuCores: number
/** Process details. */
process: any
}
/** Describes a CPU load metric, returned by [[getCpuLoad]] */
interface CpuLoad {
/** Idle counter */
idle: number
/** Total counter */
total: number
}
/** System Utilities class. */
class SystemUtils {
/**
* Return an object with general and health information about the system.
* @param options - Options to define the output.
* @returns Object with system metrics attached.
*/
static getInfo(options: GetInfoOptions): SystemMetrics {
if (options == null) {
options = {labels: true}
}
let result = {} as SystemMetrics
// Save parsed OS info to the result object.
result.uptime = moment.duration(process.uptime(), "s").humanize()
result.hostname = os.hostname()
result.title = path.basename(process.title)
result.platform = os.platform() + " " + os.arch() + " " + os.release()
result.memoryTotal = (os.totalmem() / 1024 / 1024).toFixed(0)
result.memoryUsage = (100 - (os.freemem() / os.totalmem()) * 100).toFixed(0)
result.cpuCores = os.cpus().length
// Get process memory stats.
const processMemory = process.memoryUsage()
result.process = {
pid: process.pid,
memoryUsed: (processMemory.rss / 1024 / 1024).toFixed(0),
memoryHeapTotal: (processMemory.heapTotal / 1024 / 1024).toFixed(0),
memoryHeapUsed: (processMemory.heapUsed / 1024 / 1024).toFixed(0)
}
// Calculate average CPU load.
if (lastCpuLoad == null) {
lastCpuLoad = this.getCpuLoad()
}
const currentCpuLoad = this.getCpuLoad()
const idleDifference = currentCpuLoad.idle - lastCpuLoad.idle
const totalDifference = currentCpuLoad.total - lastCpuLoad.total
result.loadAvg = 100 - ~~((100 * idleDifference) / totalDifference)
// Add labels to relevant metrics on the output?
if (options.labels) {
result.loadAvg += "%"
result.memoryTotal += " MB"
result.memoryUsage += "%"
result.process.memoryUsed += " MB"
result.process.memoryHeapTotal += " MB"
result.process.memoryHeapUsed += " MB"
}
return result
}
/**
* Get current CPU load, used by getInfo().
* @returns CPU load information with idle and total counters.
*/
static getCpuLoad(): CpuLoad {
let totalIdle = 0
let totalTick = 0
const cpus = os.cpus()
let i = 0
const len = cpus.length
while (i < len) {
const cpu = cpus[i]
for (let t in cpu.times) {
const value = cpu.times[t]
totalTick += value
}
totalIdle += cpu.times.idle
i++
}
return {idle: totalIdle / cpus.length, total: totalTick / cpus.length}
}
}
// Exports...
export = SystemUtils