ansible/roles/nagios/templates/nagios-ansible.cfg.j2

237 lines
7.3 KiB
Plaintext
Raw Normal View History

2021-09-24 20:48:41 -05:00
# {{ ansible_managed }}
2021-08-08 02:24:59 -05:00
# Templates
define host {
name ansible-linux-server
check_period 24x7
check_interval 10
retry_interval 3
2021-08-08 02:24:59 -05:00
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
2021-08-08 02:24:59 -05:00
register 0
}
# Default hostgroup
2021-08-08 02:24:59 -05:00
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
2021-08-08 02:24:59 -05:00
}
2021-10-06 15:30:12 -05: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 %}
2021-08-08 02:24:59 -05:00
# Commands
# Everything here is defined in nagios_commands
{% for command in nagios_commands %}
2021-08-15 14:24:35 -05:00
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 %}
2021-08-15 14:24:35 -05:00
}
{% endfor %}
{% endif %}
2021-08-08 02:24:59 -05:00
{% if nagios_services is defined %}
# Services
# Everything here is defined in nagios_services
{% for service in nagios_services %}
2021-08-08 02:24:59 -05:00
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 %}
2021-08-23 18:18:53 -05:00
}
{% endfor %}
{% endif %}
2021-08-15 14:24:35 -05:00
2021-08-08 02:24:59 -05:00
# 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 %}
2021-08-08 02:24:59 -05:00
{% 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 }}
2021-08-08 02:24:59 -05:00
define hostgroup {
hostgroup_name tag-{{ tag.value.slug }}
2021-08-08 02:24:59 -05:00
alias {{ tag.value.display }}
}
{% endfor %}
2022-01-10 23:46:26 -06:00
{% 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 %}
2021-08-08 02:24:59 -05:00
# 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)%}
2021-08-08 02:24:59 -05:00
{% if host.value.primary_ip %}
{% for tag in host.value.tags %}
{% if tag.slug == "nagios" %}
# {{ host }}
2021-08-08 02:24:59 -05:00
define host {
use ansible-linux-server
host_name {{ host.value.name }}
alias {{ host.value.display }}
address {{ host.value.primary_ip.address.split('/',1)[0] }}
2022-01-10 23:46:26 -06:00
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 %}
2021-08-08 02:24:59 -05:00
contact_groups ansible
2021-08-08 02:24:59 -05:00
}
{% 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 %}
{# #}
2021-08-08 02:24:59 -05:00
{% 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 %}
{# #}
{% if tag.slug == "nagios-checkminecraft" %}
{% for port in service.value.ports %}
define service {
use ansible-generic-service
service_description Minecraft - {{ service.value.name }} - {{ port }}
check_command check_by_ssh!/usr/local/bin/monitoring-scripts/check_minecraft -H {{ host_name }} -p {{ port }} -m "{{ service.value.description }}" -f -w 3 -c 5
host_name {{ host_name }}
contact_groups ansible
}
{% endfor %}
{% endif %}
{# #}
{% if tag.slug == "nagios-checkhttp" %}
{% for port in service.value.ports %}
define service {
use ansible-generic-service
service_description HTTP - {{ service.value.name }} - {{ port }}
check_command check_http!-H {{ service.value.name }} -p {{ port }} -f sticky
host_name {{ host_name }}
contact_groups ansible
}
{% endfor %}
{% endif %}
{# #}
{% if tag.slug == "nagios-checkhttps" %}
{% for port in service.value.ports %}
define service {
use ansible-generic-service
service_description HTTPS - {{ service.value.name }} - {{ port }}
check_command check_http!--ssl -H {{ service.value.name }} -p {{ port }} -f sticky
host_name {{ host_name }}
contact_groups ansible
}
{% endfor %}
{% endif %}
{# #}
{% if tag.slug == "nagios-checktcp" %}
{% for port in service.value.ports %}
define service {
use ansible-generic-service
service_description TCP {{ service.value.name }} - {{ port }}
check_command check_tcp!{{ port }}
host_name {{ host_name }}
contact_groups ansible
}
{% endfor %}
{% endif %}
{# #}
{% endfor %}
{% endif %}
{% endfor %}