Skip to content

Instantly share code, notes, and snippets.

@sacreman
Created November 16, 2013 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sacreman/7504775 to your computer and use it in GitHub Desktop.
Save sacreman/7504775 to your computer and use it in GitHub Desktop.
awesomeness
#!/usr/bin/env python
import setuptools # needed for pyinstaller
import os
import sys
import psutil
import time
import re
import argparse
def check_disks(**kwargs):
"""returns a dict of mount point : % used"""
if isinstance(kwargs['disk_threshold'], str):
_threshold = kwargs['disk_threshold'].split(',')
else:
_threshold = kwargs['disk_threshold']
_warning = int(_threshold[0])
_critical = int(_threshold[1])
disk_usage = {}
for partition in psutil.disk_partitions(all=False):
if os.name == 'nt':
if 'cdrom' in partition.opts or partition.fstype == '':
continue
usage = psutil.disk_usage(partition.mountpoint)
disk = "disk_" + re.sub(" ", "_", partition.mountpoint)
disk_usage[disk] = "%d%%" % int(usage.percent)
if int(usage.percent) > _critical:
disk_usage[disk + "_status"] = disk + " is critical! "
if _critical > int(usage.percent) > _warning:
disk_usage[disk + "_status"] = disk + " is warning! "
return disk_usage
def check_memory(**kwargs):
"""returns a dict of memory type : % used"""
if isinstance(kwargs['memory_threshold'], str):
_threshold = kwargs['memory_threshold'].split(',')
else:
_threshold = kwargs['memory_threshold']
_warning = int(_threshold[0])
_critical = int(_threshold[1])
memory = "%d%%" % int(psutil.virtual_memory().percent)
swap = "%d%%" % int(psutil.swap_memory().percent)
memory_used = dict(memory=memory, swap=swap)
if int(psutil.virtual_memory().percent) > _critical:
memory_used["memory_status"] = "memory is critical! "
if _critical > int(psutil.virtual_memory().percent) > _warning:
memory_used["memory_status"] = "memory is warning! "
return memory_used
def check_cpu(**kwargs):
"""returns a dict of cpu type : % used"""
if isinstance(kwargs['cpu_threshold'], str):
_threshold = kwargs['cpu_threshold'].split(',')
else:
_threshold = kwargs['cpu_threshold']
_warning = int(_threshold[0])
_critical = int(_threshold[1])
cpu = "%d%%" % int(psutil.cpu_percent(interval=1))
cpu_used = dict(cpu=cpu)
if int(psutil.cpu_percent(interval=1)) > _critical:
cpu_used["cpu_status"] = "cpu is critical! "
if _critical > int(psutil.cpu_percent(interval=1)) > _warning:
cpu_used["cpu_status"] = "cpu is warning! "
return cpu_used
def check_net():
"""returns a dict of network stats in kps"""
space_apart = 1
rx_before = psutil.net_io_counters().bytes_recv
sx_before = psutil.net_io_counters().bytes_sent
time.sleep(space_apart)
rx_after = psutil.net_io_counters().bytes_recv
sx_after = psutil.net_io_counters().bytes_sent
rx = "%dKps" % (((rx_after - rx_before) / 1024) / space_apart)
sx = "%dKps" % (((sx_after - sx_before) / 1024) / space_apart)
net = dict(net_download=rx, net_upload=sx)
return net
def main(**kwargs):
dicts = [check_disks(**kwargs), check_memory(**kwargs), check_cpu(**kwargs), check_net()]
perf = ""
result = {}
for d in dicts:
for k, v in d.iteritems():
if "critical" in v or "warning" in v:
result[k] = v
else:
perf += "%s=%s;;;; " % (k, v)
error_message = ""
for v in result.itervalues():
if "critical" in v:
error_message += v
warning_message = ""
for v in result.itervalues():
if "warning" in v:
warning_message += v
if len(error_message) > 0:
print error_message + warning_message + "| " + perf
sys.exit(2)
if len(warning_message) > 0:
print error_message + warning_message + "| " + perf
sys.exit(1)
print "OK | " + perf
sys.exit(0)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Dataloop Base Plugin')
parser.add_argument('-d', '--disk', metavar='disk', action="store",
default='95,98', nargs=2, help='<warning> <critical>')
parser.add_argument('-m', '--memory', metavar='memory', action="store",
default='90,95', nargs=2, help='<warning> <critical>')
parser.add_argument('-c', '--cpu', metavar='cpu', action="store",
default='90,95', nargs=2, help='<warning> <critical>')
args = parser.parse_args()
main(disk_threshold=args.disk, memory_threshold=args.memory, cpu_threshold=args.cpu)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment