# Andy Bavier <acb@cs.princeton.edu>
# Copyright (C) 2004-2006 The Trustees of Princeton University
#
-# $Id: swapmon.py,v 1.1 2006/04/28 19:26:59 mlhuang Exp $
+# $Id: swapmon.py,v 1.2 2006/04/28 19:32:18 mlhuang Exp $
#
import syslog
--reboot-thresh=PERCENT Swap utilization at which the machine is rebooted
--min-thresh=PERCENT Minimum physical memory utilization to be considered a hog
--system-slice=SLICE System slice that should not be reset
+ --status Print memory usage statistics and exit
-h, --help This message
""".lstrip() % (sys.argv[0], debug, verbose, datafile, format_period(period))
return 100 * total_used / total_swap
+def summary(names = None, total_rss = memtotal()):
+ """
+ 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")
+ for slice in slicelist:
+ table += "%-20s%10d%16s (%4.1f%%)\n" % \
+ (slice['name'], len(slice['procs']),
+ format_bytes(slice['rss'] * 1024, si = False),
+ 100. * slice['rss'] / total_rss)
+
+ return table
+
def main():
# Defaults
global debug, verbose, datafile
names = []
try:
- longopts = ["debug", "verbose", "file=", "slice=", "help"]
+ longopts = ["debug", "verbose", "file=", "slice=", "status", "help"]
longopts += ["period=", "reset-thresh=", "reboot-thresh=", "min-thresh=", "system-slice="]
(opts, argv) = getopt.getopt(sys.argv[1:], "dvf:s:ph", longopts)
except getopt.GetoptError, err:
min_thresh = int(optval)
elif opt == "--system-slice":
system_slices.append(optval)
+ elif opt == "--status":
+ print summary(names)
+ sys.exit(0)
else:
usage()
sys.exit(0)
(version, slices) = pickle.load(f)
f.close()
# Check version of data file
- if version != "$Id: swapmon.py,v 1.1 2006/04/28 19:26:59 mlhuang Exp $":
+ if version != "$Id: swapmon.py,v 1.2 2006/04/28 19:32:18 mlhuang Exp $":
print "Not using old version '%s' data file %s" % (version, datafile)
raise Exception
- # Send notification if we rebooted the node because of swap exhaustion
- slicelist = slices.values()
- slicelist.sort(lambda a, b: b['rss'] - a['rss'])
-
- table = "%-20s%10s%24s\n\n" % ("Slice", "Processes", "Memory Usage")
- for slice in slicelist:
- table += "%-20s%10d%16s (%4.1f%%)\n" % \
- (slice['name'], len(slice['procs']),
- format_bytes(slice['rss'] * 1024, si = False),
- 100. * slice['rss'] / total_rss)
-
params = {'hostname': socket.gethostname(),
'date': time.asctime(time.gmtime()) + " GMT",
- 'table': table}
+ 'table': summary(total_rss)}
if debug:
print rebooted_subject % params
# Delete data file
os.unlink(datafile)
except Exception:
- version = "$Id: swapmon.py,v 1.1 2006/04/28 19:26:59 mlhuang Exp $"
+ version = "$Id: swapmon.py,v 1.2 2006/04/28 19:32:18 mlhuang Exp $"
slices = {}
# Query process table every 30 seconds, or when a large change in