André Bolinhas
André Bolinhas

Reputation: 165

Node.js - spawn is cutting off the results

I'm creating a node program to return the output of linux top command, is working fine the only issue is that the name of command is cutted, instead the full command name like /usr/local/libexec/netdata/plugins.d/apps.plugin 1 returns /usr/local+

My code

const topparser=require("topparser")
const spawn = require('child_process').spawn
let proc=null
let startTime=0

exports.start=function(pid_limit,callback){
    startTime=new Date().getTime()
    proc = spawn('top', ['-c','-b',"-d","3"])
    console.log("started process, pid: "+proc.pid)
    let top_data=""

    proc.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
    })

    proc.on('close', function (code) {
      console.log('child process exited with code ' + code);
    });


}//start

exports.stop=function(){
    console.log("stoped process...")
    if(proc){proc.kill('SIGINT')}// SIGHUP -linux ,SIGINT -windows
}//stop

The results

14861 root      20   0       0      0      0 S   0.0  0.0   0:00.00 [kworker/1+
14864 root      20   0       0      0      0 S   0.0  0.0   0:00.02 [kworker/0+
15120 root      39  19  102488   3344   2656 S   0.0  0.1   0:00.09 /usr/bin/m+
16904 root      20   0       0      0      0 S   0.0  0.0   0:00.00 [kworker/0+
19031 root      20   0       0      0      0 S   0.0  0.0   0:00.00 [kworker/u+
21500 root      20   0       0      0      0 Z   0.0  0.0   0:00.00 [dsc] <def+
22571 root      20   0       0      0      0 S   0.0  0.0   0:00.00 [kworker/0+

Any way to fix it? Best regards

Upvotes: 0

Views: 419

Answers (2)

Costa Tsaousis
Costa Tsaousis

Reputation: 359

Since you work with node, you can query netdata running on localhost for this.

Example:

http://london.my-netdata.io/api/v1/data?chart=apps.cpu&after=-1&options=ms

For localhost netdata: http://localhost:19999/api/v1/data?chart=apps.cpu&after=-1&options=ms

You can also get systemd services:

http://london.my-netdata.io/api/v1/data?chart=services.cpu&after=-1&options=ms

If you are not planning to update the screen per second, you can instruct netdata to return the average of a longer duration:

http://london.my-netdata.io/api/v1/data?chart=apps.cpu&after=-5&points=1&group=average&options=ms

The above returns the average of the last 5 seconds.

Finally, you get the latest values all the metrics netdata monitors, with this:

http://london.my-netdata.io/api/v1/allmetrics?format=json

For completeness, netdata can export all the metrics in BASH format for shell scripts. Check this: https://github.com/firehol/netdata/wiki/receiving-netdata-metrics-from-shell-scripts

Upvotes: 0

Ry-
Ry-

Reputation: 224904

From a top manpage:

In Batch mode, when used without an argument top will format output using the COLUMNS= and LINES= environment variables, if set. Otherwise, width will be fixed at the maximum 512 columns. With an argument, output width can be decreased or increased (up to 512) but the number of rows is consid‐ ered unlimited.

Add '-w', '512' to the arguments.

Upvotes: 1

Related Questions