fixed deletion logic. ..this is sloppy but it works. Also fixed network shares.
[nodemanager.git] / bwmon.py
index ec8c552..ad860be 100644 (file)
--- a/bwmon.py
+++ b/bwmon.py
@@ -32,26 +32,22 @@ import database
 
 from sets import Set
 
+# Defaults
+debug = False
+verbose = False
+datafile = "/var/lib/misc/bwmon.dat"
+
 try:
     sys.path.append("/etc/planetlab")
     from plc_config import *
 except:
     logger.log("bwmon:  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"
+    logger.log("bwmon:  Running in DEBUG mode.  Logging to file and not emailing.")
 
 # Constants
 seconds_per_day = 24 * 60 * 60
 bits_per_byte = 8
 
-# Defaults
-debug = False
-verbose = False
-datafile = "/var/lib/misc/bwmon.dat"
-#nm = None
-
 # Burst to line rate (or node cap).  Set by NM. in KBit/s
 default_MaxRate = int(bwlimit.get_bwcap() / 1000)
 default_Maxi2Rate = int(bwlimit.bwmax / 1000)
@@ -133,13 +129,8 @@ def slicemail(slice, subject, body):
 
     sendmail = os.popen("/usr/sbin/sendmail -N never -t -f%s" % PLC_MAIL_SUPPORT_ADDRESS, "w")
 
-    # PLC and PLE has a separate list for pl_mom messages
-    if PLC_MAIL_SUPPORT_ADDRESS == "support@planet-lab.org":
-        to = ["pl-mom@planet-lab.org"]
-    elif PLC_MAIL_SUPPORT_ADDRESS == "support@planet-lab.eu":
-        to = ["pl-mom@planet-lab.eu"]
-    else:
-        to = [PLC_MAIL_SUPPORT_ADDRESS]
+    # Parsed from MyPLC config
+    to = [PLC_MAIL_MOM_LIST_ADDRESS]
 
     if slice is not None and slice != "root":
         to.append(PLC_MAIL_SLICE_ADDRESS.replace("SLICE", slice))
@@ -369,7 +360,7 @@ class Slice:
         self.updateSliceAttributes(rspec)    
 
         # Check shares for Sirius loans.
-        if runningshare != self.share:
+        if runningshare != self.Share:
             logger.log("bwmon:  Updating share to %s" % self.share)
             bwlimit.set(xid = self.xid, 
                 minrate = self.MinRate * 1000, 
@@ -568,6 +559,9 @@ def sync(nmdbcopy):
     for nohtbslice in nohtbslices:
         if live.has_key(nohtbslice): 
             slices[nohtbslice].reset( 0, 0, 0, 0, live[nohtbslice]['_rspec'] )
+        else:
+            logger.log("bwmon:  Removing abondoned slice %s from dat." % nohtbslice)
+            del slices[nohtbslice]
 
     # The dat file doesnt have HTB for the slice but kern has HTB
     slicesnodat = Set(kernelhtbs.keys()) - Set(slices.keys())
@@ -619,10 +613,9 @@ def sync(nmdbcopy):
                                     deadslice['htb']['share'], 
                                     live[newslice]['_rspec'])
                 # Since the slice has been reinitialed, remove from dead database.
-                del deaddb[deadslice]
+                del deaddb[deadslice['slice'].name]
         else:
-            logger.log("bwmon  Slice %s doesn't have xid.  Must be delegated."\
-                       "Skipping." % live[newslice]['name'])
+            logger.log("bwmon  Slice %s doesn't have xid.  Skipping." % live[newslice]['name'])
 
     # Move dead slices that exist in the pickle file, but
     # aren't instantiated by PLC into the dead dict until
@@ -633,19 +626,20 @@ def sync(nmdbcopy):
     for deadxid in deadxids:
         if deadxid == root_xid or deadxid == default_xid:
             continue
-        logger.log("bwmon:  removing dead slice  %s " % deadxid)
-        if slices.has_key(deadxid):
+        logger.log("bwmon:  removing dead slice %s " % deadxid)
+        if slices.has_key(deadxid) and kernelhtbs.has_key(deadxid):
             # add slice (by name) to deaddb
+            logger.log("bwmon:  Saving bandwidth totals for %s." % slices[deadxid].name)
             deaddb[slices[deadxid].name] = {'slice': slices[deadxid], 'htb': kernelhtbs[deadxid]}
             del slices[deadxid]
         if kernelhtbs.has_key(deadxid): 
             bwlimit.off(deadxid)
        
        # Clean up deaddb
-       for (deadslice, deadhtb) in deaddb.iteritems():
-               if (time.time() >= (deadslice.time() + period)):
+       for deadslice in deaddb.itervalues():
+               if (time.time() >= (deadslice['slice'].time + period)):
                        logger.log("bwmon: Removing dead slice %s from dat." % deadslice.name)
-                       del deaddb[deadslice.name]
+                       del deaddb[deadslice['slice'].name]
 
     # Get actual running values from tc since we've added and removed buckets.
     # Update slice totals and bandwidth. {xid: {values}}