is there a way to find out the % cpu usage for a node.js process with the code? so that when the node.js server is running and detect the CPU is over certain%, then it will put an alert or console output.
有没有办法找到node.js进程的%cpu使用情况与代码?这样当node.js服务器运行并检测到CPU超过某个%时,它就会发出警报或控制台输出。
6 个解决方案
#1
15
Try looking at this code: https://github.com/last/healthjs
试着看看这段代码:https://github.com/last/healthjs
Network service for getting CPU of remote system and receiving CPU usage alerts...
获取远程系统CPU和接收CPU使用率警报的网络服务......
Health.js serves 2 primary modes: "streaming mode" and "event mode". Streaming mode allows a client to connect and receive streaming CPU usage data. Event mode enables Health.js to notify a remote server when CPU usage hits a certain threshold. Both modes can be run simultaneously...
Health.js提供2种主要模式:“流模式”和“事件模式”。流模式允许客户端连接和接收流式CPU使用数据。事件模式使Health.js能够在CPU使用率达到特定阈值时通知远程服务器。两种模式都可以同时运行......
#2
26
On *nix systems can get process stats by reading the /proc/[pid]/stat virtual file.
在* nix系统上,可以通过读取/ proc / [pid] / stat虚拟文件来获取进程统计信息。
For example this will check the CPU usage every ten seconds, and print to the console if it's over 20%. It works by checking the number of cpu ticks used by the process and comparing the value to a second measurement made one second later. The difference is the number of ticks used by the process during that second. On POSIX systems, there are 10000 ticks per second (per processor), so dividing by 10000 gives us a percentage.
例如,这将每十秒检查CPU使用率,如果超过20%则打印到控制台。它的工作原理是检查过程使用的cpu滴答数,并将该值与一秒钟后的第二次测量值进行比较。差异是该过程在该秒期间使用的滴答数。在POSIX系统上,每秒有10000个刻度(每个处理器),因此除以10000得到一个百分比。
var fs = require('fs');
var getUsage = function(cb){
fs.readFile("/proc/" + process.pid + "/stat", function(err, data){
var elems = data.toString().split(' ');
var utime = parseInt(elems[13]);
var stime = parseInt(elems[14]);
cb(utime + stime);
});
}
setInterval(function(){
getUsage(function(startTime){
setTimeout(function(){
getUsage(function(endTime){
var delta = endTime - startTime;
var percentage = 100 * (delta / 10000);
if (percentage > 20){
console.log("CPU Usage Over 20%!");
}
});
}, 1000);
});
}, 10000);
#3
7
see node-usage for tracking process CPU and Memory Usage (not the system)
查看用于跟踪进程CPU和内存使用情况的节点用法(不是系统)
#4
6
You can use the os module now.
您现在可以使用os模块。
var os = require('os');
var loads = os.loadavg();
This gives you the load average for the last 60seconds, 5minutes and 15minutes. This doesnt give you the cpu usage as a % though.
这为您提供了最后60秒,5分钟和15分钟的平均负载。虽然这并没有给你cpu的使用率。
#5
1
Use node process.cpuUsage function (introduced in node v6.1.0). It shows time that cpu spent on your node process. Example taken from docs:
使用node process.cpuUsage函数(在节点v6.1.0中引入)。它显示了cpu在节点进程上花费的时间。从文档中获取的示例:
const previousUsage = process.cpuUsage();
// { user: 38579, system: 6986 }
// spin the CPU for 500 milliseconds
const now = Date.now();
while (Date.now() - now < 500);
// set 2 sec "non-busy" timeout
setTimeout(function() {
console.log(process.cpuUsage(previousUsage);
// { user: 514883, system: 11226 } ~ 0,5 sec
}, 2000);
#1
15
Try looking at this code: https://github.com/last/healthjs
试着看看这段代码:https://github.com/last/healthjs
Network service for getting CPU of remote system and receiving CPU usage alerts...
获取远程系统CPU和接收CPU使用率警报的网络服务......
Health.js serves 2 primary modes: "streaming mode" and "event mode". Streaming mode allows a client to connect and receive streaming CPU usage data. Event mode enables Health.js to notify a remote server when CPU usage hits a certain threshold. Both modes can be run simultaneously...
Health.js提供2种主要模式:“流模式”和“事件模式”。流模式允许客户端连接和接收流式CPU使用数据。事件模式使Health.js能够在CPU使用率达到特定阈值时通知远程服务器。两种模式都可以同时运行......
#2
26
On *nix systems can get process stats by reading the /proc/[pid]/stat virtual file.
在* nix系统上,可以通过读取/ proc / [pid] / stat虚拟文件来获取进程统计信息。
For example this will check the CPU usage every ten seconds, and print to the console if it's over 20%. It works by checking the number of cpu ticks used by the process and comparing the value to a second measurement made one second later. The difference is the number of ticks used by the process during that second. On POSIX systems, there are 10000 ticks per second (per processor), so dividing by 10000 gives us a percentage.
例如,这将每十秒检查CPU使用率,如果超过20%则打印到控制台。它的工作原理是检查过程使用的cpu滴答数,并将该值与一秒钟后的第二次测量值进行比较。差异是该过程在该秒期间使用的滴答数。在POSIX系统上,每秒有10000个刻度(每个处理器),因此除以10000得到一个百分比。
var fs = require('fs');
var getUsage = function(cb){
fs.readFile("/proc/" + process.pid + "/stat", function(err, data){
var elems = data.toString().split(' ');
var utime = parseInt(elems[13]);
var stime = parseInt(elems[14]);
cb(utime + stime);
});
}
setInterval(function(){
getUsage(function(startTime){
setTimeout(function(){
getUsage(function(endTime){
var delta = endTime - startTime;
var percentage = 100 * (delta / 10000);
if (percentage > 20){
console.log("CPU Usage Over 20%!");
}
});
}, 1000);
});
}, 10000);
#3
7
see node-usage for tracking process CPU and Memory Usage (not the system)
查看用于跟踪进程CPU和内存使用情况的节点用法(不是系统)
#4
6
You can use the os module now.
您现在可以使用os模块。
var os = require('os');
var loads = os.loadavg();
This gives you the load average for the last 60seconds, 5minutes and 15minutes. This doesnt give you the cpu usage as a % though.
这为您提供了最后60秒,5分钟和15分钟的平均负载。虽然这并没有给你cpu的使用率。
#5
1
Use node process.cpuUsage function (introduced in node v6.1.0). It shows time that cpu spent on your node process. Example taken from docs:
使用node process.cpuUsage函数(在节点v6.1.0中引入)。它显示了cpu在节点进程上花费的时间。从文档中获取的示例:
const previousUsage = process.cpuUsage();
// { user: 38579, system: 6986 }
// spin the CPU for 500 milliseconds
const now = Date.now();
while (Date.now() - now < 500);
// set 2 sec "non-busy" timeout
setTimeout(function() {
console.log(process.cpuUsage(previousUsage);
// { user: 514883, system: 11226 } ~ 0,5 sec
}, 2000);