26
26
27
27
Requirements :
28
28
- FreeBSD : top
29
- - Linux : mpstat
30
29
31
30
In addition, for FreeBSD, it reports :
32
31
- load average (1m, 5m, 15m)
@@ -96,16 +95,10 @@ def main():
96
95
signal .signal (signal .SIGINT , handlesignal )
97
96
98
97
try :
99
- if collect_every_cpu :
100
- p_top = subprocess .Popen (
101
- ["mpstat" , "-P" , "ALL" , str (collection_interval )],
102
- stdout = subprocess .PIPE ,
103
- )
104
- else :
105
- p_top = subprocess .Popen (
106
- ["mpstat" , str (collection_interval )],
107
- stdout = subprocess .PIPE ,
108
- )
98
+ p_top = subprocess .Popen (
99
+ ["top" , "-b" , "-d" , str (collection_interval )],
100
+ stdout = subprocess .PIPE ,
101
+ )
109
102
except OSError as e :
110
103
if e .errno == errno .ENOENT :
111
104
# it makes no sense to run this collector here
@@ -128,18 +121,16 @@ def main():
128
121
129
122
# CPU: --> CPU all: : FreeBSD, to match the all CPU
130
123
# %( [uni][a-z]+,?)? : FreeBSD, so that top output matches mpstat output
131
- # AM : Linux, mpstat output depending on locale
132
- # PM : Linux, mpstat output depending on locale
133
124
# .* load : FreeBSD, to correctly match load averages
134
125
# , : FreeBSD, to correctly match processes: Mem: ARC: and Swap:
135
- fields = re .sub ("CPU:" , "CPU all:" , re .sub (r"%( [uni][a-z]+,?)?| AM | PM | .* load |," , "" , line )).split ()
126
+ fields = re .sub ("CPU:" , "CPU all:" , re .sub (r"%( [uni][a-z]+,?)?|.* load |," , "" , line )).split ()
136
127
if len (fields ) <= 0 :
137
128
continue
138
129
139
- if ((( fields [0 ] == "CPU" ) or ( re . match ( "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]" , fields [ 0 ]))) and (( collect_every_cpu and re . match ( "[0-9]+:?" , fields [ 1 ])) or (( not collect_every_cpu ) and re . match ( "all:?" , fields [ 1 ])))) :
140
- if (( fields [1 ] == "all" ) or ( fields [ 1 ] == "0" )) :
130
+ if fields [0 ] == "CPU" :
131
+ if fields [1 ] == "all" :
141
132
timestamp = int (time .time ())
142
- cpuid = fields [1 ].replace (":" ,"" )
133
+ cpuid = fields [1 ].replace (":" , "" )
143
134
cpuuser = fields [2 ]
144
135
cpunice = fields [3 ]
145
136
cpusystem = fields [4 ]
@@ -157,7 +148,7 @@ def main():
157
148
print ("load.5m %s %s" % (timestamp , fields [2 ]))
158
149
print ("load.15m %s %s" % (timestamp , fields [3 ]))
159
150
160
- elif re .match ("[0-9]+ processes:" ,line ):
151
+ elif re .match ("[0-9]+ processes:" , line ):
161
152
starting = 0
162
153
running = 0
163
154
sleeping = 0
@@ -225,17 +216,17 @@ def main():
225
216
other = 0
226
217
for i in range (len (fields )):
227
218
if (fields [i ] == "Total" ):
228
- total = convert_to_bytes (fields [i - 1 ])
219
+ total = convert_to_bytes (fields [i - 1 ])
229
220
if (fields [i ] == "MRU" ):
230
- mru = convert_to_bytes (fields [i - 1 ])
221
+ mru = convert_to_bytes (fields [i - 1 ])
231
222
if (fields [i ] == "MFU" ):
232
- mfu = convert_to_bytes (fields [i - 1 ])
223
+ mfu = convert_to_bytes (fields [i - 1 ])
233
224
if (fields [i ] == "Anon" ):
234
- anon = convert_to_bytes (fields [i - 1 ])
225
+ anon = convert_to_bytes (fields [i - 1 ])
235
226
if (fields [i ] == "Header" ):
236
- header = convert_to_bytes (fields [i - 1 ])
227
+ header = convert_to_bytes (fields [i - 1 ])
237
228
if (fields [i ] == "Other" ):
238
- other = convert_to_bytes (fields [i - 1 ])
229
+ other = convert_to_bytes (fields [i - 1 ])
239
230
print ("arc.total %s %s" % (timestamp , total ))
240
231
print ("arc.mru %s %s" % (timestamp , mru ))
241
232
print ("arc.mfu %s %s" % (timestamp , mfu ))
@@ -244,25 +235,25 @@ def main():
244
235
print ("arc.other %s %s" % (timestamp , other ))
245
236
246
237
elif (fields [0 ] == "Swap:" ):
247
- total = 0
248
- used = 0
249
- free = 0
250
- inuse = 0
251
- inps = 0
252
- outps = 0
238
+ total = 0
239
+ used = 0
240
+ free = 0
241
+ inuse = 0
242
+ inps = 0
243
+ outps = 0
253
244
for i in range (len (fields )):
254
245
if (fields [i ] == "Total" ):
255
- total = convert_to_bytes (fields [i - 1 ])
246
+ total = convert_to_bytes (fields [i - 1 ])
256
247
if (fields [i ] == "Used" ):
257
- used = convert_to_bytes (fields [i - 1 ])
248
+ used = convert_to_bytes (fields [i - 1 ])
258
249
if (fields [i ] == "Free" ):
259
- free = convert_to_bytes (fields [i - 1 ])
250
+ free = convert_to_bytes (fields [i - 1 ])
260
251
if (fields [i ] == "Inuse" ):
261
- inuse = convert_to_bytes (fields [i - 1 ])
252
+ inuse = convert_to_bytes (fields [i - 1 ])
262
253
if (fields [i ] == "In" ):
263
- inps = convert_to_bytes (fields [i - 1 ])/ collection_interval
254
+ inps = convert_to_bytes (fields [i - 1 ]) / collection_interval
264
255
if (fields [i ] == "Out" ):
265
- outps = convert_to_bytes (fields [i - 1 ])/ collection_interval
256
+ outps = convert_to_bytes (fields [i - 1 ]) / collection_interval
266
257
print ("swap.total %s %s" % (timestamp , total ))
267
258
print ("swap.used %s %s" % (timestamp , used ))
268
259
print ("swap.free %s %s" % (timestamp , free ))
0 commit comments