find bwlimit at plnode-utils
[mom.git] / swapmon.py
index f2654e6..6f995fa 100755 (executable)
@@ -22,12 +22,8 @@ import pickle
 import socket
 import time
 
-# util-vserver/python/vserver.py allows us to control slices directly
-# from Python
-from vserver import VServer
-
 # bwlimit exports a few useful functions like run(), get_xid(), and get_slice()
-import bwlimit
+import plnode.bwlimit as bwlimit
 
 # Utility functions
 from pl_mom import *
@@ -133,6 +129,7 @@ Options:
         --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
+        --memstatus             Print total memory, total swap, and swap used
         -h, --help              This message
 """.lstrip() % (sys.argv[0], debug, verbose, DATAFILE, format_period(period))
 
@@ -154,12 +151,13 @@ def slicestat(names = None):
     # 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', 'vsize', 'sz', 'rss', 'pmem', 'cmd']
+    fields = ['pid', 'xid', 'vsname', 'vsize', 'sz', 'rss', 'pmem', 'cmd']
 
     # vps inserts xid after pid in the output, but ps doesn't know
     # what the field means.
     ps_fields = list(fields)
     ps_fields.remove('xid')
+    ps_fields.remove('vsname')
 
     slices = {}
 
@@ -170,14 +168,13 @@ def slicestat(names = None):
         # Chomp newline
         line = line.strip()
 
-        # Replace "0 MAIN" and "1 ALL_PROC" (the special monikers that
-        # vps uses to denote the root context and the "all contexts"
-        # context) with "0" so that we can just split() on whitespace.
-        line = line.replace("0 MAIN", "0").replace("1 ALL_PROC", "0")
-
         # Represent process as a dict of fields
         values = line.split(None, len(fields) - 1)
         if len(values) != len(fields):
+            if "ERR" in line:
+                pass # ignore spurious error message from vps
+            else:
+                print "slicestat: failed to parse line: " + line
             continue
         proc = dict(zip(fields, values))
 
@@ -191,11 +188,15 @@ def slicestat(names = None):
                 except ValueError:
                     pass
 
-        # vps sometimes prints ERR or the name of the slice 
+        # vps sometimes prints ERR or the name of the slice
             # instead of a context ID if it
         # cannot identify the context of an orphaned (usually dying)
         # process. Skip these processes.
         if (type(proc['xid']) != int) or (type(proc['vsize']) !=int):
+            if "ERR" in line:
+                pass # ignore spurious error message from vps
+            else:
+                print "slicestat: failed to parse line: " + line
             continue
 
         # Assign (pl_)sshd processes to slice instead of root
@@ -240,7 +241,7 @@ def slicestat(names = None):
         slice['rss'] += proc['rss']
 
         slices[proc['xid']] = slice
-       
+
     return slices
 
 def memtotal():
@@ -389,7 +390,7 @@ def main():
     emailed = {}
 
     try:
-        longopts = ["debug", "verbose", "file=", "slice=", "status", "help"]
+        longopts = ["debug", "verbose", "file=", "slice=", "status", "memstatus", "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:
@@ -421,6 +422,13 @@ def main():
         elif opt == "--status":
             print summary(slicestat(names))
             sys.exit(0)
+        elif opt == "--memstatus":
+            (mem, swap) = memtotal()
+            swap_pct = swap_used()
+            print "memory total:", mem
+            print "swap total:", swap
+            print "swap used:", swap_pct
+            sys.exit(0)
         else:
             usage()
             sys.exit(0)
@@ -446,7 +454,6 @@ def main():
     while True:
         used = swap_used()
         if last_used is None:  last_used = used
-        
    
         if used >= reboot_thresh:
             # Dump slice state before rebooting