Beispiel #1
0
    def delete(self, jwt, service_id):
        if service_id is None:
            return jsonify(error=True)

        service = Service.get_by_id(service_id)
        if service is None:
            return jsonify(error=True)
        else:
            try:
                Service.delete_all_hosts_relations(service_id)
                Service.delete_all_contacts_relations(service_id)
                Service.delete_all_contactgroups_relations(service_id)
                Service.delete_all_servicegroups_relations(service_id)
                Service.delete_all_servicetemplate_relations(service_id)

                service = Service.get_by_id(service_id)
                deleteNagiosServicesConfigFile(service)
                service.delete()

                if not restartNagios():
                    syncNagiosAllConfigWithDb()
                    return jsonify(error=True, msg="Invalid process")

                return jsonify(error=False)
            except Exception as e:
                syncNagiosAllConfigWithDb()
                return jsonify(error=True, msg=str(e))
Beispiel #2
0
    def get(self, jwt, service_id):
        data = []

        # If no service_id is passed in get all services.
        if service_id is None:
            services = Service.get_all()
        else:
            if Service.get_by_id(service_id) is not None:
                services = [Service.get_by_id(service_id)]
            else:
                services = None

        # Loop over results and get json form of service to return.
        if services is not None:
            for service in services:
                data.append(service.serialize())
                pass
            return jsonify(data=data)
        else:
            return jsonify(error=True, msg="Service does not exist.")
Beispiel #3
0
    def delete(self, jwt, contactgroup_id):
        if contactgroup_id is None:
            return jsonify(error=True)

        contactgroup = ContactGroup.get_by_id(contactgroup_id)
        if contactgroup is None:
            return jsonify(error=True)
        else:
            try:
                deleteNagiosContactGroupsConfigFile(contactgroup)
                db.session.delete(contactgroup)

                # process contact_contactgroup table
                relations = Contact2Group.query.filter_by(contactgroup_id=contactgroup_id).all()
                relation_contact_ids = []
                if relations is not None:
                    for relation in relations:
                        relation_contact_ids.append(relation.contact_id)

                # delete from contact_contactgroup table
                connection = db.session.connection()
                connection.execute("DELETE FROM contact_contactgroup WHERE contactgroup_id = '%s'", (contactgroup_id))

                # update contact table
                for relation_contact_id in relation_contact_ids:
                    contact = Contact.get_by_id(relation_contact_id)
                    if contact is None:
                        continue

                    connection = db.session.connection()
                    result = connection.execute(
                        "SELECT GROUP_CONCAT(B.contactgroup_name) contactgroup_names FROM contact_contactgroup A" +
                        " LEFT JOIN contactgroups B ON A.contactgroup_id=B.id" +
                        " WHERE A.contact_id = '%s'" +
                        " GROUP BY A.contact_id"
                        , (contact.id))
                    if len(result._saved_cursor._result.rows) == 0:
                        contact.contactgroups = ''
                        writeNagiosContactsConfigFile(contact)
                    else:
                        for row in result:
                            contactgroup_names_str = row['contactgroup_names']
                            contact.contactgroups = contactgroup_names_str
                            writeNagiosContactsConfigFile(contact)
                            break

                # process contactgroup_service table
                csrelations = ContactgroupService.query.filter_by(contactgroup_id=contactgroup_id).all()
                relation_service_ids = []
                if csrelations is not None:
                    for csrelation in csrelations:
                        relation_service_ids.append(csrelation.service_id)

                # delete from contactgroup_service table
                connection = db.session.connection()
                connection.execute("DELETE FROM contactgroup_service WHERE contactgroup_id = '%s'", (contactgroup_id))

                # update service table
                for relation_service_id in relation_service_ids:
                    service = Service.get_by_id(relation_service_id)
                    if service is None:
                        continue

                    connection = db.session.connection()
                    result = connection.execute(
                        "SELECT GROUP_CONCAT(B.contactgroup_name) contactgroup_names FROM contactgroup_service A" +
                        " LEFT JOIN contactgroups B ON A.contactgroup_id=B.id" +
                        " WHERE A.service_id = '%s'" +
                        " GROUP BY A.service_id"
                        , (service.id))
                    if len(result._saved_cursor._result.rows) == 0:
                        service.contact_groups = ''
                        tmp_checkInterval = service.check_interval
                        service.check_interval = round(int(service.check_interval) / 60, 1)
                        writeNagiosServicesConfigFile(service)
                        service.check_interval = tmp_checkInterval
                    else:
                        for row in result:
                            contactgroup_names_str = row['contactgroup_names']
                            service.contact_groups = contactgroup_names_str
                            tmp_checkInterval = service.check_interval
                            service.check_interval = round(int(service.check_interval) / 60, 1)
                            writeNagiosServicesConfigFile(service)
                            service.check_interval = tmp_checkInterval
                            break

                if restartNagios() == False:
                    db.session.rollback()
                    syncNagiosAllConfigWithDb()
                    return jsonify(error=True, msg="Invalid process")
                    
                db.session.commit()
                return jsonify(error=False)
            except Exception as e:
                db.session.rollback()
                syncNagiosAllConfigWithDb()
                return jsonify(error=True, msg=str(e))
            finally:
                db.session.close()
        return jsonify(error=True)
