Ejemplo n.º 1
0
def list_hosts(param):
    """List currently monitored hosts."""
    live = sites.live()

    default_columns = [
        'name',
        'address',
        'alias',
        'downtimes_with_info',
        'scheduled_downtime_depth',
    ]
    column_names = add_if_missing(param.get('columns', default_columns),
                                  ['name', 'address'])
    columns = verify_columns(Hosts, column_names)
    q = Query(columns)

    host_name = param.get('host_name')
    if host_name is not None:
        q = q.filter(Hosts.name.contains(host_name))

    host_alias = param.get('host_alias')
    if host_alias is not None:
        q = q.filter(Hosts.alias.contains(host_alias))

    in_downtime = param.get('in_downtime')
    if in_downtime is not None:
        q = q.filter(Hosts.scheduled_downtime_depth == int(in_downtime))

    acknowledged = param.get('acknowledged')
    if acknowledged is not None:
        q = q.filter(Hosts.acknowledged.equals(acknowledged))

    status = param.get('status')
    if status is not None:
        q = q.filter(Hosts.state.equals(status))

    result = q.iterate(live)

    return constructors.object_collection(
        name='all',
        domain_type='host',
        entries=[
            constructors.domain_object(
                domain_type='host',
                title=f"{entry['name']} ({entry['address']})",
                identifier=entry['name'],
                editable=False,
                deletable=False,
                extensions=entry,
            ) for entry in result
        ],
        base='',
    )
Ejemplo n.º 2
0
def _list_services(param):
    live = sites.live()

    q = Query([
        Services.host_name,
        Services.description,
        Services.last_check,
        Services.state,
        Services.state_type,
        Services.acknowledged,
    ])

    host_name = param.get('host_name')
    if host_name is not None:
        q = q.filter(Services.host_name.contains(host_name))

    alias = param.get('host_alias')
    if alias is not None:
        q = q.filter(Services.host_alias.contains(alias))

    in_downtime = param.get('in_downtime')
    if in_downtime is not None:
        q = q.filter(Services.scheduled_downtime_depth == int(in_downtime))

    acknowledged = param.get('acknowledged')
    if acknowledged is not None:
        q = q.filter(Services.acknowledged.equals(acknowledged))

    status = param.get('status')
    if status is not None:
        q = q.filter(Services.state.equals(status))

    result = q.iterate(live)

    return constructors.object_collection(
        name='all',
        domain_type='service',
        entries=[
            constructors.domain_object(
                domain_type='service',
                title=f"{entry['description']} on {entry['host_name']}",
                identifier=entry['description'],
                editable=False,
                deletable=False,
                extensions=dict(entry),
            ) for entry in result
        ],
        base='',
    )
Ejemplo n.º 3
0
def list_hosts(param):
    """Show hosts of specific condition"""
    live = sites.live()
    sites_to_query = param['sites']
    if sites_to_query:
        live.only_sites = sites_to_query

    columns = verify_columns(Hosts, param['columns'])
    q = Query(columns)

    # TODO: add sites parameter
    query_expr = param.get('query')
    if query_expr:
        q = q.filter(query_expr)

    result = q.iterate(live)

    return constructors.serve_json(
        constructors.collection_object(
            domain_type='host',
            value=[
                constructors.domain_object(
                    domain_type='host',
                    title=f"{entry['name']}",
                    identifier=entry['name'],
                    editable=False,
                    deletable=False,
                    extensions=entry,
                ) for entry in result
            ],
        ))
Ejemplo n.º 4
0
def list_hosts(param):
    live = sites.live()

    q = Query([
        Hosts.name,
        Hosts.address,
        Hosts.alias,
        Hosts.downtimes_with_info,
        Hosts.scheduled_downtime_depth,
    ])

    hostname = param.get('host_name')
    if hostname is not None:
        q = q.filter(Hosts.name.contains(hostname))

    alias = param.get('host_alias')
    if alias is not None:
        q = q.filter(Hosts.alias.contains(alias))

    in_downtime = param.get('in_downtime')
    if in_downtime is not None:
        q = q.filter(Hosts.scheduled_downtime_depth == int(in_downtime))

    acknowledged = param.get('acknowledged')
    if acknowledged is not None:
        q = q.filter(Hosts.acknowledged.equals(acknowledged))

    status = param.get('status')
    if status is not None:
        q = q.filter(Hosts.state.equals(status))

    result = q.iterate(live)

    return constructors.object_collection(
        name='all',
        domain_type='host',
        entries=[
            constructors.domain_object(
                domain_type='host',
                title=f"{entry['name']} ({entry['address']})",
                identifier=entry['name'],
                editable=False,
                deletable=False,
            ) for entry in result
        ],
        base='',
    )
Ejemplo n.º 5
0
def delete_downtime(params):
    live = sites.live()

    q = Query([Downtimes.id, Downtimes.is_service])

    q = q.filter(Downtimes.id.contains(params['downtime_id']))
    gen_downtime = q.iterate(live)
    downtime_info = next(gen_downtime)
    downtime_type = "SVC" if downtime_info['is_service'] else "HOST"
    command_delete = remove_downtime_command(downtime_type,
                                             params['downtime_id'])
    execute_livestatus_command(command_delete, params['host_name'])
    return Response(status=204)