X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=swapmon.py;h=37743b17a6f60f3cad11d178f4f5c09896c6a5d2;hb=7aca3983cb46f3e88d2873cd1935622e61c10223;hp=a29a59e12d3f74ad163a695a87e0b30285af20bb;hpb=d12d2ba5e6dce633dc95fd53f07448f77ac0fe9c;p=mom.git diff --git a/swapmon.py b/swapmon.py index a29a59e..37743b1 100755 --- a/swapmon.py +++ b/swapmon.py @@ -10,7 +10,7 @@ # Faiyaz Ahmed # Copyright (C) 2004-2006 The Trustees of Princeton University # -# $Id: swapmon.py,v 1.5 2006/05/09 03:23:57 mlhuang Exp $ +# $Id: swapmon.py,v 1.10 2006/08/16 16:18:45 faiyaza Exp $ # import syslog @@ -45,22 +45,26 @@ period = 30 change_thresh = 5 # Swap utilization at which the largest consumer of physical memory is reset -reset_thresh = 75 +reset_thresh = 80 # Swap utilization at which the machine is rebooted reboot_thresh = 95 # Time to wait before checking slice again after reset -reset_timeout = 10 +reset_timeout = 15 # Number of strikes before killing (strike, strike, kill) kill_thresh = 2 -# Time to wait before removing slice from kill queue -kill_timeout = 30 +# Time to wait before removing slice from kill queue (probation) +kill_timeout = 120 -# Minimum physical memory utilization to be considered the largest consumer -min_thresh = 10 +# Don't email the same message more than once in the same emailtimeout interval +email_timeout = 1800 + +# Physical size threshold to be considered a consumer. Rationale is if there are no procs +# with a size at least as large as this, then there is a slow leaker; better to just reboot. +rss_min = 150 * 1024 # System slices that should not be reset (regexps) system_slices = ['root', PLC_SLICE_PREFIX + '_'] @@ -88,7 +92,7 @@ Sometime before %(date)s, swap space was nearly exhausted on %(hostname)s. Slice %(slice)s was reset since it was the largest consumer of -physical memory at %(rss)s (%(percent)4.1f%%). +physical memory at %(rss)s (%(percent)4.1f%%) (%(sz)s writable). Please reply to this message explaining the nature of your experiment, and what you are doing to address the problem. @@ -108,8 +112,8 @@ Sometime before %(date)s, swap space was nearly exhausted on %(hostname)s. System slice %(slice)s was the largest consumer of physical memory at -%(rss)s (%(percent)4.1f%%). It was not reset, but please verify its -behavior. +%(rss)s (%(percent)4.1f%%) (%(sz)s writable). It was not reset, +but please verify its behavior. %(slice)s processes prior to alarm: @@ -126,7 +130,8 @@ Sometime before %(date)s, swap space was nearly exhausted on %(hostname)s. Slice %(slice)s was killed since it was the largest consumer of -physical memory at %(rss)s (%(percent)4.1f%%) after repeated restarts. +physical memory at %(rss)s (%(percent)4.1f%%) (%(sz)s writable) +after repeated restarts. Please reply to this message explaining the nature of your experiment, and what you are doing to address the problem. @@ -146,14 +151,18 @@ class Reset: resettimeleft - timeout before checking for next reset resetcount - number of strikes - killtimeleft - time out before removing from kill queue + killtimeleft - time out before removing from kill queue + {kill,reset}mail - Time of last email + kill - State of kill. If slice is already being killed, wait before retry. """ def __init__(self,name): self.name = name self.resettimeleft = reset_timeout self.resetcount = 0 + self.resetmail = 0 self.killtimeleft = kill_timeout + self.killmail = 0 def __repr__(self): return self.name @@ -163,20 +172,20 @@ class Reset: if self.resettimeleft > 0: self.resettimeleft -= 1 if debug and verbose: - print "%s has %s seconds to die and has been reset %s times" \ - %(self.name, self.resettimeleft, self.resetcount) print "%s has %s seconds in probation" \ %(self.name, self.killtimeleft) if self.killtimeleft > 0: # Count down kill probation timer (killtimeleft) self.killtimeleft -= 1 + if self.killtimeleft == 1: + print "%s is out of probation" % self.name else: - # Once out of probation period (killtimeleft), remove strike + # Once out of probation period (killtimeleft), remove strikes self.resetcount = 0 - # Check to see if a slice needs to be killed. If it has rules more than kill_thresh in - # the probation period (kill_timeout) send an email, kill the slice. + # Check to see if a slice needs to be killed. If it has been killed more + # than kill_thresh in the probation period (kill_timeout) send an email, kill the slice. def checkkill(self,params): if self.killtimeleft > 0 and self.resetcount >= kill_thresh: if debug: @@ -186,20 +195,26 @@ class Reset: pid = os.fork() if pid == 0: print "Slice %s is being killed." % self.name - vserver = VServer(self.name) - vserver.stop() + vserver = VServer(self.name) + vserver.stop() + os._exit(0) else: os.waitpid(pid,0) except Exception, err: - print "Warning: Exception received while killing slice %s: %s" % self.name, err - slicemail(self.name, kill_subject % params, kill_body % params) + print "Warning: Exception received while killing slice %s: %s" \ + % self.name, err + if (time.time() - self.killmail) > email_timeout: + slicemail(self.name, kill_subject % params, kill_body % params) + print "Sending KILL email for slice %s" % self.name + self.killmail = time.time() return True return False # Reset slice after checking to see if slice is out of timeout. # Increment resetcount, check to see if larger than kill_thresh. def reset(self, params): - # If its the first reset or if its been reset before + # If its the first reset (came back after kill) + # or if its been reset before # and we are out of the reset timeout. if self.resetcount == 0 or self.resettimeleft == 0: # Do we need to kill this slice? Check history first. @@ -209,24 +224,28 @@ class Reset: self.resetcount += 1 self.killtimeleft = kill_timeout self.resettimeleft = reset_timeout + print "%s has %s seconds to die and has been reset %s times" \ + %(self.name, self.resettimeleft, self.resetcount) if debug: print reset_subject % params print reset_body % params try: pid = os.fork() if pid == 0: - print "Resetting slice " + self.name - vserver = VServer(self.name) - vserver.stop() - vserver.start(wait = False) - os._exit(0) - else: - os.waitpid(pid, 0) + print "Resetting slice " + self.name + vserver = VServer(self.name) + vserver.stop() + vserver.start(wait = False) + os._exit(0) + else: + os.waitpid(pid,0) except Exception, err: - print "Warning: Exception received while resetting slice %s:" \ + print "Warning: Exception received while resetting slice %s:" \ % self.name, err - slicemail(self.name, reset_subject % params, reset_body % params) - + if (time.time() - self.resetmail) > email_timeout: + slicemail(self.name, reset_subject % params, reset_body % params) + print "Sending Reset email for slice %s" % self.name + self.resetmail = time.time() def usage(): @@ -250,21 +269,22 @@ Options: def slicestat(names = None): """ Get status of specified slices (if names is None or empty, all - slices). vsize and rss are in KiB. Returns + slices). vsize, sz, and rss are in KiB. Returns {xid: {'xid': slice_id, 'name': slice_name, 'procs': [{'pid': pid, 'xid': slice_id, 'user', username, 'cmd': command, - 'vsize': virtual_kib, 'rss': physical_kib, + 'vsize': virtual_kib, 'sz': potential_kib, 'rss': physical_kib, 'pcpu': cpu_percent, 'pmem': mem_percent}] 'vsize': total_virtual_kib, + 'sz': total_potential_kib, 'rss': total_physical_kib}} """ # Mandatory fields. xid is a virtual field inserted by vps. Make # sure cmd is last so that it does not get truncated # automatically. - fields = ['pid', 'xid', 'user', 'vsize', 'rss', 'pcpu', 'pmem', 'cmd'] + fields = ['pid', 'xid', 'user', 'vsize', 'sz', 'rss', 'pcpu', 'pmem', 'cmd'] # vps inserts xid after pid in the output, but ps doesn't know # what the field means. @@ -338,10 +358,11 @@ def slicestat(names = None): if slices.has_key(proc['xid']): slice = slices[proc['xid']] else: - slice = {'xid': proc['xid'], 'name': name, 'procs': [], 'vsize': 0, 'rss': 0} + slice = {'xid': proc['xid'], 'name': name, 'procs': [], 'vsize': 0, 'sz': 0, 'rss': 0} slice['procs'].append(proc) slice['vsize'] += proc['vsize'] + slice['sz'] += proc['sz'] slice['rss'] += proc['rss'] slices[proc['xid']] = slice @@ -350,18 +371,25 @@ def slicestat(names = None): def memtotal(): """ - Returns total physical memory on the system in KiB. + Returns total physical and swap memory on the system in KiB. """ + mem = 0 + swap = 0 + meminfo = open("/proc/meminfo", "r") - line = meminfo.readline() + for line in meminfo.readlines(): + try: + (name, value, kb) = line.split() + except: + continue + if name == "MemTotal:": + mem = int(value) + elif name == "SwapTotal:": + swap = int(value) meminfo.close() - if line[0:8] == "MemTotal": - # MemTotal: 255396 kB - (name, value, kb) = line.split() - return int(value) - return 0 + return (mem, swap) def swap_used(): """ @@ -389,26 +417,33 @@ def swap_used(): return 100 * total_used / total_swap -def summary(names = None, total_rss = memtotal()): +def summary(slices = None, total_mem = None, total_swap = None): """ Return a summary of memory usage by slice. """ - slicelist = slicestat(names).values() - slicelist.sort(lambda a, b: b['rss'] - a['rss']) - - table = "%-20s%10s%24s\n\n" % ("Slice", "Processes", "Memory Usage") + if not slices: + slices = slicestat() + slicelist = slices.values() + slicelist.sort(lambda a, b: b['sz'] - a['sz']) + if total_mem is None or total_swap is None: + (total_mem, total_swap) = memtotal() + + table = "%-20s%10s%24s%24s\n\n" % ("Slice", "Processes", "Memory Usage", "Potential Usage") for slice in slicelist: - table += "%-20s%10d%16s (%4.1f%%)\n" % \ + table += "%-20s%10d%16s (%4.1f%%)%16s (%4.1f%%)\n" % \ (slice['name'], len(slice['procs']), format_bytes(slice['rss'] * 1024, si = False), - 100. * slice['rss'] / total_rss) + 100. * slice['rss'] / total_mem, + format_bytes(slice['sz'] * 1024, si = False), + 100. * slice['sz'] / (total_mem + total_swap)) + return table def main(): # Defaults global debug, verbose, datafile - global period, change_thresh, reset_thresh, reboot_thresh, min_thresh, system_slices + global period, change_thresh, reset_thresh, reboot_thresh, rss_min, system_slices # All slices names = [] @@ -439,11 +474,11 @@ def main(): elif opt == "--reboot-thresh": reboot_thresh = int(optval) elif opt == "--min-thresh": - min_thresh = int(optval) + rss_min = int(optval) elif opt == "--system-slice": system_slices.append(optval) elif opt == "--status": - print summary(names) + print summary(slicestat(names)) sys.exit(0) else: usage() @@ -460,8 +495,8 @@ def main(): syslog.openlog("swapmon") sys.stdout = sys.stderr = Logger() - # Get total physical memory - total_rss = memtotal() + # Get total memory + (total_mem, total_swap) = memtotal() try: f = open(datafile, "r+") @@ -470,13 +505,13 @@ def main(): (version, slices) = pickle.load(f) f.close() # Check version of data file - if version != "$Id: swapmon.py,v 1.5 2006/05/09 03:23:57 mlhuang Exp $": + if version != "$Id: swapmon.py,v 1.10 2006/08/16 16:18:45 faiyaza Exp $": print "Not using old version '%s' data file %s" % (version, datafile) raise Exception params = {'hostname': socket.gethostname(), 'date': time.asctime(time.gmtime()) + " GMT", - 'table': summary(total_rss)} + 'table': summary(slices, total_mem, total_swap)} if debug: print rebooted_subject % params @@ -487,7 +522,7 @@ def main(): # Delete data file os.unlink(datafile) except Exception: - version = "$Id: swapmon.py,v 1.5 2006/05/09 03:23:57 mlhuang Exp $" + version = "$Id: swapmon.py,v 1.10 2006/08/16 16:18:45 faiyaza Exp $" slices = {} # Query process table every 30 seconds, or when a large change in @@ -506,10 +541,11 @@ def main(): used = swap_used() for resetslice in resetlist.keys(): - resetlist[resetslice].update() + resetlist[resetslice].update() if last_used is None: last_used = used + if verbose: print "%d%% swap consumed" % used @@ -527,13 +563,15 @@ def main(): bwlimit.run("/bin/sync; /sbin/reboot -f") elif used >= reset_thresh: + if debug: + print "Memory used = %s" %(used) # Try and find a hog slicelist = slices.values() slicelist.sort(lambda a, b: b['rss'] - a['rss']) for slice in slicelist: - percent = 100. * slice['rss'] / total_rss + percent = 100. * slice['rss'] / total_mem - if percent < min_thresh: + if slice['rss'] < rss_min: continue print "%d%% swap consumed, slice %s is using %s (%d%%) of memory" % \ @@ -544,11 +582,12 @@ def main(): slice['procs'].sort(lambda a, b: b['rss'] - a['rss']) - table = "%5s %10s %10s %4s %4s %s\n\n" % ("PID", "VIRT", "RES", '%CPU', '%MEM', 'COMMAND') + table = "%5s %10s %10s %10s %4s %4s %s\n\n" % ("PID", "VIRT", "SZ", "RES", '%CPU', '%MEM', 'COMMAND') for proc in slice['procs']: - table += "%5s %10s %10s %4.1f %4.1f %s\n" % \ + table += "%5s %10s %10s %10s %4.1f %4.1f %s\n" % \ (proc['pid'], format_bytes(proc['vsize'] * 1024, si = False), + format_bytes(proc['sz'] * 1024, si = False), format_bytes(proc['rss'] * 1024, si = False), proc['pcpu'], proc['pmem'], proc['cmd']) @@ -557,28 +596,31 @@ def main(): 'table': table, 'slice': slice['name'], 'rss': format_bytes(slice['rss'] * 1024, si = False), + 'sz': format_bytes(slice['sz'] * 1024, si = False), 'percent': percent} # Match slice name against system slice patterns is_system_slice = filter(None, [re.match(pattern, slice['name']) for pattern in system_slices]) - if is_system_slice: - # Do not reset system slices, just warn once - if slice['name'] not in warned: - warned.append(slice['name']) - if debug: - print alarm_subject % params - print alarm_body % params - else: - print "Warning slice " + slice['name'] - slicemail(slice['name'], alarm_subject % params, alarm_body % params) + if is_system_slice: + # Do not reset system slices, just warn once + if slice['name'] not in warned: + warned.append(slice['name']) + if debug: + print alarm_subject % params + print alarm_body % params + else: + print "Warning slice " + slice['name'] + slicemail(slice['name'], alarm_subject % params, + alarm_body % params) else: - # Reset slice - if not resetlist.has_key(slice['name']): - resetlist[slice['name']] = Reset(slice['name']) - resetlist[slice['name']].reset(params) + # Reset slice + if not resetlist.has_key(slice['name']): + resetlist[slice['name']] = Reset(slice['name']) + resetlist[slice['name']].reset(params) + slices = slicestat(names) - elif timer <= 0 or used >= (last_used + change_thresh): + if timer <= 0 or used >= (last_used + change_thresh): if used >= (last_used + change_thresh): print "%d%% swap consumed, %d%% in last %d seconds" % \ (used, used - last_used, period - timer)