Beispiel #4
0
    def put(self, jwt, service_id):
        OID_CPU_LOAD_1 = "laLoad.1"
        OID_CPU_LOAD_5 = "laLoad.2"
        OID_CPU_LOAD_15 = "laLoad.3"

        OID_PHYSICAL_MEM = "hrStorageSize.1"
        OID_VIRTUAL_MEM = "hrStorageSize.3"
        OID_SWAP_MEM = "hrStorageSize.10"

        if service_id is None:
            return jsonify(error=True)

        service = Service.get_by_id(service_id)
        if service is None:
            return jsonify(error=True)

        if request.is_json and request.get_json(silent=True) is not None:
            try:
                post_data = request.get_json()

                host_name = post_data.get('host_name')
                service_description = post_data.get('service_description')
                display_name = post_data.get('display_name')
                check_command_pre = post_data.get('check_command_pre')
                check_command_param = post_data.get('check_command_param')
                snmp_type = post_data.get('snmp_type')
                snmp_option = post_data.get('snmp_option')
                snmp_check = post_data.get('snmp_check')
                max_check_attempts = post_data.get('max_check_attempts')
                check_interval = post_data.get('check_interval')
                retry_interval = post_data.get('retry_interval')
                contacts = post_data.get('contacts')
                contact_groups = post_data.get('contact_groups')
                servicegroups = post_data.get('servicegroups')
                servicetemplates = post_data.get('use')
                warning_limit = post_data.get('warning_limit')
                critical_limit = post_data.get('critical_limit')

                servicegroup_names_to_update = []

                if service.servicegroups:
                    servicegroup_names_to_update = servicegroup_names_to_update + service.servicegroups.split(
                        ',')
                if servicegroups:
                    servicegroup_names_to_update = servicegroup_names_to_update + servicegroups

                if host_name:
                    service.host_name = ','.join(host_name)

                if service_description is not None:
                    service.service_description = service_description.strip()

                if check_command_pre is not None:
                    service.check_command_pre = check_command_pre.strip()
                    if service.check_command_param is not None:
                        if snmp_type != COMMON_SNTP_TRAFFIC:
                            service.check_command = check_command_pre + service.check_command_param.strip(
                            )
                    else:
                        service.check_command = check_command_pre.strip()

                if check_command_param is not None and len(
                        check_command_param) > 0:
                    service.check_command_param = check_command_param.strip()
                    service.check_command = service.check_command_pre + check_command_param.strip(
                    )

                command = Command.get_by_commandname(check_command_pre)
                if command is not None and command.command_line.find(
                        'check_snmp') != -1:
                    if snmp_type is not None and len(snmp_type) > 0:
                        temp = ""
                        service.snmp_type = snmp_type.strip()
                        if snmp_type == COMMON_SNTP_TRAFFIC:
                            for option in snmp_option:
                                if snmp_check is not None and len(
                                        snmp_check) > 0:
                                    for check in snmp_check:
                                        if check == "ifUcastPkts":
                                            temp += "!ifHCInUcastPkts." + str(
                                                option
                                            ) + "!ifHCOutUcastPkts." + str(
                                                option) + "!" + str(
                                                    check_interval
                                                ) + "!" + str(
                                                    warning_limit) + "!" + str(
                                                        critical_limit)
                                        elif check == "ifMulticastPkts":
                                            temp += "!ifHCInMulticastPkts." + str(
                                                option
                                            ) + "!ifHCOutMulticastPkts." + str(
                                                option) + "!" + str(
                                                    check_interval
                                                ) + "!" + str(
                                                    warning_limit) + "!" + str(
                                                        critical_limit)
                                        elif check == "ifErrors":
                                            temp += "!ifInErrors." + str(
                                                option
                                            ) + "!ifOutErrors." + str(
                                                option) + "!" + str(
                                                    check_interval
                                                ) + "!" + str(
                                                    warning_limit) + "!" + str(
                                                        critical_limit)
                                else:
                                    temp += "!ifHCInOctets." + str(
                                        option) + "!ifHCOutOctets." + str(
                                            option) + "!" + str(
                                                check_interval) + "!" + str(
                                                    warning_limit) + "!" + str(
                                                        critical_limit)
                                break
                            service.check_command = check_command_pre + temp
                        elif snmp_type == COMMON_SNTP_CPULOAD:
                            temp = "!" + OID_CPU_LOAD_1 + "!" + OID_CPU_LOAD_5 + "!" + OID_CPU_LOAD_15
                            service.check_command = check_command_pre + temp
                        elif snmp_type == COMMON_SNTP_MEMORY:
                            temp = "!" + OID_PHYSICAL_MEM + "!" + OID_VIRTUAL_MEM + "!" + OID_SWAP_MEM
                            service.check_command = check_command_pre + temp

                if snmp_option is not None and len(snmp_option) > 0:
                    snmp_option_str = ','.join("{0}".format(n)
                                               for n in snmp_option)
                    service.snmp_option = snmp_option_str

                if snmp_check is not None and len(snmp_check) > 0:
                    snmp_check_str = ','.join(snmp_check)
                    service.snmp_check = snmp_check_str

                if max_check_attempts is not None and str(
                        max_check_attempts).isdigit():
                    service.max_check_attempts = max_check_attempts

                if check_interval is not None and str(
                        check_interval).isdigit():
                    service.check_interval = str(
                        round(int(check_interval) / 60, 1))

                if retry_interval is not None and str(
                        retry_interval).isdigit():
                    service.retry_interval = retry_interval

                if warning_limit is not None and str(warning_limit).isdigit():
                    service.warning = warning_limit

                if critical_limit is not None and str(
                        critical_limit).isdigit():
                    service.critical = str(critical_limit)

                if contacts is not None:
                    service.contacts = ','.join(contacts)

                if contact_groups is not None:
                    service.contact_groups = ','.join(contact_groups)

                if servicegroups is not None:
                    service.servicegroups = ','.join(servicegroups)

                if servicetemplates is not None:
                    service.use = ','.join(servicetemplates)

                writeNagiosServicesConfigFile(service)

                if check_interval is not None and str(
                        check_interval).isdigit():
                    service.check_interval = str(check_interval)

                updated = service.update()

                if host_name:
                    Service.delete_all_hosts_relations(service_id)
                    Service.create_hosts_relations(service_id, host_name)
                if contacts:
                    Service.delete_all_contacts_relations(service_id)
                    Service.create_contacts_relations(service_id, contacts)
                if contact_groups:
                    Service.delete_all_contactgroups_relations(service_id)
                    Service.create_contactgroups_relations(
                        service_id, contact_groups)
                if servicegroups:
                    Service.delete_all_servicegroups_relations(service_id)
                    Service.create_servicegroups_relations(
                        service_id, servicegroups)
                if servicetemplates:
                    Service.delete_all_servicetemplate_relations(service_id)
                    Service.create_servicetemplate_relations(
                        service_id, servicetemplates)

                if not restartNagios():
                    syncNagiosAllConfigWithDb()
                    return jsonify(error=True, msg="Invalid process")

                if updated:
                    service = Service.get_by_id(service_id)
                    return jsonify(data=service.serialize())
            except Exception as e:
                syncNagiosAllConfigWithDb()
                return jsonify(error=True, msg=str(e))
        return jsonify(error=True)
