added sitelist option for find* scripts.
[monitor.git] / web / MonitorWeb / monitorweb / controllers.py
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
5 import cherrypy
6 # from monitorweb import model
7 # import logging
8 # log = logging.getLogger("monitorweb.controllers")
9 import re
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
15 from pcucontrol import reboot
16 from monitor.wrapper.plccache import plcdb_id2lb as site_id2lb
17 from monitor.wrapper.plccache import plcdb_hn2lb as site_hn2lb
18 from monitor.wrapper.plccache import plcdb_lb2hn as site_lb2hn
19
20 from monitorweb.templates.links import *
21
22 from monitor import scanapi
23
24
25 def query_to_dict(query):
26         """ take a url query string and chop it up """
27         val = {}
28         query_fields = query.split('&')
29         for f in query_fields:
30                 (k,v) = urllib.splitvalue(f)
31                 val[k] = v
32
33         return val
34
35 def format_ports(data, pcumodel=None):
36         retval = []
37         filtered_length=0
38
39         if pcumodel:
40                 supported_ports=reboot.model_to_object(pcumodel).supported_ports
41         else:
42                 # ports of a production node
43                 supported_ports=[22,80,806]
44
45         if data and len(data.keys()) > 0 :
46                 for port in supported_ports:
47                         try:
48                                 state = data[str(port)]
49                         except:
50                                 state = "unknown"
51
52                         if state == "filtered":
53                                 filtered_length += 1
54                                 
55                         retval.append( (port, state) )
56
57         if retval == []: 
58                 retval = [( "Closed/Filtered", "state" )]
59
60         if filtered_length == len(supported_ports):
61                 retval = [( "All Filtered", "state" )]
62
63         return retval
64
65 def format_pcu_shortstatus(pcu):
66         status = "error"
67         if pcu:
68                 if pcu.reboot_trial_status == str(0):
69                         status = "Ok"
70                 elif pcu.reboot_trial_status == "NetDown" or pcu.reboot_trial_status == "Not_Run":
71                         status = pcu.reboot_trial_status
72                 else:
73                         status = "error"
74
75         return status
76
77 def prep_pcu_for_display(pcu):
78                 
79         try:
80                 pcu.loginbase = site_id2lb[pcu.plc_pcu_stats['site_id']]
81         except:
82                 pcu.loginbase = "unknown"
83
84         pcu.ports = format_ports(pcu.port_status, pcu.plc_pcu_stats['model'])
85         pcu.status = format_pcu_shortstatus(pcu)
86
87         #print pcu.entry_complete
88         pcu.entry_complete_str = pcu.entry_complete
89         #pcu.entry_complete_str += "".join([ f[0] for f in pcu.entry_complete.split() ])
90         if pcu.dns_status == "NOHOSTNAME":
91                 pcu.dns_short_status = 'NoHost'
92         elif pcu.dns_status == "DNS-OK":
93                 pcu.dns_short_status = 'Ok'
94         elif pcu.dns_status == "DNS-NOENTRY":
95                 pcu.dns_short_status = 'NoEntry'
96         elif pcu.dns_status == "NO-DNS-OR-IP":
97                 pcu.dns_short_status = 'NoHostOrIP'
98         elif pcu.dns_status == "DNS-MISMATCH":
99                 pcu.dns_short_status = 'Mismatch'
100
101 class NodeWidget(widgets.Widget):
102         pass
103
104 def prep_node_for_display(node):
105         if node.plc_pcuid:
106                 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid).first()
107                 if pcu:
108                         node.pcu_status = pcu.reboot_trial_status
109                         node.pcu_short_status = format_pcu_shortstatus(pcu)
110                         node.pcu = pcu
111                         prep_pcu_for_display(node.pcu)
112                 else:
113                         node.pcu_short_status = "none"
114                         node.pcu_status = "nodata"
115                         node.pcu = None
116
117         else:
118                 node.pcu_status = "nopcu"
119                 node.pcu_short_status = "none"
120                 node.pcu = None
121
122
123         if node.kernel_version:
124                 node.kernel = node.kernel_version.split()[2]
125         else:
126                 node.kernel = ""
127
128         try:
129                 node.loginbase = site_id2lb[node.plc_node_stats['site_id']]
130         except:
131                 node.loginbase = "unknown"
132
133         if node.loginbase:
134                 node.site = HistorySiteRecord.by_loginbase(node.loginbase)
135
136         node.history = HistoryNodeRecord.by_hostname(node.hostname)
137
138         node.ports = format_ports(node.port_status)
139
140         try:
141                 exists = node.plc_node_stats['last_contact']
142         except:
143                 node.plc_node_stats = {'last_contact' : None}
144
145
146
147 class Root(controllers.RootController):
148         @expose(template="monitorweb.templates.welcome")
149         def index(self):
150                 import time
151                 # log.debug("Happy TurboGears Controller Responding For Duty")
152                 flash("Your application is now running")
153                 return dict(now=time.ctime())
154
155         @expose(template="monitorweb.templates.pcuview")
156         def nodeview(self, hostname=None):
157                 nodequery=[]
158                 if hostname:
159                         for node in FindbadNodeRecord.get_latest_by(hostname=hostname):
160                                 # NOTE: reformat some fields.
161                                 prep_node_for_display(node)
162                                 nodequery += [node]
163
164                 return self.pcuview(None, hostname) # dict(nodequery=nodequery)
165
166         @expose(template="monitorweb.templates.nodelist")
167         def node(self, filter='BOOT'):
168                 import time
169                 fbquery = FindbadNodeRecord.get_all_latest()
170                 query = []
171                 filtercount = {'DOWN' : 0, 'BOOT': 0, 'DEBUG' : 0, 'neverboot' : 0, 'pending' : 0, 'all' : 0}
172                 for node in fbquery:
173                         # NOTE: reformat some fields.
174                         prep_node_for_display(node)
175
176                         # NOTE: count filters
177                         if node.observed_status != 'DOWN':
178                                 filtercount[node.observed_status] += 1
179                         else:
180                                 if node.plc_node_stats and node.plc_node_stats['last_contact'] != None:
181                                         filtercount[node.observed_status] += 1
182                                 else:
183                                         filtercount['neverboot'] += 1
184
185                         # NOTE: apply filter
186                         if filter == node.observed_status:
187                                 if filter == "DOWN":
188                                         if node.plc_node_stats['last_contact'] != None:
189                                                 query.append(node)
190                                 else:
191                                         query.append(node)
192                         elif filter == "neverboot":
193                                 if not node.plc_node_stats or node.plc_node_stats['last_contact'] == None:
194                                         query.append(node)
195                         elif filter == "pending":
196                                 # TODO: look in message logs...
197                                 pass
198                         elif filter == "all":
199                                 query.append(node)
200                                 
201                 widget = NodeWidget(template='monitorweb.templates.node_template')
202                 return dict(now=time.ctime(), query=query, fc=filtercount, nodewidget=widget)
203         
204         def nodeaction_handler(self, tg_exceptions=None):
205                 """Handle any kind of error."""
206
207                 if 'pcuid' in request.params:
208                         pcuid = request.params['pcuid']
209                 else:
210                         refurl = request.headers.get("Referer",link("pcu"))
211                         print refurl
212
213                         # TODO: do this more intelligently...
214                         uri_fields = urllib.splitquery(refurl)
215                         if uri_fields[1] is not None:
216                                 val = query_to_dict(uri_fields[1])
217                                 if 'pcuid' in val:
218                                         pcuid = val['pcuid']
219                                 elif 'hostname' in val:
220                                         pcuid = FindbadNodeRecord.get_latest_by(hostname=val['hostname']).first().plc_pcuid
221                                 else:
222                                         pcuid=None
223                         else:
224                                 pcuid=None
225
226                 cherry_trail = cherrypy._cputil.get_object_trail()
227                 for i in cherry_trail:
228                         print "trail: ", i
229
230                 print pcuid
231                 return self.pcuview(None, pcuid, **dict(exceptions=tg_exceptions))
232
233         def nodeaction(self, **data):
234                 for item in data.keys():
235                         print "%s %s" % ( item, data[item] )
236
237                 if 'hostname' in data:
238                         hostname = data['hostname']
239                 else:
240                         flash("No hostname given in submitted data")
241                         return
242
243                 if 'submit' in data or 'type' in data:
244                         try:
245                                 action = data['submit']
246                         except:
247                                 action = data['type']
248                 else:
249                         flash("No submit action given in submitted data")
250                         return
251
252                 if action == "Reboot":
253                         print "REBOOT: %s" % hostname
254                         ret = reboot.reboot_str(str(hostname))
255                         print ret
256                         if ret: raise RuntimeError("Error using PCU: " + str(ret))
257                         flash("Reboot appeared to work.  All at most 5 minutes.  Run ExternalScan to check current status.")
258
259                 elif action == "ExternalScan":
260                         scanapi.externalprobe(str(hostname))
261                         flash("External Scan Successful!")
262                 elif action == "InternalScan":
263                         scanapi.internalprobe(str(hostname))
264                         flash("Internal Scan Successful!")
265                 else:
266                         # unknown action
267                         raise RuntimeError("Unknown action given")
268                 return
269
270         # TODO: add form validation
271         @expose(template="monitorweb.templates.pcuview")
272         @exception_handler(nodeaction_handler,"isinstance(tg_exceptions,RuntimeError)")
273         def pcuview(self, loginbase=None, pcuid=None, hostname=None, **data):
274                 sitequery=[]
275                 pcuquery=[]
276                 nodequery=[]
277                 exceptions = None
278
279                 for key in data:
280                         print key, data[key]
281
282                 if 'submit' in data.keys() or 'type' in data.keys():
283                         if hostname: data['hostname'] = hostname
284                         self.nodeaction(**data)
285                 if 'exceptions' in data:
286                         exceptions = data['exceptions']
287
288                 if loginbase:
289                         sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
290                         pcus = {}
291                         for plcnode in site_lb2hn[loginbase]:
292                                 for node in FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname']):
293                                         # NOTE: reformat some fields.
294                                         prep_node_for_display(node)
295                                         nodequery += [node]
296                                         if node.plc_pcuid:      # not None
297                                                 pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid).first()
298                                                 prep_pcu_for_display(pcu)
299                                                 pcus[node.plc_pcuid] = pcu
300
301                         for pcuid_key in pcus:
302                                 pcuquery += [pcus[pcuid_key]]
303
304                 if pcuid and hostname is None:
305                         print "pcuid: %s" % pcuid
306                         for pcu in FindbadPCURecord.get_latest_by(plc_pcuid=pcuid):
307                                 # NOTE: count filter
308                                 prep_pcu_for_display(pcu)
309                                 pcuquery += [pcu]
310                         if 'site_id' in pcu.plc_pcu_stats:
311                                 sitequery = [HistorySiteRecord.by_loginbase(pcu.loginbase)]
312                                 
313                         if 'nodenames' in pcu.plc_pcu_stats:
314                                 for nodename in pcu.plc_pcu_stats['nodenames']: 
315                                         print "query for %s" % nodename
316                                         q = FindbadNodeRecord.get_latest_by(hostname=nodename)
317                                         node = q.first()
318                                         print "%s" % node.port_status
319                                         print "%s" % node.to_dict()
320                                         print "%s" % len(q.all())
321                                         if node:
322                                                 prep_node_for_display(node)
323                                                 nodequery += [node]
324
325                 if hostname and pcuid is None:
326                         for node in FindbadNodeRecord.get_latest_by(hostname=hostname):
327                                 # NOTE: reformat some fields.
328                                 prep_node_for_display(node)
329                                 sitequery = [node.site]
330                                 nodequery += [node]
331                                 if node.plc_pcuid:      # not None
332                                         pcu = FindbadPCURecord.get_latest_by(plc_pcuid=node.plc_pcuid).first()
333                                         prep_pcu_for_display(pcu)
334                                         pcuquery += [pcu]
335                         
336                 return dict(sitequery=sitequery, pcuquery=pcuquery, nodequery=nodequery, exceptions=exceptions)
337
338         @expose(template="monitorweb.templates.pculist")
339         def pcu(self, filter='all'):
340                 import time
341                 fbquery = FindbadPCURecord.get_all_latest()
342                 query = []
343                 filtercount = {'ok' : 0, 'NetDown': 0, 'Not_Run' : 0, 'pending' : 0, 'all' : 0}
344                 for node in fbquery:
345
346                         # NOTE: count filter
347                         if node.reboot_trial_status == str(0):
348                                 filtercount['ok'] += 1
349                         elif node.reboot_trial_status == 'NetDown' or node.reboot_trial_status == 'Not_Run':
350                                 filtercount[node.reboot_trial_status] += 1
351                         else:
352                                 filtercount['pending'] += 1
353
354                         prep_pcu_for_display(node)
355
356                         # NOTE: apply filter
357                         if filter == "all":
358                                 query.append(node)
359                         elif filter == "ok" and node.reboot_trial_status == str(0):
360                                 query.append(node)
361                         elif filter == node.reboot_trial_status:
362                                 query.append(node)
363                         elif filter == "pending":
364                                 # TODO: look in message logs...
365                                 if node.reboot_trial_status != str(0) and \
366                                         node.reboot_trial_status != 'NetDown' and \
367                                         node.reboot_trial_status != 'Not_Run':
368
369                                         query.append(node)
370                                 
371                 return dict(query=query, fc=filtercount)
372
373         @expose(template="monitorweb.templates.siteview")
374         def siteview(self, loginbase='pl'):
375                 # get site query
376                 sitequery = [HistorySiteRecord.by_loginbase(loginbase)]
377                 nodequery = []
378                 for plcnode in site_lb2hn[loginbase]:
379                         for node in FindbadNodeRecord.get_latest_by(hostname=plcnode['hostname']):
380                                 # NOTE: reformat some fields.
381                                 prep_node_for_display(node)
382                                 nodequery += [node]
383                 return dict(sitequery=sitequery, nodequery=nodequery, fc={})
384
385         @expose(template="monitorweb.templates.sitelist")
386         def site(self, filter='all'):
387                 filtercount = {'good' : 0, 'down': 0, 'new' : 0, 'pending' : 0, 'all' : 0}
388                 fbquery = HistorySiteRecord.query.all()
389                 query = []
390                 for site in fbquery:
391                         # count filter
392                         filtercount['all'] += 1
393                         if site.new and site.slices_used == 0 and not site.enabled:
394                                 filtercount['new'] += 1
395                         elif not site.enabled:
396                                 filtercount['pending'] += 1
397                         else:
398                                 filtercount[site.status] += 1
399
400                         # apply filter
401                         if filter == "all":
402                                 query.append(site)
403                         elif filter == 'new' and site.new and site.slices_used == 0 and not site.enabled:
404                                 query.append(site)
405                         elif filter == "pending" and not site.enabled:
406                                 query.append(site)
407                         elif filter == site.status:
408                                 query.append(site)
409                                 
410                 return dict(query=query, fc=filtercount)
411
412         @expose(template="monitorweb.templates.actionlist")
413         def action(self, filter='all'):
414                 session.bind = metadata.bind
415                 filtercount = {'active' : 0, 'acknowledged': 0, 'all' : 0}
416                 # With Acknowledgement
417                 sql_ack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,a.message,e.eventid '+ \
418               ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
419               ' WHERE f.itemid=i.itemid ' + \
420                   ' AND h.hostid=i.hostid ' + \
421                   ' AND hg.hostid=h.hostid ' + \
422                   ' AND t.triggerid=f.triggerid ' + \
423                   ' AND t.triggerid=e.triggerid ' + \
424                   ' AND a.eventid=e.eventid ' + \
425                   ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
426                   ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
427                   ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
428                   ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
429               ' ORDER BY t.lastchange DESC';
430
431                 # WithOUT Acknowledgement
432                 sql_noack = 'SELECT DISTINCT h.host,t.description,t.priority,t.lastchange,e.eventid ' + \
433               ' FROM triggers t,hosts h,items i,functions f, hosts_groups hg,escalations e,acknowledges a ' + \
434               ' WHERE f.itemid=i.itemid ' + \
435                   ' AND h.hostid=i.hostid ' + \
436                   ' AND hg.hostid=h.hostid ' + \
437                   ' AND t.triggerid=f.triggerid ' + \
438                   ' AND t.triggerid=e.triggerid ' + \
439                   ' AND e.eventid not in (select eventid from acknowledges) ' + \
440                   ' AND t.status=' + str(defines.TRIGGER_STATUS_ENABLED) + \
441                   ' AND i.status=' + str(defines.ITEM_STATUS_ACTIVE) + \
442                   ' AND h.status=' + str(defines.HOST_STATUS_MONITORED) + \
443                   ' AND t.value=' + str(defines.TRIGGER_VALUE_TRUE) + \
444               ' ORDER BY t.lastchange DESC';
445                 # for i in session.execute(sql): print i
446
447                 query=[]
448                 replace = re.compile(' {.*}')
449                 for sql,ack in [(sql_ack,True), (sql_noack,False)]:
450                         result = session.execute(sql)
451                         for row in result:
452                                 try:
453                                         newrow = [ site_hn2lb[row[0].lower()] ] + [ r for r in row ]
454                                 except:
455                                         print site_hn2lb.keys()
456                                         newrow = [ "unknown" ] + [ r for r in row ]
457
458                                 newrow[2] = replace.sub("", newrow[2]) # strip {.*} expressions
459
460                                 # NOTE: filter count
461                                 filtercount['all'] += 1
462                                 if not ack: # for unacknowledged
463                                         filtercount['active'] += 1
464                                         if filter == 'active':
465                                                 query.append(newrow)
466                                 else:
467                                         filtercount['acknowledged'] += 1
468                                         if filter == 'acknowledged':
469                                                 query.append(newrow)
470                                         
471                                 if filter != "acknowledged" and filter != "active":
472                                         query.append(newrow)
473
474                 return dict(query=query, fc=filtercount)