Nagios Core 4.x Setup for CentOS 7.x

Introduction

A few years ago I blogged about setting up Nagios Core 4.x for CentOS 6.x. I later made a blog on setting up NSCA and NRPE too. But times have changed so I figured it was time to do an updated blog for CentOS 7.x and Nagios.

Some new great tools I’ve packaged to make a load and go setup for everyone is:

  • Nagios Core v4.x: updated RPM packaging which I continued to maintain and carry forward from my previous blogs.
  • Nagios Plugins v2.x: A ton of out of the box working plugins.

Best of all, with my RPMs, you can run SELinux in full Enforcing mode for that extra piece of mind from a security standpoint!

Nagios Core

Nagios (for those who don’t know) is an application that allows us to monitor other system/applications we manage. It’s primary function is to immediately bring to our attention any outage or anomaly is detected with our systems. This tool is completely free and should be an essential component of anyone’s business infrastructure.

The current version of Nagios (at the time of writing this blog) is v4.2.2. You can download the latest version from my repository (if you’re set up) as follows:

# Install Nagios Core using NuxRef repositories
# at: https://nuxref.com/repo
yum install -y nagios nagios-selinux

You can also download the packages manually if you wish using this table:

Package Download Description
nagios el7.rpm Nagios Core IV is the the actual monitoring server we can use to monitor our applications.
nagios-selinux el7.rpm An add-on package that allows you to run Nagios in Enforcing Mode.

Note: This RPM is not required by Nagios to run correctly.
nagios-contrib el7.rpm Extra tools that add to the great features Nagios already offers (such as distributed monitoring). These tools are not discussed in this blog entry; but maybe useful to you.
Note: This RPM is not required by Nagios to run correctly.
nagios-devel el7.rpm Header files for developers who want to build using the libnagios shared library.
Note: This RPM is not required by Nagios to run correctly.

Note: The source rpm can be obtained here which builds everything you see in the table above. It’s not required for the application to run, but might be useful for developers or those who want to inspect how I put the package together.

Configure Nagios Core

Once Nagios is installed it can be started like so:

# Start Nagios
systemctl start nagios.service

# If you want Nagios to start after the system is rebooted, you
# can type the following:
systemctl enable nagios.service

# Now we want to turn on Apache if it's not running, otherwise
# reload the configuration if it is:
systemctl status httpd.service && \
   systemctl start httpd.service ||\
   systemctl reload httpd.service

If you followed the instructions above you should be able to access the (Nagios) monitoring website right away by visiting http://localhost/nagios. If you’re installing this on another server you may need to open your web ports to access the Nagios Monitoring site:

# The following commands should be ran on your Nagios Server.
# It will enable our http (and secure https) port on our firewall so our
# monitoring website can be accessed remotely:
firewall-cmd --permanent --add-service=http
firewall-cmd --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --add-service=https
Nagios Credentials
Login nagiosadmin
Password nagiosadmin

You’ll be prompted for a user/pass combination; at this time the default values are defined in the table:

Once you’ve logged in, you can click on links like Services (under the Current Status heading) which will list to you all of the system services you’re currently monitoring and their status:

Nagios > Current Status > Services
Nagios > Current Status > Services

In the example, you can see Nagios has picked up on a high system load (denoted by the yellow warning entry).

Nagios at it’s most basic level is set up at this point and can be maintained by having a look at the following directories:

/etc/nagios/nagios.cfg:
The main configuration file that is read by Nagios when starting up. The only things you may want to change in here are:

Directive Description
date_format The default is us (MM-DD-YYYY HH:MM:SS), but personally I like iso8601 (YYYY-MM-DD HH:MM:SS).
# simple 1-liner to toggle date_format to iso8601:
sed -i -e 's/^\(date_format\)=.*$/\1=iso8601/g' \
   /etc/nagios/nagios.cfg
check_for_updates The default is 1 (which is to check for updates). Personally, I don’t want my web page pinging Nagios every time i access the website for updates. Here is how you can do the same:
# disable update check:
sed -i -e 's/^\(check_for_updates\)=.*$/\1=0/g' \
   /etc/nagios/nagios.cfg

/etc/nagios/objects/contacts.cfg:
This is where a default contact has been created. Feel free to open this up and add your name and (especially the) email address.
/etc/nagios/objects/commands.cfg:
All of the possible checks Nagios can perform on it’s own (without any plugins or extensions) are identified here. It’s as easy as defining a command_name (give it some name) and then tell it what you want to execute with the command_line directive.

