+def formtable(slice, percent):
+ '''
+ Makes pretty message to email with human readable ps values.
+ '''
+ 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" % \
+ (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'])
+
+ prettytable = {'hostname': socket.gethostname(),
+ 'date': time.asctime(time.gmtime()) + " GMT",
+ 'table': table,
+ 'slice': slice['name'],
+ 'rss': format_bytes(slice['rss'] * 1024, si = False),
+ 'sz': format_bytes(slice['sz'] * 1024, si = False),
+ 'percent': percent}
+ return prettytable
+
+def readdat():
+ '''
+ Return dictionary of vps (slicestat) from datfile left behind by OOM
+ before rebooting. If none file, just grab the latest dict (slicestat)
+ and return that. If dat file found, means we rebooted, send an email to
+ pl_mom@pl.
+ '''
+ try:
+ f = open(datafile, "r+")
+ if verbose:
+ print "Loading %s" % datafile
+ (version, 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)
+ raise Exception
+
+ params = {'hostname': socket.gethostname(),
+ 'date': time.asctime(time.gmtime()) + " GMT",
+ 'table': summary(slices, total_mem, total_swap)}
+ if debug:
+ print rebooted_subject % params
+ print rebooted_body % params
+ else:
+ slicemail(None, rebooted_subject % params, rebooted_body % params)
+
+ # Delete data file
+ os.unlink(datafile)
+ except Exception:
+ version = "$Id$"
+ slices = slicestat()
+
+ return slices
+
+
+def writedat(slices):
+ """
+ Write (slices) to pickled datfile.
+ """
+ if verbose: print "Saving %s" % datafile
+ f = open(datafile, "w")
+ pickle.dump((version, slices), f)
+ f.close()
+
+