Nagios with Puppet: Adding Hosts and Groups - KeegMitch/Operations-Engineering-group-c GitHub Wiki

Create hosts

  1. Manually install this package to get the nagios resource types working on puppet 6: sudo puppet module install --modulepath /opt/puppetlabs/puppet/modules puppetlabs-nagios_core

Reference: https://forge.puppet.com/modules/puppetlabs/nagios_core/reference

  1. Add this to the end of the config.pp file:

# In order for all this code to work though, manually download the puppetlabs/nagios_core module first

nagios_host { "db-c":
    target                => "/etc/nagios3/conf.d/ppt_hosts.cfg",
    alias                 => "db",
    check_period          => "24x7",
    max_check_attempts    => 3,
    check_command         => "check-host-alive",
    notification_interval => 30,
    notification_period   => "24x7",
    notification_options  => "d,u,r",
    mode                  => "0444",
 }

nagios_host { "app-c":
    target                => "/etc/nagios3/conf.d/ppt_hosts.cfg",
    alias                 => "app",
    check_period          => "24x7",
    max_check_attempts    => 3,
    check_command         => "check-host-alive",
    notification_interval => 30,
    notification_period   => "24x7",
    notification_options  => "d,u,r",
    mode                  => "0444",
 }



nagios_host { "backup-c":
    target                => "/etc/nagios3/conf.d/ppt_hosts.cfg",
    alias                 => "backup",
    check_period          => "24x7",
    max_check_attempts    => 3,
    check_command         => "check-host-alive",
    notification_interval => 30,
    notification_period   => "24x7",
    notification_options  => "d,u,r",
    mode                  => "0444",
 }



nagios_host { "mgmt-c":
    target                => "/etc/nagios3/conf.d/ppt_hosts.cfg",
    alias                 => "mgmt",
    check_period          => "24x7",
    max_check_attempts    => 3,
    check_command         => "check-host-alive",
    notification_interval => 30,
    notification_period   => "24x7",
    notification_options  => "d,u,r",
    mode                  => "0444",
 }

}

  1. Restart nagios3 for changes to make effect: sudo systemctl restart nagios3

  2. Run using test_puppet_agent, and go to sudo cat /etc/nagios3/conf.d/ppt_hosts.cfg, should look like this:

image

  1. Go onto the nagios web server, http://20.191.245.182/nagios3/, the host names should be up (ignore the .foo.org.nz ones, they didn't work):

image

Host groups

  1. Similar to the first step once everything is installed and configured properly, add this to config.pp
# nagios hostgroups

nagios_hostgroup {"my-ssh-servers":
target => "/etc/nagios3/conf.d/ppt_hostgroups.cfg",
mode => "0444",
alias => "My SSH servers",
members => "db-c, app-c, backup-c, mgmt-c",
}

nagios_hostgroup {"my-mariaDB-servers":
  target => "/etc/nagios3/conf.d/ppt_hostgroups.cfg",
  mode => "0444",
  alias => "My mariaDB servers",
  members => "db-c",
}

image

  1. test_puppet_agent, the sudo systemctl restart nagios3

image

Services

  1. Add this code to config.pp
# nagios services

nagios_service {"ssh":
service_description => "ssh servers",
hostgroup_name => "my-ssh-servers",
target => "/etc/nagios3/conf.d/ppt_services.cfg",
check_command => "check_ssh",
max_check_attempts => 3,
retry_check_interval => 1,
normal_check_interval => 5,
check_period => "24x7",
notification_interval => 30,
notification_period => "24x7",
notification_options => "w,u,c",
contact_groups => "admins",
mode => "0444",
}

nagios_service {"mariaDB":
service_description => "mariaDB servers",
hostgroup_name => "my-mariaDB-servers",
target => "/etc/nagios3/conf.d/ppt_services.cfg",
check_command => "check_mysql",
max_check_attempts => 3,
retry_check_interval => 1,
normal_check_interval => 5,
check_period => "24x7",
notification_interval => 30,
notification_period => "24x7",
notification_options => "w,u,c",
contact_groups => "admins",
mode => "0444",
}


  1. test_puppet_agent command:

image

  1. Restart nagios: sudo systemctl restart nagios3

  2. Check if they've been applied to nagios web server

image

Adding a Nagios check for HTTP in the app server

  • For using an IP address of app server to track HTTP requests, I added a custom command inside /etc/nagios-plugins/config/http.cfg (sudo vim /etc/nagios-plugins/config/http.cfg)

image

The check_http_ip is the one that I have created here:

image

#  'check_http_ip' command definition

define command{
        command_name    check_http_ip
        command_line    /usr/lib/nagios/plugins/check_http -I '$HOSTADDRESS$' '$ARG1$'
        }


  • Restart Nagios : sudo systemctl restart nagios3

  • Add the HTTP nagios service and hostgroup inside your Nagios puppet module inside config.pp

nagios_hostgroup {"owncloud-app-http":
   target => "/etc/nagios3/conf.d/ppt_hostgroups.cfg",
   mode => "0444",
   alias => "HTTP requests for owncloud",
   members => "app-c",
}

# created a custom command inside the /etc/nagios-plugins/config/http.cfg
nagios_service { 'check_http_ip':
  service_description    => 'Check ownCloud HTTP',
  hostgroup_name         => 'owncloud-app-http',
  target                 => '/etc/nagios3/conf.d/ppt_services.cfg',
  check_command          => 'check_http_ip!10.2.0.4',
  max_check_attempts     => 3,
  retry_check_interval   => 1,
  normal_check_interval  => 5,
  check_period           => '24x7',
  notification_interval  => 30,
  notification_period    => '24x7',
  notification_options   => 'w,u,c',
  contact_groups         => 'slackgroup',
  mode => "0444",

}

Note that the check command is check_http_ip and NOT check_http, we're just specifying the ip address of the app server here

  • Apply the puppet agent: test_puppet_agent or sudo /opt/puppetlabs/puppet/bin/puppet agent --test

  • Restart nagios again

  • Verify that it shows up as "OK" on your server

image