vigiboard / dashboard / controllers / root.py @ 805cc54a
History | View | Annotate | Download (13.8 KB)
1 | 805cc54a | Thomas ANDREJAK | # -*- coding: utf-8 -*-
|
---|---|---|---|
2 | """Main Controller"""
|
||
3 | |||
4 | import tg |
||
5 | from tg import expose, flash, require, url, request, redirect, validate, tmpl_context |
||
6 | |||
7 | from tw.forms import validators |
||
8 | |||
9 | from sets import Set |
||
10 | |||
11 | import pylons |
||
12 | from pylons.i18n import ugettext as _, lazy_ugettext as l_ |
||
13 | from catwalk.tg2 import Catwalk |
||
14 | from repoze.what import predicates |
||
15 | |||
16 | from sqlalchemy import not_ , and_ , asc , desc, or_ |
||
17 | |||
18 | from tw.jquery import JQueryUIDialog |
||
19 | |||
20 | from dashboard.lib.base import BaseController |
||
21 | from dashboard.model import DBSession, metadata |
||
22 | from dashboard.controllers.error import ErrorController |
||
23 | from dashboard import model |
||
24 | from dashboard.controllers.secure import SecureController |
||
25 | |||
26 | from dashboard.model.bdd_dashboard import * |
||
27 | from dashboard.model.auth import Permission |
||
28 | from repoze.what.predicates import Any,not_anonymous |
||
29 | |||
30 | from dashboard.widgets.edit_event import Edit_Event_Form , Search_Form , edit_event_status_options |
||
31 | |||
32 | __all__ = ['RootController']
|
||
33 | |||
34 | def GetUserGroups(): |
||
35 | |||
36 | gr = DBSession.query(Groups.name).join( |
||
37 | ( GroupPermissions , Groups.name == GroupPermissions.groupname ), |
||
38 | ( Permission , Permission.permission_id == GroupPermissions.idpermission ) |
||
39 | ).filter(Permission.permission_name.in_(request.environ.get('repoze.who.identity').get('permissions'))) |
||
40 | |||
41 | lst_grp = Set([i.name for i in gr]) |
||
42 | lst_tmp = lst_grp |
||
43 | |||
44 | while len(lst_tmp) > 0: |
||
45 | gr = DBSession.query(Groups.name).filter(Groups.parent.in_(lst_tmp)) |
||
46 | tmp = Set([]) |
||
47 | for i in gr : |
||
48 | tmp.add(i.name) |
||
49 | lst_grp.add(i.name) |
||
50 | lst_tmp = tmp |
||
51 | |||
52 | return lst_grp
|
||
53 | |||
54 | def FormatEventsImgStatu (event): |
||
55 | if event.active and event.status == 'AAClosed': |
||
56 | return { 'src': url('/images_vigilo/crossed.png') } |
||
57 | elif event.status == 'Acknowledged' : |
||
58 | return { 'src': url('/images_vigilo/checked.png') } |
||
59 | else:
|
||
60 | return None |
||
61 | |||
62 | |||
63 | def FormatEvents(events,first_row,last_row): |
||
64 | ev = [] |
||
65 | i=0
|
||
66 | severity = { 0 : 'Minor' , 1 : 'Minor', 2 : 'Minor', 3 : 'Minor', 4 : 'Minor', 5 : 'Minor' , 6 : 'Major' , 7 : 'Critical' } |
||
67 | class_tr = ['odd','even'] |
||
68 | class_ack = { 'Acknowledged' : 'Ack' , 'None' : '' , 'AAClosed' : 'Ack' } |
||
69 | ids = [] |
||
70 | for event in events[first_row : last_row]: |
||
71 | ids.append(event.idevent) |
||
72 | if event.active :
|
||
73 | ev.append([ |
||
74 | event, |
||
75 | {'class': class_tr[i%2]}, |
||
76 | {'class' : severity[event.severity] + class_ack[event.status]},
|
||
77 | {'class' : severity[event.severity] + class_ack[event.status] },
|
||
78 | {'src' : url('/images_vigilo/%s2.png' % severity[event.severity].upper())}, |
||
79 | FormatEventsImgStatu(event) |
||
80 | ]) |
||
81 | else :
|
||
82 | ev.append([ |
||
83 | event, |
||
84 | {'class': class_tr[i%2]}, |
||
85 | {'class' : severity[event.severity] + class_ack[event.status] },
|
||
86 | {'class' : 'Cleared' + class_ack[event.status] }, |
||
87 | {'src' : url('/images_vigilo/%s2.png' % severity[event.severity].upper())}, |
||
88 | FormatEventsImgStatu(event) |
||
89 | ]) |
||
90 | |||
91 | i=i+1
|
||
92 | return ev,ids
|
||
93 | |||
94 | def FormatHistory (idevents,hostname,servicename): |
||
95 | history = DBSession.query(EventHistory).filter(EventHistory.idevent.in_(idevents)).order_by(desc(EventHistory.timestamp)) |
||
96 | severity = { 0 : 'None' , 1 : 'OK', 2 : 'Suppressed', 3 : 'Initial', 4 : 'Maintenance', 5 : 'Minor' , 6 : 'Major' , 7 : 'Critical' } |
||
97 | hist = [] |
||
98 | i = 0
|
||
99 | class_tr = ['odd','even'] |
||
100 | for h in history : |
||
101 | if h.value :
|
||
102 | hist.append([ |
||
103 | h.idhistory, |
||
104 | hostname, |
||
105 | servicename, |
||
106 | h.timestamp, |
||
107 | h.username, |
||
108 | h.type_action, |
||
109 | severity[min(int(h.value),7)], |
||
110 | h.text, |
||
111 | {'class' : class_tr[i%2]}, |
||
112 | {'class':severity[min(int(h.value),7)]} |
||
113 | ]) |
||
114 | else:
|
||
115 | hist.append([ |
||
116 | h.idhistory, |
||
117 | hostname, |
||
118 | servicename, |
||
119 | h.timestamp, |
||
120 | h.username, |
||
121 | h.type_action, |
||
122 | severity[0],
|
||
123 | h.text, |
||
124 | {'class' : class_tr[i%2]}, |
||
125 | {'class':severity[0]} |
||
126 | ]) |
||
127 | i = i+1
|
||
128 | |||
129 | return hist
|
||
130 | |||
131 | def GenerateTmplContext(): |
||
132 | tmpl_context.edit_eventdialog = JQueryUIDialog(id='Edit_EventsDialog',autoOpen=False,title='Edit Event',width=400) |
||
133 | tmpl_context.searchdialog = JQueryUIDialog(id='SearchDialog',autoOpen=False,title='Search Event') |
||
134 | tmpl_context.historydialog = JQueryUIDialog(id='HistoryDialog',autoOpen=False,title='History') |
||
135 | tmpl_context.edit_event_form = Edit_Event_Form('edit_event_form',action=url('/update_event')) |
||
136 | tmpl_context.search_form = Search_Form('search_form',action=url('/dashboard')) |
||
137 | |||
138 | class RootController(BaseController): |
||
139 | """
|
||
140 | The root controller for the dashboard application.
|
||
141 |
|
||
142 | All the other controllers and WSGI applications should be mounted on this
|
||
143 | controller. For example::
|
||
144 |
|
||
145 | panel = ControlPanelController()
|
||
146 | another_app = AnotherWSGIApplication()
|
||
147 |
|
||
148 | Keep in mind that WSGI applications shouldn't be mounted directly: They
|
||
149 | must be wrapped around with :class:`tg.controllers.WSGIAppController`.
|
||
150 |
|
||
151 | """
|
||
152 | secc = SecureController() |
||
153 | |||
154 | admin = Catwalk(model, DBSession) |
||
155 | |||
156 | error = ErrorController() |
||
157 | |||
158 | @expose('dashboard.templates.index') |
||
159 | def index(self): |
||
160 | """Handle the front-page."""
|
||
161 | return dict(page='index') |
||
162 | |||
163 | @expose('dashboard.templates.about') |
||
164 | def about(self): |
||
165 | """Handle the 'about' page."""
|
||
166 | return dict(page='about') |
||
167 | |||
168 | @expose('dashboard.templates.authentication') |
||
169 | def auth(self): |
||
170 | """Display some information about auth* on this application."""
|
||
171 | return dict(page='auth') |
||
172 | |||
173 | @expose('dashboard.templates.index') |
||
174 | @require(predicates.has_permission('manage', msg=l_('Only for managers'))) |
||
175 | def manage_permission_only(self, **kw): |
||
176 | """Illustrate how a page for managers only works."""
|
||
177 | return dict(page='managers stuff') |
||
178 | |||
179 | @expose('dashboard.templates.index') |
||
180 | @require(predicates.is_user('editor', msg=l_('Only for the editor'))) |
||
181 | def editor_user_only(self, **kw): |
||
182 | """Illustrate how a page exclusive for the editor works."""
|
||
183 | return dict(page='editor stuff') |
||
184 | |||
185 | @expose('dashboard.templates.login') |
||
186 | def login(self, came_from=url('/')): |
||
187 | """Start the user login."""
|
||
188 | login_counter = request.environ['repoze.who.logins']
|
||
189 | if login_counter > 0: |
||
190 | flash(_('Wrong credentials'), 'warning') |
||
191 | return dict(page='login', login_counter=str(login_counter), |
||
192 | came_from=came_from) |
||
193 | |||
194 | @expose()
|
||
195 | def post_login(self, came_from=url('/')): |
||
196 | """
|
||
197 | Redirect the user to the initially requested page on successful
|
||
198 | authentication or redirect her back to the login page if login failed.
|
||
199 |
|
||
200 | """
|
||
201 | if not request.identity: |
||
202 | login_counter = request.environ['repoze.who.logins'] + 1 |
||
203 | redirect(url('/login', came_from=came_from, __logins=login_counter))
|
||
204 | userid = request.identity['repoze.who.userid']
|
||
205 | flash(_('Welcome back, %s!') % userid)
|
||
206 | redirect(came_from) |
||
207 | |||
208 | @expose()
|
||
209 | def post_logout(self, came_from=url('/')): |
||
210 | """
|
||
211 | Redirect the user to the initially requested page on logout and say
|
||
212 | goodbye as well.
|
||
213 |
|
||
214 | """
|
||
215 | flash(_('We hope to see you soon!'))
|
||
216 | redirect(came_from) |
||
217 | |||
218 | @expose('dashboard.templates.dashboard') |
||
219 | @require(Any(not_anonymous(),msg="You need to be authenticated")) |
||
220 | def dashboard(self,page='1',host=None,service=None,output=None,trouble_ticket=None): |
||
221 | try :
|
||
222 | page = int(page)
|
||
223 | except :
|
||
224 | page = 1
|
||
225 | if page < 1 : |
||
226 | page = 1
|
||
227 | |||
228 | events = DBSession.query(Events).join( |
||
229 | ( Host, Events.hostname == Host.name ), |
||
230 | ( Service, Events.servicename == Service.name ), |
||
231 | ( HostGroups , Host.name == HostGroups.hostname ) |
||
232 | ).filter(HostGroups.groupname.in_(GetUserGroups()) |
||
233 | ).filter(not_(and_(Events.active == False,Events.status == 'AAClosed')) |
||
234 | ).filter(Events.timestamp_active != None
|
||
235 | ).filter(not_(Events.timestamp_active.like('0000-00-00 00:00:00'))
|
||
236 | ) |
||
237 | |||
238 | if host :
|
||
239 | events = events.filter(Events.hostname.like('%%%s%%' % host))
|
||
240 | if service :
|
||
241 | events = events.filter(Events.servicename.like('%%%s%%' % service))
|
||
242 | if output :
|
||
243 | events = events.filter(Events.output.like('%%%s%%' % output))
|
||
244 | if trouble_ticket :
|
||
245 | events = events.filter(Events.trouble_ticket.like('%%%s%%' % trouble_ticket))
|
||
246 | |||
247 | events= events.order_by(asc(Events.status) |
||
248 | ).order_by(desc(Events.active) |
||
249 | ).order_by(desc(Events.severity) |
||
250 | ).order_by(asc(Events.hostname) |
||
251 | ).order_by(desc(Events.timestamp)) |
||
252 | |||
253 | total_row = events.count() |
||
254 | |||
255 | item_per_page = int(tg.config['dashboard_item_per_page']) |
||
256 | |||
257 | if total_row <= item_per_page * (page-1) : |
||
258 | page = 1
|
||
259 | id_first_row = item_per_page * (page-1)
|
||
260 | id_last_row = min(id_first_row + item_per_page,total_row)
|
||
261 | |||
262 | GenerateTmplContext() |
||
263 | events , ids = FormatEvents(events,id_first_row,id_last_row) |
||
264 | return dict( |
||
265 | events=events, |
||
266 | id_first_row=id_first_row, |
||
267 | id_last_row=id_last_row, |
||
268 | total_row=total_row, |
||
269 | pages=range(1,(total_row/item_per_page) + 2), |
||
270 | page=page, |
||
271 | event_edit_status_options=edit_event_status_options, |
||
272 | history=[], |
||
273 | hist_error = False
|
||
274 | ) |
||
275 | |||
276 | |||
277 | @expose('json') |
||
278 | def dashboard_HistoryDialog ( self , id ) : |
||
279 | events = DBSession.query(Events.severity,Events.idevent,Events.hostname,Events.servicename).filter(Events.idevent == id)[0] |
||
280 | initial_state = DBSession.query(EventHistory).filter(EventHistory.idevent == id).order_by(asc(EventHistory.timestamp)).order_by(asc(EventHistory.type_action))
|
||
281 | if initial_state.count() > 0 : |
||
282 | initial_state = initial_state[0].value
|
||
283 | else :
|
||
284 | initial_state = 0
|
||
285 | severity = { 0 : 'None' , 1 : 'OK', 2 : 'Suppressed', 3 : 'Initial', 4 : 'Maintenance', 5 : 'Minor' , 6 : 'Major' , 7 : 'Critical' } |
||
286 | return dict( |
||
287 | initial_state=severity[int(initial_state)],
|
||
288 | current_state=severity[events.severity], |
||
289 | idevent = events.idevent, |
||
290 | host = events.hostname, |
||
291 | service = events.servicename, |
||
292 | nagios_link = tg.config['dashboard_links.nagios'] % {'idevent':events.idevent}, |
||
293 | metrology_link = tg.config['dashboard_links.metrology'] % {'idevent':events.idevent}, |
||
294 | security_link = tg.config['dashboard_links.security'] % {'idevent':events.idevent}, |
||
295 | servicetype_link = tg.config['dashboard_links.servicetype'] % {'idevent':events.idevent} |
||
296 | ) |
||
297 | |||
298 | @expose('dashboard.templates.dashboard') |
||
299 | @require(Any(not_anonymous(),msg="You need to be authenticated")) |
||
300 | def dashboard_event(self,idevent): |
||
301 | |||
302 | events = DBSession.query(Events).join( |
||
303 | ( Host, Events.hostname == Host.name ), |
||
304 | ( Service, Events.servicename == Service.name ), |
||
305 | ( HostGroups , Host.name == HostGroups.hostname ) |
||
306 | ).filter(HostGroups.groupname.in_(GetUserGroups()) |
||
307 | ).filter(not_(and_(Events.active == False,Events.status == 'AAClosed')) |
||
308 | ).filter(Events.timestamp_active != None
|
||
309 | ).filter(not_(Events.timestamp_active.like('0000-00-00 00:00:00'))
|
||
310 | ).filter(Events.idevent == idevent) |
||
311 | |||
312 | if events.count() != 1 : |
||
313 | flash(_('Error in DB'),'error') |
||
314 | redirect('/dashboard')
|
||
315 | |||
316 | GenerateTmplContext() |
||
317 | events , ids = FormatEvents(events,0,1) |
||
318 | return dict( |
||
319 | events=events, |
||
320 | id_first_row=1,
|
||
321 | id_last_row=1,
|
||
322 | total_row=1,
|
||
323 | pages=[1],
|
||
324 | page=1,
|
||
325 | event_edit_status_options=edit_event_status_options, |
||
326 | history=FormatHistory(ids,events[0][0].hostname,events[0][0].servicename), |
||
327 | hist_error = True
|
||
328 | ) |
||
329 | |||
330 | @expose('dashboard.templates.dashboard') |
||
331 | @require(Any(not_anonymous(),msg="You need to be authenticated")) |
||
332 | def dashboard_host_service(self,host,service): |
||
333 | |||
334 | events = DBSession.query(Events).join( |
||
335 | ( Host, Events.hostname == Host.name ), |
||
336 | ( Service, Events.servicename == Service.name ), |
||
337 | ( HostGroups , Host.name == HostGroups.hostname ) |
||
338 | ).filter(HostGroups.groupname.in_(GetUserGroups()) |
||
339 | ).filter(not_(and_(Events.active == False,Events.status == 'AAClosed')) |
||
340 | ).filter(Events.timestamp_active != None
|
||
341 | ).filter(not_(Events.timestamp_active.like('0000-00-00 00:00:00'))
|
||
342 | ).filter(Events.hostname == host |
||
343 | ).filter(Events.servicename == service) |
||
344 | |||
345 | if events.count() == 0 : |
||
346 | redirect('/dashboard')
|
||
347 | |||
348 | GenerateTmplContext() |
||
349 | |||
350 | events , ids = FormatEvents(events,0,events.count())
|
||
351 | |||
352 | return dict( |
||
353 | events=events, |
||
354 | id_first_row=1,
|
||
355 | id_last_row=1,
|
||
356 | total_row=1,
|
||
357 | pages=[1],
|
||
358 | page=1,
|
||
359 | event_edit_status_options=edit_event_status_options, |
||
360 | history=FormatHistory(ids,host,service), |
||
361 | hist_error = True
|
||
362 | ) |
||
363 | |||
364 | def edit_event_form_errors (self,**argv): |
||
365 | flash(_(tmpl_context.form_errors),'error')
|
||
366 | redirect(request.environ.get('HTTP_REFERER').split(request.environ.get('HTTP_HOST'))[1]) |
||
367 | |||
368 | @expose('dashboard.templates.dashboard_update') |
||
369 | @validate(validators={
|
||
370 | "id":validators.Regex(r'^[0-9]+(,[0-9]*)*,?$'), |
||
371 | "tt":validators.Regex(r'^[0-9]*$'), |
||
372 | "status":validators.OneOf(['None', 'Acknowledged', 'AAClosed']) |
||
373 | },error_handler=edit_event_form_errors) |
||
374 | @require(Any(not_anonymous(),msg="You need to be authenticated")) |
||
375 | def update_event(self,*argv,**krgv): |
||
376 | |||
377 | ids = krgv['id'].split(',') |
||
378 | |||
379 | if len(ids) > 1 : |
||
380 | ids = ids[:-1]
|
||
381 | |||
382 | events = DBSession.query(Events).join( |
||
383 | ( Host, Events.hostname == Host.name ), |
||
384 | ( Service, Events.servicename == Service.name ), |
||
385 | ( HostGroups , Host.name == HostGroups.hostname ) |
||
386 | ).filter(HostGroups.groupname.in_(GetUserGroups()) |
||
387 | ).filter(Events.idevent.in_(ids)) |
||
388 | |||
389 | if events.count() <= 0 : |
||
390 | flash(_('No access to this event'),'error') |
||
391 | redirect('/dashboard')
|
||
392 | |||
393 | for event in events : |
||
394 | if krgv['tt'] != '' : |
||
395 | event.trouble_ticket = krgv['tt']
|
||
396 | history = EventHistory(type_action="Ticket change",idevent=event.idevent,value='',text='',username=request.environ.get('repoze.who.identity').get('repoze.who.userid')) |
||
397 | DBSession.add(history) |
||
398 | |||
399 | if events[0].status != krgv['status'] : |
||
400 | event.status = krgv['status']
|
||
401 | history = EventHistory(type_action="Acknowlegement change state",idevent=event.idevent,value='',text='',username=request.environ.get('repoze.who.identity').get('repoze.who.userid')) |
||
402 | DBSession.add(history) |
||
403 | |||
404 | flash(_('Updated successfully'))
|
||
405 | redirect(request.environ.get('HTTP_REFERER').split(request.environ.get('HTTP_HOST'))[1]) |