def post(self, jwt): 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') alias = post_data.get('alias') address = post_data.get('address') contact_groups = post_data.get('contact_groups') contacts = post_data.get('contacts') sms = post_data.get('sms') street_address = post_data.get('street_address', '') notes = post_data.get('notes') notes_url = post_data.get('notes_url') hosttemplates = post_data.get('use') host_templates_str = "" if hosttemplates is not None: host_templates_str = ','.join(hosttemplates) notification_period = post_data.get('notification_period', "24x7") notification_options = "d,u,r" if post_data.get('notification_options') is not None: notification_options = ','.join( post_data.get('notification_options')) notifications_enabled = 1 if post_data.get( 'notifications_enabled') == True else 0 check_interval = post_data.get('check_interval', 5) retry_interval = post_data.get('retry_interval', 1) max_check_attempts = post_data.get('max_check_attempts', 5) notification_interval = post_data.get('notification_interval', 120) check_command = post_data.get('check_command', "check-host-alive") _SNMPVERSION = post_data.get('_SNMPVERSION', "2c") _SNMPCOMMUNITY = post_data.get('_SNMPCOMMUNITY', '') # Confirm this hostname doesn't already exist first. if Host.get_by_hostname(host_name): return jsonify(error=True, msg="Hostname already exists.") if host_name is None: return jsonify(error=True, msg="Missing host_name required field.") if alias is None: return jsonify(error=True, msg="Missing alias required field.") if address is None: return jsonify(error=True, msg="Missing address required field.") contacts_str = '' if contacts is not None and len(contacts) > 0: contacts_str = ','.join(contacts) newhost = Host(host_name=host_name.strip(), alias=alias.strip(), display_name=None, address=address.strip(), importance=None, check_command=check_command.strip(), max_check_attempts=max_check_attempts, check_interval=check_interval, retry_interval=retry_interval, active_checks_enabled=True, passive_checks_enabled=True, check_period="24x7", obsess_over_host=False, check_freshness=True, freshness_threshold=None, event_handler=None, event_handler_enabled=None, low_flap_threshold=None, high_flap_threshold=None, flap_detection_enabled=None, flap_detection_options=None, process_perf_data=True, retain_status_information=True, retain_nonstatus_information=True, contacts=contacts_str, contact_groups="", notification_interval=notification_interval, first_notification_delay=None, notification_period=notification_period.strip(), notification_options=notification_options, notifications_enabled=notifications_enabled, use=host_templates_str, hostgroups="null", street_address=street_address.strip(), sms=sms.strip(), notes=notes.strip(), notes_url=notes_url.strip(), icon_image='', icon_image_alt='', _SNMPVERSION=_SNMPVERSION, _SNMPCOMMUNITY=_SNMPCOMMUNITY) host_id = newhost.save() newservice = Service( host_name=host_name.strip(), hostgroup_name=None, service_description="PING", display_name="PING", importance=None, servicegroups="", is_volatile=None, check_command="PING", check_command_pre=None, check_command_param=None, snmp_type=None, snmp_option=None, snmp_check=None, max_check_attempts=5, check_interval=str(1), retry_interval=1, active_checks_enabled=True, passive_checks_enabled=False, check_period="24x7", obsess_over_host=None, check_freshness=None, freshness_threshold=None, event_handler=None, event_handler_enabled=None, low_flap_threshold=None, high_flap_threshold=None, flap_detection_enabled=None, flap_detection_options=None, process_perf_data=True, retain_status_information=True, retain_nonstatus_information=True, contacts=contacts_str, contact_groups="", notification_interval=60, first_notification_delay=5, notification_period="24x7", notification_options="w,c,r", notifications_enabled=True, use="", command=None, retry_check_interval=None, normal_check_interval=None, name=None, warning=None, critical=None, ) service_id = newservice.save() # create relations if contacts: Host.create_contacts_relations(host_id, contacts) Service.create_contacts_relations(service_id, contacts) if contact_groups: Host.create_contactgroups_relations( host_id, contact_groups) Service.create_contactgroups_relations( service_id, contact_groups) if hosttemplates: Host.create_hosttemplate_relations(host_id, hosttemplates) Service.create_hosts_relations(service_id, [host_name.strip()]) writeNagiosConfigFile(newhost) writeNagiosServicesConfigFile(newservice) if not restartNagios(): syncNagiosAllConfigWithDb() return jsonify(error=True, msg="Invalid process") return jsonify(data=newhost.serialize()) except Exception as e: syncNagiosAllConfigWithDb() return jsonify(error=True, msg=str(e)) return jsonify(error=True)
def put(self, jwt, host_id, host_name, page_id): if request.is_json and request.get_json(silent=True) is not None: try: post_data = request.get_json() host = None hostname_org = None index = post_data.get('index') if index is not None: host = Host.get_by_id(index) if host is None: return jsonify(error=True) hostname_org = host.host_name host_name = post_data.get('host_name') alias = post_data.get('alias') address = post_data.get('address') contact_groups = post_data.get('contact_groups') contacts = post_data.get('contacts') sms = post_data.get('sms') street_address = post_data.get('street_address', '') notes = post_data.get('notes') notes_url = post_data.get('notes_url') hosttemplates = post_data.get('use') notification_period = "24x7" if post_data.get('notification_period') is not None and len( post_data.get('notification_period')) > 0: notification_period = post_data.get('notification_period') notification_options = "d,u,r" if post_data.get('notification_options') is not None and len( post_data.get('notification_options')) > 0: notification_options = ','.join( post_data.get('notification_options')) notifications_enabled = 1 if post_data.get('notifications_enabled') is not None and len( str(post_data.get('notifications_enabled'))) > 0: notifications_enabled = 1 if post_data.get( 'notifications_enabled') == True else 0 check_interval = 5 if post_data.get('check_interval') is not None and len( str(post_data.get('check_interval'))) > 0: check_interval = post_data.get('check_interval') retry_interval = 1 if post_data.get('retry_interval') is not None and len( str(post_data.get('retry_interval'))) > 0: retry_interval = post_data.get('retry_interval') max_check_attempts = 5 if post_data.get('max_check_attempts') is not None and len( str(post_data.get('max_check_attempts'))) > 0: max_check_attempts = post_data.get('max_check_attempts') notification_interval = 120 if post_data.get('notification_interval') is not None and len( str(post_data.get('notification_interval'))) > 0: notification_interval = post_data.get( 'notification_interval') check_command = "check-host-alive" if post_data.get('check_command') is not None and len( post_data.get('check_command')) > 0: check_command = post_data.get('check_command') _SNMPVERSION = "2c" if post_data.get('_SNMPVERSION') is not None and len( post_data.get('_SNMPVERSION')) > 0: _SNMPVERSION = post_data.get('_SNMPVERSION') _SNMPCOMMUNITY = "" if post_data.get('_SNMPCOMMUNITY') is not None and len( post_data.get('_SNMPCOMMUNITY')) > 0: _SNMPCOMMUNITY = post_data.get('_SNMPCOMMUNITY') # Confirm this hostname doesn't already exist first. if hostname_org != host_name and Host.get_by_hostname( host_name): return jsonify(error=True, msg="Hostname already exists.") if host_name is None: return jsonify(error=True, msg="Missing host_name required field.") if alias is None: return jsonify(error=True, msg="Missing alias required field.") if address is None: return jsonify(error=True, msg="Missing address required field.") host.host_name = host_name.strip() host.alias = alias.strip() host.address = address.strip() if contact_groups is not None: host.contact_groups = ','.join(contact_groups) if contacts is not None: host.contacts = ','.join(contacts) if sms is not None: host.sms = sms.strip() if street_address is not None: host.street_address = street_address.strip() if notes is not None: host.notes = notes.strip() if notes_url is not None: host.notes_url = notes_url.strip() host_templates_str = host.use if hosttemplates is not None: host_templates_str = ','.join(hosttemplates) host.use = host_templates_str host.notification_period = notification_period.strip() host.notification_options = notification_options host.notifications_enabled = notifications_enabled host.check_interval = check_interval host.retry_interval = retry_interval host.max_check_attempts = max_check_attempts host.notification_interval = notification_interval host.check_command = check_command.strip() host._SNMPVERSION = _SNMPVERSION host._SNMPCOMMUNITY = _SNMPCOMMUNITY writeNagiosConfigFile(host) host_id = host.update() # update host_contact table Host.delete_all_contact_relations(host_id) Host.delete_all_contactgroup_relations(host_id) Host.delete_all_host_template_relations(host_id) Host.create_contacts_relations(host_id, contacts) Host.create_contactgroups_relations(host_id, contact_groups) Host.create_hosttemplate_relations(host_id, hosttemplates) # re-name of host in services table if hostname_org != host_name: services = Service.get_by_hostname_keyword(hostname_org) for service in services: hostnames = service.host_name hostnames = hostnames.replace(hostname_org, host.host_name) service.host_name = hostnames tmp_checkInterval = service.check_interval service.check_interval = round( int(service.check_interval) / 60, 1) writeNagiosServicesConfigFile(service) service.check_interval = tmp_checkInterval service.update() # re-name of host in hostgroups table hostgroups = Hostgroup.get_by_hostname_keyword( hostname_org) for hostgroup in hostgroups: hostnames = hostgroup.members hostnames = hostnames.replace(hostname_org, host.host_name) hostgroup.members = hostnames hostgroup.update() writeNagiosHostgroupsConfigFile(hostgroup) if not restartNagios(): syncNagiosAllConfigWithDb() return jsonify(error=True, msg="Invalid process") if host_id: host = Host.get_by_id(host_id) return jsonify(data=host.serialize()) except Exception as e: syncNagiosAllConfigWithDb() return jsonify(error=True, msg=str(e)) return jsonify(error=True)