Ejemplo n.º 1
0
def alerts_add():
    name = request.form.get("name")
    entity_selection = request.form.get("entity-selection")
    host_groups = request.form.getlist("host-groups[]")
    hosts = request.form.getlist("hosts[]")
    checks = request.form.getlist("checks[]")
    services = request.form.getlist("services[]")
    plugins = request.form.getlist("plugins[]")
    from_states = request.form.getlist("from-states[]")
    to_states = request.form.getlist("to-states[]")
    module_selection = request.form.get("module-selection")

    if not name:
        return error_response("You must specify a name for this alert")

    if not from_states or not to_states:
        return error_response("You must specify at least one from state and at " "least one to state")

    if not module_selection:
        return error_response("You must select a module to use for this alert")

    alert = Alert(name=name, entity_selection_type=entity_selection, module=module_selection)
    session.add(alert)
    session.flush()
    for state in to_states:
        session.add(AlertTransitionTo(alert_id=alert.id, state=state))

    for state in from_states:
        session.add(AlertTransitionFrom(alert_id=alert.id, state=state))

    if entity_selection == "custom":
        added_entity = False
        for host_id in hosts:
            session.add(AlertCheckEntity(alert_id=alert.id, host_id=host_id))
            added_entity = True
        for host_group_id in host_groups:
            session.add(AlertCheckEntity(alert_id=alert.id, host_group_id=host_group_id))
            added_entity = True
        for service_id in services:
            session.add(AlertCheckEntity(alert_id=alert.id, service_id=service_id))
            added_entity = True

        if not added_entity:
            session.rollback()
            return error_response("You must select at least one entity to alert" " for state changes in")

    for check_id in checks:
        session.add(AlertRestrictToEntity(alert_id=alert.id, check_id=check_id))
    for plugin_id in plugins:
        session.add(AlertRestrictToEntity(alert_id=alert.id, plugin_id=plugin_id))

    for key in request.form.keys():
        if key.startswith("module-option-"):
            option_key = key.replace("module-option-", "", 1)
            option_value = request.form.get(key)
            session.add(AlertModuleOption(alert_id=alert.id, key=option_key, value=option_value))

    session.commit()
    return jsonify(success=True, message="Alert has been added successfully")
Ejemplo n.º 2
0
def services_edit():
    name = request.form.get("name")
    description = request.form.get("description")
    service_id = request.form.get("service-id")
    try:
        dependencies = json.loads(request.form.get("dependencies"))
    except ValueError:
        return error_response("Could not understand format of request sent")

    if not name.strip():
        return error_response("You must specify a name")

    try:
        service = Service.query.get(service_id)
        if not service:
            return error_response("Service could not be found")
        service.name = name
        service.description = description

        gs = RedundancyGroup.query.filter(RedundancyGroup.service_id == service.id)
        for g in gs:
            session.delete(g)

        ds = ServiceDependency.query.filter(ServiceDependency.service_id == service.id)
        for d in ds:
            session.delete(d)

        for dependency in dependencies["dependencies"]:
            if dependency["type"] == "host":
                sd = ServiceDependency(service_id=service.id, host_id=dependency["id"])
            else:
                sd = ServiceDependency(service_id=service.id, host_group_id=dependency["id"])
            session.add(sd)

        for redundancy_group in dependencies["redundancyGroups"]:
            rg = RedundancyGroup(service_id=service.id)
            session.add(rg)
            session.flush()
            sd = ServiceDependency(service_id=service.id, redundancy_group_id=rg.id)
            session.add(sd)
            for item in redundancy_group["items"]:
                if item["type"] == "host":
                    rgc = RedundancyGroupComponent(redundancy_group_id=rg.id, host_id=item["id"])
                else:
                    rgc = RedundancyGroupComponent(redundancy_group_id=rg.id, host_group_id=item["id"])
                session.add(rgc)

        session.commit()
    except ValueError:
        session.rollback()
        return error_response(
            "The data sent to the server could not be " "understood.  Please refresh the page and try again."
        )

    return jsonify(success=True, message="Service has been saved successfully")
Ejemplo n.º 3
0
def services_add():
    name = request.form.get("name")
    description = request.form.get("description")
    try:
        dependencies = json.loads(request.form.get("dependencies"))
    except ValueError:
        return error_response("Could not understand format of request sent")

    if not name.strip():
        return error_response("You must specify a name")

    try:
        service = Service(name=name, description=description)
        session.add(service)
        session.flush()

        for dependency in dependencies["dependencies"]:
            if dependency["type"] == "host":
                sd = ServiceDependency(service_id=service.id,
                    host_id=dependency["id"])
            else:
                sd = ServiceDependency(service_id=service.id,
                    host_group_id=dependency["id"])
            session.add(sd)

        for redundancy_group in dependencies["redundancyGroups"]:
            rg = RedundancyGroup(service_id=service.id)
            session.add(rg)
            session.flush()
            sd = ServiceDependency(service_id=service.id,
                redundancy_group_id=rg.id)
            session.add(sd)
            for item in redundancy_group["items"]:
                if item["type"] == "host":
                    rgc = RedundancyGroupComponent(redundancy_group_id=rg.id,
                        host_id=item["id"])
                else:
                    rgc = RedundancyGroupComponent(redundancy_group_id=rg.id,
                        host_group_id=item["id"])
                session.add(rgc)

        session.commit()
    except ValueError:
        session.rollback()
        return error_response("The data sent to the server could not be "
            "understood.  Please refresh the page and try again.")

    return jsonify(success=True, message="Service has been added successfully")
