def add_downtime(): """ Add a downtime """ user = request.environ['beaker.session']['current_user'] target_user = request.environ['beaker.session']['target_user'] datamgr = request.environ['beaker.session']['datamanager'] user_id = user.id if not target_user.is_anonymous(): user_id = target_user.id livestate_ids = request.forms.getall('livestate_id') if not livestate_ids: logger.error("request to send an downtime: missing livestate_id parameter!") return webui.response_invalid_parameters(_('Missing livestate identifier: livestate_id')) problem = False status = "" for livestate_id in livestate_ids: livestate = datamgr.get_livestate({'where': {'_id': livestate_id}}) if not livestate: status += _('Livestate element %s does not exist') % livestate_id continue livestate = livestate[0] # Prepare post request ... data = { 'action': 'add', 'host': livestate.host.id, 'service': None, 'user': user_id, 'start_time': request.forms.get('start_time'), 'end_time': request.forms.get('end_time'), 'fixed': request.forms.get('fixed', 'false') == 'true', 'duration': int(request.forms.get('duration', '86400')), 'comment': request.forms.get('comment', _('No comment')) } if livestate.service != 'service': data.update({'service': livestate.service.id}) logger.critical("Request a downtime, data: %s", data) if not datamgr.add_downtime(data=data): status += _("Failed adding a downtime for %s") % livestate.name problem = True else: status += _('downtime sent for %s.') % livestate.name logger.info("Request a downtime, result: %s", status) if not problem: return webui.response_ok(message=status) else: return webui.response_ko(message=status)
def show_recheck_add(): """ Show form to request a forced check """ return { 'title': request.query.get('title', _('Send a check request')), 'livestate_id': request.query.getall('livestate_id'), 'element_name': request.query.getall('element_name'), 'comment': request.query.get('comment', _('Re-check requested from WebUI')), 'read_only': request.query.get('read_only', '0') == '1', 'auto_post': request.query.get('auto_post', '0') == '1' }
def get_html_business_impact(business_impact, icon=True, text=False): """ Give a business impact as text and stars if needed. If text=True, returns text+stars, else returns stars only ... """ if not 0 <= business_impact <= 5: return 'n/a - value' if not icon and not text: return 'n/a - parameters' bi_texts = { 0: _('None'), 1: _('Low'), 2: _('Normal'), 3: _('Important'), 4: _('Very important'), 5: _('Business critical') } # nb_stars = max(0, business_impact - 2) stars = '<i class="fa fa-star text-primary"></i>' * business_impact if not text: return stars if not icon: return bi_texts.get(business_impact, _('Unknown')) text = "%s %s" % (bi_texts.get(business_impact, _('Unknown')), stars) return text.strip()
def add_recheck(): """ Request a forced check """ user = request.environ['beaker.session']['current_user'] target_user = request.environ['beaker.session']['target_user'] datamgr = request.environ['beaker.session']['datamanager'] user_id = user.id if not target_user.is_anonymous(): user_id = target_user.id livestate_ids = request.forms.getall('livestate_id') if not livestate_ids: logger.error("request to send an recheck: missing livestate_id parameter!") return webui.response_invalid_parameters(_('Missing livestate identifier: livestate_id')) problem = False status = "" for livestate_id in livestate_ids: livestate = datamgr.get_livestate({'where': {'_id': livestate_id}}) if not livestate: status += _('Livestate element %s does not exist') % livestate_id continue livestate = livestate[0] # Prepare post request ... data = { 'host': livestate.host.id, 'service': None, 'user': user_id, 'comment': request.forms.get('comment', _('No comment')) } if livestate.service != 'service': data.update({'service': livestate.service.id}) if not datamgr.add_recheck(data=data): status += _("Failed adding a check request for %s") % livestate.name problem = True else: status += _('Check request sent for %s.') % livestate.name logger.info("Request a re-check, result: %s", status) if not problem: return webui.response_ok(message=status) else: return webui.response_ko(message=status)
def show_acknowledge_add(): """ Show form to add an acknowledge """ return { 'title': request.query.get('title', _('Request an acknowledge')), 'action': request.query.get('action', 'add'), 'livestate_id': request.query.getall('livestate_id'), 'element_name': request.query.getall('element_name'), 'sticky': request.query.get('sticky', '1') == '1', 'notify': request.query.get('notify', '0') == '1', 'persistent': request.query.get('persistent', '1') == '1', 'comment': request.query.get('comment', _('Acknowledge requested from WebUI')), 'read_only': request.query.get('read_only', '0') == '1', 'auto_post': request.query.get('auto_post', '0') == '1' }
def get_realm(realm_id): """ Display the element linked to a realm item """ datamgr = request.environ['beaker.session']['datamanager'] realm = datamgr.get_realm({'where': {'_id': realm_id}}) if not realm: # pragma: no cover, should not happen return webui.response_invalid_parameters( _('Realm element does not exist')) return { 'realm_id': realm_id, 'realm': realm, 'title': request.query.get('title', _('Realm view')) }
def show_downtime_add(): """ Show form to add a downtime """ return { 'title': request.query.get('title', _('Request a downtime')), 'action': request.query.get('action', 'add'), 'livestate_id': request.query.getall('livestate_id'), 'element_name': request.query.getall('element_name'), 'start_time': request.query.get('start_time'), 'end_time': request.query.get('end_time'), 'fixed': request.query.get('fixed', '1') == '1', 'duration': request.query.get('duration', 86400), 'comment': request.query.get('comment', _('Downtime requested from WebUI')), 'read_only': request.query.get('read_only', '0') == '1', 'auto_post': request.query.get('auto_post', '0') == '1' }
def get_on_off(status=False, title=None, message=''): """ Give an enabled/disabled state based on glyphicons with optional title and message """ if not title: title = _('Enabled') if status else _('Disabled') if isinstance(title, list): if status: title = title[0] else: title = title[1] if status: return '''<i title="%s" class="fa fa-fw fa-check text-success">%s</i>''' % ( title, message ) else: return '''<i title="%s" class="fa fa-fw fa-close text-danger">%s</i>''' % ( title, message )
def get_hostgroup(hostgroup_id): """ Display the element linked to a hostgroup item """ datamgr = request.environ['beaker.session']['datamanager'] hostgroup = datamgr.get_hostgroup(hostgroup_id) if not hostgroup: # pragma: no cover, should not happen return webui.response_invalid_parameters( _('Hosts group element does not exist')) return get_hostgroups_table()
def get_livestate(livestate_id): """ Display the element linked to a livestate item """ datamgr = request.environ['beaker.session']['datamanager'] livestate = datamgr.get_livestate({'where': {'_id': livestate_id}}) if not livestate: return webui.response_invalid_parameters(_('Livestate element does not exist')) livestate = livestate[0] if livestate.type == 'host': redirect('/host/' + livestate.host.id) else: redirect('/host/' + livestate.host.id + '#services')
def get_userrestrictroles(): """ Show list of users """ user = request.environ['beaker.session']['current_user'] target_user = request.environ['beaker.session']['target_user'] datamgr = request.environ['beaker.session']['datamanager'] username = user.get_username() if not target_user.is_anonymous(): username = target_user.get_username() # Fetch elements per page preference for user, default is 25 elts_per_page = datamgr.get_user_preferences(username, 'elts_per_page', 25) elts_per_page = elts_per_page['value'] # Pagination and search start = int(request.query.get('start', '0')) count = int(request.query.get('count', elts_per_page)) where = webui.helper.decode_search(request.query.get('search', '')) search = { 'page': start // (count + 1), 'max_results': count, 'where': where } # Get userrestrictroles userrestrictroles = datamgr.get_userrestrictroles(search) # Get last total elements count total = datamgr.get_objects_count('userrestrictrole', search=where, refresh=True, log=True) count = min(count, total) return { 'userrestrictroles': userrestrictroles, 'pagination': webui.helper.get_pagination_control('/userrestrictroles', total, start, count), 'title': request.query.get('title', _('All users roles')) }
def get_servicegroup_members(servicegroup_id): """ Get the servicegroup services list """ datamgr = request.environ['beaker.session']['datamanager'] servicegroup = datamgr.get_servicegroup(servicegroup_id) if not servicegroup: # pragma: no cover, should not happen return webui.response_invalid_parameters( _('Services group element does not exist')) # Not JSON serializable! # items = servicegroup.members items = [] for service in servicegroup.members: lv_service = datamgr.get_livestate( {'where': { 'type': 'service', 'service': service.id }}) lv_service = lv_service[0] title = "%s - %s (%s)" % (lv_service.status, Helper.print_duration( lv_service.last_check, duration_only=True, x_elts=0), lv_service.output) items.append({ 'id': service.id, 'name': service.name, 'alias': service.alias, 'icon': lv_service.get_html_state(text=None, title=title), 'url': lv_service.get_html_link() }) response.status = 200 response.content_type = 'application/json' return json.dumps(items)
def get_currently(): """ Display currently page """ user = request.environ['beaker.session']['current_user'] target_user = request.environ['beaker.session']['target_user'] datamgr = request.environ['beaker.session']['datamanager'] username = user.get_username() if not target_user.is_anonymous(): username = target_user.get_username() # Get the stored panels panels = datamgr.get_user_preferences(username, 'panels', {'panels': {}}) # Get the stored graphs graphs = datamgr.get_user_preferences(username, 'graphs', {'graphs': {}}) return { 'panels': panels, 'graphs': graphs, 'title': request.query.get('title', _('Dashboard')) }