59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from re import findall
|
|
from subprocess import check_output
|
|
from sys import exit
|
|
|
|
try:
|
|
top_output = check_output("top -b -n1 -d1 | grep -i '^\%cpu'", shell=True).decode('UTF-8').split(': ', 2)[1].strip()
|
|
|
|
cpu_usage = {}
|
|
for value, identifier in findall('([0-9\.\,]{3,5}) ([a-z]{2})', top_output):
|
|
cpu_usage[identifier] = float(value.replace(',', '.'))
|
|
|
|
warn = set()
|
|
crit = set()
|
|
|
|
print(top_output)
|
|
|
|
# steal
|
|
if cpu_usage['st'] > 5:
|
|
crit.add('CPU steal is {}% (>5%)'.format(cpu_usage['st']))
|
|
elif cpu_usage['st'] > 2:
|
|
warn.add('CPU steal is {}% (>2%)'.format(cpu_usage['st']))
|
|
|
|
# iowait
|
|
if cpu_usage['wa'] > 60:
|
|
crit.add('IOwait is {}% (>60%)'.format(cpu_usage['wa']))
|
|
elif cpu_usage['wa'] > 30:
|
|
warn.add('IOwait is {}% (>30%)'.format(cpu_usage['wa']))
|
|
|
|
total_usage = cpu_usage['us'] + cpu_usage['sy']
|
|
if total_usage > 90:
|
|
crit.add('Total CPU usage is {:.1f}% ({}% user, {}% system, >90%)'.format(
|
|
total_usage,
|
|
cpu_usage['us'],
|
|
cpu_usage['sy'],
|
|
))
|
|
elif total_usage > 80:
|
|
warn.add('Total CPU usage is {:.1f}% ({}% user, {}% system > 80%)'.format(
|
|
total_usage,
|
|
cpu_usage['us'],
|
|
cpu_usage['sy'],
|
|
))
|
|
|
|
for line in sorted(crit):
|
|
print(line)
|
|
|
|
for line in sorted(warn):
|
|
print(line)
|
|
|
|
if crit:
|
|
exit(2)
|
|
elif warn:
|
|
exit(1)
|
|
else:
|
|
exit(0)
|
|
except Exception as e:
|
|
print(repr(e))
|
|
exit(3)
|