Ejemplo n.º 4
0
def scheduling_edit():
    schedule_id = request.form.get("schedule-id")
    name = request.form.get("name")
    description = request.form.get("description")
    interval_starts = request.form.getlist("interval-start[]")
    interval_values = request.form.getlist("interval-value[]")
    interval_units = request.form.getlist("interval-unit[]")
    checks = request.form.getlist("checks[]")

    if not name:
        return error_response("You must supply a name for this schedule")

    s = Schedule.query.get(schedule_id)
    if not s:
        abort(404)

    s.name = name
    s.description = description

    ScheduleCheck.query.filter(ScheduleCheck.schedule_id == s.id).delete()
    for check_id in checks:
        sc = ScheduleCheck(check_id=check_id, schedule_id=s.id)
        session.add(sc)

    ScheduleInterval.query.filter(
        ScheduleInterval.schedule_id == s.id).delete()
    for interval in zip(interval_starts, interval_values, interval_units):
        try:
            start_timestamp = datetime.strptime(interval[0], "%Y-%m-%d %H:%M:%S")
        except ValueError:
            session.rollback()
            return error_response("Start timestamp for interval was not "
                "understood")
        si = ScheduleInterval(schedule_id=s.id,
            start_timestamp=start_timestamp)
        try:
            si.set_interval(int(interval[1]), interval[2])
        except ValueError:
            session.rollback()
            return error_response("Interval must be an integer")
        session.add(si)

    session.commit()

    return jsonify(success=True,
        message="Schedule has been saved successfully")
Ejemplo n.º 5
0
def scheduling_edit():
    schedule_id = request.form.get("schedule-id")
    name = request.form.get("name")
    description = request.form.get("description")
    interval_starts = request.form.getlist("interval-start[]")
    interval_values = request.form.getlist("interval-value[]")
    interval_units = request.form.getlist("interval-unit[]")
    checks = request.form.getlist("checks[]")

    if not name:
        return error_response("You must supply a name for this schedule")

    s = Schedule.query.get(schedule_id)
    if not s:
        abort(404)

    s.name = name
    s.description = description

    ScheduleCheck.query.filter(ScheduleCheck.schedule_id == s.id).delete()
    for check_id in checks:
        sc = ScheduleCheck(check_id=check_id, schedule_id=s.id)
        session.add(sc)

    ScheduleInterval.query.filter(ScheduleInterval.schedule_id == s.id).delete()
    for interval in zip(interval_starts, interval_values, interval_units):
        try:
            start_timestamp = datetime.strptime(interval[0], "%Y-%m-%d %H:%M:%S")
        except ValueError:
            session.rollback()
            return error_response("Start timestamp for interval was not " "understood")
        si = ScheduleInterval(schedule_id=s.id, start_timestamp=start_timestamp)
        try:
            si.set_interval(int(interval[1]), interval[2])
        except ValueError:
            session.rollback()
            return error_response("Interval must be an integer")
        session.add(si)

    session.commit()

    return jsonify(success=True, message="Schedule has been saved successfully")
Ejemplo n.º 6
0
def alerts_edit():
    name = request.form.get("name")
    alert_id = request.form.get("alert-id")
    entity_selection = request.form.get("entity-selection")
    host_groups = request.form.getlist("host-groups[]")
    hosts = request.form.getlist("hosts[]")
    checks = request.form.getlist("checks[]")
    services = request.form.getlist("services[]")
    plugins = request.form.getlist("plugins[]")
    from_states = request.form.getlist("from-states[]")
    to_states = request.form.getlist("to-states[]")
    module_selection = request.form.get("module-selection")

    if not name:
        return error_response("You must specify a name for this alert")

    alert = Alert.query.get(alert_id)
    if not alert:
        abort(404)

    if not from_states or not to_states:
        return error_response("You must specify at least one from state and at "
            "least one to state")

    alert.name = name
    alert.entity_selection_type = entity_selection
    alert.module = module_selection

    AlertTransitionTo.query.filter(
        AlertTransitionTo.alert_id==alert_id).delete()
    AlertTransitionFrom.query.filter(
        AlertTransitionFrom.alert_id==alert_id).delete()
    AlertCheckEntity.query.filter(AlertCheckEntity.alert_id==alert_id).delete()
    AlertRestrictToEntity.query.filter(
        AlertRestrictToEntity.alert_id==alert_id).delete()

    for state in to_states:
        session.add(AlertTransitionTo(alert_id=alert.id, state=state))

    for state in from_states:
        session.add(AlertTransitionFrom(alert_id=alert.id, state=state))

    if entity_selection == "custom":
        added_entity = False
        for host_id in hosts:
            session.add(AlertCheckEntity(alert_id=alert.id, host_id=host_id))
            added_entity = True
        for host_group_id in host_groups:
            session.add(AlertCheckEntity(alert_id=alert.id,
                host_group_id=host_group_id))
            added_entity = True
        for service_id in services:
            session.add(AlertCheckEntity(alert_id=alert.id,
                service_id=service_id))
            added_entity = True

        if not added_entity:
            session.rollback()
            return error_response("You must select at least one entity to alert"
                " for state changes in")

    for check_id in checks:
        session.add(AlertRestrictToEntity(alert_id=alert.id, check_id=check_id))
    for plugin_id in plugins:
        session.add(AlertRestrictToEntity(alert_id=alert.id,
            plugin_id=plugin_id))

    AlertModuleOption.query.filter(
        AlertModuleOption.alert_id==alert.id).delete()
    for key in request.form.keys():
        if key.startswith("module-option-"):
            option_key = key.replace("module-option-", "", 1)
            option_value = request.form.get(key)
            session.add(AlertModuleOption(alert_id=alert.id, key=option_key,
                value=option_value))

    session.commit()
    return jsonify(success=True, message="Alert has been saved successfully")