add actions to gui
[monitor.git] / web / MonitorWeb / monitorweb / controllers.py
index 0cb05d4..617c46a 100644 (file)
@@ -132,11 +132,15 @@ def prep_node_for_display(node):
 
        if node.loginbase:
                node.site = HistorySiteRecord.by_loginbase(node.loginbase)
+               if node.site is None:
+                       # TODO: need a cleaner fix for this...
+                       node.site = HistorySiteRecord.by_loginbase("pl")
+                       
 
        node.history = HistoryNodeRecord.by_hostname(node.hostname)
 
-       if node.port_status:
-               node.ports = format_ports(node.port_status)
+       node.ports = format_ports(node.port_status)
+
        try:
                exists = node.plc_node_stats['last_contact']
        except:
@@ -168,13 +172,14 @@ class Root(controllers.RootController):
                import time
                fbquery = FindbadNodeRecord.get_all_latest()
                query = []
-               filtercount = {'DOWN' : 0, 'BOOT': 0, 'DEBUG' : 0, 'neverboot' : 0, 'pending' : 0, 'all' : 0}
+               filtercount = {'DOWN' : 0, 'BOOT': 0, 'DEBUG' : 0, 'neverboot' : 0, 'pending' : 0, 'all' : 0, None : 0}
                for node in fbquery:
                        # NOTE: reformat some fields.
                        prep_node_for_display(node)
 
                        # NOTE: count filters
                        if node.observed_status != 'DOWN':
+                               print node.hostname, node.observed_status
                                filtercount[node.observed_status] += 1
                        else:
                                if node.plc_node_stats and node.plc_node_stats['last_contact'] != None:
@@ -203,26 +208,32 @@ class Root(controllers.RootController):
        
        def nodeaction_handler(self, tg_exceptions=None):
                """Handle any kind of error."""
-               refurl = request.headers.get("Referer",link("pcu"))
-               print refurl
-
-               # TODO: do this more intelligently...
-               uri_fields = urllib.splitquery(refurl)
-               if uri_fields[1] is not None:
-                       val = query_to_dict(uri_fields[1])
-                       if 'pcuid' in val:
-                               pcuid = val['pcuid']
-                       elif 'hostname' in val:
-                               pcuid = FindbadNodeRecord.get_latest_by(hostname=val['hostname']).first().plc_pcuid
+
+               if 'pcuid' in request.params:
+                       pcuid = request.params['pcuid']
                else:
-                       pcuid=None
+                       refurl = request.headers.get("Referer",link("pcu"))
+                       print refurl
+
+                       # TODO: do this more intelligently...
+                       uri_fields = urllib.splitquery(refurl)
+                       if uri_fields[1] is not None:
+                               val = query_to_dict(uri_fields[1])
+                               if 'pcuid' in val:
+                                       pcuid = val['pcuid']
+                               elif 'hostname' in val:
+                                       pcuid = FindbadNodeRecord.get_latest_by(hostname=val['hostname']).first().plc_pcuid
+                               else:
+                                       pcuid=None
+                       else:
+                               pcuid=None
 
                cherry_trail = cherrypy._cputil.get_object_trail()
                for i in cherry_trail:
                        print "trail: ", i
 
                print pcuid
-               return self.pcuview(pcuid, **dict(exceptions=tg_exceptions))
+               return self.pcuview(None, pcuid, **dict(exceptions=tg_exceptions))
 
        def nodeaction(self, **data):
                for item in data.keys():
@@ -247,7 +258,7 @@ class Root(controllers.RootController):
                        print "REBOOT: %s" % hostname
                        ret = reboot.reboot_str(str(hostname))
                        print ret
-                       if ret: raise RuntimeError("Error using PCU: " + ret)
+                       if ret: raise RuntimeError("Error using PCU: " + str(ret))
                        flash("Reboot appeared to work.  All at most 5 minutes.  Run ExternalScan to check current status.")
 
                elif action == "ExternalScan":
@@ -268,6 +279,7 @@ class Root(controllers.RootController):
                sitequery=[]
                pcuquery=[]
                nodequery=[]
+               actions=[]
                exceptions = None
 
                for key in data:
@@ -280,6 +292,8 @@ class Root(controllers.RootController):
                        exceptions = data['exceptions']
 
                if loginbase:
+                       actions = ActionRecord.query.filter_by(loginbase=loginbase).order_by(ActionRecord.date_created.desc())
+                       actions = [ a for a in actions ]
                        sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
                        pcus = {}
                        for plcnode in site_lb2hn[loginbase]:
@@ -327,7 +341,7 @@ class Root(controllers.RootController):
                                        prep_pcu_for_display(pcu)
                                        pcuquery += [pcu]
                        
-               return dict(sitequery=sitequery, pcuquery=pcuquery, nodequery=nodequery, exceptions=exceptions)
+               return dict(sitequery=sitequery, pcuquery=pcuquery, nodequery=nodequery, actions=actions, exceptions=exceptions)
 
        @expose(template="monitorweb.templates.pculist")
        def pcu(self, filter='all'):
@@ -378,7 +392,7 @@ class Root(controllers.RootController):
 
        @expose(template="monitorweb.templates.sitelist")
        def site(self, filter='all'):
-               filtercount = {'good' : 0, 'down': 0, 'new' : 0, 'pending' : 0, 'all' : 0}
+               filtercount = {'good' : 0, 'down': 0, 'online':0, 'offline' : 0, 'new' : 0, 'pending' : 0, 'all' : 0}
                fbquery = HistorySiteRecord.query.all()
                query = []
                for site in fbquery:
@@ -408,7 +422,7 @@ class Root(controllers.RootController):
                session.bind = metadata.bind
                filtercount = {'active' : 0, 'acknowledged': 0, 'all' : 0}
                # With Acknowledgement
-               sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message '+ \
+               sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message,e.eventid '+ \
               ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
               ' WHERE f.itemid=i.itemid ' + \
                   ' AND h.hostid=i.hostid ' + \