Exemple #1
0
def custom_view(request, viewname):
    c = {}
    c['messages'] = []
    c['errors'] = []


    # View management
    user = userdata.User(request)
    c['viewname'] = viewname
    if not user.views:
        user.views = {}
        user.save()
        raise Http404(_("You don't have any view defined."))
    if viewname not in user.views.keys():
        raise Http404(_("This view doesn't exist."))
    view = user.views[viewname]

    # Data pre-processing
    livestatus = utils.livestatus(request)
    livestatus_query = custom_utils.data_to_query(view)

    # Query execution
    try:
        # the split is a workaround for pynag and the 'Stats:' clause
        c['data'] = livestatus.query(*livestatus_query.split('\n'))
    except Exception as e:
        # Any exception in livestatus query here is a critical error
        error = _('Error in LiveStatus query')
        c['errors'].append(error)
        c['errors'].append(e.message)
        return error_page(request, c)

    # Data post-processing
    # sorting
    c['data'] = sorted(c['data'],
                       key=functools.partial(custom_utils.sort_data,
                                             sorts=view['sorts']))
    
    # Moaaaar data for our templates
    c['view'] = view

    # For the template 'table.html' (and whynot others), determine if displaying
    # action_buttons is relevant, i.e. if we have results with at least
    # host_name and service_description.
    columns = set()
    for col in view['columns']: # no set comprehension in 2.6 :(
        columns.add(col['name'])

    if 'host_name' in columns and 'description' in columns:
        c['action_buttons'] = True
    else:
        c['action_buttons'] = False

    template = view['metadata'][0].get('template', 'table.html')

    return render_to_response(adagios.settings.CUSTOM_TEMPLATES_DIR + template, c,
                              context_instance=RequestContext(request))
Exemple #2
0
def custom_view(request, viewname):
    c = {}
    c['messages'] = []
    c['errors'] = []


    # View management
    user = userdata.User(request)
    c['viewname'] = viewname
    if not user.views:
        user.views = {}
        user.save()
        raise Http404(_("You don't have any view defined."))
    if viewname not in user.views.keys():
        raise Http404(_("This view doesn't exist."))
    view = user.views[viewname]

    # Data pre-processing
    livestatus = utils.livestatus(request)
    livestatus_query = custom_utils.data_to_query(view)

    # Query execution
    try:
        # the split is a workaround for pynag and the 'Stats:' clause
        c['data'] = livestatus.query(*livestatus_query.split('\n'))
    except Exception as e:
        # Any exception in livestatus query here is a critical error
        error = _('Error in LiveStatus query')
        c['errors'].append(error)
        c['errors'].append(e.message)
        return error_page(request, c)

    # Data post-processing
    # sorting
    c['data'] = sorted(c['data'],
                       key=functools.partial(custom_utils.sort_data,
                                             sorts=view['sorts']))
    
    # Moaaaar data for our templates
    c['view'] = view

    template = view['metadata'][0].get('template', 'table.html')

    return render_to_response(adagios.settings.CUSTOM_TEMPLATES_DIR + template, c,
                              context_instance=RequestContext(request))
