mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-04 05:28:20 +03:00
1 line
4.8 KiB
JavaScript
1 line
4.8 KiB
JavaScript
var PDH_FMT_LONG=256;var PDH_FMT_DOUBLE=512;var promise=require("promise");if(process.platform=="win32"){var GM=require("_GenericMarshal");GM.kernel32=GM.CreateNativeProxy("kernel32.dll");GM.kernel32.CreateMethod("GlobalMemoryStatusEx");GM.pdh=GM.CreateNativeProxy("pdh.dll");GM.pdh.CreateMethod("PdhAddEnglishCounterA");GM.pdh.CreateMethod("PdhCloseQuery");GM.pdh.CreateMethod("PdhCollectQueryData");GM.pdh.CreateMethod("PdhGetFormattedCounterValue");GM.pdh.CreateMethod("PdhGetFormattedCounterArrayA");GM.pdh.CreateMethod("PdhOpenQueryA");GM.pdh.CreateMethod("PdhRemoveCounter")}function windows_cpuUtilization(){var b=new promise(function(d,c){this._res=d;this._rej=c});b.counter=GM.CreateVariable(16);b.cpu=GM.CreatePointer();b.cpuTotal=GM.CreatePointer();var a=0;if((a=GM.pdh.PdhOpenQueryA(0,0,b.cpu).Val)!=0){b._rej(a);return}if((a=GM.pdh.PdhAddEnglishCounterA(b.cpu.Deref(),GM.CreateVariable("\\Processor(*)\\% Processor Time"),0,b.cpuTotal).Val)!=0){b._rej(a);return}if((a=GM.pdh.PdhCollectQueryData(b.cpu.Deref()).Val!=0)){b._rej(a);return}b._timeout=setTimeout(function(k){var m={cpus:[]};var d=GM.CreateVariable(4);var j=GM.CreateVariable(4);var c,l,h;var f;if((f=GM.pdh.PdhCollectQueryData(k.cpu.Deref()).Val!=0)){k._rej(f);return}if((f=GM.pdh.PdhGetFormattedCounterArrayA(k.cpuTotal.Deref(),PDH_FMT_DOUBLE,d,j,0).Val)==-2147481646){c=GM.CreateVariable(d.toBuffer().readUInt32LE())}else{k._rej(f);return}if((f=GM.pdh.PdhGetFormattedCounterArrayA(k.cpuTotal.Deref(),PDH_FMT_DOUBLE,d,j,c).Val)!=0){k._rej(f);return}for(var g=0;g<j.toBuffer().readUInt32LE();++g){h=c.Deref(g*24,24);l=h.Deref(0,GM.PointerSize).Deref();if(l.String=="_Total"){m.total=h.Deref(16,8).toBuffer().readDoubleLE().toFixed(2)}else{m.cpus[parseInt(l.String)]=h.Deref(16,8).toBuffer().readDoubleLE().toFixed(2)}}GM.pdh.PdhRemoveCounter(k.cpuTotal.Deref());GM.pdh.PdhCloseQuery(k.cpu.Deref());b._res(m)},100,b);return(b)}function windows_memUtilization(){var a=GM.CreateVariable(64);a.Deref(0,4).toBuffer().writeUInt32LE(64);GM.kernel32.GlobalMemoryStatusEx(a);var b={MemTotal:require("bignum").fromBuffer(a.Deref(8,8).toBuffer(),{endian:"little"}),MemFree:require("bignum").fromBuffer(a.Deref(16,8).toBuffer(),{endian:"little"})};b.percentFree=((b.MemFree.div(require("bignum")("1048576")).toNumber()/b.MemTotal.div(require("bignum")("1048576")).toNumber())*100).toFixed(2);b.percentConsumed=((b.MemTotal.sub(b.MemFree).div(require("bignum")("1048576")).toNumber()/b.MemTotal.div(require("bignum")("1048576")).toNumber())*100).toFixed(2);b.MemTotal=b.MemTotal.toString();b.MemFree=b.MemFree.toString();return(b)}function linux_cpuUtilization(){var g={cpus:[]};var d=require("fs").readFileSync("/proc/stat");var e=d.toString().split("\n");var a;var k,l;var h,c,j;for(var b in e){a=e[b].split(" ");if(!a[0].startsWith("cpu")){break}k=0,h=0;while(a[++k]==""){}for(l=k;l<a.length;++l){h+=parseInt(a[l])}c=parseInt(a[3+k]);j=(100-((c/h)*100)).toFixed(2);if(!g.total){g.total=j}else{g.cpus.push(j)}}var f=new promise(function(m,i){this._res=m;this._rej=i});f._res(g);return(f)}function linux_memUtilization(){var c={};var b=require("fs").readFileSync("/proc/meminfo").toString().split("\n");var d;for(var a in b){d=b[a].split(" ");switch(d[0]){case"MemTotal:":c.total=parseInt(d[d.length-2]);break;case"MemFree:":c.free=parseInt(d[d.length-2]);break}}c.percentFree=((c.free/c.total)*100).toFixed(2);c.percentConsumed=(((c.total-c.free)/c.total)*100).toFixed(2);return(c)}function macos_cpuUtilization(){var d=new promise(function(h,g){this._res=h;this._rej=g});var b=require("child_process").execFile("/bin/sh",["sh"]);b.stdout.str="";b.stdout.on("data",function(g){this.str+=g.toString()});b.stdin.write('top -l 1 | grep -E "^CPU"\nexit\n');b.waitExit();var c=b.stdout.str.split("\n");if(c[0].length>0){var f=c[0].split(":")[1];var a=f.split(",");var e=parseFloat(a[0].split("%")[0].trim())+parseFloat(a[1].split("%")[0].trim());d._res({total:e,cpus:[]})}else{d._rej("parse error")}return(d)}function macos_memUtilization(){var d={};var e=new promise(function(h,g){this._res=h;this._rej=g});var b=require("child_process").execFile("/bin/sh",["sh"]);b.stdout.str="";b.stdout.on("data",function(g){this.str+=g.toString()});b.stdin.write('top -l 1 | grep -E "^Phys"\nexit\n');b.waitExit();var c=b.stdout.str.split("\n");if(c[0].length>0){var f=c[0].split(":")[1];var a=f.split(",");d.MemTotal=parseInt(a[0].trim().split(" ")[0]);d.MemFree=parseInt(a[1].trim().split(" ")[0]);d.percentFree=((d.MemFree/d.MemTotal)*100).toFixed(2);d.percentConsumed=(((d.MemTotal-d.MemFree)/d.MemTotal)*100).toFixed(2);return(d)}else{throw ("Parse Error")}}switch(process.platform){case"linux":module.exports={cpuUtilization:linux_cpuUtilization,memUtilization:linux_memUtilization};break;case"win32":module.exports={cpuUtilization:windows_cpuUtilization,memUtilization:windows_memUtilization};break;case"darwin":module.exports={cpuUtilization:macos_cpuUtilization,memUtilization:macos_memUtilization};break}; |