Ticket ID #217 ‐ Set Up Nagios Monitoring Using Puppet Module - GriffinKat/group-a GitHub Wiki

Configure Nagios using the config.pp and service.pp manifests

  • service.pp The service class enables nagios4 service and ensures that it is running. When the service is running, it is accessible through its web interface. Paste the following code in the service.pp file in the directory /etc/puppetlabs/code/modules/nagios/manifests$
class nagios::service {
  service { 'nagios4':
     ensure    => running,
     enable    => true,
     hasstatus => true,
     hasrestart => true,
     require => Class["nagios::config"],
  }
}

image

  • config.pp The config class manages nagios configuration files and resources. We will create the following resources in the config.pp file located at /etc/puppetlabs/code/modules/nagios/manifests$
  1. A file resource that manages /etc/nagios4/nagios.cfg. We will use the default configuration file installed by the package for this. After installing the nagios package we will find the file in the /etc/nagios4/nagios.cfg location and copy it to /etc/puppetlabs/code/modules/nagios/files using the command sudo cp //etc/nagios4/nagios.cfg /etc/puppetlabs/code/modules/nagios/files

    # Manage main Nagios config file
    file { '/etc/nagios4/nagios.cfg':
     ensure  => present,
     source  => 'puppet:///modules/nagios/nagios.cfg',
     owner   => 'nagios',
     group   => 'nagios',
     mode    => '0644',
     require => Class['nagios::install'],
     notify => Class['nagios::service'],
    }
    
  2. A secong file resource to manage /etc/nagios4/htpasswd.users. This file contains usernames and passwords used to log into the Nagios web interface. We created the file using the command htpasswd -c /etc/puppetlabs/code/modules/nagios/files/htpasswd.users nagiosadmin

     # Manage the htpasswd.users file for web authentication
     file { '/etc/nagios4/htpasswd.users':
      ensure  => present,
      source  => 'puppet:///modules/nagios/htpasswd.users',  # Reference the file created earlier
      owner   => 'nagios',
      group   => 'nagios',
      mode    => '0640',
      require => Class['nagios::install'],
      notify  => Class['nagios::service'],
    }
    
  3. A file resource that ensures the directory /etc/nagios4/conf.d is present, sets its group ownership to puppet, and its mode to 0775.

    # Ensure Nagios configuration directory exists
    file { '/etc/nagios4/conf.d':
     ensure  => directory,
     owner   => 'nagios',
     group   => 'puppet',
     mode    => '0775',
     recurse => true,
     require => Class['nagios::install'],
    }
    
  4. We set up one nagios-specific resource, a nagios host. This will direct Nagios to monitor the status of our database server.

    # Define a Nagios host to monitor the database server
    nagios_host { 'db-a':
     target               => '/etc/nagios4/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',
    }
    

    Below is the screenshot of the above processes respectively.

    image

    image

Apply the module

To apply the Nagios module on the mgmt server, include the module in the node definition: node ’mgmt-x’ {include nagios}

image

and apply using the command sudo /opt/puppetlabs/bin/puppet agent --test

image

Verification

Below is the screenshot of the nagios4 and apache2 services running on the management server and the nagios web interface.

image

image

image

Errors and troubleshooting

  1. Forbidden error on the web interface page: This error was associated with the apache2 configuration file not having the required permission to access the nagios web interface. To solve this error we edited config file located at /etc/apache2/conf-enabled/nagios4-cgi.conf. The change was to comment out the Require ip line and adding the line Require all granted.

    image

  2. Incorrect permissions to apache2 user to execute the config files located at /usr/lib/cgi-bin/nagios4$. These are the files that apache2 executes in order for the user to interact with the nagios dashboard.

    image

    The files were being downloaded instead of executing, because the apache2 user did not have execute permissions. Correct permissions were set using the command.

    sudo chown -R www-data:www-data /usr/lib/cgi-bin/nagios4

    sudo chmod -R 755 /usr/lib/cgi-bin/nagios4

  3. At first, while writing the nagios_host directive in the config.pp file I named the nagios host as db-a.oe2.org.nz and later changed it to db-a. So, the additional user was showing in the nagios dashboard with the red color.

    image

    To remove this entry, delete the ppt_hosts.cfg file located at /etc/nagios4/conf.d

    image

    Apply the module again and restart the nagios and apache service. A new ppt_hosts.cfg will be created and the additional entry will be removed.

    Note: All the errors were solved one by one and the apache and nagios services were restarted each time.

  4. Clear web browser's cache if error still persists as a troubleshooting process.