├── .gitignore ├── LICENSE.txt ├── README.md ├── defaults └── main.yml ├── handlers └── main.yml ├── meta └── main.yml ├── tasks └── main.yml └── templates ├── commands.cfg ├── hostgroups.cfg ├── hosts.cfg └── services.cfg /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Contributors 2 | 3 | Permission to use, copy, modify, and/or distribute this software 4 | for any purpose with or without fee is hereby granted, provided 5 | that the above copyright notice and this permission notice 6 | appear in all copies. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 10 | OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE 11 | LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 12 | OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 13 | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 14 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ansible-Nagios-Config 2 | ==================== 3 | 4 | Before going any further, this role is designed to be run on servers that 5 | have been provisioned using the npm [Nagios Role](http://github.com/npm/ansible-nagios). 6 | Provision a new server using this role. 7 | 8 | What is This? 9 | ------------ 10 | 11 | Nagios is a pain to configure, Ansible-Nagios-Config alleviates this by: 12 | 13 | * providing sane default settings for hosts/services. 14 | * automating the generation of configuration, through a simple DSL. 15 | 16 | Usage 17 | ----- 18 | 19 | * Install [librarian-ansible](https://github.com/bcoe/librarian-ansible) a tool for 20 | managing your third-party Ansible dependencies. 21 | * Provision a new Nagios server using [ansible-nagios](https://github.com/npm/ansible-nagios). 22 | * Set the *nagios_host_groups*, *nagios_commands*, and *nagios_hosts* variables described 23 | in the next section of this document. 24 | * Create a playbook that references the *ansible-nagios-config* role, and use it to deploy 25 | your configuration. 26 | 27 | Configuration 28 | ------------- 29 | 30 | * **nagios_base_dir:** the nagios installation directory. *default: /usr/local/nagios* 31 | * **nagios_object_dir:** the nagios object directory. *default: {{nagios_base_dir}}/etc/objects* 32 | * **nagios_cfg_dir_enabled:** if true, configures nagios_object_dir as a cfg_dir. *default: false* 33 | * **nagios_user:** id of nagios user. *default: nagios* 34 | * **nagios_group:** id of nagios group. *default: nagios* 35 | 36 | nagios_hosts 37 | ------------ 38 | 39 | Used to describe the hosts you would like to monitor with Nagios: 40 | 41 | * **name:** the name of the host, used to reference it in *host_groups.* 42 | * **address:** the actual address of the host. 43 | * *other variables:* other variables represent *host_groups* that the host 44 | belongs to, e.g., `www`, `aws`. 45 | 46 | ```yaml 47 | nagios_hosts: 48 | - {name: 'www', address: 'www.example.com', www: true, aws: true} 49 | - {name: 'redis', address: 'redis.example.com', redis: true, aws: true} 50 | ``` 51 | 52 | nagios_groups 53 | ------------- 54 | 55 | Used to organize your hosts into types of servers, e.g., web, redis, MySQL, and 56 | to describe the checks that should be applied to these groups. 57 | 58 | ```yaml 59 | nagios_host_groups: 60 | - name: 'www' 61 | alias: 'Web Servers' 62 | checks: 63 | - {command: 'check_http_npmjs_org', description: 'Check HTTP'} 64 | - {command: 'check_nrpe2!check_disk_nrpe', description: 'Disk Space Left'} 65 | - name: 'aws' 66 | alias: 'AWS Servers' 67 | - name: 'redis' 68 | alias: 'Redis Server' 69 | checks: 70 | - {command: 'check_nrpe2!check_disk_nrpe', description: 'Disk Space Left'} 71 | ``` 72 | 73 | nagios_commands 74 | --------------- 75 | 76 | Describe custom commands that can be used to perform host checks: 77 | 78 | ```yaml 79 | nagios_commands: 80 | - {name: 'check_http_npmjs_org', command: '$USER1$/check_http -H npmjs.org -I $HOSTADDRESS$ $ARG1$'} 81 | - {name: 'check_nrpe2', command: '$USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$'} 82 | ``` 83 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | # base directory for nagios installation 2 | nagios_base_dir: /usr/local/nagios 3 | 4 | # nagios object directory 5 | nagios_object_dir: "{{nagios_base_dir}}/etc/objects" 6 | 7 | # if true, nagios_object_dir is a cfg_dir and nagios will 8 | # autoload all files ending in .cfg 9 | nagios_cfg_dir_enabled: false 10 | 11 | # id of nagios user 12 | nagios_user: nagios 13 | 14 | # id of nagios group 15 | nagios_group: nagios 16 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | - name: restart nagios 2 | become: true 3 | service: name=nagios state=restarted 4 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: Benjamin Coe 4 | description: A role for deploying nagios configuration. 5 | min_ansible_version: 1.4 6 | version: 1.0.0 7 | license: ISC 8 | platforms: 9 | - name: Ubuntu 10 | versions: 11 | - trusty 12 | - precise 13 | categories: 14 | - system 15 | - monitoring 16 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | - name: Install config files 2 | become: true 3 | notify: restart nagios 4 | template: > 5 | src={{item.src}} 6 | dest={{nagios_object_dir}}/{{item.dest}} 7 | owner={{nagios_user}} 8 | group={{nagios_group}} 9 | mode=640 10 | with_items: 11 | - { src: hosts.cfg, dest: hosts.cfg } 12 | - { src: hostgroups.cfg, dest: hostgroups.cfg } 13 | - { src: services.cfg, dest: services.cfg } 14 | - { src: commands.cfg, dest: additional_commands.cfg } 15 | 16 | - name: Update nagios.cfg with config files 17 | become: true 18 | notify: restart nagios 19 | lineinfile: > 20 | dest={{nagios_base_dir}}/etc/nagios.cfg 21 | line="cfg_file={{nagios_object_dir}}/{{item}}.cfg" 22 | regexp="^cfg_file\={{nagios_object_dir}}/{{item}}\.cfg" 23 | with_items: 24 | - hosts 25 | - hostgroups 26 | - services 27 | - additional_commands 28 | when: nagios_cfg_dir_enabled == false 29 | 30 | - name: Update nagios.cfg with config dir 31 | become: true 32 | notify: restart nagios 33 | lineinfile: > 34 | dest={{nagios_base_dir}}/etc/nagios.cfg 35 | line="cfg_dir={{nagios_object_dir}}" 36 | regexp="^cfg_dir={{nagios_object_dir}}" 37 | when: nagios_cfg_dir_enabled == true 38 | -------------------------------------------------------------------------------- /templates/commands.cfg: -------------------------------------------------------------------------------- 1 | {% for command in nagios_commands %} 2 | define command { 3 | command_name {{command.name}} 4 | command_line {{command.command}} 5 | } 6 | {% endfor %} 7 | -------------------------------------------------------------------------------- /templates/hostgroups.cfg: -------------------------------------------------------------------------------- 1 | {% for host_group in nagios_host_groups %} 2 | define hostgroup { 3 | hostgroup_name {{host_group.name}}; 4 | alias {{host_group.alias}}; 5 | members {{nagios_hosts|selectattr(host_group.name, 'defined')|map(attribute='name')|join(',')}}; 6 | } 7 | {% endfor %} 8 | -------------------------------------------------------------------------------- /templates/hosts.cfg: -------------------------------------------------------------------------------- 1 | define host{ 2 | name base-host ; The name of this host template 3 | notifications_enabled 1 ; Host notifications are enabled 4 | event_handler_enabled 1 ; Host event handler is enabled 5 | flap_detection_enabled 0 ; Flap detection is disabled 6 | process_perf_data 1 ; Process performance data 7 | retain_status_information 1 ; Retain status information across program restarts 8 | retain_nonstatus_information 1 ; Retain non-status information across program restarts 9 | notification_period 24x7 ; Send host notifications at any time 10 | notification_options d,u,r ; Only send notifications for specific host states 11 | contact_groups admins ; Notifications get sent to the admins by default 12 | register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE! 13 | check_interval 1 ; Actively check the host every minute 14 | retry_interval 1 ; Schedule host check retries at 1 minute intervals 15 | max_check_attempts 3 ; Check each Linux host 3 times (max) before alerting 16 | check_command check-host-alive ; Default command to check Linux hosts 17 | notification_interval 30 ; Resend notifications every 30 minutes 18 | } 19 | 20 | {% for host in nagios_hosts %} 21 | define host{ 22 | use base-host 23 | host_name {{host.name}} 24 | alias {{host.address}} 25 | address {{host.address}} 26 | {% if host.check_command|default(false) %} 27 | check_command {{host.check_command}} 28 | {% endif %} 29 | {% if host.contact_groups|default(false) %} 30 | contact_groups {{host.contact_groups}} 31 | {% endif %} 32 | {% if host.check_interval|default(false) %} 33 | check_interval {{host.check_interval}} 34 | {% endif %} 35 | {% if host.retry_interval|default(false) %} 36 | retry_interval {{host.retry_interval}} 37 | {% endif %} 38 | {% if host.max_check_attempts|default(false) %} 39 | max_check_attempts {{host.max_check_attempts}} 40 | {% endif %} 41 | {% if host.notification_interval|default(false) %} 42 | notification_interval {{host.notification_interval}} 43 | {% endif %} 44 | } 45 | {% endfor %} 46 | -------------------------------------------------------------------------------- /templates/services.cfg: -------------------------------------------------------------------------------- 1 | {% for host_group in nagios_host_groups %} 2 | {% if 'checks' in host_group %} 3 | {% for check in host_group.checks %} 4 | define service { 5 | use local-service; 6 | hostgroup_name {{host_group.name}} 7 | service_description {{check.description}} 8 | check_command {{check.command}} 9 | {% if check.contact_groups|default(false) %} 10 | contact_groups {{check.contact_groups}} 11 | {% endif %} 12 | {% if check.check_interval|default(false) %} 13 | check_interval {{check.check_interval}} 14 | {% endif %} 15 | {% if check.retry_interval|default(false) %} 16 | retry_interval {{check.retry_interval}} 17 | {% endif %} 18 | {% if check.max_check_attempts|default(false) %} 19 | max_check_attempts {{check.max_check_attempts}} 20 | {% endif %} 21 | {% if check.notification_interval|default(false) %} 22 | notification_interval {{check.notification_interval}} 23 | {% endif %} 24 | } 25 | {% endfor %} 26 | {% endif %} 27 | {% endfor %} 28 | --------------------------------------------------------------------------------