+ # TODO: remove 'diagnose_out',
+ # or at least the entries that were acted on.
+ soltesz.dbDump("act_all", self.act_all)
+
+ def accumSites(self):
+ """
+ Take all nodes, from l_action, look them up in the diagnose_db database,
+ and insert them into sickdb[] as:
+
+ This way only the given l_action nodes will be acted on regardless
+ of how many from diagnose_db are available.
+
+ sickdb[loginbase][nodename] = diag_record
+ """
+ # TODO: what if l_action == None ?
+ for nodename in self.l_action:
+
+ loginbase = self.plcdb_hn2lb[nodename]
+
+ if loginbase in self.diagnose_db and \
+ nodename in self.diagnose_db[loginbase]['nodes']:
+
+ diag_record = self.diagnose_db[loginbase]['nodes'][nodename]
+
+ if loginbase not in self.sickdb:
+ self.sickdb[loginbase] = {'nodes' : {}}
+
+ # NOTE: don't copy all node records, since not all will be in l_action
+ self.sickdb[loginbase]['nodes'][nodename] = diag_record
+ # NOTE: but, we want to get the loginbase config settings,
+ # this is the easiest way.
+ self.sickdb[loginbase]['config'] = self.diagnose_db[loginbase]['config']
+ #else:
+ #print "%s not in diagnose_db!!" % loginbase
+ return
+
+ def __emailSite(self, loginbase, roles, message, args):
+ """
+ loginbase is the unique site abbreviation, prepended to slice names.
+ roles contains TECH, PI, USER roles, and derive email aliases.
+ record contains {'message': [<subj>,<body>], 'args': {...}}
+ """
+ ticket_id = 0
+ args.update({'loginbase':loginbase})
+
+ if not config.mail and not config.debug and config.bcc:
+ roles = ADMIN
+ if config.mail and config.debug:
+ roles = ADMIN
+
+ # build targets
+ contacts = []
+ if ADMIN & roles:
+ contacts += [config.email]
+ if TECH & roles:
+ contacts += [TECHEMAIL % loginbase]
+ if PI & roles:
+ contacts += [PIEMAIL % loginbase]
+ if USER & roles:
+ 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
+
+ try:
+ subject = message[0] % args
+ body = message[1] % args
+ if ADMIN & roles:
+ # send only to admin
+ if 'ticket_id' in args:
+ subj = "Re: [PL #%s] %s" % (args['ticket_id'], subject)
+ else:
+ subj = "Re: [PL noticket] %s" % subject
+ mailer.email(subj, body, contacts)
+ ticket_id = args['ticket_id']
+ else:
+ ticket_id = mailer.emailViaRT(subject, body, contacts, args['ticket_id'])
+ except Exception, err:
+ print "exception on message:"
+ import traceback
+ print traceback.print_exc()
+ print message
+
+ return ticket_id
+
+
+ def _format_diaginfo(self, diag_node):
+ info = diag_node['info']
+ if diag_node['stage'] == 'monitor-end-record':
+ hlist = " %s went from '%s' to '%s'\n" % (info[0], info[1], info[2])
+ else:
+ hlist = " %s %s - %s\n" % (info[0], info[2], info[1]) #(node,ver,daysdn)
+ return hlist
+
+
+ def get_email_args(self, act_recordlist):
+
+ email_args = {}
+ email_args['hostname_list'] = ""
+
+ for act_record in act_recordlist:
+ email_args['hostname_list'] += act_record['msg_format']
+ email_args['hostname'] = act_record['nodename']
+ if 'plcnode' in act_record and \
+ 'pcu_ids' in act_record['plcnode'] and \
+ len(act_record['plcnode']['pcu_ids']) > 0:
+ print "setting 'pcu_id' for email_args %s"%email_args['hostname']
+ email_args['pcu_id'] = act_record['plcnode']['pcu_ids'][0]
+ else:
+ email_args['pcu_id'] = "-1"
+
+ if 'ticket_id' in act_record:
+ email_args['ticket_id'] = act_record['ticket_id']
+
+ return email_args
+
+ def get_unique_issues(self, act_recordlist):
+ # NOTE: only send one email per site, per problem...
+ unique_issues = {}
+ for act_record in act_recordlist:
+ act_key = act_record['action'][0]
+ if act_key not in unique_issues:
+ unique_issues[act_key] = []
+
+ unique_issues[act_key] += [act_record]
+
+ return unique_issues
+
+
+ def __actOnSite(self, loginbase, site_record):
+ i_nodes_actedon = 0
+ i_nodes_emailed = 0
+
+ act_recordlist = []
+
+ for nodename in site_record['nodes'].keys():
+ diag_record = site_record['nodes'][nodename]
+ act_record = self.__actOnNode(diag_record)
+ #print "nodename: %s %s" % (nodename, act_record)
+ if act_record is not None:
+ act_recordlist += [act_record]
+
+ unique_issues = self.get_unique_issues(act_recordlist)
+
+ for issue in unique_issues.keys():
+ print "\tworking on issue: %s" % issue
+ issue_record_list = unique_issues[issue]
+ email_args = self.get_email_args(issue_record_list)
+
+ # for each record.
+ for act_record in issue_record_list:
+ # if there's a pcu record and email config is set
+ if 'email_pcu' in act_record:
+ if act_record['email_pcu'] and \
+ site_record['config']['email']:
+
+ email_args['hostname'] = act_record['nodename']
+ ticket_id = self.__emailSite(loginbase,
+ act_record['email'],
+ emailTxt.mailtxt.pcudown[0],
+ email_args)
+ email_args['ticket_id'] = ticket_id
+
+
+ act_record = issue_record_list[0]
+ # send message before squeezing
+ print "\t\tconfig.email: %s and %s" % (act_record['message'] != None,
+ site_record['config']['email'])
+ if act_record['message'] != None and site_record['config']['email']:
+ ticket_id = self.__emailSite(loginbase, act_record['email'],
+ act_record['message'], email_args)
+
+ # Add ticket_id to ALL nodenames
+ for act_record in issue_record_list:
+ nodename = act_record['nodename']
+ # update node record with RT ticket_id
+ if nodename in self.act_all:
+ self.act_all[nodename][0]['ticket_id'] = "%s" % ticket_id
+ if config.mail: i_nodes_emailed += 1
+
+ print "\t\tconfig.squeeze: %s and %s" % (config.squeeze,
+ site_record['config']['squeeze'])
+ if config.squeeze and site_record['config']['squeeze']:
+ for act_key in act_record['action']:
+ self.actions[act_key](email_args)
+ i_nodes_actedon += 1
+
+ if config.policysavedb:
+ print "Saving Databases... act_all, diagnose_out"