X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=swapmon.py;h=5f6afd805e1582f8a28de4a19562cd036ff42d2d;hb=cec191ea114b80511881ea3d94c3443b124be939;hp=83b4ec15d01a376bdfe2e4e82bcd77cfe285b0e3;hpb=56bcba1ce3efd74a51cf58bf6d21e39731473326;p=mom.git diff --git a/swapmon.py b/swapmon.py index 83b4ec1..5f6afd8 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.9 2006/07/19 19:40:55 faiyaza Exp $ # import syslog @@ -62,8 +62,9 @@ kill_timeout = 120 # Don't email the same message more than once in the same emailtimeout interval email_timeout = 1800 -# Minimum physical memory utilization to be considered the largest consumer -min_thresh = 10 +# 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 + '_'] @@ -159,7 +160,6 @@ class Reset: self.resettimeleft = reset_timeout self.resetcount = 0 self.resetmail = 0 - self.kill = False self.killtimeleft = kill_timeout self.killmail = 0 @@ -181,27 +181,27 @@ class Reset: else: # Once out of probation period (killtimeleft), remove strikes self.resetcount = 0 - self.kill = False - # 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 and \ - self.kill == False: - self.kill = True + if self.killtimeleft > 0 and self.resetcount >= kill_thresh: if debug: print kill_subject % params print kill_body % params try: - self.pid = os.fork() - if self.pid == 0: + pid = os.fork() + if pid == 0: print "Slice %s is being killed." % self.name 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 + 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 @@ -212,7 +212,8 @@ class Reset: # 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. @@ -235,6 +236,8 @@ class Reset: 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:" \ % self.name, err @@ -423,7 +426,7 @@ def summary(names = None, total_rss = memtotal()): 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 = [] @@ -453,8 +456,6 @@ def main(): reset_thresh = int(optval) elif opt == "--reboot-thresh": reboot_thresh = int(optval) - elif opt == "--min-thresh": - min_thresh = int(optval) elif opt == "--system-slice": system_slices.append(optval) elif opt == "--status": @@ -485,7 +486,7 @@ 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.9 2006/07/19 19:40:55 faiyaza Exp $": print "Not using old version '%s' data file %s" % (version, datafile) raise Exception @@ -502,7 +503,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.9 2006/07/19 19:40:55 faiyaza Exp $" slices = {} # Query process table every 30 seconds, or when a large change in @@ -525,6 +526,7 @@ def main(): if last_used is None: last_used = used + if verbose: print "%d%% swap consumed" % used @@ -542,13 +544,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 - if percent < min_thresh: + if slice['rss'] < rss_min: continue print "%d%% swap consumed, slice %s is using %s (%d%%) of memory" % \ @@ -577,23 +581,24 @@ def main(): # 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 and params['rss'] > 100: - # 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: + 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) + 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)