UNPKG

meshcentral

Version:

Web based remote computer management server

1 lines • 6.47 kB
var GM,PDH_FMT_LONG=256,PDH_FMT_DOUBLE=512,promise=require("promise");function windows_cpuUtilization(){var u=new promise(function(e,t){this._res=e,this._rej=t}),e=(u.counter=GM.CreateVariable(16),u.cpu=GM.CreatePointer(),u.cpuTotal=GM.CreatePointer(),0);if(0==(e=GM.pdh.PdhOpenQueryA(0,0,u.cpu).Val)&&(0==(e=GM.pdh.PdhAddEnglishCounterA(u.cpu.Deref(),GM.CreateVariable("\\Processor(*)\\% Processor Time"),0,u.cpuTotal).Val)&&!(e=0!=GM.pdh.PdhCollectQueryData(u.cpu.Deref()).Val)))return u._timeout=setTimeout(function(e){var t,r,i,o,s={cpus:[]},n=GM.CreateVariable(4),a=GM.CreateVariable(4);if(o=0!=GM.pdh.PdhCollectQueryData(e.cpu.Deref()).Val)e._rej(o);else if(-2147481646!=(o=GM.pdh.PdhGetFormattedCounterArrayA(e.cpuTotal.Deref(),PDH_FMT_DOUBLE,n,a,0).Val))e._rej(o);else if(t=GM.CreateVariable(n.toBuffer().readUInt32LE()),0!=(o=GM.pdh.PdhGetFormattedCounterArrayA(e.cpuTotal.Deref(),PDH_FMT_DOUBLE,n,a,t).Val))e._rej(o);else{for(var l=0;l<a.toBuffer().readUInt32LE();++l)"_Total"==(r=(i=t.Deref(24*l,24)).Deref(0,GM.PointerSize).Deref()).String?s.total=i.Deref(16,8).toBuffer().readDoubleLE():s.cpus[parseInt(r.String)]=i.Deref(16,8).toBuffer().readDoubleLE();GM.pdh.PdhRemoveCounter(e.cpuTotal.Deref()),GM.pdh.PdhCloseQuery(e.cpu.Deref()),u._res(s)}},100,u),u;u._rej(e)}function windows_memUtilization(){var e=GM.CreateVariable(64),e=(e.Deref(0,4).toBuffer().writeUInt32LE(64),GM.kernel32.GlobalMemoryStatusEx(e),{MemTotal:require("bignum").fromBuffer(e.Deref(8,8).toBuffer(),{endian:"little"}),MemFree:require("bignum").fromBuffer(e.Deref(16,8).toBuffer(),{endian:"little"})});return e.percentFree=e.MemFree.div(require("bignum")("1048576")).toNumber()/e.MemTotal.div(require("bignum")("1048576")).toNumber()*100,e.percentConsumed=e.MemTotal.sub(e.MemFree).div(require("bignum")("1048576")).toNumber()/e.MemTotal.div(require("bignum")("1048576")).toNumber()*100,e.MemTotal=e.MemTotal.toString(),e.MemFree=e.MemFree.toString(),e}"win32"==process.platform&&((GM=require("_GenericMarshal")).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"));var cpuLastIdle=[],cpuLastSum=[];function linux_cpuUtilization(){var e,t,r,i,o,s={cpus:[]},n=require("fs").readFileSync("/proc/stat").toString().split("\n"),a=0;for(o in n){if(!(e=n[o].split(" "))[0].startsWith("cpu"))break;for(i=t=0;""==e[++t];);for(r=t;r<e.length;++r)i+=parseInt(e[r]);var l,u=100-((l=parseInt(e[3+t]))-cpuLastIdle[a])/(i-cpuLastSum[a])*100;cpuLastSum[a]=i,cpuLastIdle[a]=l,s.total?s.cpus.push(u):s.total=u,++a}var d=new promise(function(e,t){this._res=e,this._rej=t});return d._res(s),d}function linux_memUtilization(){var e,t,r={},i=require("fs").readFileSync("/proc/meminfo").toString().split("\n");for(t in i)switch((e=i[t].split(" "))[0]){case"MemTotal:":r.total=parseInt(e[e.length-2]);break;case"MemFree:":r.free=parseInt(e[e.length-2])}return r.percentFree=r.free/r.total*100,r.percentConsumed=(r.total-r.free)/r.total*100,r}function macos_cpuUtilization(){var e=new promise(function(e,t){this._res=e,this._rej=t}),t=require("child_process").execFile("/bin/sh",["sh"]),t=(t.stdout.str="",t.stdout.on("data",function(e){this.str+=e.toString()}),t.stdin.write('top -l 1 | grep -E "^CPU"\nexit\n'),t.waitExit(),t.stdout.str.split("\n"));return 0<t[0].length?(t=t[0].split(":")[1].split(","),t=parseFloat(t[0].split("%")[0].trim())+parseFloat(t[1].split("%")[0].trim()),e._res({total:t,cpus:[]})):e._rej("parse error"),e}function macos_memUtilization(){var e={},t=(new promise(function(e,t){this._res=e,this._rej=t}),require("child_process").execFile("/bin/sh",["sh"])),t=(t.stdout.str="",t.stdout.on("data",function(e){this.str+=e.toString()}),t.stdin.write('top -l 1 | grep -E "^Phys"\nexit\n'),t.waitExit(),t.stdout.str.split("\n"));if(0<t[0].length)return t=t[0].split(":")[1].split(","),e.MemTotal=parseInt(t[0].trim().split(" ")[0]),e.MemFree=parseInt(t[1].trim().split(" ")[0]),e.percentFree=e.MemFree/e.MemTotal*100,e.percentConsumed=(e.MemTotal-e.MemFree)/e.MemTotal*100,e;throw"Parse Error"}function windows_thermals(){var e=[];if((child=require("child_process").execFile(process.env.windir+"\\System32\\wbem\\wmic.exe",["wmic","/namespace:\\\\root\\wmi","PATH","MSAcpi_ThermalZoneTemperature","get","CurrentTemperature"])).stdout.str="",child.stdout.on("data",function(e){this.str+=e.toString()}),child.stderr.str="",child.stderr.on("data",function(e){this.str+=e.toString()}),child.waitExit(),""!=child.stdout.str.trim)for(var t=child.stdout.str.trim().split("\r\n"),r=1;r<t.length;++r)""!=t[r].trim()&&e.push((parseFloat(t[r])/10-273.15).toFixed(2));return e}function linux_thermals(){(child=require("child_process").execFile("/bin/sh",["sh"])).stdout.str="",child.stdout.on("data",function(e){this.str+=e.toString()}),child.stderr.str="",child.stderr.on("data",function(e){this.str+=e.toString()}),child.stdin.write("cat /sys/class/thermal/thermal_zone*/temp | awk '{ print $0 / 1000 }'\nexit\n"),child.waitExit();var e=child.stdout.str.trim().split("\n");return e=1==e.length&&""==e[0]?[]:e}function macos_thermals(){var i=[],e=require("child_process").execFile("/bin/sh",["sh"]);return e.stdout.str="",e.stdout.on("data",function(e){this.str+=e.toString()}),e.stderr.on("data",function(){}),e.stdin.write("powermetrics --help | grep SMC\nexit\n"),e.waitExit(),""!=e.stdout.str.trim()&&((e=require("child_process").execFile("/bin/sh",["sh"])).stdout.str="",e.stdout.on("data",function(e){this.str+=e.toString();var t,r=this.str.trim().split("\n");for(t in r)1<r[t].split(" die temperature: ").length&&(i.push(r[t].split(" ")[3]),this.parent.kill())}),e.stderr.str="",e.stderr.on("data",function(e){this.str+=e.toString()}),e.stdin.write("powermetrics -s smc\n"),e.waitExit(5e3)),i}switch(process.platform){case"linux":module.exports={cpuUtilization:linux_cpuUtilization,memUtilization:linux_memUtilization,thermals:linux_thermals};break;case"win32":module.exports={cpuUtilization:windows_cpuUtilization,memUtilization:windows_memUtilization,thermals:windows_thermals};break;case"darwin":module.exports={cpuUtilization:macos_cpuUtilization,memUtilization:macos_memUtilization,thermals:macos_thermals}}