1 import turbogears as tg
2 from turbogears import controllers, expose, flash, exception_handler
3 from turbogears import widgets
4 from cherrypy import request, response
6 # from monitorweb import model
8 # log = logging.getLogger("monitorweb.controllers")
10 from monitor.database.info.model import *
11 #from monitor.database.zabbixapi.model import *
12 #from monitor.database.dborm import zab_session as session
13 #from monitor.database.dborm import zab_metadata as metadata
14 from monitor_xmlrpc import MonitorXmlrpcServer
16 from monitor import reboot
17 from monitor import scanapi
19 from monitor.wrapper.plccache import plcdb_id2lb as site_id2lb
20 from monitor.wrapper.plccache import plcdb_hn2lb as site_hn2lb
21 from monitor.wrapper.plccache import plcdb_lb2hn as site_lb2hn
23 from monitorweb.templates.links import *
27 def query_to_dict(query):
28 """ take a url query string and chop it up """
30 query_fields = query.split('&')
31 for f in query_fields:
32 (k,v) = urllib.splitvalue(f)
37 def format_ports(data, pcumodel=None):
42 supported_ports=reboot.model_to_object(pcumodel).supported_ports
44 # ports of a production node
45 supported_ports=[22,80,806]
47 if data and len(data.keys()) > 0 :
48 for port in supported_ports:
50 state = data[str(port)]
54 if state == "filtered":
57 retval.append( (port, state) )
60 retval = [( "Closed/Filtered", "state" )]
62 if filtered_length == len(supported_ports):
63 retval = [( "All Filtered", "state" )]
67 def format_pcu_shortstatus(pcu):
70 if pcu.reboot_trial_status == str(0):
72 elif pcu.reboot_trial_status == "NetDown" or pcu.reboot_trial_status == "Not_Run":
73 status = pcu.reboot_trial_status
79 def prep_pcu_for_display(pcu):
82 pcu.loginbase = site_id2lb[pcu.plc_pcu_stats['site_id']]
84 pcu.loginbase = "unknown"
86 pcu.ports = format_ports(pcu.port_status, pcu.plc_pcu_stats['model'])
87 pcu.status = format_pcu_shortstatus(pcu)
89 #print pcu.entry_complete
90 pcu.entry_complete_str = pcu.entry_complete
91 #pcu.entry_complete_str += "".join([ f[0] for f in pcu.entry_complete.split() ])
92 if pcu.dns_status == "NOHOSTNAME":
93 pcu.dns_short_status = 'NoHost'
94 elif pcu.dns_status == "DNS-OK":
95 pcu.dns_short_status = 'Ok'
96 elif pcu.dns_status == "DNS-NOENTRY":
97 pcu.dns_short_status = 'NoEntry'
98 elif pcu.dns_status == "NO-DNS-OR-IP":
99 pcu.dns_short_status = 'NoHostOrIP'
100 elif pcu.dns_status == "DNS-MISMATCH":
101 pcu.dns_short_status = 'Mismatch'
103 class NodeWidget(widgets.Widget):
106 def prep_node_for_display(node):
108 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid)
110 node.pcu_status = pcu.reboot_trial_status
111 node.pcu_short_status = format_pcu_shortstatus(pcu)
113 prep_pcu_for_display(node.pcu)
115 node.pcu_short_status = "none"
116 node.pcu_status = "nodata"
120 node.pcu_status = "nopcu"
121 node.pcu_short_status = "none"
125 if node.kernel_version:
126 node.kernel = node.kernel_version.split()[2]
131 node.loginbase = site_id2lb[node.plc_node_stats['site_id']]
133 node.loginbase = "unknown"
136 node.site = HistorySiteRecord.by_loginbase(node.loginbase)
137 if node.site is None:
138 # TODO: need a cleaner fix for this...
139 node.site = HistorySiteRecord.by_loginbase("pl")
141 node.site = HistorySiteRecord.by_loginbase("ple")
144 node.history = HistoryNodeRecord.by_hostname(node.hostname)
146 node.ports = format_ports(node.port_status)
149 exists = node.plc_node_stats['last_contact']
151 node.plc_node_stats = {'last_contact' : None}
155 class Root(controllers.RootController, MonitorXmlrpcServer):
156 @expose(template="monitorweb.templates.welcome")
159 # log.debug("Happy TurboGears Controller Responding For Duty")
160 flash("Your application is now running")
161 return dict(now=time.ctime())
163 @expose(template="monitorweb.templates.pcuview")
164 def nodeview(self, hostname=None):
167 node = FindbadNodeRecord.get_latest_by(hostname=hostname)
168 # NOTE: reformat some fields.
169 prep_node_for_display(node)
172 return self.pcuview(None, None, hostname) # dict(nodequery=nodequery)
174 @expose(template="monitorweb.templates.nodelist")
175 def node(self, filter='boot'):
177 fbquery = FindbadNodeRecord.get_all_latest()
179 filtercount = {'down' : 0, 'boot': 0, 'debug' : 0, 'diagnose' : 0, 'disabled': 0,
180 'neverboot' : 0, 'pending' : 0, 'all' : 0, None : 0}
182 # NOTE: reformat some fields.
183 prep_node_for_display(node)
188 if node.history.status in ['down', 'offline']:
189 if node.plc_node_stats and node.plc_node_stats['last_contact'] != None:
190 filtercount['down'] += 1
192 filtercount['neverboot'] += 1
193 elif node.history.status in ['good', 'online']:
194 filtercount['boot'] += 1
195 elif node.history.status in ['debug', 'monitordebug']:
196 filtercount['debug'] += 1
198 # TODO: need a better fix. filtercount
199 # doesn't maps to GetBootStates() on
200 # 4.3 so this one fails quite often.
201 if filtercount.has_key(node.history.status):
202 filtercount[node.history.status] += 1
204 ## NOTE: count filters
205 #if node.observed_status != 'DOWN':
206 # print node.hostname, node.observed_status
207 # if node.observed_status == 'DEBUG':
208 # if node.plc_node_stats['boot_state'] in ['debug', 'diagnose', 'disabled']:
209 # filtercount[node.plc_node_stats['boot_state']] += 1
211 # filtercount['debug'] += 1
214 # filtercount[node.observed_status] += 1
216 # if node.plc_node_stats and node.plc_node_stats['last_contact'] != None:
217 # filtercount[node.observed_status] += 1
219 # filtercount['neverboot'] += 1
222 if filter == "neverboot":
223 if not node.plc_node_stats or node.plc_node_stats['last_contact'] == None:
225 elif filter == "all":
227 elif filter == node.history.status:
229 elif filter == 'boot':
232 #if filter == node.observed_status:
233 # if filter == "DOWN":
234 # if node.plc_node_stats['last_contact'] != None:
238 #elif filter == "neverboot":
239 # if not node.plc_node_stats or node.plc_node_stats['last_contact'] == None:
241 #elif filter == "pending":
242 # # TODO: look in message logs...
244 #elif filter == node.plc_node_stats['boot_state']:
246 #elif filter == "all":
249 widget = NodeWidget(template='monitorweb.templates.node_template')
250 return dict(now=time.ctime(), query=query, fc=filtercount, nodewidget=widget)
252 def nodeaction_handler(self, tg_exceptions=None):
253 """Handle any kind of error."""
254 print "NODEACTION_HANDLER------------------"
256 if 'pcuid' in request.params:
257 pcuid = request.params['pcuid']
259 refurl = request.headers.get("Referer",link("pcu"))
262 # TODO: do this more intelligently...
263 uri_fields = urllib.splitquery(refurl)
264 if uri_fields[1] is not None:
265 val = query_to_dict(uri_fields[1])
268 elif 'hostname' in val:
269 pcuid = FindbadNodeRecord.get_latest_by(hostname=val['hostname']).plc_pcuid
275 cherry_trail = cherrypy._cputil.get_object_trail()
276 for i in cherry_trail:
280 return self.pcuview(None, pcuid, **dict(exceptions=tg_exceptions))
282 def nodeaction(self, **data):
283 print "NODEACTION------------------"
284 for item in data.keys():
285 print "%s %s" % ( item, data[item] )
287 if 'hostname' in data:
288 hostname = data['hostname']
290 flash("No hostname given in submitted data")
293 if 'submit' in data or 'type' in data:
295 action = data['submit']
297 action = data['type']
299 flash("No submit action given in submitted data")
302 if action == "Reboot":
303 print "REBOOT: %s" % hostname
304 ret = reboot.reboot_str(str(hostname))
306 if ret: raise RuntimeError("Error using PCU: " + str(ret))
307 flash("Reboot appeared to work. Allow at most 5 minutes. Then run ExternalScan to check current status.")
309 elif action == "ExternalScan":
310 scanapi.externalprobe(str(hostname))
311 flash("External Scan Successful!")
312 elif action == "InternalScan":
313 scanapi.internalprobe(str(hostname))
314 flash("Internal Scan Successful!")
317 raise RuntimeError("Unknown action given")
320 # TODO: add form validation
321 @expose(template="monitorweb.templates.pcuview")
322 @exception_handler(nodeaction_handler,"isinstance(tg_exceptions,RuntimeError)")
323 def pcuview(self, loginbase=None, pcuid=None, hostname=None, **data):
324 print "PCUVIEW------------------"
325 print "befor-len: ", len( [ i for i in session] )
326 session.flush(); session.clear()
327 print "after-len: ", len( [ i for i in session] )
337 if 'submit' in data.keys() or 'type' in data.keys():
338 if hostname: data['hostname'] = hostname
339 self.nodeaction(**data)
340 if 'exceptions' in data:
341 exceptions = data['exceptions']
344 actions = ActionRecord.query.filter_by(loginbase=loginbase
345 ).filter(ActionRecord.date_created >= datetime.now() - timedelta(14)
346 ).order_by(ActionRecord.date_created.desc())
347 actions = [ a for a in actions ]
348 sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
350 for plcnode in site_lb2hn[loginbase]:
351 node = FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname'])
352 # NOTE: reformat some fields.
353 prep_node_for_display(node)
355 if node.plc_pcuid: # not None
356 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid)
357 prep_pcu_for_display(pcu)
358 pcus[node.plc_pcuid] = pcu
360 for pcuid_key in pcus:
361 pcuquery += [pcus[pcuid_key]]
363 if pcuid and hostname is None:
364 print "pcuid: %s" % pcuid
365 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=pcuid)
367 prep_pcu_for_display(pcu)
369 if 'site_id' in pcu.plc_pcu_stats:
370 sitequery = [HistorySiteRecord.by_loginbase(pcu.loginbase)]
372 if 'nodenames' in pcu.plc_pcu_stats:
373 for nodename in pcu.plc_pcu_stats['nodenames']:
374 print "query for %s" % nodename
375 node = FindbadNodeRecord.get_latest_by(hostname=nodename)
376 print "%s" % node.port_status
377 print "%s" % node.to_dict()
379 prep_node_for_display(node)
382 if hostname and pcuid is None:
383 node = FindbadNodeRecord.get_latest_by(hostname=hostname)
384 # NOTE: reformat some fields.
385 prep_node_for_display(node)
386 sitequery = [node.site]
388 if node.plc_pcuid: # not None
389 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid)
390 prep_pcu_for_display(pcu)
393 return dict(sitequery=sitequery, pcuquery=pcuquery, nodequery=nodequery, actions=actions, exceptions=exceptions)
395 @expose(template="monitorweb.templates.nodehistory")
396 def nodehistory(self, hostname=None):
399 #fbnode = FindbadNodeRecord.get_by(hostname=hostname)
400 ## TODO: add links for earlier history if desired.
401 #l = fbnode.versions[-100:]
404 # prep_node_for_display(node)
407 fbnode = HistoryNodeRecord.get_by(hostname=hostname)
408 l = fbnode.versions[-100:]
411 #prep_node_for_display(node)
414 return dict(query=query, hostname=hostname)
416 @expose(template="monitorweb.templates.sitehistory")
417 def sitehistory(self, loginbase=None):
420 fbsite = HistorySiteRecord.get_by(loginbase=loginbase)
421 # TODO: add links for earlier history if desired.
422 l = fbsite.versions[-100:]
426 return dict(query=query, loginbase=loginbase)
429 @expose(template="monitorweb.templates.pculist")
430 def pcu(self, filter='all'):
432 fbquery = FindbadPCURecord.get_all_latest()
434 filtercount = {'ok' : 0, 'NetDown': 0, 'Not_Run' : 0, 'pending' : 0, 'all' : 0}
438 if node.reboot_trial_status == str(0):
439 filtercount['ok'] += 1
440 elif node.reboot_trial_status == 'NetDown' or node.reboot_trial_status == 'Not_Run':
441 filtercount[node.reboot_trial_status] += 1
443 filtercount['pending'] += 1
445 prep_pcu_for_display(node)
450 elif filter == "ok" and node.reboot_trial_status == str(0):
452 elif filter == node.reboot_trial_status:
454 elif filter == "pending":
455 # TODO: look in message logs...
456 if node.reboot_trial_status != str(0) and \
457 node.reboot_trial_status != 'NetDown' and \
458 node.reboot_trial_status != 'Not_Run':
462 return dict(query=query, fc=filtercount)
464 @expose(template="monitorweb.templates.siteview")
465 def siteview(self, loginbase='pl'):
467 sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
469 for plcnode in site_lb2hn[loginbase]:
470 for node in FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname']):
471 # NOTE: reformat some fields.
472 prep_node_for_display(node)
474 return dict(sitequery=sitequery, nodequery=nodequery, fc={})
476 @expose(template="monitorweb.templates.sitelist")
477 def site(self, filter='all'):
478 filtercount = {'good' : 0, 'down': 0, 'online':0, 'offline' : 0, 'new' : 0, 'pending' : 0, 'all' : 0}
479 fbquery = HistorySiteRecord.query.all()
483 filtercount['all'] += 1
484 if site.new and site.slices_used == 0 and not site.enabled:
485 filtercount['new'] += 1
486 elif not site.enabled:
487 filtercount['pending'] += 1
488 elif site.status in ['good', 'online']:
489 filtercount['good'] += 1
490 elif site.status in ['down', 'offline']:
491 filtercount['down'] += 1
496 elif filter == 'new' and site.new and site.slices_used == 0 and not site.enabled:
498 elif filter == "pending" and not site.enabled:
500 elif filter == 'good' and site.status in ['good', 'online']:
502 elif filter == 'down' and site.status in ['down', 'offline']:
505 return dict(query=query, fc=filtercount)
507 @expose(template="monitorweb.templates.actionlist")
508 def action(self, filter='all'):
509 session.bind = metadata.bind
510 filtercount = {'active' : 0, 'acknowledged': 0, 'all' : 0}
511 # With Acknowledgement
512 sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message,e.eventid '+ \
513 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
514 ' WHERE f.itemid=i.itemid ' + \
515 ' AND h.hostid=i.hostid ' + \
516 ' AND hg.hostid=h.hostid ' + \
517 ' AND t.triggerid=f.triggerid ' + \
518 ' AND t.triggerid=e.triggerid ' + \
519 ' AND a.eventid=e.eventid ' + \
520 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
521 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
522 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
523 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
524 ' ORDER BY t.lastchange DESC';
526 # WithOUT Acknowledgement
527 sql_noack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,e.eventid ' + \
528 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
529 ' WHERE f.itemid=i.itemid ' + \
530 ' AND h.hostid=i.hostid ' + \
531 ' AND hg.hostid=h.hostid ' + \
532 ' AND t.triggerid=f.triggerid ' + \
533 ' AND t.triggerid=e.triggerid ' + \
534 ' AND e.eventid not in (select eventid from acknowledges) ' + \
535 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
536 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
537 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
538 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
539 ' ORDER BY t.lastchange DESC';
540 # for i in session.execute(sql): print i
543 replace = re.compile(' {.*}')
544 for sql,ack in [(sql_ack,True), (sql_noack,False)]:
545 result = session.execute(sql)
548 newrow = [ site_hn2lb[row[0].lower()] ] + [ r for r in row ]
550 print site_hn2lb.keys()
551 newrow = [ "unknown" ] + [ r for r in row ]
553 newrow[2] = replace.sub("", newrow[2]) # strip {.*} expressions
556 filtercount['all'] += 1
557 if not ack: # for unacknowledged
558 filtercount['active'] += 1
559 if filter == 'active':
562 filtercount['acknowledged'] += 1
563 if filter == 'acknowledged':
566 if filter != "acknowledged" and filter != "active":
569 return dict(query=query, fc=filtercount)