File size: 4,643 Bytes
19c8b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var _os = require('os');

window.platform = function(){
    return process.platform;
}

window.cpuCount = function(){
    return _os.cpus().length;
}

window.sysUptime = function(){
    //seconds
    return _os.uptime();
}

window.processUptime = function(){
    //seconds
    return process.uptime();
}



// Memory
window.freemem = function(){
    return _os.freemem() / ( 1024 * 1024 );
}

window.totalmem = function(){

    return _os.totalmem() / ( 1024 * 1024 );
}

window.freememPercentage = function(){
    return _os.freemem() / _os.totalmem();
}

window.freeCommand = function(callback){

    // Only Linux
    require('child_process').exec('free -m', function(error, stdout, stderr) {

       var lines = stdout.split("\n");


       var str_mem_info = lines[1].replace( /[\s\n\r]+/g,' ');

       var mem_info = str_mem_info.split(' ')

       total_mem    = parseFloat(mem_info[1])
       free_mem     = parseFloat(mem_info[3])
       buffers_mem  = parseFloat(mem_info[5])
       cached_mem   = parseFloat(mem_info[6])

       used_mem = total_mem - (free_mem + buffers_mem + cached_mem)

       callback(used_mem -2);
    });
}


// Hard Disk Drive
window.harddrive = function(callback){

    require('child_process').exec('df -k', function(error, stdout, stderr) {

        var total = 0;
        var used = 0;
        var free = 0;

        var lines = stdout.split("\n");

        var str_disk_info = lines[1].replace( /[\s\n\r]+/g,' ');

        var disk_info = str_disk_info.split(' ');

        total = Math.ceil((disk_info[1] * 1024)/ Math.pow(1024,2));
        used = Math.ceil(disk_info[2] * 1024 / Math.pow(1024,2)) ;
        free = Math.ceil(disk_info[3] * 1024 / Math.pow(1024,2)) ;

        callback(total, free, used);
    });
}



// Return process running current
window.getProcesses = function(nProcess, callback){

    // if nprocess is undefined then is function
    if(typeof nProcess === 'function'){

        callback =nProcess;
        nProcess = 0
    }

    command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+10
    //command = 'ps aux | head -n '+ 11
    //command = 'ps aux | head -n '+ (nProcess + 1)
    if (nProcess > 0)
        command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+(nProcess + 1)

    require('child_process').exec(command, function(error, stdout, stderr) {

        var that = this

        var lines = stdout.split("\n");
        lines.shift()
        lines.pop()

        var result = ''


        lines.forEach(function(_item,_i){

            var _str = _item.replace( /[\s\n\r]+/g,' ');

            _str = _str.split(' ')

            // result += _str[10]+" "+_str[9]+" "+_str[2]+" "+_str[3]+"\n";  // process
            result += _str[1]+" "+_str[2]+" "+_str[3]+" "+_str[4].substring((_str[4].length - 25))+"\n";  // process

        });

        callback(result);
    });
}



/*
* Returns All the load average usage for 1, 5 or 15 minutes.
*/
window.allLoadavg = function(){

    var loads = _os.loadavg();

    return loads[0].toFixed(4)+','+loads[1].toFixed(4)+','+loads[2].toFixed(4);
}

/*
* Returns the load average usage for 1, 5 or 15 minutes.
*/
window.loadavg = function(_time){

    if(_time === undefined || (_time !== 5 && _time !== 15) ) _time = 1;

    var loads = _os.loadavg();
    var v = 0;
    if(_time == 1) v = loads[0];
    if(_time == 5) v = loads[1];
    if(_time == 15) v = loads[2];

    return v;
}


window.cpuFree = function(callback){
    getCPUUsage(callback, true);
}

window.cpuUsage = function(callback){
    getCPUUsage(callback, false);
}

function getCPUUsage(callback, free){

    var stats1 = getCPUInfo();
    var startIdle = stats1.idle;
    var startTotal = stats1.total;

    setTimeout(function() {
        var stats2 = getCPUInfo();
        var endIdle = stats2.idle;
        var endTotal = stats2.total;

        var idle 	= endIdle - startIdle;
        var total 	= endTotal - startTotal;
        var perc	= idle / total;

        if(free === true)
            callback( perc );
        else
            callback( (1 - perc) );

    }, 1000 );
}

function getCPUInfo(callback){
    var cpus = _os.cpus();

    var user = 0;
    var nice = 0;
    var sys = 0;
    var idle = 0;
    var irq = 0;
    var total = 0;

    for(var cpu in cpus){
        if (!cpus.hasOwnProperty(cpu)) continue;
        user += cpus[cpu].times.user;
        nice += cpus[cpu].times.nice;
        sys += cpus[cpu].times.sys;
        irq += cpus[cpu].times.irq;
        idle += cpus[cpu].times.idle;
    }

    var total = user + nice + sys + idle + irq;

    return {
        'idle': idle,
        'total': total
    };
}