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
20 from monitor.wrapper.plccache import plcdb_id2lb as site_id2lb
21 from monitor.wrapper.plccache import plcdb_hn2lb as site_hn2lb
22 from monitor.wrapper.plccache import plcdb_lb2hn as site_lb2hn
24 from monitorweb.templates.links import *
28 def query_to_dict(query):
29 """ take a url query string and chop it up """
31 query_fields = query.split('&')
32 for f in query_fields:
33 (k,v) = urllib.splitvalue(f)
38 def format_ports(data, pcumodel=None):
43 supported_ports=reboot.model_to_object(pcumodel).supported_ports
45 # ports of a production node
46 supported_ports=[22,80,806]
48 if data and len(data.keys()) > 0 :
49 for port in supported_ports:
51 state = data[str(port)]
55 if state == "filtered":
58 retval.append( (port, state) )
61 retval = [( "Closed/Filtered", "state" )]
63 if filtered_length == len(supported_ports):
64 retval = [( "All Filtered", "state" )]
68 def format_pcu_shortstatus(pcu):
71 if pcu.reboot_trial_status == str(0):
73 elif pcu.reboot_trial_status == "NetDown" or pcu.reboot_trial_status == "Not_Run":
74 status = pcu.reboot_trial_status
80 def prep_pcu_for_display(pcu):
83 pcu.loginbase = site_id2lb[pcu.plc_pcu_stats['site_id']]
85 pcu.loginbase = "unknown"
87 pcu.ports = format_ports(pcu.port_status, pcu.plc_pcu_stats['model'])
88 pcu.status = format_pcu_shortstatus(pcu)
90 #print pcu.entry_complete
91 pcu.entry_complete_str = pcu.entry_complete
92 #pcu.entry_complete_str += "".join([ f[0] for f in pcu.entry_complete.split() ])
93 if pcu.dns_status == "NOHOSTNAME":
94 pcu.dns_short_status = 'NoHost'
95 elif pcu.dns_status == "DNS-OK":
96 pcu.dns_short_status = 'Ok'
97 elif pcu.dns_status == "DNS-NOENTRY":
98 pcu.dns_short_status = 'NoEntry'
99 elif pcu.dns_status == "NO-DNS-OR-IP":
100 pcu.dns_short_status = 'NoHostOrIP'
101 elif pcu.dns_status == "DNS-MISMATCH":
102 pcu.dns_short_status = 'Mismatch'
104 class NodeWidget(widgets.Widget):
107 def prep_node_for_display(node):
109 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid)
111 node.pcu_status = pcu.reboot_trial_status
112 node.pcu_short_status = format_pcu_shortstatus(pcu)
114 prep_pcu_for_display(node.pcu)
116 node.pcu_short_status = "none"
117 node.pcu_status = "nodata"
121 node.pcu_status = "nopcu"
122 node.pcu_short_status = "none"
126 if node.kernel_version:
127 node.kernel = node.kernel_version.split()[2]
132 node.loginbase = site_id2lb[node.plc_node_stats['site_id']]
134 node.loginbase = "unknown"
137 node.site = HistorySiteRecord.by_loginbase(node.loginbase)
138 if node.site is None:
139 # TODO: need a cleaner fix for this...
140 node.site = HistorySiteRecord.by_loginbase("pl")
142 node.site = HistorySiteRecord.by_loginbase("ple")
145 node.history = HistoryNodeRecord.by_hostname(node.hostname)
147 node.ports = format_ports(node.port_status)
150 exists = node.plc_node_stats['last_contact']
152 node.plc_node_stats = {'last_contact' : None}
156 class Root(controllers.RootController, MonitorXmlrpcServer):
157 @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'):
176 print "NODE------------------"
177 print "befor-len: ", len( [ i for i in session] )
178 session.flush(); session.clear()
179 print "after-len: ", len( [ i for i in session] )
180 fbquery = FindbadNodeRecord.get_all_latest()
182 filtercount = {'down' : 0, 'boot': 0, 'debug' : 0, 'diagnose' : 0, 'disabled': 0,
183 'neverboot' : 0, 'pending' : 0, 'all' : 0, None : 0}
185 # NOTE: reformat some fields.
186 prep_node_for_display(node)
191 if node.history.status in ['down', 'offline']:
192 if node.plc_node_stats and node.plc_node_stats['last_contact'] != None:
193 filtercount['down'] += 1
195 filtercount['neverboot'] += 1
196 elif node.history.status in ['good', 'online']:
197 filtercount['boot'] += 1
198 elif node.history.status in ['debug', 'monitordebug']:
199 filtercount['debug'] += 1
201 # TODO: need a better fix. filtercount
202 # doesn't maps to GetBootStates() on
203 # 4.3 so this one fails quite often.
204 if filtercount.has_key(node.history.status):
205 filtercount[node.history.status] += 1
207 ## NOTE: count filters
208 #if node.observed_status != 'DOWN':
209 # print node.hostname, node.observed_status
210 # if node.observed_status == 'DEBUG':
211 # if node.plc_node_stats['boot_state'] in ['debug', 'diagnose', 'disabled']:
212 # filtercount[node.plc_node_stats['boot_state']] += 1
214 # filtercount['debug'] += 1
217 # filtercount[node.observed_status] += 1
219 # if node.plc_node_stats and node.plc_node_stats['last_contact'] != None:
220 # filtercount[node.observed_status] += 1
222 # filtercount['neverboot'] += 1
225 if filter == "neverboot":
226 if not node.plc_node_stats or node.plc_node_stats['last_contact'] == None:
228 elif filter == "all":
230 elif filter == node.history.status:
232 elif filter == 'boot':
235 #if filter == node.observed_status:
236 # if filter == "DOWN":
237 # if node.plc_node_stats['last_contact'] != None:
241 #elif filter == "neverboot":
242 # if not node.plc_node_stats or node.plc_node_stats['last_contact'] == None:
244 #elif filter == "pending":
245 # # TODO: look in message logs...
247 #elif filter == node.plc_node_stats['boot_state']:
249 #elif filter == "all":
252 widget = NodeWidget(template='monitorweb.templates.node_template')
253 return dict(now=time.ctime(), query=query, fc=filtercount, nodewidget=widget)
255 def nodeaction_handler(self, tg_exceptions=None):
256 """Handle any kind of error."""
257 print "NODEACTION_HANDLER------------------"
259 if 'pcuid' in request.params:
260 pcuid = request.params['pcuid']
262 refurl = request.headers.get("Referer",link("pcu"))
265 # TODO: do this more intelligently...
266 uri_fields = urllib.splitquery(refurl)
267 if uri_fields[1] is not None:
268 val = query_to_dict(uri_fields[1])
271 elif 'hostname' in val:
272 pcuid = FindbadNodeRecord.get_latest_by(hostname=val['hostname']).plc_pcuid
278 cherry_trail = cherrypy._cputil.get_object_trail()
279 for i in cherry_trail:
283 return self.pcuview(None, pcuid, **dict(exceptions=tg_exceptions))
285 def nodeaction(self, **data):
286 print "NODEACTION------------------"
287 for item in data.keys():
288 print "%s %s" % ( item, data[item] )
290 if 'hostname' in data:
291 hostname = data['hostname']
293 flash("No hostname given in submitted data")
296 if 'submit' in data or 'type' in data:
298 action = data['submit']
300 action = data['type']
302 flash("No submit action given in submitted data")
305 if action == "Reboot":
306 print "REBOOT: %s" % hostname
307 ret = reboot.reboot_str(str(hostname))
309 if ret: raise RuntimeError("Error using PCU: " + str(ret))
310 flash("Reboot appeared to work. Allow at most 5 minutes. Then run ExternalScan to check current status.")
312 elif action == "ExternalScan":
313 scanapi.externalprobe(str(hostname))
314 flash("External Scan Successful!")
315 elif action == "InternalScan":
316 scanapi.internalprobe(str(hostname))
317 flash("Internal Scan Successful!")
320 raise RuntimeError("Unknown action given")
323 # TODO: add form validation
324 @expose(template="monitorweb.templates.pcuview")
325 @exception_handler(nodeaction_handler,"isinstance(tg_exceptions,RuntimeError)")
326 def pcuview(self, loginbase=None, pcuid=None, hostname=None, **data):
327 print "PCUVIEW------------------"
328 print "befor-len: ", len( [ i for i in session] )
329 session.flush(); session.clear()
330 print "after-len: ", len( [ i for i in session] )
340 if 'submit' in data.keys() or 'type' in data.keys():
341 if hostname: data['hostname'] = hostname
342 self.nodeaction(**data)
343 if 'exceptions' in data:
344 exceptions = data['exceptions']
347 actions = ActionRecord.query.filter_by(loginbase=loginbase
348 ).filter(ActionRecord.date_created >= datetime.now() - timedelta(14)
349 ).order_by(ActionRecord.date_created.desc())
350 actions = [ a for a in actions ]
351 sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
353 for plcnode in site_lb2hn[loginbase]:
354 node = FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname'])
355 # NOTE: reformat some fields.
356 prep_node_for_display(node)
358 if node.plc_pcuid: # not None
359 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid)
360 prep_pcu_for_display(pcu)
361 pcus[node.plc_pcuid] = pcu
363 for pcuid_key in pcus:
364 pcuquery += [pcus[pcuid_key]]
366 if pcuid and hostname is None:
367 print "pcuid: %s" % pcuid
368 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=pcuid)
370 prep_pcu_for_display(pcu)
372 if 'site_id' in pcu.plc_pcu_stats:
373 sitequery = [HistorySiteRecord.by_loginbase(pcu.loginbase)]
375 if 'nodenames' in pcu.plc_pcu_stats:
376 for nodename in pcu.plc_pcu_stats['nodenames']:
377 print "query for %s" % nodename
378 node = FindbadNodeRecord.get_latest_by(hostname=nodename)
379 print "%s" % node.port_status
380 print "%s" % node.to_dict()
382 prep_node_for_display(node)
385 if hostname and pcuid is None:
386 node = FindbadNodeRecord.get_latest_by(hostname=hostname)
387 # NOTE: reformat some fields.
388 prep_node_for_display(node)
389 sitequery = [node.site]
391 if node.plc_pcuid: # not None
392 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid)
393 prep_pcu_for_display(pcu)
396 return dict(sitequery=sitequery, pcuquery=pcuquery, nodequery=nodequery, actions=actions, exceptions=exceptions)
398 @expose(template="monitorweb.templates.nodehistory")
399 def nodehistory(self, hostname=None):
402 #fbnode = FindbadNodeRecord.get_by(hostname=hostname)
403 ## TODO: add links for earlier history if desired.
404 #l = fbnode.versions[-100:]
407 # prep_node_for_display(node)
410 fbnode = HistoryNodeRecord.get_by(hostname=hostname)
411 l = fbnode.versions[-100:]
414 #prep_node_for_display(node)
417 return dict(query=query, hostname=hostname)
419 @expose(template="monitorweb.templates.sitehistory")
420 def sitehistory(self, loginbase=None):
423 fbsite = HistorySiteRecord.get_by(loginbase=loginbase)
424 # TODO: add links for earlier history if desired.
425 l = fbsite.versions[-100:]
429 return dict(query=query, loginbase=loginbase)
432 @expose(template="monitorweb.templates.pculist")
433 def pcu(self, filter='all'):
434 print "PCUVIEW------------------"
435 print "befor-len: ", len( [ i for i in session] )
436 session.flush(); session.clear()
437 print "after-len: ", len( [ i for i in session] )
438 fbquery = FindbadPCURecord.get_all_latest()
440 filtercount = {'ok' : 0, 'NetDown': 0, 'Not_Run' : 0, 'pending' : 0, 'all' : 0}
444 if node.reboot_trial_status == str(0):
445 filtercount['ok'] += 1
446 elif node.reboot_trial_status == 'NetDown' or node.reboot_trial_status == 'Not_Run':
447 filtercount[node.reboot_trial_status] += 1
449 filtercount['pending'] += 1
451 prep_pcu_for_display(node)
456 elif filter == "ok" and node.reboot_trial_status == str(0):
458 elif filter == node.reboot_trial_status:
460 elif filter == "pending":
461 # TODO: look in message logs...
462 if node.reboot_trial_status != str(0) and \
463 node.reboot_trial_status != 'NetDown' and \
464 node.reboot_trial_status != 'Not_Run':
468 return dict(query=query, fc=filtercount)
470 @expose(template="monitorweb.templates.siteview")
471 def siteview(self, loginbase='pl'):
473 sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
475 for plcnode in site_lb2hn[loginbase]:
476 for node in FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname']):
477 # NOTE: reformat some fields.
478 prep_node_for_display(node)
480 return dict(sitequery=sitequery, nodequery=nodequery, fc={})
482 @expose(template="monitorweb.templates.sitelist")
483 def site(self, filter='all'):
484 print "SITE------------------"
485 print "befor-len: ", len( [ i for i in session] )
486 session.flush(); session.clear()
487 print "after-len: ", len( [ i for i in session] )
488 filtercount = {'good' : 0, 'down': 0, 'online':0, 'offline' : 0, 'new' : 0, 'pending' : 0, 'all' : 0}
489 fbquery = HistorySiteRecord.query.all()
493 filtercount['all'] += 1
494 if site.new and site.slices_used == 0 and not site.enabled:
495 filtercount['new'] += 1
496 elif not site.enabled:
497 filtercount['pending'] += 1
498 elif site.status in ['good', 'online']:
499 filtercount['good'] += 1
500 elif site.status in ['down', 'offline']:
501 filtercount['down'] += 1
506 elif filter == 'new' and site.new and site.slices_used == 0 and not site.enabled:
508 elif filter == "pending" and not site.enabled:
510 elif filter == 'good' and site.status in ['good', 'online']:
512 elif filter == 'down' and site.status in ['down', 'offline']:
515 return dict(query=query, fc=filtercount)
517 @expose(template="monitorweb.templates.actionlist")
518 def action(self, filter='all'):
519 session.bind = metadata.bind
520 filtercount = {'active' : 0, 'acknowledged': 0, 'all' : 0}
521 # With Acknowledgement
522 sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message,e.eventid '+ \
523 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
524 ' WHERE f.itemid=i.itemid ' + \
525 ' AND h.hostid=i.hostid ' + \
526 ' AND hg.hostid=h.hostid ' + \
527 ' AND t.triggerid=f.triggerid ' + \
528 ' AND t.triggerid=e.triggerid ' + \
529 ' AND a.eventid=e.eventid ' + \
530 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
531 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
532 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
533 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
534 ' ORDER BY t.lastchange DESC';
536 # WithOUT Acknowledgement
537 sql_noack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,e.eventid ' + \
538 ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
539 ' WHERE f.itemid=i.itemid ' + \
540 ' AND h.hostid=i.hostid ' + \
541 ' AND hg.hostid=h.hostid ' + \
542 ' AND t.triggerid=f.triggerid ' + \
543 ' AND t.triggerid=e.triggerid ' + \
544 ' AND e.eventid not in (select eventid from acknowledges) ' + \
545 ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
546 ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
547 ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
548 ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
549 ' ORDER BY t.lastchange DESC';
550 # for i in session.execute(sql): print i
553 replace = re.compile(' {.*}')
554 for sql,ack in [(sql_ack,True), (sql_noack,False)]:
555 result = session.execute(sql)
558 newrow = [ site_hn2lb[row[0].lower()] ] + [ r for r in row ]
560 print site_hn2lb.keys()
561 newrow = [ "unknown" ] + [ r for r in row ]
563 newrow[2] = replace.sub("", newrow[2]) # strip {.*} expressions
566 filtercount['all'] += 1
567 if not ack: # for unacknowledged
568 filtercount['active'] += 1
569 if filter == 'active':
572 filtercount['acknowledged'] += 1
573 if filter == 'acknowledged':
576 if filter != "acknowledged" and filter != "active":
579 return dict(query=query, fc=filtercount)