A Nagios command is really simple to write; you can write one in any language you want. Here is one written in bash shell:

#!/bin/bash
# Path: /usr/lib64/nagios/plugins/check_temp_file_count
# Please keep in mind this script is pretty useless but
# it's goal is to show you how easy it is to write a
# script for Nagios.
#
# The rules are simple:
# Whatever we echo to the screen get's displayed to Nagios
# and our return value from our script/program will
# determine the color code (and whether or not we alarm)

# A return code of zero (0) tells Nagios everything is okay
RET_OKAY=0

# A return code of one (1) tells Nagios we're reporting a warning
# about whatever it is we're monitoring
RET_WARN=1

# A return code of two (2) tells Nagios we're reporting a critical
# error
RET_CRIT=2

# I define 3 here, but quite honestly, anything you return that
# does not fit in the 0, 1 or 2 response type (as identified
# above) is considered an unknown state.  Try to avoid this
# type if you can.
RET_UNKN=3

# As a test script we'll count all of the files and directories
# in the /tmp folder
COUNT=$(find /tmp 2>/dev/null | wc -l)

# If we have less then 10 files we'll tell Nagios everything
# is okay:
if [ $COUNT -lt 10 ]; then
   echo "$COUNT files; everything is good!"
   exit $RET_OKAY
fi

# If we have less then 30 files we'll tell Nagios that
# it should report a warning
if [ $COUNT -lt 30 ]; then
   echo "$COUNT files; caution!"
   exit $RET_WARN
fi

# Anything more we should report a critical alarm
echo "$COUNT files; Critical!!"
exit $RET_CRIT

If you’re familiar with Perl, the Nagios team has made a framework with it which you use to make your packages too! I’ve already gone ahead and packaged the perl-Nagios-Monitoring-Plugin rpm for you if you want it.

Nagios will associate /usr/lib64/nagios/plugins/ as it’s plugin directory; so you should save any plugins you create there (to keep them all in a common location). Plus if you intend to use SELinux, this is the directory that Nagios is allowed to execute from.

Our entry in the commands.cfg for this new script might look like this:

; 'check_temp_file_count' command definition
; $USER1$ gets translated automatically to our Nagios Plugin directory
; so in our case: /usr/lib64/nagios/plugins/
; Ideally you should call the command the same name as the checking
; tool you wrote.
define command{
	command_name	check_temp_file_count
	command_line	$USER1$/check_temp_file_count
	}

/etc/nagios/objects/localhost.cfg:
This is just a general configuration file for the very machine Nagios is running on. If you open it up, you’ll see that it defines a lot of entries that reference commands (defined already in the commands.cfg file).

A new entry in the commands.cfg for this new script might look like this:

