From 493e557afb272871dc030bcd5ac56f18bd85e3e9 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Mon, 1 May 2006 18:28:01 +0000 Subject: [PATCH] support one-shot --status argument to get quick summary --- swapmon.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/swapmon.py b/swapmon.py index 7373d08..af89ef9 100755 --- a/swapmon.py +++ b/swapmon.py @@ -9,7 +9,7 @@ # Andy Bavier # 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 @@ -122,6 +122,7 @@ Options: --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)) @@ -261,6 +262,22 @@ def swap_used(): 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 @@ -269,7 +286,7 @@ def main(): 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: @@ -298,6 +315,9 @@ def main(): 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) @@ -323,24 +343,13 @@ def main(): (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 @@ -351,7 +360,7 @@ def main(): # 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 -- 2.43.0