Пример #1
0
def _set_acknowlegement_on_queried_hosts(
    connection,
    query: str,
    sticky: bool,
    notify: bool,
    persistent: bool,
    comment: str,
):
    q = Query([Hosts.name,
               Hosts.state]).filter(tree_to_expr(query, Hosts.__tablename__))
    hosts = list(q.iterate(connection))

    if not hosts:
        return problem(status=404,
                       title="The provided query returned no monitored hosts")

    for host in hosts:
        if host.state == 0:
            continue
        acknowledge_host_problem(
            connection,
            host.name,
            sticky=sticky,
            notify=notify,
            persistent=persistent,
            user=_user_id(),
            comment=comment,
        )

    return http.Response(status=204)
Пример #2
0
def list_service_downtimes(param):
    """Show all scheduled downtimes"""
    live = sites.live()

    q = Query([
        Downtimes.id,
        Downtimes.host_name,
        Downtimes.service_description,
        Downtimes.is_service,
        Downtimes.author,
        Downtimes.start_time,
        Downtimes.end_time,
        Downtimes.recurring,
        Downtimes.comment,
    ])

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

    service_description = param.get('service_description')
    if service_description is not None:
        q = q.filter(
            Downtimes.service_description.contains(service_description))

    gen_downtimes = q.iterate(live)
    return _serve_downtimes(gen_downtimes)
Пример #3
0
def _list_services(param):
    live = sites.live()

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

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

    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='service',
            value=[
                constructors.domain_object(
                    domain_type='service',
                    title=f"{entry['description']} on {entry['host_name']}",
                    identifier=entry['description'],
                    editable=False,
                    deletable=False,
                    extensions=entry,
                ) for entry in result
            ],
        ))
Пример #4
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
            ],
        ))
Пример #5
0
def schedule_services_downtimes_with_query(
    connection,
    query: QueryExpression,
    start_time: dt.datetime,
    end_time: dt.datetime,
    recur: RecurMode = 'fixed',
    duration: int = 0,
    user_id: str = '',
    comment: str = '',
):
    """Schedule downtimes for services based upon a query"""

    q = Query(
        [Services.description, Services.host_name],
        query,
    )
    for host_name, service_description in [
        (row['host_name'], row['description']) for row in q.iterate(connection)
    ]:
        if not comment:
            downtime_comment = f"Downtime for service {service_description}@{host_name}"
        else:
            downtime_comment = comment

        schedule_service_downtime(
            connection,
            host_name=host_name,
            service_description=service_description,
            start_time=start_time,
            end_time=end_time,
            recur=recur,
            duration=duration,
            user_id=user_id,
            comment=downtime_comment,
        )
Пример #6
0
def show_downtimes(param):
    """Show all scheduled downtimes"""
    live = sites.live()
    sites_to_query = param.get('sites')
    if sites_to_query:
        live.only_sites = sites_to_query

    q = Query([
        Downtimes.id,
        Downtimes.host_name,
        Downtimes.service_description,
        Downtimes.is_service,
        Downtimes.author,
        Downtimes.start_time,
        Downtimes.end_time,
        Downtimes.recurring,
        Downtimes.comment,
    ])

    filter_tree = param.get('query')
    host_name = param.get('host_name')
    service_description = param.get('service_description')
    if filter_tree is not None:
        expr = tree_to_expr(filter_tree)
        q = q.filter(expr)

    if host_name is not None:
        q = q.filter(Downtimes.host_name.contains(host_name))

    if service_description is not None:
        q = q.filter(
            Downtimes.service_description.contains(service_description))

    gen_downtimes = q.iterate(live)
    return _serve_downtimes(gen_downtimes)
Пример #7
0
def schedule_hosts_downtimes_with_query(
    connection,
    query: QueryExpression,
    start_time: dt.datetime,
    end_time: dt.datetime,
    include_all_services=False,
    recur: RecurMode = 'fixed',
    duration: int = 0,
    user_id: str = '',
    comment: str = '',
):
    """Schedule a downtimes for hosts based upon a query"""

    q = Query([Hosts.name]).filter(query)
    hosts = [row['name'] for row in q.iterate(connection)]
    if not comment:
        comment = f"Downtime for hosts {', '.join(hosts)}"

    schedule_host_downtime(
        connection,
        host_name=hosts,
        start_time=start_time,
        end_time=end_time,
        include_all_services=include_all_services,
        recur=recur,
        duration=duration,
        user_id=user_id,
        comment=comment,
    )
