changes for 3.0
[monitor.git] / grouprins.py
index 1896f41..b85bbad 100755 (executable)
@@ -64,17 +64,61 @@ class Reboot(object):
                        self.action = "reboot.reboot('%s')" % host
 
                        pflags = PersistFlags(host, 2*60*60*24, db='pcu_persistflags')
-                       pflags.resetRecentFlag('pcutried')
+                       #pflags.resetRecentFlag('pcutried')
                        if not pflags.getRecentFlag('pcutried'):
-                               pflags.setRecentFlag('pcutried')
                                try:
-                                       print "CALLING REBOOT!!!"
-                                       ret = reboot.reboot(host)
+                                       node_pf = PersistFlags(host, 1, db='node_persistflags')
+                                       if  node_pf.checkattr('last_change') and \
+                                               node_pf.last_change < time.time() - 60*60*24 and \
+                                               node_pf.checkattr('status') and \
+                                               node_pf.status != "good":
 
+                                               print "CALLING REBOOT!!!"
+                                               ret = reboot.reboot(host)
+
+                                               pflags.setRecentFlag('pcutried')
+                                               pflags.save()
+                                               return ret
+                                       else:
+                                               return True
+
+                               except Exception,e:
+                                       email_exception()
+                                       print traceback.print_exc(); print e
+
+                                       # NOTE: this failure could be an implementation issue on
+                                       #               our end.  So, extra notices are confusing...
+                                       # self._send_pcunotice(host) 
+
+                                       pflags.setRecentFlag('pcufailed')
                                        pflags.save()
-                                       return ret
+                                       return False
+
+                       elif not pflags.getRecentFlag('pcu_rins_tried'):
+                               try:
+                                       # NOTE: check that the node has been down for at least a
+                                       # day before rebooting it.  this avoids false-reboots/rins
+                                       # from failed node detections. circa 03-12-09
+                                       node_pf = PersistFlags(host, 1, db='node_persistflags')
+                                       if  node_pf.checkattr('last_change') and \
+                                               node_pf.last_change < time.time() - 60*60*24 and \
+                                               node_pf.checkattr('status') and \
+                                               node_pf.status != "good":
+
+                                               # set node to 'rins' boot state.
+                                               print "CALLING REBOOT +++ RINS"
+                                               plc.nodeBootState(host, 'reinstall')
+                                               ret = reboot.reboot(host)
+
+                                               pflags.setRecentFlag('pcu_rins_tried')
+                                               pflags.save()
+                                               return ret
+
+                                       else:
+                                               return True
 
                                except Exception,e:
+                                       email_exception()
                                        print traceback.print_exc(); print e
 
                                        # NOTE: this failure could be an implementation issue on
@@ -93,12 +137,12 @@ class Reboot(object):
 
                                        pflags.setRecentFlag('pcumessagesent')
                                        pflags.save()
-                                       # NOTE: this will result in just one message sent at a time.
-                                       return True
 
-                               else:
-                                       print "GetRecentFlag()"
-                                       return False
+                               # This will result in mail() being called next, to try to
+                               # engage the technical contact to take care of it also.
+                               print "RETURNING FALSE"
+                               return False
+
                else:
                        print "NO PCUOK"
                        self.action = "None"
@@ -118,6 +162,7 @@ class Reboot(object):
                try:
                        return mailmonitor.reboot(host)
                except Exception, e:
+                       email_exception(host)
                        print traceback.print_exc(); print e
                        return False
 
@@ -144,11 +189,11 @@ def set_node_to_rins(host, fb):
        node = api.GetNodes(host, ['boot_state', 'last_contact', 'last_updated', 'date_created'])
        record = {'observation' : node[0], 
                          'model' : 'USER_REQUEST', 
-                         'action' : 'api.UpdateNode(%s, {"boot_state" : "rins"})' % host, 
+                         'action' : 'api.UpdateNode(%s, {"boot_state" : "reinstall"})' % host, 
                          'time' : time.time()}
        l = Log(host, record)
 
-       ret = api.UpdateNode(host, {'boot_state' : 'rins'})
+       ret = api.UpdateNode(host, {'boot_state' : 'reinstall'})
        if ret:
                # it's nice to see the current status rather than the previous status on the console
                node = api.GetNodes(host)[0]
@@ -174,8 +219,6 @@ parser.set_defaults( timewait=0,
                                        force=False, 
                                        nosetup=False, 
                                        verbose=False, 
-                                       stopkey=None,
-                                       stopvalue=None,
                                        quiet=False,
                                        )
 
