Nagios Solution

Configuring and Installing Nagios Core 4 on CentOS 6

Introduction

Nagios is a powerful tool that allows you to monitor just about anything. It’s so easy to even take existing legacy system and adapt it to work with Nagios. When I came across kingkiwix and his blog here I wanted to repackage the content in a more user-friendly way for sharing. I wanted a solution that didn’t require me to pull in a bunch of development libraries and compilers. I also wanted a solution that I could re-use on other systems through a version controlled and packaged interface.


Those running CentOS 7.x might be interested in this new blog entry: Nagios Core 4.x Setup for CentOS 7.x.


RPM Solution

RPMs provide a version control and an automated set of scripts to configure the system how I want it. The beauty of them is that if you disagree with
something the tool you’re packaging does, you can feed RPMs patch files to accommodate it without obstructing the original authors intention.

Now I won’t lie and claim I wrote these SPEC files from scratch because I certainly didn’t. I took the stock ones that ship with both products (both Nagios and Nagios-Plugins) and modified them significantly to accommodate and satisfy my compulsive needs. πŸ™‚

My needs required a bit more automation in the setup as well as including:

  • The use of nagiocmd user group.
  • A /etc/nagios/conf.d directory to behave similar to how Apache works. I want to be able to drop configuration files into here and just have it work without re-adjusting configuration files.
  • Alerting Apache on new updates/installs so just installing the package alone is all you need to do.
  • Nagios Plugins should work right away once they are installed.
  • Nagios Plugins and permissions should adapt to the new nagiocmd user and place in a common directory Nagios was already configured to look in.
  • Nagios Plugins has to many dependencies; I wanted to break this up into separate packages for those who needed them. For example, I don’t use MySQL at all; so why should I need the MySQL Libraries installed on my system just to use Nagios Plugins.
  • Nagios group permissions added to /etc/nagios directory for people using third party management applications. Just add any third party app to the Nagios group and they’ll be able to manage your configuration too:
    # substitute <3rdpartyid> for the actual group/id belonging
    # to the software in question.
    usermod -a -G nagios <3rdpartyid>
    

Stop Babbling, Just Give Me All Your Hard Work

Of course here it is:

At a minimum you need to install both Nagios Core (v4.2.0) and it’s accompanied Nagios Plugins (v1.5) package.

For those who are interested, here is a quick direct link to my additions to the building environment and deployment:

I Installed the Packages, Now What?

The RPMs take care of just about everything for you, so there isn’t really much to do at this point.

  • Make sure Apache is running and if it isn’t start it:
    # the following way is a harmless way of checking if Apache is
    # running and starting it if it wasn't (requires root)
    service httpd status || service httpd start
    
    # If it wasn't started in the above command, you may want to
    # consider having it start up each time you reboot your machine
    # through the following command:
    chkconfig httpd --levels 345 on
    
  • Start Nagios
    # Startup Nagios (requires root)
    service nagios start
    
    # Consider having Nagios (Backend) start up each time you reboot
    # your machine through the following command:
    chkconfig nagios --levels 345 on
    
    # You'll also want to set up a nagiosadmin password
    # The below will set you up with one using the pass of 'password'
    sudo htpasswd -cb /etc/nagios/htpasswd.users nagiosadmin password
    

Visiting http://localhost/nagios/ should look similar to this if you followed the steps correctly

Visiting http://localhost/nagios/ should look similar to this if you followed the steps correctly

Yes, that’s right… you’re done! You can access your Nagios Web Interface by visiting http://localhost/nagios. How you configure your system further is beyond the scope of this tutorial/blog. My goal was just to get you started with an out of the box working Nagios solution :).

If your not already familiar with Nagios, you may want to read the documentation so you can monitor more then just the 8 example checks it sets up for you out of the box. There are also some great web configuration tools you can check out as well here.

