Sends mail to MOM mail address from plc_config. Updated to use new vserver.
authorFaiyaz Ahmed <faiyaza@cs.princeton.edu>
Thu, 24 Apr 2008 20:40:32 +0000 (20:40 +0000)
committerFaiyaz Ahmed <faiyaza@cs.princeton.edu>
Thu, 24 Apr 2008 20:40:32 +0000 (20:40 +0000)
pl_mom.py
swapmon.py

index b4e09b0..bcb77c4 100644 (file)
--- a/pl_mom.py
+++ b/pl_mom.py
@@ -23,8 +23,8 @@ except:
     print "Warning: Configuration file /etc/planetlab/plc_config.py not found"
     PLC_NAME = "PlanetLab"
     PLC_SLICE_PREFIX = "pl"
-    PLC_MAIL_SUPPORT_ADDRESS = "support@planet-lab.org"
-    PLC_MAIL_SLICE_ADDRESS = "SLICE@slices.planet-lab.org"
+    PLC_MAIL_SUPPORT_ADDRESS = "root@" + socket.gethostname()
+    PLC_MAIL_SLICE_ADDRESS = "SLICE@" + socket.gethostname()
 
 def format_bytes(bytes, si = True):
     """
@@ -88,11 +88,7 @@ def removepid(prog):
 def slicemail(slice, subject, body):
     sendmail = os.popen("/usr/sbin/sendmail -N never -t -f%s" % PLC_MAIL_SUPPORT_ADDRESS, "w")
 
-    # PLC has a separate list for pl_mom messages
-    if PLC_MAIL_SUPPORT_ADDRESS == "support@planet-lab.org":
-        to = ["pl-mom@planet-lab.org"]
-    else:
-        to = [PLC_MAIL_SUPPORT_ADDRESS]
+    to = [PLC_MAIL_MOM_LIST_ADDRESS]
 
     if slice is not None and slice != "root":
         to.append(PLC_MAIL_SLICE_ADDRESS.replace("SLICE", slice))
index da8f3a0..b25d57f 100755 (executable)
@@ -35,8 +35,8 @@ from pl_mom import *
 # Defaults
 debug = False
 verbose = 0
-datafile = "/var/lib/misc/swapmon.dat"
-
+DATAFILE = "/var/lib/misc/swapmon.dat"
+VERSION = "$Id$"
 # Seconds between process analysis
 period = 30
 
@@ -198,6 +198,9 @@ class Reset:
                    print "Slice %s is being killed." % self.name   
                    vserver = VServer(self.name)
                    vserver.stop()
+                   # ignore initscripts.  Don't run anything at start.
+                   vserver.INITSCRIPTS = []
+                   vserver.start()
                    os._exit(0)
                 else:
                     os.waitpid(pid,0)
@@ -229,18 +232,18 @@ class Reset:
             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)
-                except Exception, err:
-                    print "Warning: Exception received while resetting slice %s:" \
+            try:
+                pid = os.fork()
+                if pid == 0:  
+                    print "Resetting slice " + self.name 
+                    vserver = VServer(self.name)
+                    vserver.stop()
+                    vserver.start()
+                    os._exit(0)
+                else:
+                    os.waitpid(pid,0)
+            except Exception, err:
+                print "Warning: Exception received while resetting slice %s:" \
                         % self.name, err
             if (time.time() - self.resetmail) > email_timeout:
                 slicemail(self.name, reset_subject % params, reset_body % params)
@@ -263,16 +266,16 @@ Options:
         --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))
+""".lstrip() % (sys.argv[0], debug, verbose, DATAFILE, format_period(period))
 
 def slicestat(names = None):
     """
     Get status of specified slices (if names is None or empty, all
     slices). vsize, sz, and rss are in KiB. Returns
-
+    PID CONTEXT             VSZ    SZ   RSS %MEM CMD
     {xid: {'xid': slice_id,
             'name': slice_name,
-            'procs': [{'pid': pid, 'xid': slice_id, 'user', username, 'cmd': command,
+            'procs': [{'pid': pid, 'xid': slice_id, 'cmd': command,
                       'vsize': virtual_kib, 'sz': potential_kib, 'rss': physical_kib,
                       'pcpu': cpu_percent, 'pmem': mem_percent}]
             'vsize': total_virtual_kib,
@@ -283,7 +286,7 @@ 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', 'user', 'vsize', 'sz', 'rss', 'pcpu', 'pmem', 'cmd']
+    fields = ['pid', 'xid', 'vsize', 'sz', 'rss', 'pmem', 'cmd']
 
     # vps inserts xid after pid in the output, but ps doesn't know
     # what the field means.
@@ -295,7 +298,7 @@ def slicestat(names = None):
     # Eat the header line. vps depends on the header to figure out
     # which column is the PID column, so we can't just tell ps not to
     # print it.
-    for line in bwlimit.run("/usr/sbin/vps -e -o " + ",".join(ps_fields))[1:]:
+    for line in bwlimit.run("/usr/sbin/vps -e -o " + ":16,".join(ps_fields))[1:]:
         # Chomp newline
         line = line.strip()
 
@@ -320,14 +323,16 @@ def slicestat(names = None):
                 except ValueError:
                     pass
 
-        # vps sometimes prints ERR instead of a context ID if it
+        # 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:
+        if (type(proc['xid']) != int) or (type(proc['vsize']) !=int):
             continue
 
         # Assign (pl_)sshd processes to slice instead of root
         m = re.search(r"sshd: ([a-zA-Z_]+)", proc['cmd'])
+
         if m is not None:
             xid = bwlimit.get_xid(m.group(1))
             if xid is not None:
@@ -365,7 +370,7 @@ def slicestat(names = None):
         slice['rss'] += proc['rss']
 
         slices[proc['xid']] = slice
-
+       
     return slices
 
 def memtotal():
@@ -439,12 +444,11 @@ def formtable(slice, percent):
     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 %10s %4.1f %4.1f %s\n" % \
+        table += "%5s %10s %10s %10s %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'])
     
@@ -465,14 +469,14 @@ def readdat():
     pl_mom@pl.
     '''
     try:
-        f = open(datafile, "r+")
+        f = open(DATAFILE, "r+")
         if verbose:
-            print "Loading %s" % datafile
-        (version, slices) = pickle.load(f)
+            print "Loading %s" % DATAFILE
+        (v, slices) = pickle.load(f)
         f.close()
         # Check version of data file
-        if version != "$Id$":
-            print "Not using old version '%s' data file %s" % (version, datafile)
+        if v != VERSION:
+            print "Not using old version '%s' data file %s" % (v, DATAFILE)
             raise Exception
 
         params = {'hostname': socket.gethostname(),
@@ -485,9 +489,8 @@ def readdat():
             slicemail(None, rebooted_subject % params, rebooted_body % params)
 
         # Delete data file
-        os.unlink(datafile)
+        os.unlink(DATAFILE)
     except Exception:
-        version = "$Id$"
         slices = slicestat()
 
     return slices
@@ -497,15 +500,15 @@ def writedat(slices):
     """
     Write (slices) to pickled datfile.
     """
-    if verbose:  print "Saving %s" % datafile
-    f = open(datafile, "w")
-    pickle.dump((version, slices), f)
+    if verbose:  print "Saving %s" % DATAFILE
+    f = open(DATAFILE, "w")
+    pickle.dump((VERSION, slices), f)
     f.close()
 
 
 def main():
     # Defaults
-    global debug, verbose, datafile
+    global debug, verbose, DATAFILE, VERSION 
     global period, change_thresh, reset_thresh, reboot_thresh, rss_min, system_slices
     # All slices
     names = []
@@ -525,7 +528,7 @@ def main():
         elif opt == "-v" or opt == "--verbose":
             verbose += 1
         elif opt == "-f" or opt == "--file":
-            datafile = optval
+            DATAFILE = optval
         elif opt == "-s" or opt == "--slice":
             names.append(optval)
         elif opt == "-p" or opt == "--period":
@@ -585,8 +588,6 @@ def main():
             if resetlist[resetslice].killtimeleft == 0 and \
             resetlist[resetslice].resettimeleft == 0:
                 del resetlist[resetslice]
-
-        if verbose:  print "%d%% swap consumed" % used
     
         if used >= reboot_thresh:
             # Dump slice state before rebooting