# {{ ansible_managed }}

# Templates
define host {
	name			ansible-linux-server
	check_period		24x7
	check_interval		10
	retry_interval		3
	max_check_attempts	10
	check_command		check-host-alive
	notification_period	24x7
	notification_interval	120
	hostgroups		ansible
	check_period		24x7
	contacts		salt
	register		0
}
define service {
	use			generic-service
	name			ansible-generic-service
	max_check_attempts	10
	check_interval		10
	retry_interval		2
	register		0
}

# Default hostgroup
define hostgroup {
	hostgroup_name		ansible
	alias			Ansible-managed Hosts
}

# Additional timeperiods for convenience
define timeperiod {
	timeperiod_name		ansible-not-late-at-night
	alias			Not Late at Night
	sunday			07:00-22:00
	monday			07:00-22:00
	tuesday			07:00-22:00
	wednesday		07:00-22:00
	thursday		07:00-22:00
	friday			07:00-22:00
	saturday		07:00-22:00
}

{% if nagios_contacts is defined %}
# Contacts
# Everything here is defined in nagios_contacts
{% for contact in nagios_contacts %}
define contact {
	contact_name			{{ contact.name }}
	alias				{{ contact.alias | default(contact.name, true ) }}
	host_notifications_enabled	{{ contact.host_notifications_enabled | default('1', true) }}
	host_notification_period	{{ contact.host_notification_period | default('24x7', true) }}
	host_notification_options	{{ contact.host_notification_options | default('d,u,r,f', true ) }}
	host_notification_commands	{{ contact.host_notification_commands }}
	service_notifications_enabled	{{ contact.service_notifications_enabled | default('1', true) }}
	service_notification_period	{{ contact.service_notification_period | default('24x7', true) }}
	service_notification_options	{{ contact.service_notification_options | default('w,c,r,f', true ) }}
	service_notification_commands	{{ contact.service_notification_commands }}
	{% if contact.extra is defined %}
	{% for kvp in contact.extra %}
	{{ kvp.key }} {{ kvp.value }}
	{% endfor %}
	{% endif %}
}
{% endfor %}
{% endif %}

# And a contactgroup
define contactgroup {
	contactgroup_name		ansible
	alias				Ansible notification contacts
	members				nagiosadmin
}

{% if nagios_commands is defined %}
# Commands
# Everything here is defined in nagios_commands
{% for command in nagios_commands %}
define command {
	command_name		{{ command.name }}
	command_line		{{ command.command }}
	{% if command.extra is defined %}
	{% for kvp in command.extra %}
	{{ kvp.key }} {{ kvp.value }}
	{% endfor %}
	{% endif %}
}
{% endfor %}
{% endif %}

{% if nagios_services is defined %}
# Services
# Everything here is defined in nagios_services
{% for service in nagios_services %}
define service {
	use			ansible-generic-service
	service_description	{{ service.name }}
	check_command		{{ service.command }}
	hostgroup_name		{{ service.hostgroup | default('ansible', true) }}
	contact_groups		ansible
	{% if service.extra is defined %}
	{% for kvp in service.extra %}
	{{ kvp.key }} {{ kvp.value }}
	{% endfor %}
	{% endif %}
}
{% endfor %}
{% endif %}

# Hostgroups
{% for role in query('netbox.netbox.nb_lookup', 'device-roles', api_endpoint='https://netbox.desu.ltd', token=netbox_token) %}
# Device Role:		{{ role.value.name }}
# 	Description:	{{ role.value.description }}
# 	Created:	{{ role.value.created }}
# 	Updated:	{{ role.value.last_updated }}
define hostgroup {
	hostgroup_name		role-{{ role.value.slug }}
	alias			{{ role.value.display }}
}
{% endfor %}
{% for tag in query('netbox.netbox.nb_lookup', 'tags', api_endpoint='https://netbox.desu.ltd', token=netbox_token) %}
# Tag:			{{ tag.value.name }}
# 	Description:	{{ tag.value.description }}
define hostgroup {
	hostgroup_name		tag-{{ tag.value.slug }}
	alias			{{ tag.value.display }}
}
{% endfor %}
{% for type in query('netbox.netbox.nb_lookup', 'device-types', api_endpoint='https://netbox.desu.ltd', token=netbox_token) %}
# Type:			{{ type.value.display }}
define hostgroup {
	hostgroup_name		device-type-{{ type.value.slug }}
	alias			{{ type.value.display }}
}
{% endfor %}

# Hosts
{% for host in query('netbox.netbox.nb_lookup', 'devices', api_filter='status=active', api_endpoint='https://netbox.desu.ltd', token=netbox_token) + query('netbox.netbox.nb_lookup', 'virtual-machines', api_filter='status=active', api_endpoint='https://netbox.desu.ltd', token=netbox_token)%}
{% if host.value.primary_ip %}
{% for tag in host.value.tags %}
{% if tag.slug == "nagios" %}
# {{ host }}
define host {
	use			ansible-linux-server
	host_name		{{ host.value.name }}
	alias			{{ host.value.display }}
	address			{{ host.value.primary_ip.address.split('/',1)[0] }}
	hostgroups		ansible{% for tag in host.value.tags %},tag-{{ tag.slug }}{% endfor %}{% if host.value.device_role is defined -%},role-{{ host.value.device_role.slug }}{% endif %}{% if host.value.role is defined %},role-{{ host.value.role.slug }}{% endif %}{% if host.value.device_type is defined %},device-type-{{ host.value.device_type.slug }}{% endif %}

	contact_groups		ansible
}
{% if host.value.config_context.extra_checks is defined %}
{% for check in host.value.config_context.extra_checks %}
	define service {
		# Config Context check
		use			ansible-generic-service
		service_description	{{ check.description }}
		check_command		{{ check.command }}
		host_name		{{ host.value.name }}
		contact_groups		ansible
	}
{% endfor %}
{% endif %}
{# #}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}

# Services unique to hosts
{% for service in query('netbox.netbox.nb_lookup', 'services', api_endpoint='https://netbox.desu.ltd', token=netbox_token) %}
{% if service.value.device %}
{% set host_name = service.value.device.name %}
{% elif service.value.virtual_machine %}
{% set host_name = service.value.virtual_machine.name %}
{% endif %}
{% if host_name is defined %}
# {{ host_name }} - {{ service.value.display }}
# 	Description:	{{ service.value.description }}
# 	Created:	{{ service.value.created }}
# 	Updated:	{{ service.value.last_updated }}
{% for tag in service.value.tags %}
{% endfor %}
{% endif %}
{% endfor %}