- stats = self.diagnoseAll()
- except Exception, err:
- print "----------------"
- import traceback
- print traceback.print_exc()
- print err
- #if config.policysavedb:
- sys.exit(1)
-
- print_stats("sites_observed", stats)
- print_stats("sites_diagnosed", stats)
- print_stats("nodes_diagnosed", stats)
-
- if config.policysavedb:
- print "Saving Databases... diagnose_out"
- soltesz.dbDump("diagnose_out", self.diagnose_out)
-
- def accumSickSites(self):
- """
- Take all nodes, from l_diagnose, look them up in the diagnose_out database,
- and insert them into diagnose_in[] as:
-
- diagnose_in[loginbase] = [diag_node1, diag_node2, ...]
- """
- while 1:
- node_record = self.fromRT.get(block = True)
- if node_record == None:
- break;
-
- nodename = node_record['nodename']
- loginbase = self.plcdb_hn2lb[nodename]
-
- if loginbase not in self.diagnose_in:
- self.diagnose_in[loginbase] = {}
-
- self.diagnose_in[loginbase][nodename] = node_record
-
- return
-
- def diagnoseAll(self):
- i_sites_observed = 0
- i_sites_diagnosed = 0
- i_nodes_diagnosed = 0
- i_nodes_actedon = 0
- i_sites_emailed = 0
- l_allsites = []
-
- sorted_sites = self.diagnose_in.keys()
- sorted_sites.sort()
- self.diagnose_out= {}
- for loginbase in sorted_sites:
- l_allsites += [loginbase]
-
- d_diag_nodes = self.diagnose_in[loginbase]
- d_act_records = self.__diagnoseSite(loginbase, d_diag_nodes)
- # store records in diagnose_out, for saving later.
- self.diagnose_out.update(d_act_records)
-
- if len(d_act_records[loginbase]['nodes'].keys()) > 0:
- i_nodes_diagnosed += (len(d_act_records[loginbase]['nodes'].keys()))
- i_sites_diagnosed += 1
- i_sites_observed += 1
-
- return {'sites_observed': i_sites_observed,
- 'sites_diagnosed': i_sites_diagnosed,
- 'nodes_diagnosed': i_nodes_diagnosed,
- 'allsites':l_allsites}
-
- pass
-
- def __getDaysDown(self, diag_record, nodename):
- daysdown = -1
- if diag_record['comonstats']['sshstatus'] != "null":
- daysdown = int(diag_record['comonstats']['sshstatus']) // (60*60*24)
- elif diag_record['comonstats']['lastcotop'] != "null":
- daysdown = int(diag_record['comonstats']['lastcotop']) // (60*60*24)
- else:
- now = time.time()
- last_contact = diag_record['plcnode']['last_contact']
- if last_contact == None:
- # the node has never been up, so give it a break
- daysdown = -1
- else:
- diff = now - last_contact
- daysdown = diff // (60*60*24)
- return daysdown
-
- def __getStrDaysDown(self, diag_record, nodename):
- daysdown = self.__getDaysDown(diag_record, nodename)
- if daysdown > 0:
- return "(%d days down)"%daysdown
- else:
- return "Unknown number of days"
-
- def __getCDVersion(self, diag_record, nodename):
- cdversion = ""
- #print "Getting kernel for: %s" % diag_record['nodename']
- cdversion = diag_record['kernel']
- return cdversion
-
- def __diagnoseSite(self, loginbase, d_diag_nodes):
- """
- d_diag_nodes are diagnose_in entries.
- """
- d_diag_site = {loginbase : { 'config' :
- {'squeeze': False,
- 'email': False
- },
- 'nodes': {}
- }
- }
- sorted_nodes = d_diag_nodes.keys()
- sorted_nodes.sort()
- for nodename in sorted_nodes:
- node_record = d_diag_nodes[nodename]
- diag_record = self.__diagnoseNode(loginbase, node_record)
-
- if diag_record != None:
- d_diag_site[loginbase]['nodes'][nodename] = diag_record
-
- # NOTE: improvement means, we need to act/squeeze and email.
- #print "DIAG_RECORD", diag_record
- if 'monitor-end-record' in diag_record['stage'] or \
- 'nmreset' in diag_record['stage']:
- # print "resetting loginbase!"
- d_diag_site[loginbase]['config']['squeeze'] = True
- d_diag_site[loginbase]['config']['email'] = True
- #else:
- # print "NO IMPROVEMENT!!!!"
- else:
- pass # there is nothing to do for this node.
-
- # NOTE: these settings can be overridden by command line arguments,
- # or the state of a record, i.e. if already in RT's Support Queue.
- nodes_up = self.getUpAtSite(loginbase, d_diag_site)
- if nodes_up < MINUP:
- d_diag_site[loginbase]['config']['squeeze'] = True
-
- max_slices = self.getMaxSlices(loginbase)
- num_nodes = self.getNumNodes(loginbase)
- # NOTE: when max_slices == 0, this is either a new site (the old way)
- # or an old disabled site from previous monitor (before site['enabled'])
- if nodes_up < num_nodes and max_slices != 0:
- d_diag_site[loginbase]['config']['email'] = True
-
- if len(d_diag_site[loginbase]['nodes'].keys()) > 0:
- print "SITE: %20s : %d nodes up, at most" % (loginbase, nodes_up)
-
- return d_diag_site
-
- def diagRecordByCategory(self, node_record):
- nodename = node_record['nodename']
- category = node_record['category']
- state = node_record['state']
- loginbase = self.plcdb_hn2lb[nodename]
- diag_record = None
-
- if "ERROR" in category: # i.e. "DOWN"
- diag_record = {}
- diag_record.update(node_record)
- daysdown = self.__getDaysDown(diag_record, nodename)
- if daysdown < 7:
- format = "DIAG: %20s : %-40s Down only %s days NOTHING DONE"
- print format % (loginbase, nodename, daysdown)
- return None
-
- s_daysdown = self.__getStrDaysDown(diag_record, nodename)
- diag_record['message'] = emailTxt.mailtxt.newdown
- diag_record['args'] = {'nodename': nodename}
- diag_record['info'] = (nodename, s_daysdown, "")
-
- if 'reboot_node_failed' in node_record:
- # there was a previous attempt to use the PCU.
- if node_record['reboot_node_failed'] == False:
- # then the last attempt apparently, succeeded.
- # But, the category is still 'ERROR'. Therefore, the
- # PCU-to-Node mapping is broken.
- #print "Setting message for ERROR node to PCU2NodeMapping: %s" % nodename
- diag_record['message'] = emailTxt.mailtxt.pcutonodemapping
- diag_record['email_pcu'] = True
-
- if diag_record['ticket_id'] == "":
- diag_record['log'] = "DOWN: %20s : %-40s == %20s %s" % \
- (loginbase, nodename, diag_record['info'][1:], diag_record['found_rt_ticket'])
- else:
- diag_record['log'] = "DOWN: %20s : %-40s == %20s %s" % \
- (loginbase, nodename, diag_record['info'][1:], diag_record['ticket_id'])
-
- elif "OLDBOOTCD" in category:
- # V2 boot cds as determined by findbad
- s_daysdown = self.__getStrDaysDown(node_record, nodename)
- s_cdversion = self.__getCDVersion(node_record, nodename)
- diag_record = {}
- diag_record.update(node_record)
- #if "2.4" in diag_record['kernel'] or "v2" in diag_record['bootcd']:
- diag_record['message'] = emailTxt.mailtxt.newbootcd
- diag_record['args'] = {'nodename': nodename}
- diag_record['info'] = (nodename, s_daysdown, s_cdversion)
- if diag_record['ticket_id'] == "":
- diag_record['log'] = "BTCD: %20s : %-40s == %20s %20s %s" % \
- (loginbase, nodename, diag_record['kernel'],
- diag_record['bootcd'], diag_record['found_rt_ticket'])
- else:
- diag_record['log'] = "BTCD: %20s : %-40s == %20s %20s %s" % \
- (loginbase, nodename, diag_record['kernel'],
- diag_record['bootcd'], diag_record['ticket_id'])
-
- elif "PROD" in category:
- if "DEBUG" in state:
- # Not sure what to do with these yet. Probably need to
- # reboot, and email.
- print "DEBG: %20s : %-40s NOTHING DONE" % (loginbase, nodename)
- return None
- elif "BOOT" in state:
- # no action needed.
- # TODO: remove penalties, if any are applied.
- now = time.time()
- last_contact = node_record['plcnode']['last_contact']
- if last_contact == None:
- time_diff = 0
- else:
- time_diff = now - last_contact;
-
- if 'improvement' in node_record['stage']:
- # then we need to pass this on to 'action'
- diag_record = {}
- diag_record.update(node_record)
- diag_record['message'] = emailTxt.mailtxt.newthankyou
- diag_record['args'] = {'nodename': nodename}
- diag_record['info'] = (nodename, node_record['prev_category'],
- node_record['category'])
- if 'email_pcu' in diag_record:
- if diag_record['email_pcu']:
- # previously, the pcu failed to reboot, so send
- # email. Now, reset these values to try the reboot
- # again.
- diag_record['email_pcu'] = False
- del diag_record['reboot_node_failed']
-
- if diag_record['ticket_id'] == "":
- diag_record['log'] = "IMPR: %20s : %-40s == %20s %20s %s %s" % \
- (loginbase, nodename, diag_record['stage'],
- state, category, diag_record['found_rt_ticket'])
- else:
- diag_record['log'] = "IMPR: %20s : %-40s == %20s %20s %s %s" % \
- (loginbase, nodename, diag_record['stage'],
- state, category, diag_record['ticket_id'])
- return diag_record
- elif time_diff >= 6*SPERHOUR:
- # heartbeat is older than 30 min.
- # then reset NM.
- #print "Possible NM problem!! %s - %s = %s" % (now, last_contact, time_diff)
- diag_record = {}
- diag_record.update(node_record)
- diag_record['message'] = emailTxt.mailtxt.NMReset
- diag_record['args'] = {'nodename': nodename}
- diag_record['stage'] = "nmreset"
- diag_record['info'] = (nodename,
- node_record['prev_category'],
- node_record['category'])
- if diag_record['ticket_id'] == "":
- diag_record['log'] = "NM : %20s : %-40s == %20s %20s %s %s" % \
- (loginbase, nodename, diag_record['stage'],
- state, category, diag_record['found_rt_ticket'])
+ lb = plccache.plcdb_hn2lb[host]
+ except:
+ print "unknown host in plcdb_hn2lb %s" % host
+ email_exception(host)
+ continue
+
+ nodeblack = BlacklistRecord.get_by(hostname=host)
+
+ if nodeblack and not nodeblack.expired():
+ print "skipping %s due to blacklist. will expire %s" % (host, nodeblack.willExpire() )
+ continue
+
+ sitehist = SiteInterface.get_or_make(loginbase=lb)
+
+ recent_actions = sitehist.getRecentActions(hostname=host)
+
+ nodehist = HistoryNodeRecord.findby_or_create(hostname=host)
+
+ print "%s %s %s" % (i, nodehist.hostname, nodehist.status)
+ if nodehist.status == 'good' and \
+ changed_lessthan(nodehist.last_changed, 1.0) and \
+ found_within(recent_actions, 'down_notice', 7.0) and \
+ not found_within(recent_actions, 'online_notice', 0.5):
+ # NOTE: chronicly flapping nodes will not get 'online' notices
+ # since, they are never up long enough to be 'good'.
+ # NOTE: searching for down_notice proves that the node has
+ # gone through a 'down' state first, rather than just
+ # flapping through: good, offline, online, ...
+ #
+ # NOTE: there is a narrow window in which this command must be
+ # evaluated, otherwise the notice will not go out.
+ # this is not ideal.
+ sitehist.sendMessage('online_notice', hostname=host, viart=False, saveact=True)
+ print "send message for host %s online" % host
+
+
+ # if a node is offline and doesn't have a PCU, remind the user that they should have one.
+ #if not nodehist.haspcu and nodehist.status in ['offline', 'down'] and \
+ # changed_greaterthan(nodehist.last_changed,1.0) and \
+ # not found_within(recent_actions, 'pcumissing_notice', 7.0):
+ #
+ # sitehist.sendMessage('pcumissing_notice', hostname=host)
+ # print "send message for host %s pcumissing_notice" % host
+
+ # if it is offline and HAS a PCU, then try to use it.
+ if nodehist.haspcu and nodehist.status in ['offline', 'down'] and \
+ changed_greaterthan(nodehist.last_changed,1.0) and \
+ not nodehist.firewall and \
+ not found_between(recent_actions, 'try_reboot', 3.5, 1):
+
+ # TODO: there MUST be a better way to do this...
+ # get fb node record for pcuid
+ fbpcu = None
+ fbnode = FindbadNodeRecord.get_latest_by(hostname=host)
+ if fbnode:
+ fbpcu = FindbadPCURecord.get_latest_by(plc_pcuid=fbnode.plc_pcuid)
+
+ sitehist.attemptReboot(host)
+ print "send message for host %s try_reboot" % host
+ if not fbpcu.test_is_ok() and \
+ not found_within(recent_actions, 'pcuerror_notice', 3.0):
+
+ args = {}
+ if fbpcu:
+ args['pcu_name'] = fbpcu.pcu_name()
+ args['pcu_errors'] = fbpcu.pcu_errors()
+ args['plc_pcuid'] = fbpcu.plc_pcuid