Skip to content

Commit 55a4eb1

Browse files
authored
Refactored sysload.py to get rid of linux code
1 parent e0cc9e7 commit 55a4eb1

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

collectors/available/freebsd/long-lived/sysload.py

+27-36
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
2727
Requirements :
2828
- FreeBSD : top
29-
- Linux : mpstat
3029
3130
In addition, for FreeBSD, it reports :
3231
- load average (1m, 5m, 15m)
@@ -96,16 +95,10 @@ def main():
9695
signal.signal(signal.SIGINT, handlesignal)
9796

9897
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+
)
109102
except OSError as e:
110103
if e.errno == errno.ENOENT:
111104
# it makes no sense to run this collector here
@@ -128,18 +121,16 @@ def main():
128121

129122
# CPU: --> CPU all: : FreeBSD, to match the all CPU
130123
# %( [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
133124
# .* load : FreeBSD, to correctly match load averages
134125
# , : 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()
136127
if len(fields) <= 0:
137128
continue
138129

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":
141132
timestamp = int(time.time())
142-
cpuid = fields[1].replace(":","")
133+
cpuid = fields[1].replace(":", "")
143134
cpuuser = fields[2]
144135
cpunice = fields[3]
145136
cpusystem = fields[4]
@@ -157,7 +148,7 @@ def main():
157148
print("load.5m %s %s" % (timestamp, fields[2]))
158149
print("load.15m %s %s" % (timestamp, fields[3]))
159150

160-
elif re.match("[0-9]+ processes:",line):
151+
elif re.match("[0-9]+ processes:", line):
161152
starting = 0
162153
running = 0
163154
sleeping = 0
@@ -225,17 +216,17 @@ def main():
225216
other = 0
226217
for i in range(len(fields)):
227218
if (fields[i] == "Total"):
228-
total=convert_to_bytes(fields[i-1])
219+
total = convert_to_bytes(fields[i-1])
229220
if(fields[i] == "MRU"):
230-
mru=convert_to_bytes(fields[i-1])
221+
mru = convert_to_bytes(fields[i-1])
231222
if(fields[i] == "MFU"):
232-
mfu=convert_to_bytes(fields[i-1])
223+
mfu = convert_to_bytes(fields[i-1])
233224
if(fields[i] == "Anon"):
234-
anon=convert_to_bytes(fields[i-1])
225+
anon = convert_to_bytes(fields[i-1])
235226
if(fields[i] == "Header"):
236-
header=convert_to_bytes(fields[i-1])
227+
header = convert_to_bytes(fields[i-1])
237228
if(fields[i] == "Other"):
238-
other=convert_to_bytes(fields[i-1])
229+
other = convert_to_bytes(fields[i-1])
239230
print("arc.total %s %s" % (timestamp, total))
240231
print("arc.mru %s %s" % (timestamp, mru))
241232
print("arc.mfu %s %s" % (timestamp, mfu))
@@ -244,25 +235,25 @@ def main():
244235
print("arc.other %s %s" % (timestamp, other))
245236

246237
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
253244
for i in range(len(fields)):
254245
if(fields[i] == "Total"):
255-
total=convert_to_bytes(fields[i-1])
246+
total = convert_to_bytes(fields[i-1])
256247
if(fields[i] == "Used"):
257-
used=convert_to_bytes(fields[i-1])
248+
used = convert_to_bytes(fields[i-1])
258249
if(fields[i] == "Free"):
259-
free=convert_to_bytes(fields[i-1])
250+
free = convert_to_bytes(fields[i-1])
260251
if(fields[i] == "Inuse"):
261-
inuse=convert_to_bytes(fields[i-1])
252+
inuse = convert_to_bytes(fields[i-1])
262253
if(fields[i] == "In"):
263-
inps=convert_to_bytes(fields[i-1])/collection_interval
254+
inps = convert_to_bytes(fields[i-1]) / collection_interval
264255
if(fields[i] == "Out"):
265-
outps=convert_to_bytes(fields[i-1])/collection_interval
256+
outps = convert_to_bytes(fields[i-1]) / collection_interval
266257
print("swap.total %s %s" % (timestamp, total))
267258
print("swap.used %s %s" % (timestamp, used))
268259
print("swap.free %s %s" % (timestamp, free))

0 commit comments

Comments
 (0)