UNPKG

5.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs");
4const LogManager_1 = require("../log/LogManager");
5exports.CPUOSMetricNames = ['ctxtSwaps', 'steal', 'softirq', 'irq', 'iowait', 'idle', 'system', 'nice', 'user'];
6exports.LoadOSMetricNames = ['load15', 'load5', 'load1'];
7class OSMetrics {
8}
9exports.OSMetrics = OSMetrics;
10const PROC_STAT = '/proc/stat';
11const PROC_LOADAVG = '/proc/loadavg';
12const logger = LogManager_1.LogManager.getLogger(__filename);
13class OSMetricsService {
14 constructor() {
15 this.procStatExists = fs.existsSync(PROC_STAT);
16 this.procLoadAvgExists = fs.existsSync(PROC_LOADAVG);
17 this.procStatContentReader = () => fs.readFileSync(PROC_STAT, 'utf8');
18 this.procLoadAvgContentReader = () => fs.readFileSync(PROC_LOADAVG, 'utf8');
19 }
20 readCPUStat(oSMetrics) {
21 const fileContent = this.procStatContentReader();
22 // logger.debug(`Got ${PROC_STAT}: ${fileContent}`);
23 this.parseCPUStatFile(fileContent, oSMetrics);
24 }
25 parseCPUStatFile(fileContent, oSMetrics) {
26 const lines = fileContent.split(/[\n\r]+/);
27 lines.forEach((line) => {
28 this.parseCPUStatLine(line, oSMetrics);
29 });
30 }
31 readLoadAvg(oSMetrics) {
32 const fileContent = this.procLoadAvgContentReader();
33 // logger.debug(`Got ${PROC_LOADAVG}: ${fileContent}`);
34 this.parseLoadAvgFile(fileContent, oSMetrics);
35 }
36 parseLoadAvgFile(fileContent, oSMetrics) {
37 const parts = fileContent.split(/[ ]+/);
38 oSMetrics.load1 = parseFloat(parts[0]);
39 oSMetrics.load5 = parseFloat(parts[1]);
40 oSMetrics.load15 = parseFloat(parts[2]);
41 }
42 parseCPUStatLine(line, oSMetrics) {
43 const parts = line.split(/[ ]+/);
44 switch (parts[0]) {
45 case 'cpu':
46 oSMetrics.user = parseInt(parts[1], 10);
47 oSMetrics.nice = parseInt(parts[2], 10);
48 oSMetrics.system = parseInt(parts[3], 10);
49 oSMetrics.idle = parseInt(parts[4], 10);
50 oSMetrics.iowait = parseInt(parts[5], 10);
51 oSMetrics.irq = parseInt(parts[6], 10);
52 oSMetrics.softirq = parseInt(parts[7], 10);
53 oSMetrics.steal = parseInt(parts[8], 10);
54 break;
55 case 'ctxt':
56 oSMetrics.ctxtSwaps = parseInt(parts[1], 10);
57 }
58 }
59 getOSMetrics() {
60 const resp = new OSMetrics();
61 if (this.procStatExists) {
62 try {
63 this.readCPUStat(resp);
64 }
65 catch (e) {
66 logger.warn(e, 'There was an error trying to gather CPU stats');
67 }
68 }
69 if (this.procLoadAvgExists) {
70 try {
71 this.readLoadAvg(resp);
72 }
73 catch (e) {
74 logger.warn(e, 'There was an error trying to gather CPU stats');
75 }
76 }
77 return resp;
78 }
79}
80exports.OSMetricsService = OSMetricsService;
81/*
82 * Example output of /proc/stat
83 * bash-4.3# cat /proc/stat
84 * cpu 7295284 99386 1983452 15027614 314428 0 310148 24617 0 0
85 * cpu0 3501047 46818 959869 7568112 154641 0 265927 11233 0 0
86 * cpu1 3794236 52567 1023583 7459502 159787 0 44220 13384 0 0
87 * intr 598999980 31 9 0 0 1748 0 0 0 2 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 108575587 27725464 0 0 2397087 0 106721474 72735065 0 0 2494153 0 161 14493467 79855109 78647982 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
88 * ctxt 717991087
89 * btime 1520765073
90 * processes 908712
91 * procs_running 1
92 * procs_blocked 0
93 * softirq 507911845 0 55816109 30052 291757370 0 0 30751 36539917 0 123737646
94 *
95 * bash-4.3# cat /proc/loadavg
96 * 1.08 1.28 1.47 4/1304 99
97 */
98//# sourceMappingURL=OSMetricsService.js.map
\No newline at end of file