moving pcu transport code
[monitor.git] / monitor_policy.py
index 7d79fab..5049db2 100644 (file)
@@ -1,18 +1,26 @@
-from config import config
-#print "policy"
-config = config()
+import config
 import database
 import time
 import mailer
-from www.printbadnodes import cmpCategoryVal
+from unified_model import cmpCategoryVal
 import sys
 import emailTxt
 import string
+from monitor.wrapper import plccache
 
-from policy import get_ticket_id, print_stats, close_rt_backoff, reboot_node
 from rt import is_host_in_rt_tickets
 import plc
 
+def get_ticket_id(record):
+       if 'ticket_id' in record and record['ticket_id'] is not "" and record['ticket_id'] is not None:
+               return record['ticket_id']
+       elif            'found_rt_ticket' in record and \
+                record['found_rt_ticket'] is not "" and \
+                record['found_rt_ticket'] is not None:
+               return record['found_rt_ticket']
+       else:
+               return None
+
 # Time to enforce policy
 POLSLEEP = 7200
 
@@ -49,7 +57,7 @@ class Merge:
                self.merge_list = l_merge
 
                # the hostname to loginbase mapping
-               self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
+               self.plcdb_hn2lb = plccache.plcdb_hn2lb
 
                # Previous actions taken on nodes.
                self.act_all = database.if_cached_else(1, "act_all", lambda : {})
@@ -257,7 +265,7 @@ class RT:
 class Diagnose:
        def __init__(self, record_list):
                self.record_list = record_list
-               self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
+               self.plcdb_hn2lb = plccache.plcdb_hn2lb
                self.findbad = database.if_cached_else(1, "findbad", lambda : {})
 
                self.diagnose_in = {}
@@ -823,11 +831,22 @@ class Diagnose:
 
                return up
 
+def close_rt_backoff(args):
+       if 'ticket_id' in args and (args['ticket_id'] != "" and args['ticket_id'] != None):
+               mailer.closeTicketViaRT(args['ticket_id'], 
+                                                               "Ticket CLOSED automatically by SiteAssist.")
+               plc.enableSlices(args['hostname'])
+               plc.enableSliceCreation(args['hostname'])
+       return
+
+def reboot_node(args):
+       host = args['hostname']
+       return reboot.reboot_policy(host, True, config.debug)
 
 class Action:
        def __init__(self, diagnose_out):
                # the hostname to loginbase mapping
-               self.plcdb_hn2lb = database.dbLoad("plcdb_hn2lb")
+               self.plcdb_hn2lb = plccache.plcdb_hn2lb
 
                # Actions to take.
                self.diagnose_db = diagnose_out
@@ -876,7 +895,7 @@ class Action:
                #print_stats("sites_observed", stats)
                #print_stats("sites_diagnosed", stats)
                #print_stats("nodes_diagnosed", stats)
-               print_stats("sites_emailed", stats)
+               self.print_stats("sites_emailed", stats)
                #print_stats("nodes_actedon", stats)
                print string.join(stats['allsites'], ",")
 
@@ -919,18 +938,22 @@ class Action:
                if ADMIN & roles:
                        contacts += [config.email]
                if TECH & roles:
-                       contacts += [TECHEMAIL % loginbase]
+                       #contacts += [TECHEMAIL % loginbase]
+                       contacts += plc.getTechEmails(loginbase)
                if PI & roles:
-                       contacts += [PIEMAIL % loginbase]
+                       #contacts += [PIEMAIL % loginbase]
+                       contacts += plc.getPIEmails(loginbase)
                if USER & roles:
+                       contacts += plc.getSliceUserEmails(loginbase)
                        slices = plc.slices(loginbase)
                        if len(slices) >= 1:
-                               for slice in slices:
-                                       contacts += [SLICEMAIL % slice]
                                print "SLIC: %20s : %d slices" % (loginbase, len(slices))
                        else:
                                print "SLIC: %20s : 0 slices" % loginbase
 
+               unique_contacts = set(contacts)
+               contacts = [ c for c in unique_contacts ]       # convert back into list
+
                try:
                        subject = message[0] % args
                        body = message[1] % args