- name: Add nagios group as 420
  group: name=nagios gid=420 state=present system=yes
  tags:
  - nagios_server

- name: Add nagios user as 420
  user: name=nagios uid=420 state=present home=/var/spool/nagios comment="Nagios Monitoring User" createhome=yes system=yes shell=/bin/bash
  tags:
  - nagios_server

# Add the apache user to the nagios group so that it has permissions
# to do stuff in /var/spool/nagios/*
- name: Add apache to nagios group
  user: name=apache append=yes groups=nagios
  tags:
  - nagios_server
    
- name: Install nagios packages
  yum: name={{ item }} state=present
  with_items:
  - php
  - nagios-plugins-http
  - nagios-plugins-dns
  - nagios-plugins-dig
  - nagios-plugins-ssh
  - nagios-plugins-nagios
  - nagios-plugins-nrpe
  - nagios-plugins-tcp
  - nagios-plugins-pgsql
  - nagios-plugins-smtp
  - nagios.x86_64
  - nagios-plugins
  - perl-Mail-IMAPClient
  - nagios-plugins-dummy
  - stunnel
  - mod_auth_openid
  tags:
  - nagios_server

- name: Copy /etc/nagios config
  copy: src={{nagios_srcdir}}/ dest=/etc/nagios owner=nagios group=nagios
  tags:
  - nagios-config
  - nagios_server
  notify: restart nagios

- name: Copy checkcommands.cfg
  template: src={{nagios_srcdir}}/checkcommands.cfg dest=/etc/nagios/checkcommands.cfg owner=nagios group=nagios
  tags:
  - nagios_server

- name: Copy httpd config
  template: src=nagios-httpd.conf dest=/etc/httpd/conf.d/nagios.conf
  tags:
  - nagios_server

- name: Create eventhandlers directory
  file: dest=/usr/lib64/nagios/plugins/eventhandlers/ state=directory
  tags:
  - nagios_server

- name: Copy plugins
  copy: src=plugins/ dest=/usr/lib64/nagios/plugins/ mode=0755 owner=root group=root
  tags:
  - nagios_server

- name: Create log directory
  file: dest=/var/log/nagios state=directory group=nagios owner=nagios mode=0755
  tags:
  - nagios_server

- name: Create spool directory
  file: dest=/var/log/nagios/spool state=directory group=nagios owner=nagios mode=0755
  tags:
  - nagios_server

- name: Create checkresults directory
  file: dest=/var/log/nagios/spool/checkresults state=directory group=nagios owner=nagios mode=0755
  tags:
  - nagios_server

- name: Copy irc-colorize.py
  copy: src=irc-colorize.py dest=/usr/local/bin/irc-colorize.py mode=0755 group=root owner=root
  tags:
  - nagios_server

- name: Copy specialized nrpe.cfg for nagios server
  copy: src=nrpe.cfg dest=/etc/nagios/nrpe.cfg mode=0644 group=root owner=root
  notify:
  - restart nrpe
  tags:
  - config
  - nagios_server

- name: Copy check_nagios_notifications.py
  copy: src=check_nagios_notifications.py dest=/usr/local/bin/check_nagios_notifications.py mode=0755 group=root owner=root
  tags:
  - nagios_server

- name: Nuke default nagios passwd
  file: dest=/etc/nagios/passwd state=absent
  tags:
  - nagios_server

- name: Nuke default nagios objects
  file: dest=/etc/nagios/objects state=absent
  tags:
  - nagios_server

- name: Install check_nagios_notifications cron
  cron: name="check_nagios_notifications" minute=0 hour=0 weekday=1 user=nagios job="/usr/local/bin/check_nagios_notifications.py"
  tags:
  - nagios_server

- name: Override config.inc.php for the given environment
  template: src=config.inc.php dest=/usr/share/nagios/html/config.inc.php mode=0640 owner=root group=apache
  tags:
  - nagios_server


# Handle selinux annoyances - roughly copied from fedmsg role
- name: Ensure a directory exists for our custom selinux module
  file: dest=/usr/local/share/nagios-policy state=directory
  tags:
  - nagios_server

- name: Copy over our custom selinux module
  copy: src=selinux/nagios_hostname.pp dest=/usr/local/share/nagios-policy/nagios_hostname.pp
  register: selinux_module
  tags:
  - nagios_server

- name: Install our custom selinux module
  command: semodule -i /usr/local/share/nagios-policy/nagios_hostname.pp
  when: selinux_module|changed
  tags:
  - nagios_server

- name: Start and autostart services
  service: name={{item}} state=started enabled=yes
  with_items:
  - httpd
  - nagios
  - nrpe
  - postfix
  - rsyslog
  tags:
  - nagios_server