; the 'use' directive identifies a template of information to save
; us from typing it all out here.  For now; just leave this as
; local-service.
;
; The 'hostname' defines our server (defined at the very top of this same
; file. Since the host at the to was defined as 'localhost', we need
; to use this same name here too
;
; The next field is just a description field; it will be how this service
; is presented on Nagios through the website
;
; The check_command is the name we gave it in the commands.cfg
; file.
define service{
        use                             local-service
        host_name                       localhost
        service_description             Count our Temporary Files
        check_command                   check_temp_file_count
}

Check to see if Nagios has any errors with any new configuration you provided:

# This command just tells Nagios to read in it's configuration
# and check if it appears valid:
nagios -v /etc/nagios/nagios.cfg

If everything checks out okay, go ahead and reload Nagios with our
new configuration:

# Reload Nagios
systemctl reload nagios.service

Nagios Plugins

If you’ve installed Nagios, there isn’t really any good reason why you shouldn’t just install the Nagios Plugins too. This is just more tools and checking scripts to make Nagios all that more powerful. The best part is, these tools have been tested over the years, so they’re already proven to be reliable and will allow you to accomplish most monitoring without much effort.

Nagios Plugins
Nagios Plugins

It’s important to note that the Nagios Plugin RPMs are NOT required by Nagios to run correctly. They merely just improve it’s existing functionality. You may however want to install the plugins you’re interested in that monitor systems you’re using.

The current version of the Nagios Plugins (at the time of writing this blog) is v2.1.3. You can download the latest version from my repository (if you’re set up) as follows:

# Install Nagios Core using NuxRef repositories
# See: https://nuxref.com/repo for more information
yum install -y nagios-plugins nagios-plugins-selinux

You can also download the packages manually if you wish using this table:

Package Download Description
nagios-plugins el7.rpm 50+ plugins that are fully adaptable to Nagios in every way. If you’re planning on installing Nagios, don’t forget about adding this package for it’s convenience!
nagios-plugins-selinux el7.rpm An optional add-on package that allows you to use the Nagios Plugins in Enforcing Mode.
nagios-plugins-ldap el7.rpm A Nagios plugin that can be used to check integrity and data entries within an LDAP database.
nagios-plugins-mysql el7.rpm A Nagios plugin that can be used to check integrity and data entries within an MySQL (or Maria) database.
nagios-plugins-ntp el7.rpm A Nagios plugin that can be used to check the NTP status of the machine it’s called on.
nagios-plugins-pgsql el7.rpm A Nagios plugin that can be used to check integrity and data entries within an PostgreSQL database.
nagios-plugins-samba el7.rpm A Nagios plugin that can be used to check status of your Samba mounts and their availability.
nagios-plugins-snmp el7.rpm A Nagios plugin that can query SNMP enabled appliances (routers, firewalls, switches, servers) and convert their output back to something Nagios can monitor or report.

Note: The source rpm can be obtained here which builds everything you see in the table above. It’s not required for the application to run, but might be useful for developers or those who want to inspect how I put the package together.

The main thing to know about this package after it is installed is the slew of new plugins available to you in /usr/lib64/nagios/plugins/ and a config file to get you started which references most of them in /etc/nagios/conf.d/nagios-plugin-commands.cfg.

Extra Plugins

There are a lot of great plugins on Nagios Exchange! I packaged just a few of them because they required patches and tweaks to work out of the box. All of these are available on my repository, but feel free to haul them down directly here:

Package Download Description
nagios-plugins-lvm el7.rpm / src.rpm / NE Source This plugin finds all LVM logical volumes, checks their used space, and compares against the supplied thresholds.
nagios-plugins-crm el7.rpm / src.rpm / NE Source A plugin for monitoring a Pacemaker/Corosync cluster.
Note: that this plugin requires perl-Nagios-Monitoring-Plugin to work.
nagios-plugins-drbd84 el7.rpm / src.rpm / NE Source A plugin for monitoring a DRBD v8.4 setup.

Credit

This blog took me a very (,very) long time to put together and test! The repository hosting alone accommodates all my blog entries up to this date. All of the custom packaging in this blog was done by me personally. If you like what you see and wish to copy and paste this HOWTO, please reference back to this blog post at the very least. It’s really all I ask.

Sources

The remaining portions of this series can be found here:

  • Part 2 – NRDP for Nagios Core on CentOS 7.x: This blog explains how awesome NRDP really is and why it might become a vital asset to your own environment. It’s also provides the first set of working RPMs (with SELinux support of course) of it’s kind.
  • Part 3 – NRPE for Nagios Core on CentOS 7.x: This blog explains how to set up NRPE (v3.x) for your Nagios environment. At the time this blog was written, there was no packaging of it’s kind for this version. So allow me be the first to share it with you!

30 thoughts on “Nagios Core 4.x Setup for CentOS 7.x”

  1. Hello,

    systemctl start httpd.status doesn’t work on my machine.
    systemctl start httpd worked πŸ˜‰

    greets

  2. Although yum appears to install the nagios plugins for selinux, in fact nothing gets installed. When I try to install the regular nagios-plugins package, it fails due to conflicts in the common as follows:
    Transaction check error:
    file /etc/nagios from install of nagios-common-4.0.8-2.el7.x86_64 conflicts with file from package nagios-4.2.2-2.el7.nuxref.x86_64
    file /usr/lib64/nagios/plugins from install of nagios-common-4.0.8-2.el7.x86_64 conflicts with file from package nagios-4.2.2-2.el7.nuxref.x86_64
    So, all service checks are failing because plugins are not here. What do I do?

    1. I honestly never tried upgrading from the EPEL version of Nagios (4.0.8) to my version. It certainly makes sense to support this though! I just created a small change to my packaging that will hopefully accommodate your situation. You might have to clear your cache first to see it (yum clean all).

      Try your upgrade again when you have the chance.

      1. Thank you! I completely removed nagios, cleaned the yum cache, removed the EPEL repository to eliminate any possibility of conflicts, set SELINUX to permissive, rebooted to make sure the decks were totally clear, then reinstalled all 4 of your packages: nagios, nagios-selinux, nagios-plugins & nagios-plugins-selinux. That went without a hitch so rebooted and launched the web UI. All came up red, but in a few minutes started to perform the checks and turn green. All the plugins are now in their proper directory. Woohoo! Thanks a million!!

  3. Hi again!!

    in centos7 with selinux enforced when try to execute one process again:

    avc: denied { getattr } for pid=2244 comm=”cmd.cgi” path=”/var/nagios/rw/nagios.cmd in audit.log

    Can fix!?

      1. Hi!!! nagios-selinux is installed!

        i installed the new one few minutes ago…

        [root@nagios ~]# getenforce
        Enforcing
        [root@nagios ~]# rpm -qa|grep nagios
        nagios-4.3.2-5.el7.x86_64
        nagios-plugins-mysql-2.2.1-1.el7.nuxref.x86_64
        nagios-common-4.3.2-5.el7.x86_64
        nagios-selinux-4.3.2-5.el7.x86_64
        nagios-plugins-selinux-2.2.1-1.el7.nuxref.x86_64
        nagios-plugins-pgsql-2.2.1-1.el7.nuxref.x86_64
        nagios-plugins-snmp-2.2.1-1.el7.nuxref.x86_64
        pnp4nagios-0.6.25-1.el7.x86_64
        nagios-plugins-2.2.1-1.el7.nuxref.x86_64
        nagios-plugins-ntp-2.2.1-1.el7.nuxref.x86_64

        now dont show status in the browser, say dont running…

        1. HI!!!

          when try to process nagios_epel.pp with semodule -i this fail…

          the last 2 lines say:

          /etc/selinux/final/targeted/contexts/files/file_contexts: Invalid argument
          libsemanage.semanage_validate_and_compile_fcontexts: setfiles returned error code 1.

          1. my hosted version of nagios doesn’t use the nagios_epel.pp.

            semodule -l | egrep nuxref-nagios
            # expected output:
            # nuxref-nagios-plugins	1.0	
            # nuxref-nagios	1.0	
            

            You could do the following to make sure you’re running the proper module:

            semodule -i /usr/share/selinux/targeted/nuxref-nagios.pp
            
  4. hi!!!!

    i found de error!

    the epel nagios overwrite your version…

    your version is nagios-4.3.2-2 and epel is 4.3.2-5.el7 ahahah.

    Can create a version with a release number more high? 4.3.2-6 for example.. !?

    the epel version have error with selinux ¬¬!

    thx

    1. Ah,

      Honestly, it might be better to use the EPEL version since it will probably have a bigger following. For years (and years) the EPEL version of Nagios was never maintained which was why i ended up making these blogs.

      It’s great to see that somewhere along the lines a/the maintainer picked up this project and is keeping it up to date now! πŸ™‚

      If you really need to use my copy you could do the following:

      # remove your current copy of nagios
      yum remove nagios*
      # install it again but make sure you tell yum to 'not' look at the epel repository
      yum install nagios --disablerepo=epel*
      
  5. HI!

    I use my own version but with centos6 without selinux. Now with centos7 i want try to use selinux always active…

    Is good to see some people like you post version like that, is a incredible job!!

    And put in exclude nagios from epel πŸ˜€

    Thx for all…

  6. Hi Again!

    I have a little issue again… Now only when rescheduled a job the nagios say:

    Error: Could not stat() command file ‘/var/nagios/rw/nagios.cmd’!

    and the audit log say:

    type=AVC msg=audit(1502232157.971:483): avc: denied { getattr } for pid=11202 comm=”cmd.cgi” path=”/var/nagios/rw/nagios.cmd” dev=”dm-0″ ino=4206095 scontext=system_u:system_r:nagios_script_t:s0 tcontext=system_u:object_r:httpd_sys_content_t:s0 tclass=fifo_file
    type=SYSCALL msg=audit(1502232157.971:483): arch=c000003e syscall=4 success=no exit=-13 a0=65b220 a1=7ffdb319bd00 a2=7ffdb319bd00 a3=0 items=0 ppid=9866 pid=11202 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm=”cmd.cgi” exe=”/usr/lib64/nagios/cgi/cmd.cgi” subj=system_u:system_r:nagios_script_t:s0 key=(null)

    This is the only issue, all work fine include when use pnp4nagios πŸ™‚

    Some help to fix that!?

    Thx.

  7. Hi!!

    And when stop or restart say this…
    Error: Could not create external command file ‘/var/nagios/rw/nagios.cmd’ as named pipe:

    this file not delete when stop, is not a really error but is cosmetic issue…
    Thx.

    1. Thank you for all of your feedback and testing! I’ll do my best to review everything you’ve presented and see if i can fix some/all of it this weekend. I’ll try anyway…

      As per the SELinux issue you mentioned a few comments back: If you could put your system in permissive mode (temporarily) with the command setenforce 0. Then capture the rest of the SELinux logs generated by the audit log over the next 24 hours, that would be great.

      #filter out all Nagios entries (after ~24 hrs with SELinux disabled):
      cat /var/log/audit/audit.log | egrep -i nagios
      

      Send me the output to lead2gold@gmail.com and I’ll update the SELinux module. You can turn back on SELinux at anytime with the command setenforce 1.

  8. Hiii, I would like to add a postgresql service in the server nagios but i find that is not compatiple
    he is my service

    define service{
            use                             generic-service
            host_name                       remotehost
            service_description             PGSQL Bloat
            check_command                   check_postgres
    
    }
    define service{
            use                             generic-service
            host_name                       remotehost
            service_description             PGSQL Locks
            check_command                   check_postgres!locks!40!80
    }
    
    1. First make sure /usr/lib64/nagios/plugins/check_pgsql is available to you:

      yum install nagios-plugins-pgsql
      

      Then you want to change your line to:

      define service{
      use generic-service
      host_name remotehost
      service_description PostgreSQL
      check_command check_pgsql
      }
      

      Now if you want to do custom checks such as locks and stuff…, you’ll need another plugin for that… Maybe this (also found here)?

      If you follow the link below, you’ll need to copy the check_postgres.pl into /usr/lib64/nagios/plugins/ directory (and make sure it’s executable: chmod 755 check_postgres.pl)

      define command{
        command_name    check_postgres
        command_line    /usr/lib64/nagios/plugins/check_postgres.pl $ARG1$ $ARG2$ $ARG3$
      }
      define service{
        use generic-service
        host_name remotehost
        service_description PGSQL Locks
        check_command check_postgres!locks!40!80
      }
      

      I’m not sure if the above will work (i’m just guessing here), but it should be pretty close to what you need to do.

  9. Looks like a naming issue is happening with nagios-contrib. Versions look spot on but the extra text on the end does not match.

    [root@localhost ~]# yum install -y nagios-contrib
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: dallas.tx.mirror.xygenhosting.com
    * extras: mirror.dal10.us.leaseweb.net
    * updates: mirrors.sonic.net
    Resolving Dependencies
    –> Running transaction check
    —> Package nagios-contrib.x86_64 1:4.3.4-2.el7.nuxref will be installed
    –> Processing Dependency: nagios = 4.3.4-2.el7.nuxref for package: 1:nagios-contrib-4.3.4-2.el7.nuxref.x86_64
    –> Finished Dependency Resolution
    Error: Package: 1:nagios-contrib-4.3.4-2.el7.nuxref.x86_64 (nuxref)
    Requires: nagios = 4.3.4-2.el7.nuxref
    Installed: 1:nagios-4.3.4-2.el7.nuxref.x86_64 (@nuxref)
    nagios = 1:4.3.4-2.el7.nuxref
    Available: nagios-4.3.2-7.el7.nuxref.x86_64 (nuxref)
    nagios = 4.3.2-7.el7.nuxref
    Available: nagios-4.3.4-1.el7.nuxref.x86_64 (nuxref)
    nagios = 4.3.4-1.el7.nuxref

    1. Great find!

      I think i looked after the issue and pushed the update. I also brought the version up to 4.4.0 (of Nagios) since it’s been recently released!

      Let me know if you have any issues!

  10. Good morning – In your article: https://support.nagios.com/forum/viewtopic.php?f=7&t=33090 you note that you have a blog that contains a how-to for Nagios and CentOS 7 with selinux – “I provide the src.rpm files there too if you don’t trust that random stranger on the internet type thing. My blog explains how to build it yourself anyway; it’s a little customized, but in a good way…”

    Is there a blog to go with the src.rpm you mention above? I tried setting contexts and am not quite there… I was thinking perhaps I just need to run audit2allow to get a customized policy. Unfortunately I am unable to use your repo and need to get nagios working with SELinux.

    Thx

  11. Hello,

    I cannot change the password for nagiosadmin. When i run the password script I am allowed to set a password, but the nsame password exists on the server and it never updates. Has anyone else run into this issue?

    1. To change the password, this should work:

      htpasswd /etc/nagios/htpasswd.users nagiosadmin

      You’ll be prompted to put your new password in twice and all should be fine. Make sure to run this as root (or you won’t have permissions to update the file).

Leave a Reply

Your email address will not be published. Required fields are marked *