-
Notifications
You must be signed in to change notification settings - Fork 9
/
new_parser.js
168 lines (142 loc) · 5.82 KB
/
new_parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp
function parseStatLine(line,options,error){
line=line.replace(/ +(?= )/g,'')// replace multiple spaces
if(!options.root.regex.exec(line)){
return {}
}//root stat line
var result={}
var tmp=null
try{
options.params.forEach(item=>{
item.keys=item.keys||1
tmp=item.regex.exec(line)
if(tmp){
if(!item.keys||item.keys==1){result[item.name]=tmp[1]}
else{
var subarray=[]
for(var i=1;i<item.keys+1;i++){
subarray.push(tmp[i])
}//for
result[item.name]=subarray
}//else
}//if
})
}catch(err){error(err)}
return result
}//parseCpu
function parseProcessLine(str,error){
var result={}
var regex=/(?<=)\S+/g //capture values beetween spaces
var result={}
try{
var data=[...str.matchAll(regex)]
result= {
"pid":data[0][0],
"user":data[1][0],
"pr":data[2][0],
"ni":data[3][0],
"virt":data[4][0],
"res":data[5][0],
"shr":data[6][0],
"s":data[7][0],
"cpu":data[8][0],
"mem":data[9][0],
"time":data[10][0],
"command":data[11][0],
}
}catch(err){error(err)}
return result
}//parseRootTopLine
// {
// pid_limit:10,//limit number of included pids in list (default: unlimited)
// pid_filter:(proc)=>{return proc.user=="root"?proc:null},// filtering the pid list (for example: include only pid with user == root) (default: null)
// pid_sort:(a,b)=>{return a.cpu-b.cpu},// sorting pid list by cpu usage (default)
// }
module.exports=function(data,options={pid_sort(a,b){return a.cpu-b.cpu}},error=(error)=>{/*parser error messages*/ console.log(error)}){
var data=data.split("\n").filter(v=>v!="")
var result={
top:parseStatLine(data[0],
{
root:{regex:/top \-/g,name:"top"},//root line params
params:[// parse variable values
{regex: /(\d+\:\d+\:\d+) up/g, name:"time"},
// {regex: /up ([0-9,\:]+)\,/g, name:"up"},
{regex: / ([0-9,\:]+)\, \d+ user/g, name:"up_hours"},
{regex: / (\d+) days/g, name:"up_days"},
{regex: /(\d+) users/g, name:"users"},
{regex: /load average: (\d+\.\d+)\, (\d+\.\d+)\, (\d+\.\d+)/g, name:"load_average",keys:3},//need subarray with values
// {regex: /(\d+) zombie/g, name:"zombie"},
]
},error
),//stat
tasks:parseStatLine(data[1],
{
root:{regex:/Tasks/g,name:"tasks"},//root line params
params:[// parse variable values
{regex: /(\d+) total/g, name:"total"},
{regex: /(\d+) running/g, name:"running"},
{regex: /(\d+) sleeping/g, name:"sleeping"},
{regex: /(\d+) stopped/g, name:"stopped"},
{regex: /(\d+) zombie/g, name:"zombie"},
]
},error
),//stat
cpu:parseStatLine(data[2],
{
root:{regex:/%Cpu/g,name:"cpu"},//root line params
params:[// parse variable values
{regex: /(\d+\.\d) us/g, name:"us"},
{regex: /(\d+\.\d) sy/g, name:"sy"},
{regex: /(\d+\.\d) ni/g, name:"ni"},
{regex: /(\d+\.\d) id/g, name:"id"},
{regex: /(\d+\.\d) wa/g, name:"wa"},
{regex: /(\d+\.\d) hi/g, name:"hi"},
{regex: /(\d+\.\d) si/g, name:"si"},
{regex: /(\d+\.\d) st/g, name:"st"},
]
},error
),//stat
mem:parseStatLine(data[3],
{
root:{regex:/KiB Mem/g,name:"mem"},//root line params
params:[// parse variable values
{regex: /(\d+) total/g, name:"total"},
{regex: /(\d+) used/g, name:"used"},
{regex: /(\d+) free/g, name:"free"},
{regex: /(\d+) buffers/g, name:"buffers"},
{regex: /(\d+) buff\/cache/g, name:"buff_cache"},
]
}
),//stat
swap:parseStatLine(data[4],
{
root:{regex:/KiB Swap/g,name:"swap"},//root line params
params:[// parse variable values
{regex: /(\d+) total/g, name:"total"},
{regex: /(\d+) used/g, name:"used"},
{regex: /(\d+) free/g, name:"free"},
{regex: /(\d+) cached Mem/g, name:"cached_mem"},
{regex: /(\d+) avail Mem/g, name:"avail_mem"},
]
},error
),//stat
processes:[...( [
(()=>{
var result=[]
for (var i=5;i<data.length-1;i++){
var proc=null
try{
var proc=parseProcessLine(data[i],error)
if(typeof options.pid_filter=="function"){
proc=options.pid_filter(proc)
}//if
}catch(err){error(err)}
proc?result.push(proc):null
}//for
return result.slice(0,options.pid_limit||result.length).sort(options.pid_sort)
})()//for
]
)]
}//result
return result
}//export