@@ -204,13 +247,18 @@ config = parsermodule.parse_args(parser)
 
 # COLLECT nodegroups, nodes and node lists
 if config.nodegroup:
-       ng = api.GetNodeGroups({'name' : config.nodegroup})
+       ng = api.GetNodeGroups({'groupname' : config.nodegroup})
        nodelist = api.GetNodes(ng[0]['node_ids'])
        hostnames = [ n['hostname'] for n in nodelist ]
 
+if config.site:
+       site = api.GetSites(config.site)
+       l_nodes = api.GetNodes(site[0]['node_ids'], ['hostname'])
+       hostnames = [ n['hostname'] for n in l_nodes ]
+
 if config.node or config.nodelist:
        if config.node: hostnames = [ config.node ] 
-       else: hostnames = config.getListFromFile(config.nodelist)
+       else: hostnames = util.file.getListFromFile(config.nodelist)
 
 fb = database.dbLoad("findbad")
 
@@ -221,18 +269,23 @@ if config.findbad:
        # rerun findbad with the nodes in the given nodes.
        file = "findbad.txt"
        util.file.setFileFromList(file, hostnames)
-       os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
+       os.system("./findbad.py --cachenodes --increment --nodelist %s" % file)
+       # TODO: shouldn't we reload the node list now?
 
+l_blacklist = database.if_cached_else(1, "l_blacklist", lambda : [])
 # commands:
 i = 1
 count = 1
+#print "hosts: %s" % hostnames
 for host in hostnames:
 
        #if 'echo' in host or 'hptest-1' in host: continue
+
        try:
                try:
                        node = api.GetNodes(host)[0]
                except:
+                       email_exception()
                        print traceback.print_exc(); 
                        print "FAILED GETNODES for host: %s" % host
                        continue
@@ -240,6 +293,9 @@ for host in hostnames:
                print "%-2d" % i, nodegroup_display(node, fb)
                i += 1
                if i-1 <= int(config.skip): continue
+               if host in l_blacklist:
+                       print "%s is blacklisted.  Skipping." % host
+                       continue
 
                if config.stopselect:
                        dict_query = query_to_dict(config.stopselect)
@@ -249,20 +305,18 @@ for host in hostnames:
                        if verify(dict_query, fbnode) and observed_state != "dbg ":
                                # evaluates to true, therefore skip.
                                print "%s evaluates true for %s ; skipping..." % ( config.stopselect, host )
-                               continue
-
-               if config.stopkey and config.stopvalue:
-                       fbnode = fb['nodes'][host]['values']
-                       observed_state = get_current_state(fbnode)
+                               try:
+                                       # todo: clean up act_all record here.
+                                       # todo: send thank you, etc.
+                                       mailmonitor.reboot(host)
+                               except Exception, e:
+                                       email_exception()
+                                       print traceback.print_exc(); print e
 
-                       if config.stopkey in fbnode:
-                               if config.stopvalue in fbnode[config.stopkey] and observed_state != "dbg ":
-                                       print "%s has stopvalue; skipping..." % host
-                                       continue
-                       else:
-                               print "stopkey %s not in fbnode record for %s; skipping..." % (config.stopkey, host)
-                               print fbnode
                                continue
+                       #else:
+                               #print "%s failed to match %s: -%s-" % ( host, dict_query, observed_state )
+                               #sys.exit(1)
 
                if not config.force and rebootlog.find(host, {'action' : ".*reboot"}, 60*60*2):
                        print "recently rebooted %s.  skipping... " % host
@@ -315,10 +369,10 @@ for host in hostnames:
                                print "ALL METHODS OF RESTARTING %s FAILED" % host
                                args = {}
                                args['hostname'] = host
-                               m = PersistMessage(host, "ALL METHODS FAILED for %(hostname)s" % args,
-                                                                                        "CANNOT CONTACT", False, db='suspect_persistmessages')
-                               m.reset()
-                               m.send(['monitor-list@lists.planet-lab.org'])
+                               #m = PersistMessage(host, "ALL METHODS FAILED for %(hostname)s" % args,
+                               #                                                        "CANNOT CONTACT", False, db='suspect_persistmessages')
+                               #m.reset()
+                               #m.send(['monitor-list@lists.planet-lab.org'])
 
                        l = Log(host, record)
                        print l
@@ -327,6 +381,7 @@ for host in hostnames:
                print "Killed by interrupt"
                sys.exit(0)
        except:
+               email_exception()
                print traceback.print_exc();
                print "Continuing..."