├── .DS_Store ├── test ├── cpu.js ├── cpufree.js └── test.js ├── package.json ├── LICENSE ├── README.md └── lib └── osutils.js /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oscmejia/os-utils/HEAD/.DS_Store -------------------------------------------------------------------------------- /test/cpu.js: -------------------------------------------------------------------------------- 1 | var os = require('../lib/OSUtils'); 2 | 3 | console.log('\n'); 4 | console.log( 'OS Utils'); 5 | console.log('\n'); 6 | 7 | setInterval(function() { 8 | 9 | os.cpuUsage(function(v){ 10 | console.log( 'CPU Usage (%): ' + v ); 11 | }); 12 | 13 | }, 1000 ); 14 | -------------------------------------------------------------------------------- /test/cpufree.js: -------------------------------------------------------------------------------- 1 | var os = require('../lib/OSUtils'); 2 | 3 | console.log('\n'); 4 | console.log( 'OS Utils'); 5 | console.log('\n'); 6 | 7 | setInterval(function() { 8 | 9 | os.cpuFree(function(v){ 10 | console.log( 'CPU Free (%): ' + v ); 11 | }); 12 | 13 | }, 1000 ); 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "os-utils", 3 | "version" : "0.0.14", 4 | "description" : "an operating-system utility library", 5 | "url" : "http://oscar-mejia.com", 6 | "main" : "./lib/osutils.js", 7 | "directories" : { "test": "./test"}, 8 | 9 | "repository" : { 10 | "type": "git", 11 | "url": "https://github.com/oscmejia/os-utils" 12 | }, 13 | "keywords" : [ 14 | "os", 15 | "operating system", 16 | "server", 17 | "memory", 18 | "cpu", 19 | "monitor", 20 | "stats", 21 | "harddrive" 22 | ], 23 | "author" : { 24 | "name": "Oscar Mejia", 25 | "email": "oscmejia@vovsolutions.com" 26 | }, 27 | "license" : "MIT" 28 | } 29 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var os = require('../lib/OSUtils'); 2 | 3 | console.log('\n'); 4 | console.log( 'OS Utils'); 5 | console.log('\n'); 6 | 7 | console.log( 'Platform: ' + os.platform() ); 8 | console.log( 'CPUs: ' + os.cpuCount() ); 9 | console.log('\n'); 10 | 11 | console.log( 'System Uptime (s): ' + os.sysUptime() ); 12 | console.log( 'Process Uptime (s): ' + os.processUptime() ); 13 | console.log('\n'); 14 | 15 | console.log( 'Free Memory (Kb): ' + os.freemem() ); 16 | console.log( 'total Memory (Kb): ' + os.totalmem() ); 17 | console.log( 'Free Memory (%): ' + os.freememPercentage() ); 18 | console.log('\n'); 19 | 20 | console.log( 'Load Usage (%): ' + os.loadavg() ); 21 | console.log( 'Load Usage 1 (%): ' + os.loadavg(1) ); 22 | console.log( 'Load Usage 5 (%): ' + os.loadavg(5) ); 23 | console.log( 'Load Usage 15 (%): ' + os.loadavg(15) ); 24 | console.log('\n'); 25 | 26 | os.cpuUsage(function(v){ 27 | console.log( 'CPU Usage (%): ' + v ); 28 | }); 29 | 30 | os.cpuFree(function(v){ 31 | console.log( 'CPU Free (%): ' + v ); 32 | }); 33 | 34 | console.log('\n'); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2012 Oscar Mejia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | os-utils 2 | ======== 3 | 4 | An operating system utility library. Some methods are wrappers of node libraries 5 | and others are calculations made by the module. 6 | 7 | 8 | ## Installation 9 | 10 | One line installation with [npm](http://npmjs.org). 11 | ```bash 12 | npm install os-utils 13 | ``` 14 | 15 | Then in your code 16 | ```js 17 | var os = require('os-utils'); 18 | 19 | 20 | os.cpuUsage(function(v){ 21 | console.log( 'CPU Usage (%): ' + v ); 22 | }); 23 | 24 | os.cpuFree(function(v){ 25 | console.log( 'CPU Free:' + v ); 26 | }); 27 | ``` 28 | 29 | 30 | ## Usage 31 | 32 | The following methods are available: 33 | 34 | 35 | ### Calculate CPU usage for the next second 36 | This is not an average of CPU usage like it is in the "os" module. The callback will receive a parameter with the value. 37 | ```js 38 | os.cpuUsage( function(value) { /* ... */ } ); 39 | ``` 40 | 41 | 42 | ### Calculate free CPU for the next second 43 | This is not based on average CPU usage like it is in the "os" module. The callback will receive a parameter with the value. 44 | ```js 45 | os.cpuFree( function(value) { /* ... */ } ); 46 | ``` 47 | 48 | 49 | ### Get the platform name 50 | ```js 51 | os.platform(); 52 | ``` 53 | 54 | 55 | ### Get number of CPUs 56 | ```js 57 | os.cpuCount() 58 | ``` 59 | 60 | 61 | ### Get current free memory 62 | ```js 63 | os.freemem() 64 | ``` 65 | 66 | 67 | ### Get total memory 68 | ```js 69 | os.totalmem() 70 | ``` 71 | 72 | 73 | ### Get a current free memory percentage 74 | ```js 75 | os.freememPercentage() 76 | ``` 77 | 78 | 79 | ### Get the number of seconds the system has been running for 80 | ```js 81 | os.sysUptime(); 82 | ``` 83 | 84 | 85 | ### Get the number of seconds the process has been running 86 | ```js 87 | os.processUptime() 88 | ``` 89 | 90 | 91 | ### Get average load for 1, 5 or 15 minutes 92 | ```js 93 | os.loadavg(1) 94 | os.loadavg(5) 95 | os.loadavg(15) 96 | ``` 97 | -------------------------------------------------------------------------------- /lib/osutils.js: -------------------------------------------------------------------------------- 1 | var _os = require('os'); 2 | 3 | exports.platform = function(){ 4 | return process.platform; 5 | } 6 | 7 | exports.cpuCount = function(){ 8 | return _os.cpus().length; 9 | } 10 | 11 | exports.sysUptime = function(){ 12 | //seconds 13 | return _os.uptime(); 14 | } 15 | 16 | exports.processUptime = function(){ 17 | //seconds 18 | return process.uptime(); 19 | } 20 | 21 | 22 | 23 | // Memory 24 | exports.freemem = function(){ 25 | return _os.freemem() / ( 1024 * 1024 ); 26 | } 27 | 28 | exports.totalmem = function(){ 29 | 30 | return _os.totalmem() / ( 1024 * 1024 ); 31 | } 32 | 33 | exports.freememPercentage = function(){ 34 | return _os.freemem() / _os.totalmem(); 35 | } 36 | 37 | exports.freeCommand = function(callback){ 38 | 39 | // Only Linux 40 | require('child_process').exec('free -m', function(error, stdout, stderr) { 41 | 42 | var lines = stdout.split("\n"); 43 | 44 | 45 | var str_mem_info = lines[1].replace( /[\s\n\r]+/g,' '); 46 | 47 | var mem_info = str_mem_info.split(' ') 48 | 49 | total_mem = parseFloat(mem_info[1]) 50 | free_mem = parseFloat(mem_info[3]) 51 | buffers_mem = parseFloat(mem_info[5]) 52 | cached_mem = parseFloat(mem_info[6]) 53 | 54 | used_mem = total_mem - (free_mem + buffers_mem + cached_mem) 55 | 56 | callback(used_mem, cached_mem); 57 | }); 58 | } 59 | 60 | 61 | // Hard Disk Drive 62 | exports.harddrive = function(callback){ 63 | 64 | require('child_process').exec('df -k', function(error, stdout, stderr) { 65 | 66 | var total = 0; 67 | var used = 0; 68 | var free = 0; 69 | 70 | var lines = stdout.split("\n"); 71 | 72 | var str_disk_info = lines[1].replace( /[\s\n\r]+/g,' '); 73 | 74 | var disk_info = str_disk_info.split(' '); 75 | 76 | total = Math.ceil((disk_info[1] * 1024)/ Math.pow(1024,2)); 77 | used = Math.ceil(disk_info[2] * 1024 / Math.pow(1024,2)) ; 78 | free = Math.ceil(disk_info[3] * 1024 / Math.pow(1024,2)) ; 79 | 80 | callback(total, free, used); 81 | }); 82 | } 83 | 84 | 85 | 86 | // Return process running current 87 | exports.getProcesses = function(nProcess, callback){ 88 | 89 | // if nprocess is undefined then is function 90 | if(typeof nProcess === 'function'){ 91 | 92 | callback =nProcess; 93 | nProcess = 0 94 | } 95 | 96 | command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+10 97 | //command = 'ps aux | head -n '+ 11 98 | //command = 'ps aux | head -n '+ (nProcess + 1) 99 | if (nProcess > 0) 100 | command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+(nProcess + 1) 101 | 102 | require('child_process').exec(command, function(error, stdout, stderr) { 103 | 104 | var that = this 105 | 106 | var lines = stdout.split("\n"); 107 | lines.shift() 108 | lines.pop() 109 | 110 | var result = '' 111 | 112 | 113 | lines.forEach(function(_item,_i){ 114 | 115 | var _str = _item.replace( /[\s\n\r]+/g,' '); 116 | 117 | _str = _str.split(' ') 118 | 119 | // result += _str[10]+" "+_str[9]+" "+_str[2]+" "+_str[3]+"\n"; // process 120 | result += _str[1]+" "+_str[2]+" "+_str[3]+" "+_str[4].substring((_str[4].length - 25))+"\n"; // process 121 | 122 | }); 123 | 124 | callback(result); 125 | }); 126 | } 127 | 128 | 129 | 130 | /* 131 | * Returns All the load average usage for 1, 5 or 15 minutes. 132 | */ 133 | exports.allLoadavg = function(){ 134 | 135 | var loads = _os.loadavg(); 136 | 137 | return loads[0].toFixed(4)+','+loads[1].toFixed(4)+','+loads[2].toFixed(4); 138 | } 139 | 140 | /* 141 | * Returns the load average usage for 1, 5 or 15 minutes. 142 | */ 143 | exports.loadavg = function(_time){ 144 | 145 | if(_time === undefined || (_time !== 5 && _time !== 15) ) _time = 1; 146 | 147 | var loads = _os.loadavg(); 148 | var v = 0; 149 | if(_time == 1) v = loads[0]; 150 | if(_time == 5) v = loads[1]; 151 | if(_time == 15) v = loads[2]; 152 | 153 | return v; 154 | } 155 | 156 | 157 | exports.cpuFree = function(callback){ 158 | getCPUUsage(callback, true); 159 | } 160 | 161 | exports.cpuUsage = function(callback){ 162 | getCPUUsage(callback, false); 163 | } 164 | 165 | function getCPUUsage(callback, free){ 166 | 167 | var stats1 = getCPUInfo(); 168 | var startIdle = stats1.idle; 169 | var startTotal = stats1.total; 170 | 171 | setTimeout(function() { 172 | var stats2 = getCPUInfo(); 173 | var endIdle = stats2.idle; 174 | var endTotal = stats2.total; 175 | 176 | var idle = endIdle - startIdle; 177 | var total = endTotal - startTotal; 178 | var perc = idle / total; 179 | 180 | if(free === true) 181 | callback( perc ); 182 | else 183 | callback( (1 - perc) ); 184 | 185 | }, 1000 ); 186 | } 187 | 188 | function getCPUInfo(callback){ 189 | var cpus = _os.cpus(); 190 | 191 | var user = 0; 192 | var nice = 0; 193 | var sys = 0; 194 | var idle = 0; 195 | var irq = 0; 196 | var total = 0; 197 | 198 | for(var cpu in cpus){ 199 | if (!cpus.hasOwnProperty(cpu)) continue; 200 | user += cpus[cpu].times.user; 201 | nice += cpus[cpu].times.nice; 202 | sys += cpus[cpu].times.sys; 203 | irq += cpus[cpu].times.irq; 204 | idle += cpus[cpu].times.idle; 205 | } 206 | 207 | var total = user + nice + sys + idle + irq; 208 | 209 | return { 210 | 'idle': idle, 211 | 'total': total 212 | }; 213 | } 214 | 215 | --------------------------------------------------------------------------------