On vserver create, the slice goes to init 1 which causes an extra column to get print...
[mom.git] / swapmon.py
index 7ca19bc..35a98e8 100755 (executable)
@@ -198,6 +198,9 @@ class Reset:
                    print "Slice %s is being killed." % self.name   
                    vserver = VServer(self.name)
                    vserver.stop()
                    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)
                    os._exit(0)
                 else:
                     os.waitpid(pid,0)
@@ -235,7 +238,7 @@ class Reset:
                     print "Resetting slice " + self.name 
                     vserver = VServer(self.name)
                     vserver.stop()
                     print "Resetting slice " + self.name 
                     vserver = VServer(self.name)
                     vserver.stop()
-                    vserver.start(wait = False)
+                    vserver.start()
                     os._exit(0)
                 else:
                     os.waitpid(pid,0)
                     os._exit(0)
                 else:
                     os.waitpid(pid,0)
@@ -269,10 +272,10 @@ 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
     """
     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,
     {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,
                       '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.
     # 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.
 
     # 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.
     # 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()
 
         # Chomp newline
         line = line.strip()
 
@@ -320,14 +323,16 @@ def slicestat(names = None):
                 except ValueError:
                     pass
 
                 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.
         # 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'])
             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:
         if m is not None:
             xid = bwlimit.get_xid(m.group(1))
             if xid is not None:
@@ -351,8 +356,10 @@ def slicestat(names = None):
         proc['rss'] += 12
 
         # Include additional page table overhead
         proc['rss'] += 12
 
         # Include additional page table overhead
-        if proc['vsize'] > 4096:
-            proc['rss'] += 4 * ((proc['vsize'] - 1) / 4096)
+        try:
+            if proc['vsize'] > 4096:
+                proc['rss'] += 4 * ((proc['vsize'] - 1) / 4096)
+        except: pass
 
         if slices.has_key(proc['xid']):
             slice = slices[proc['xid']]
 
         if slices.has_key(proc['xid']):
             slice = slices[proc['xid']]
@@ -365,7 +372,7 @@ def slicestat(names = None):
         slice['rss'] += proc['rss']
 
         slices[proc['xid']] = slice
         slice['rss'] += proc['rss']
 
         slices[proc['xid']] = slice
-
+       
     return slices
 
 def memtotal():
     return slices
 
 def memtotal():
@@ -439,12 +446,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 %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['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'])
     
             proc['pmem'],
             proc['cmd'])
     
@@ -584,8 +590,6 @@ def main():
             if resetlist[resetslice].killtimeleft == 0 and \
             resetlist[resetslice].resettimeleft == 0:
                 del resetlist[resetslice]
             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
     
         if used >= reboot_thresh:
             # Dump slice state before rebooting