Beispiel #5
0
    def delete(self, jwt, host_name, host_id, page_id):
        # if host_name is None:
        #     return jsonify(error=True)

        if host_id is not None:
            host = Host.get_by_id(host_id)
        elif host_name is not None:
            host = Host.get_by_hostname(host_name)
        else:
            return jsonify(error=True)

        if host is None:
            return jsonify(error=True)
        else:
            try:
                host_id = host.id
                relations = HostService.query.filter_by(host_id=host_id).all()
                relation_service_ids = []
                if relations is not None:
                    for relation in relations:
                        relation_service_ids.append(relation.service_id)

                relationgroups = HostgroupHost.query.filter_by(
                    host_id=host_id).all()
                relation_hostgroup_ids = []
                if relationgroups is not None:
                    for relationgroup in relationgroups:
                        relation_hostgroup_ids.append(
                            relationgroup.hostgroup_id)

                # delete all relations

                Host.delete_all_host_service_relations(host_id)
                Host.delete_all_contact_relations(host_id)
                Host.delete_all_contactgroup_relations(host_id)
                Host.delete_all_host_template_relations(host_id)
                Host.delete_all_hostgroup_host_relations(host_id)

                host = Host.get_by_id(host_id)
                deleteNagiosConfigFile(host)
                host.delete()

                for relation in relation_service_ids:
                    service = Service.get_by_id(relation)
                    if not service:
                        continue
                    hosts = HostService.get_all_by_sevice(service.id)
                    if hosts:
                        host_ids = [h.id for h in hosts]
                        host_names_str = ','.join(host_ids)
                        service.host_name = host_names_str
                        tmp_check_interval = service.check_interval
                        service.check_interval = round(
                            int(service.check_interval) / 60, 1)
                        writeNagiosServicesConfigFile(service)
                        service.check_interval = tmp_check_interval
                        service.update()
                    else:
                        deleteNagiosServicesConfigFile(service)
                        service.delete()

                for relation in relation_hostgroup_ids:
                    host_group = Hostgroup.get_by_id(relation)
                    if not host_group:
                        continue
                    host_group_hosts = HostgroupHost.get_all_by_hostgroup(
                        host_group.id)
                    if host_group_hosts:
                        host_ids = [h.id for h in host_group_hosts]
                        host_names_str = ','.join(host_ids)
                        host_group.members = host_names_str
                        writeNagiosHostgroupsConfigFile(host_group)
                        host_group.update()
                    else:
                        deleteNagiosHostgroupsConfigFile(host_group)
                        host_group.delete()

                if not restartNagios():
                    syncNagiosAllConfigWithDb()
                    return jsonify(error=True, msg="Invalid process")

                return jsonify(error=False)
            except Exception as e:
                syncNagiosAllConfigWithDb()
                print(e.__dict__)
                return jsonify(error=True, msg=str(e))