Exemple #3
0
def service_detail(request, host_name, service_description):
    """ Displays status details for one host or service """
    c = {}
    c['messages'] = []
    c['errors'] = []

    livestatus = utils.livestatus(request)
    backend = request.GET.get('backend')
    c['pnp_url'] = adagios.settings.pnp_url
    c['nagios_url'] = adagios.settings.nagios_url
    c['request'] = request
    now = time.time()
    seconds_in_a_day = 60 * 60 * 24
    seconds_passed_today = now % seconds_in_a_day
    today = now - seconds_passed_today  # midnight of today

    try:
        c['host'] = my_host = livestatus.get_host(host_name, backend)
        my_host['object_type'] = 'host'
        my_host['short_name'] = my_host['name']
    except IndexError:
        c['errors'].append(_("Could not find any host named '%s'") % host_name)
        return error_page(request, c)

    if service_description is None:
        tmp = request.GET.get('service_description')
        if tmp is not None:
            return service_detail(request, host_name, service_description=tmp)
        primary_object = my_host
        c['service_description'] = '_HOST_'
        #c['log'] = pynag.Parsers.LogFiles(maincfg=adagios.settings.nagios_config).get_state_history(
        #    host_name=host_name, service_description=None)
    else:
        try:
            c['service'] = my_service = livestatus.get_service(
                host_name, service_description, backend=backend)
            my_service['object_type'] = 'service'
            c['service_description'] = service_description
            my_service['short_name'] = "%s/%s" % (
                my_service['host_name'], my_service['description'])
            primary_object = my_service
            #c['log'] = pynag.Parsers.LogFiles(maincfg=adagios.settings.nagios_config).get_state_history(
            #    host_name=host_name, service_description=service_description)
        except IndexError:
            c['errors'].append(
                _("Could not find any service named '%s'") % service_description)
            return error_page(request, c)

    c['my_object'] = primary_object
    c['object_type'] = primary_object['object_type']

    # Friendly statusname (i.e. turn 2 into "critical")
    primary_object['status'] = state[primary_object['state']]

    # Plugin longoutput comes to us with special characters escaped. lets undo
    # that:
    primary_object['long_plugin_output'] = primary_object[
        'long_plugin_output'].replace('\\n', '\n')

    # Service list on the sidebar should be sorted
    my_host['services_with_info'] = sorted(
        my_host.get('services_with_info', []))
    c['host_name'] = host_name

    perfdata = primary_object['perf_data']
    perfdata = pynag.Utils.PerfData(perfdata)
    for i, datum in enumerate(perfdata.metrics):
        datum.i = i
        try:
            datum.status = state[datum.get_status()]
        except pynag.Utils.PynagError:
            datum.status = state[3]
    c['perfdata'] = perfdata.metrics
    
    # Get a complete list of network parents
    try:
        c['network_parents'] = reversed(_get_network_parents(request, host_name))
    except Exception, e:
        c['errors'].append(e)
Exemple #4
0
def service_detail(request, host_name, service_description):
    """ Displays status details for one host or service """
    c = {}
    c['messages'] = []
    c['errors'] = []

    livestatus = utils.livestatus(request)
    backend = request.GET.get('backend')
    c['pnp_url'] = adagios.settings.pnp_url
    c['nagios_url'] = adagios.settings.nagios_url
    c['request'] = request
    now = time.time()
    seconds_in_a_day = 60 * 60 * 24
    seconds_passed_today = now % seconds_in_a_day
    today = now - seconds_passed_today  # midnight of today

    try:
        c['host'] = my_host = livestatus.get_host(host_name, backend)
        my_host['object_type'] = 'host'
        my_host['short_name'] = my_host['name']
    except IndexError:
        c['errors'].append(_("Could not find any host named '%s'") % host_name)
        return error_page(request, c)

    if service_description is None:
        tmp = request.GET.get('service_description')
        if tmp is not None:
            return service_detail(request, host_name, service_description=tmp)
        primary_object = my_host
        c['service_description'] = '_HOST_'
    else:
        try:
            c['service'] = my_service = livestatus.get_service(
                host_name, service_description, backend=backend)
            my_service['object_type'] = 'service'
            c['service_description'] = service_description
            my_service['short_name'] = "%s/%s" % (my_service['host_name'],
                                                  my_service['description'])
            primary_object = my_service
        except IndexError:
            c['errors'].append(
                _("Could not find any service named '%s'") %
                service_description)
            return error_page(request, c)

    c['my_object'] = primary_object
    c['object_type'] = primary_object['object_type']

    # Friendly statusname (i.e. turn 2 into "critical")
    primary_object['status'] = state[primary_object['state']]

    # Plugin longoutput comes to us with special characters escaped. lets undo
    # that:
    primary_object['long_plugin_output'] = primary_object[
        'long_plugin_output'].replace('\\n', '\n')

    # Service list on the sidebar should be sorted
    my_host['services_with_info'] = sorted(
        my_host.get('services_with_info', []))
    c['host_name'] = host_name

    perfdata = primary_object['perf_data']
    perfdata = pynag.Utils.PerfData(perfdata)
    for i, datum in enumerate(perfdata.metrics):
        datum.i = i
        try:
            datum.status = state[datum.get_status()]
        except pynag.Utils.PynagError:
            datum.status = state[3]
    c['perfdata'] = perfdata.metrics

    # Get a complete list of network parents
    try:
        c['network_parents'] = reversed(
            _get_network_parents(request, host_name))
    except Exception as e:
        c['errors'].append(e)

    # Lets get some graphs
    if adagios.settings.enable_pnp4nagios:
        try:
            tmp = run_pnp("json", host=host_name)
            tmp = json.loads(tmp)
        except Exception as e:
            tmp = []
            c['pnp4nagios_error'] = e
        c['graph_urls'] = tmp

    if adagios.settings.enable_graphite:
        metrics = [x.label for x in perfdata.metrics]
        service = c['service_description'].replace(' ', '_')
        c['graphite'] = graphite.get(
            adagios.settings.graphite_url,
            c['host_name'],
            service,
            metrics,
            adagios.settings.GRAPHITE_PERIODS,
        )
        # used in the General tab - preview
        for graph in c['graphite']:
            if graph['css_id'] == adagios.settings.GRAPHITE_DEFAULT_TAB:
                default = {}
                for k, v in list(graph['metrics'].items()):
                    default[k] = v
                c['graphite_default'] = default

    return render_to_response('status_detail.html',
                              c,
                              context_instance=RequestContext(request))
