+ query.append(agg)
+ elif filter == agg.history.status:
+ query.append(agg)
+ elif filter == 'boot':
+ query.append(agg)
+
+
+ widget = NodeWidget(template='monitorweb.templates.node_template')
+ return dict(now=time.ctime(), query=query, fc=filtercount, nodewidget=widget)
+
+ def nodeaction_handler(self, tg_exceptions=None):
+ """Handle any kind of error."""
+ print "NODEACTION_HANDLER------------------"
+
+ if 'pcuid' in request.params:
+ pcuid = request.params['pcuid']
+ else:
+ 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']).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(None, pcuid, **dict(exceptions=tg_exceptions))
+
+ def nodeaction(self, **data):
+ print "NODEACTION------------------"
+ for item in data.keys():
+ print "%s %s" % ( item, data[item] )
+
+ if 'hostname' in data:
+ hostname = data['hostname']
+ else:
+ flash("No hostname given in submitted data")
+ return
+
+ if 'submit' in data or 'type' in data:
+ try:
+ action = data['submit']
+ except:
+ action = data['type']
+ else:
+ flash("No submit action given in submitted data")
+ return
+
+ if action == "Reboot":
+ print "REBOOT: %s" % hostname
+ ret = reboot.reboot_str(str(hostname))
+ print ret
+ if ret: raise RuntimeError("Error using PCU: " + str(ret))
+ flash("Reboot appeared to work. Allow at most 5 minutes. Then run ExternalScan to check current status.")
+
+ elif action == "ExternalScan":
+ scanapi.externalprobe(str(hostname))
+ flash("External Scan Successful!")
+ elif action == "InternalScan":
+ scanapi.internalprobe(str(hostname))
+ flash("Internal Scan Successful!")
+ else:
+ # unknown action
+ raise RuntimeError("Unknown action given")
+ return
+
+ # TODO: add form validation
+ @expose(template="monitorweb.templates.pcuview")
+ @exception_handler(nodeaction_handler,"isinstance(tg_exceptions,RuntimeError)")
+ def pcuview(self, loginbase=None, pcuid=None, hostname=None, **data):
+ print "PCUVIEW------------------"
+ print "befor-len: ", len( [ i for i in session] )
+ session.flush(); session.clear()
+ print "after-len: ", len( [ i for i in session] )
+ sitequery=[]
+ pcuquery=[]
+ nodequery=[]
+ actions=[]
+ exceptions = None
+
+ for key in data:
+ print key, data[key]
+
+ if 'submit' in data.keys() or 'type' in data.keys():
+ if hostname: data['hostname'] = hostname
+ self.nodeaction(**data)
+ if 'exceptions' in data:
+ exceptions = data['exceptions']
+
+ if loginbase:
+ actions = ActionRecord.query.filter_by(loginbase=loginbase
+ ).filter(ActionRecord.date_created >= datetime.now() - timedelta(14)
+ ).order_by(ActionRecord.date_created.desc())
+ actions = [ a for a in actions ]
+ sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
+ pcus = {}
+ for node in FindbadNodeRecord.query.filter_by(loginbase=loginbase):
+ # NOTE: reformat some fields.
+ agg = prep_node_for_display(node)
+ nodequery += [agg]
+ if agg.pcu: #.pcu.plc_pcuid: # not None
+ #pcu = FindbadPCURecord.get_latest_by(plc_pcuid=agg.plc_pcuid)
+ #prep_pcu_for_display(pcu)
+ pcus[agg.pcu.pcu.plc_pcuid] = agg.pcu
+
+ for pcuid_key in pcus:
+ pcuquery += [pcus[pcuid_key]]
+
+ if pcuid and hostname is None:
+ print "pcuid: %s" % pcuid
+ pcu = FindbadPCURecord.get_latest_by(plc_pcuid=pcuid)
+ # NOTE: count filter
+ aggpcu = prep_pcu_for_display(pcu)
+ pcuquery += [aggpcu]
+ if 'site_id' in pcu.plc_pcu_stats:
+ sitequery = [HistorySiteRecord.by_loginbase(aggpcu.loginbase)]