Package Information

  • Software installs to: /usr/share/nagios
  • Plugins install to: /usr/lib64/nagios/plugins. Keep in mind I packaged everything in such a way that compiling it for 32-bit will work fine. In which case you’ll find plugins installed to: /usr/lib/nagios/plugins
  • Website is accessible at: http://localhost/nagios/ out of the box, but you can adjust it’s web configuration by adjusting /etc/httpd/conf.d/nagios.conf to your likings.
  • Nagios Core 4 configuration file can be found at: /etc/nagios/nagios.cfg and a newly added /etc/nagios/conf.d/ directory has been added to make some things easier to add/remove later.
  • # SELinux Users may wish to turn this flag on if they intend to allow it
    # to call content as root (using sudo) which it must do for some status checks.
    setsebool -P nagios_run_sudo on
    

    I want to repackage this myself

    All of my blogs provide a way to build everything from scratch yourself for those who don’t want to just download the binary packages and roll with them. I use mock for everything I build so I don’t need to haul in development packages into my native environment. You’ll need to make sure mock is setup and configured properly first for yourself:

    # Install 'mock' into your environment if you don't have it already.
    # This step will require you to be the superuser (root) in your native
    # environment.
    yum install -y mock
    
    # Grant your normal every day user account access to the mock group
    # This step will also require you to be the root user.
    usermod -a -G mock YourNonRootUsername
    

    At this point it’s safe to change from the ‘root‘ user back to the user account you granted the mock group privileges to in the step above. We won’t need the root user again until the end of this tutorial when we install our built RPM.

    # Perhaps make a directory and work within it so it's easy to find
    # everything later
    mkdir nagiosbuild
    cd nagiosbuild
    ###
    # Now we want to download all the requirements we need to build
    ###
    # Nagios Core 4
    wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.2.0.tar.gz
    # Nagios Plugins v1.5
    wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
    # Customize Nagios Spec Files
    wget http://repo.nuxref.com/pub/nuxref/nagios/nagios.spec
    # Customize Nagios Plugin Spec Files
    wget http://repo.nuxref.com/pub/nuxref/nagios/nagios-plugins.spec
    # Nagios Patch to enable a conf.d directory
    wget http://repo.nuxref.com/pub/nuxref/nagios/nagios.conf.d.patch
    # Nagios Patch to fix accumulating files in /tmp/*
    wget http://repo.nuxref.com/pub/nuxref/nagios/configtest.tmp.patch
    # Nagios Plugin Patch to include some sample configurations and
    # setup Nagios to work out of the box
    wget http://repo.nuxref.com/pub/nuxref/nagios/add.sample.command.cfg.patch
    
    ###
    # Prepare our mock environment
    ###
    # Initialize Mock Environment
    mock -v -r epel-6-x86_64 --init
    
    # Copy in our downloaded content:
    mock -v -r epel-6-x86_64 --copyin nagios-4.2.0.tar.gz 
       nagios-plugins-1.5.tar.gz 
       nagios.conf.d.patch 
       add.sample.command.cfg.patch 
       onfigtest.tmp.patch 
       /builddir/build/SOURCES
    
    mock -v -r epel-6-x86_64 --copyin nagios.spec nagios-plugins.spec 
       /builddir/build/SPECS
    
    # Install Dependencies
    mock -v -r epel-6-x86_64 install gd-devel zlib-devel 
       libpng-devel libjpeg-devel doxygen gperf perl-devel net-snmp-devel
    
    # Install Plugin Dependencies
    mock -v -r epel-6-x86_64 install bind-utils mysql-devel net-snmp-utils 
                   postgresql-devel ntp openldap-devel openssh-clients 
                   samba-client /usr/bin/mailq
    
    ###
    # Build Stage
    ###
    # Shell into our enviroment
    mock -v -r epel-6-x86_64 --shell
    
    # Change to our build directory
    cd builddir/build
    
    # Build our RPMS
    rpmbuild -ba SPECS/nagios.spec
    rpmbuild -ba SPECS/nagios-plugins.spec
    
    # we're now done with our mock environment for now; Press Ctrl-D to
    # exit or simply type exit on the command line of our virtual
    # environment
    exit
    
    ###
    # Save our content that we built in the mock environment
    ###
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/SRPMS/nagios-4.2.0-2.el6.src.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/SRPMS/nagios-plugins-1.5-5.el6.src.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-plugins-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-plugins-mysql-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-plugins-pgsql-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-plugins-ldap-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-ntp-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-snmp-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-samba-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-plugins-debuginfo-1.5-5.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-4.2.0-2.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-contrib-4.2.0-2.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-devel-4.2.0-2.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-selinux-4.2.0-2.el6.x86_64.rpm .
    mock -v -r epel-6-x86_64 --copyout \
       /builddir/build/RPMS/nagios-debuginfo-4.2.0-2.el6.x86_64.rpm .
    
    # *Note that all the commands that interact with mock I pass in 
    # the -v which outputs a lot of verbose information. You don't
    # have to supply it; but I like to see what is going on at times.
    
    # **Note: You may receive this warning when calling the '--copyout'
    # above:
    # WARNING: unable to delete selinux filesystems 
    #    (/tmp/mock-selinux-plugin.??????): #
    #    [Errno 1] Operation not permitted: '/tmp/mock-selinux-plugin.??????'
    #
    # This is totally okay; and is safe to ignore, the action you called
    # still worked perfectly; so don't panic πŸ™‚
    

    Considerations

    If you take Nagios seriously, you will really want to consider expanding it to support one (or all) of the following addons:

    • NRPE - Nagios Remote Plugin Executor

      NRPE – Nagios Remote Plugin Executor

      NRPE – Nagios Remote Plugin Executor
      NRPE is an addon that allows you to execute plugins on remote Linux/Unix hosts. This is useful if you need to monitor local resources/attributes like disk usage, CPU load, memory usage, etc. on a remote host. Similar functionality can be accomplished by using the check_by_ssh plugin, although it can impose a higher CPU load on the monitoring machine – especially if you are monitoring hundreds or thousands of hosts.
    • NSCA - Nagios Service Check Acceptor

      NSCA – Nagios Service Check Acceptor

      NSCA – Nagios Service Check Acceptor
      NSCA is an addon that allows you to send passive check results from remote Linux/Unix hosts to the Nagios daemon running on the monitoring server. This is very useful in distributed and redundant/failover monitoring setups.

    I’m considering packaging these as well to conform with the packages I’ve already provided here, but I’ll do that in another blog some other time if there ends up being a demand for it πŸ™‚.

    Dec 8th, 2013 Update: I ended up creating this blog to extend this one: Configuring and Installing NRPE and NSCA into Nagios Core 4 on CentOS 6.

    Credit

    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

    I referenced the following resources to make this blog possible:

    5 thoughts on “Configuring and Installing Nagios Core 4 on CentOS 6

    1. Rick White

      This has been super helpful, thanks! Do you happen to have a patch-file/spec for splitting out nagios-plugins into multiple packages (to spread out the dependencies)? I’d like to so the same in my environment.

      If you have that for the newer nagios-plugins-2.1.1, even better!

      Reply
    2. Andy Speagle

      Thanks for the document… it’s a good primer. However, I noticed that the selinux files and nagios.systemd service file referenced in the nagios.spec file seem to be missing… do you have those available?

      Thanks!

      Reply

    Leave a Reply

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