0

I would like to create a time series and inject it into InfluxDb for a demo. I thought about using the top command (top -pid 1393 -stats cpu), and use the CPU value. And then use NodeJS to extract the data and inject it into an InfluxDB. However, there are a couple of but...: 1- The top command has a display section: can it be removed? 2- In Node, I would call (repeatedly) "top -pid 1393 -stats cpu -l 1" with the "-l 1" option to only get a singe sample. I feel it is a misuse of the fact that top generates data at given intervals (basically, I recreate in Node what top does automatically)

Is there a better way to do this - in the ideal world, I would launch top in node and "pipe" the output stream to a variable in an async way (to execute the insertion into InfluxDB).

Thanks for any hints you may have. Christian

Christian68
  • 845
  • 4
  • 13
  • 24

3 Answers3

0

In actual fact, there is a node module for this: see npm usage. Note that you may have an issue when installing the usage module, generated by node-gyp rebuild (usage requires this module, which requires XCode in Mac platform - see https://github.com/nodejs/node-gyp). To solve this, look here: xcode-select active developer directory error) Thanks - C

Christian68
  • 845
  • 4
  • 13
  • 24
0

To monitor the process' resource consuming metrics (correct me if I took your intentions wrong), you don't need your NodeJS stuff at all.

All you need is running Telegraf agent, with this plugin configured, on the machine(s) you're targeting.

Point the output plugin to your Influx - and that's it.

Yuri G
  • 1,206
  • 1
  • 9
  • 13
0

As mention in other comments, there are specific tools for that task. Anyway, if you still want to do it programmatically, I would suggest to either:

  1. run top in non-interactive mode, as explained here: https://unix.stackexchange.com/questions/255100/get-top-output-for-non-interactive-shell
  2. read directly the information that you need from /proc/<pid of your process>/<file of interest>
Andrés
  • 719
  • 1
  • 4
  • 21