Пример #8
0
def schedule_hosts_downtimes_with_query(
    connection,
    query: str,
    start_time: dt.datetime,
    end_time: dt.datetime,
    recur: RecurMode = 'fixed',
    duration: int = 0,
    user_id: str = '',
    comment: str = '',
):
    """Schedule a downtimes for hosts based upon a query"""

    q = Query([Hosts.name]).filter(tree_to_expr(query, Hosts.__tablename__))
    hosts = [row['name'] for row in q.iterate(connection)]
    if not comment:
        comment = f"Downtime for hosts {', '.join(hosts)}"

    schedule_host_downtime(
        connection,
        host_name=hosts,
        start_time=start_time,
        end_time=end_time,
        recur=recur,
        duration=duration,
        user_id=user_id,
        comment=comment,
    )
Пример #9
0
def delete_downtime_with_query(connection, query):
    """Delete scheduled downtimes based upon a query"""
    q = Query([Downtimes.id, Downtimes.is_service]).filter(query)
    for downtime_id, is_service in [(row['id'], row['is_service'])
                                    for row in q.iterate(connection)]:
        if is_service:
            del_service_downtime(connection, downtime_id)
        else:
            del_host_downtime(connection, downtime_id)
Пример #10
0
def _list_services(param):
    live = sites.live()

    default_columns = [
        'host_name',
        'description',
        'last_check',
        'state',
        'state_type',
        'acknowledged',
    ]
    column_names = add_if_missing(param.get('columns', default_columns),
                                  ['host_name', 'description'])
    columns = verify_columns(Services, column_names)
    q = Query(columns)

    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=entry,
            ) for entry in result
        ],
        base='',
    )
Пример #11
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='',
    )
Пример #12
0
def set_acknowledgement_on_service_related(params):
    """Set acknowledgement on related services"""
    body = params['body']
    live = sites.live()

    sticky = body['sticky']
    notify = body['notify']
    persistent = body['persistent']
    comment = body['comment']

    acknowledge_type = body['acknowledge_type']

    if acknowledge_type == 'service':
        return _set_acknowledgement_for_service(
            live,
            unquote(body['service_description']),
            sticky,
            notify,
            persistent,
            comment,
        )

    if acknowledge_type == 'servicegroup':
        acknowledge_servicegroup_problem(
            live,
            body['servicegroup_name'],
            sticky=sticky,
            notify=notify,
            persistent=persistent,
            user=_user_id(),
            comment=comment,
        )
        return http.Response(status=204)

    if acknowledge_type == 'service_by_query':
        q = Query([Services.host_name, Services.description,
                   Services.state]).filter(
                       tree_to_expr(body['query'], Services.__tablename__))
        return _set_acknowledgement_on_queried_services(
            live,
            [(row['host_name'], row['description'])
             for row in q.iterate(live) if row['state'] > 0],
            sticky=sticky,
            notify=notify,
            persistent=persistent,
            comment=comment,
        )

    return problem(
        status=400,
        title="Unhandled acknowledge-type.",
        detail=f"The acknowledge-type {acknowledge_type!r} is not supported.")
Пример #13
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)
Пример #14
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='',
    )
Пример #15
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='',
    )
Пример #16
0
def _set_acknowledgement_for_service(
    connection,
    service_description: str,
    sticky: bool,
    notify: bool,
    persistent: bool,
    comment: str,
):
    q = Query([Services.host_name, Services.description,
               Services.state]).filter(
                   tree_to_expr({
                       'op': '=',
                       'left': 'services.description',
                       'right': service_description
                   }))
    services = list(q.iterate(connection))

    if not len(services):
        return problem(
            status=404,
            title=f'No services with {service_description!r} were found.',
        )

    for service in services:
        if service.state == 0:
            continue
        acknowledge_service_problem(
            connection,
            service.host_name,
            service.description,
            sticky=sticky,
            notify=notify,
            persistent=persistent,
            user=_user_id(),
            comment=comment,
        )

    return http.Response(status=204)