def check_memory(c): '''Collect memory percent, used, free, available''' # svmem(total=2749374464, available=1501151232, percent=45.4, used=979968000, # free=736043008, active=1145720832, inactive=590102528, buffers=107663360, # cached=925700096, shared=86171648) memory = psutil.virtual_memory() m1 = CheckItem('memory_percent',memory.percent,"Memory used (percent)", unit = '%') c.add_item(m1) m2 = CheckItem('memory_used',memory.used,"Memory used (bytes)", unit='bytes') h_used = m2.human() c.add_item(m2) m3 = CheckItem('memory_available',memory.available,"Memory available (bytes)", unit='bytes') h_avail = m3.human() c.add_item(m3) m4 = CheckItem('memory_total',memory.total,"Memory total (bytes)", unit='bytes') h_total = m4.human() c.add_item(m4) c.add_message("used {} % - used {} - avail {} - total {}".format(memory.percent, h_used, h_avail, h_total) ) return c
def check_disk(c): #c = Check(module='disk') path = c.conf['path'] alert_threshold = int(c.conf['alert']) # sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5) disk=psutil.disk_usage(path) ci = CheckItem('disk',path,"Path") c.add_item(ci) ci = CheckItem('disk_total',disk[0],"Total (Bytes)", unit='bytes') h_total = ci.human() c.add_item(ci) ci = CheckItem('disk_used',disk[1],"Used (Bytes)", unit='bytes') h_used = ci.human() c.add_item(ci) ci = CheckItem('disk_free',disk[2],"Free (Bytes)", unit='bytes') h_free = ci.human() c.add_item(ci) ci = CheckItem('disk_percent',disk[3],"Used (percent)", unit='%') if disk[3] > alert_threshold: c.alert += 1 c.add_message("path : {} - critical capacity ({} %)".format(path,disk[3])) else: c.add_message("path : {} - used: {} % - used: {} - free: {} - total: {} ".format(path, disk[3],h_used, h_free,h_total)) c.add_item(ci) return c
def check_swap(c): # sswap(total=2147479552, used=0, free=2147479552, percent=0.0, sin=0, sout=0) swap = psutil.swap_memory() m1 = CheckItem('swap_percent',swap.percent,"Swap used (percent)", unit = '%') c.add_item(m1) m2 = CheckItem('swap_used',swap.used,'Swap used (bytes)', unit = 'bytes') h_used = m2.human() c.add_item(m2) m3 = CheckItem('swap_total',swap.total,'Swap total (bytes)', unit = 'bytes') h_total = m3.human() c.add_item(m3) c.add_message("used: {} % / {} - total {}".format(swap.percent, h_used, h_total)) return c
def check_boottime(c): #c = Check(module='boottime') boottime = int(time.time() - psutil.boot_time()) days = int(boottime / 86400) m1 = CheckItem('boottime_seconds', boottime, "Seconds since last reboot", unit='seconds') h_sec = m1.human() c.add_item(m1) m2 = CheckItem('boottime_days', days, 'Days since last reboot', unit='days') c.add_item(m2) c.add_message("days since last reboot : {} days - {} sec.".format( days, h_sec)) return c
def check_process(c): # --available option ? if cmt.ARGS["available"]: print("-" * 25) print("Process available :") print("-" * 25) for p in psutil.process_iter(): try: # Get process name & pid from process object. print(p.name()) #print(processName , ' ::: ', processID) except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass print("-" * 25) return c # real check name = c.name psname = c.conf['psname'] #{'name': 'python3', 'cpu_times': pcputimes(user=0.39, system=0.3, # children_user=0.0, children_system=0.0), # 'memory_info': pmem(rss=27049984, vms=123904000, shared=13443072, text=3883008, # lib=0, data=13901824, dirty=0), 'username': '******', 'pid': 3125} #for proc in psutil.process_iter(['pid', 'name', 'username','cpu_times','memory_info']): # #print(proc.info) ci = CheckItem('process_name', name, "") c.add_item(ci) for proc in psutil.process_iter(): try: # Get process name & pid from process object. processName = proc.name() processID = proc.pid #print(processName , ' ::: ', processID) except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass # pinfo = proc.as_dict(attrs=['name','pid','memory_info','cpu_times']) if processName == psname: mem = proc.memory_info().rss ci = CheckItem('process_memory', mem, "rss", unit="bytes") h_mem = ci.human() c.add_item(ci) cpu = proc.cpu_times().user ci = CheckItem('process_cpu', cpu, "cpu time, user", unit='seconds') c.add_item(ci) c.add_message("{} found ({}) - memory rss {} - cpu {} sec.".format( name, psname, h_mem, cpu)) return c c.alert += 1 c.add_message("{} missing ({})".format(name, psname)) return c