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))
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)