Exemple #5
0
def status_detail(request, host_name=None, service_description=None):
    """ Displays status details for one host or service """
    c = {}
    c['messages'] = []
    c['errors'] = []

    host_name = request.GET.get('host_name')
    service_description = request.GET.get('service_description')
    livestatus = utils.livestatus(request)
    c['pnp_url'] = adagios.settings.pnp_url
    c['nagios_url'] = adagios.settings.nagios_url
    c['request'] = request
    now = time.time()
    seconds_in_a_day = 60 * 60 * 24
    seconds_passed_today = now % seconds_in_a_day
    today = now - seconds_passed_today  # midnight of today

    try:
        c['host'] = my_host = livestatus.get_host(host_name)
        my_host['object_type'] = 'host'
        my_host['short_name'] = my_host['name']
    except IndexError:
        c['errors'].append("Could not find any host named '%s'" % host_name)
        return error_page(request, c)

    if service_description is None:
        tmp = request.GET.get('service_description')
        if tmp is not None:
            return status_detail(request, host_name, service_description=tmp)
        primary_object = my_host
        c['service_description'] = '_HOST_'
        c['log'] = pynag.Parsers.LogFiles(maincfg=adagios.settings.nagios_config).get_state_history(
            host_name=host_name, service_description=None)

    else:
        try:
            c['service'] = my_service = livestatus.get_service(
                host_name, service_description)
            my_service['object_type'] = 'service'
            c['service_description'] = service_description
            my_service['short_name'] = "%s/%s" % (
                my_service['host_name'], my_service['description'])
            primary_object = my_service
            c['log'] = pynag.Parsers.LogFiles(maincfg=adagios.settings.nagios_config).get_state_history(
                host_name=host_name, service_description=service_description)
        except IndexError:
            c['errors'].append(
                "Could not find any service named '%s'" % service_description)
            return error_page(request, c)

    c['my_object'] = primary_object
    c['object_type'] = primary_object['object_type']

    # Friendly statusname (i.e. turn 2 into "critical")
    primary_object['status'] = state[primary_object['state']]

    # Plugin longoutput comes to us with special characters escaped. lets undo
    # that:
    primary_object['long_plugin_output'] = primary_object[
        'long_plugin_output'].replace('\\n', '\n')

    # Service list on the sidebar should be sorted
    my_host['services_with_info'] = sorted(
        my_host.get('services_with_info', []))
    c['host_name'] = host_name

    perfdata = primary_object['perf_data']
    perfdata = pynag.Utils.PerfData(perfdata)
    for i, datum in enumerate(perfdata.metrics):
        datum.i = i
        try:
            datum.status = state[datum.get_status()]
        except pynag.Utils.PynagError:
            datum.status = state[3]
    c['perfdata'] = perfdata.metrics

    # Get a complete list of network parents
    try:
        c['network_parents'] = reversed(_get_network_parents(host_name))
    except Exception, e:
        c['errors'].append(e)