UNPKG

1.9 kBJavaScriptView Raw
1var _os = require('os');
2
3exports.platform = function(){
4 return process.platform;
5}
6
7exports.cpuCount = function(){
8 return _os.cpus().length;
9}
10
11exports.sysUptime = function(){
12 //seconds
13 return _os.uptime();
14}
15
16exports.processUptime = function(){
17 //seconds
18 return process.uptime();
19}
20
21
22
23// Memory
24
25exports.freemem = function(){
26 return _os.freemem() / ( 1024 * 1024 );
27}
28
29exports.totalmem = function(){
30
31 return _os.totalmem() / ( 1024 * 1024 );
32}
33
34exports.freememPercentage = function(){
35 return _os.freemem() / _os.totalmem();
36}
37
38/*
39* Returns the load average usage for 1, 5 or 15 minutes.
40*/
41exports.loadavg = function(_time){
42
43 if(_time === undefined || (_time !== 5 && _time !== 15) ) _time = 1;
44
45 var loads = _os.loadavg();
46 var v = 0;
47 if(_time == 1) v = loads[0];
48 if(_time == 5) v = loads[1];
49 if(_time == 15) v = loads[2];
50
51 return v;
52}
53
54
55exports.cpuFree = function(callback){
56 getCPUUsage(callback, true);
57}
58
59exports.cpuUsage = function(callback){
60 getCPUUsage(callback, false);
61}
62
63function getCPUUsage(callback, free){
64
65 var stats1 = getCPUInfo();
66 var startIdle = stats1.idle;
67 var startTotal = stats1.total;
68
69 setTimeout(function() {
70 var stats2 = getCPUInfo();
71 var endIdle = stats2.idle;
72 var endTotal = stats2.total;
73
74 var idle = endIdle - startIdle;
75 var total = endTotal - startTotal;
76 var perc = idle / total;
77
78 if(free === true)
79 callback( perc );
80 else
81 callback( (1 - perc) );
82
83 }, 1000 );
84}
85
86function getCPUInfo(callback){
87 var cpus = _os.cpus();
88
89 var user = 0;
90 var nice = 0;
91 var sys = 0;
92 var idle = 0;
93 var irq = 0;
94 var total = 0;
95
96 for(var cpu in cpus){
97
98 user += cpus[cpu].times.user;
99 nice += cpus[cpu].times.nice;
100 sys += cpus[cpu].times.sys;
101 irq += cpus[cpu].times.irq;
102 idle += cpus[cpu].times.idle;
103 }
104
105 var total = user + nice + sys + idle + irq;
106
107 return {'idle': idle, 'total': total};
108}
109