Parse linux / Unix "top" command to json format
Parse unix TOP command raw output to JSON format using node.js without external dependencies. On Windows PC you can use WSL (windows subsystem for linux).
Test
Just run:
//navigate to module folder and run
npm run start
or
node example.js
Install
//require node > v.12.0.0
npm install topparser
Usage
var topparser=//start topparsertopparserstart//then data is availabletopparser//if some error happenstopparser//if topparser exittopparser//kill topparser after 10 seconds, for example
Options
var options=pid_limit:10//limit number of included pids in list (default: unlimited){return procuser=="root"?proc:null}// filtering the pid list (for example: include only pid with user == root) (default: null){return acpu-bcpu}// sorting pid list by cpu usage (default)//start topparser, with options (optional): pid_limit, pid_sort, pid_filtertopparserstartoptions...
JSON output:
{
"top": {
"time": "01:50:41",
"up_hours": "2:21",
"users": "0",
"load_average": [
"0.52",
"0.58",
"0.59"
]
},
"tasks": {
"total": "17",
"running": "1",
"sleeping": "16",
"stopped": "0",
"zombie": "0"
},
"cpu": {
"us": "17.5",
"sy": "4.7",
"ni": "0.0",
"id": "77.5",
"wa": "0.0",
"hi": "0.4",
"si": "0.0",
"st": "0.0"
},
"mem": {
"total": "33411872",
"used": "16217872",
"free": "16964648",
"buff_cache": "229352"
},
"swap": {
"used": "186300",
"avail_mem": "17060268"
},
"processes": [
[
{
"pid": "1",
"user": "root",
"pr": "20",
"ni": "0",
"virt": "8892",
"res": "312",
"shr": "272",
"s": "S",
"cpu": "0.0",
"mem": "0.0",
"time": "0:00.07",
"command": "init"
},
{
"pid": "8",
"user": "root",
"pr": "20",
"ni": "0",
"virt": "8908",
"res": "232",
"shr": "180",
"s": "S",
"cpu": "0.0",
"mem": "0.0",
"time": "0:00.01",
"command": "init"
}
]
]
}