Reading status in a MySQL database with NDOUtils

In this recipe, you'll learn how to install the NDOUtils extension to Nagios Core in order to have all of Nagios Core's configuration and data written into a MySQL database. This allows us to easily develop custom reports and interfaces for Nagios Core data with languages such as Perl and PHP and their standard interfaces for the popular MySQL server, as opposed to interacting with Nagios Core's own logs or data format. Some plugins such as NagVis use this format to read information about Nagios Core configuration and objects.

Getting ready

You will need a Nagios Core server version 4.0 or later. NDOUtils will probably still get installed and work on older versions of Nagios Core, but the installation process is slightly different; refer to the INSTALL file included in the NDO source for more information on this.

Nagios Core uses its event broker functionality to write information to the socket for the MySQL database to pick up. You will need to compile Nagios Core with the --enable-event-broker flag:

$ ./configure --enable-event-broker
$ make
# make install

If you are unsure whether you've compiled Nagios Core with this flag, it is probably a good idea to recompile and reinstall Nagios Core from your original sources with this installed. Don't forget to back up your previous installation in case of problems.

In order to compile the ndomod part of NDOUtils, you will need to have the MySQL client libraries and headers installed on the Nagios Core server. You will also need to have a MySQL server ready to store the data. You don't need to run the MySQL server on the same host as Nagios Core, but the Nagios Core server should be able to connect to it.

Finally, you should take note of the opening paragraph of README, which at the time of writing points out that NDOUtils for Nagios Core 4.0 is still officially in beta; you should read the note and be aware of the risks in installing it. In the author's own experience, however, the code is stable.

How to do it...

We can install the NDOUtils package for Nagios Core as follows:

  1. Download the latest NDOUtils .tar.gz from its Sourceforge site at http://sourceforge.net/projects/nagios/files/ndoutils-2.x/:
    $ wget http://downloads.sourceforge.net/project/nagios/ndoutils-2.x/ndoutils-2.0.0/ndoutils-2.0.0.tar.gz
    
  2. Unpack .tar.gz and change into the uncompressed directory:
    $ tar xzf ndoutils-2.0.0.tar.gz
    $ cd ndoutils-2.0.0
    
  3. Run ./configure and make to build the software. Note that there is no install target; we will be performing the installation manually:
    $ ./configure
    $ make
    

    Read the output of ./configure carefully if the build fails, to determine if you are missing any dependencies on your system. The output of ./configure should end with something like this:

    *** Configuration summary for ndoutils 2.0.0 02-28-2014 ***:
    
     General Options:
     -------------------------
     NDO2DB user:    nagios
     NDO2DB group:   nagios
    Review the options above for accuracy. If they look okay,
    type 'make' to compile the NDO utilities.
    
  4. On the MySQL server, create a database to store the Nagios Core information and a user account to access it. In this example, the MySQL server is running on the same host as the Nagios Core server (olympus.example.net), so the access will be done from localhost:
    mysql> CREATE DATABASE nagios;
    Query OK, 1 row affected (0.10 sec)
    mysql> CREATE USER 'ndo'@'localhost' IDENTIFIED BY 'mPYxbAYqa';
    Query OK, 0 rows affected (0.62 sec)
    mysql> GRANT ALL ON nagios.* TO 'ndo'@'localhost';
    Query OK, 0 rows affected (0.02 sec)
    

    We have used a random password after IDENTIFIED BY. You should generate your own secure password.

  5. Run the installdb script in the source to create the various tables Nagios Core will use. Use the database details established in the previous step:
    $ cd db
    $ ./installdb -u ndo -p mPYxbAYqa -h localhost -d nagios
    ** Creating tables for version 2.0.1
        Using mysql.sql for installation...
    ** Updating table nagios_dbversion
    Done!
    

    Don't be concerned about the following error message; it appears because you are installing the extension for the first time:

    DBD::mysql::db do failed: Table 'nagios.nagios_dbversion' doesn't exist at ./installdb line 51.
    
  6. Copy the compiled ndomod-4x.o module into the /usr/local/nagios/bin directory:
    # cd ..
    # cp src/ndomod-4x.o /usr/local/nagios/bin/ndomod.o
    
  7. Copy the sample configuration for the module into /usr/local/nagios/etc:
    # cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
    
  8. Make sure that it is readable only by the nagios user:
    # chown nagios.nagios /usr/local/nagios/etc/ndomod.cfg
    # chmod 0600 /usr/local/nagios/etc/ndomod.cfg
    
  9. Edit your nagios.cfg file:
    # vi /usr/local/nagios/etc/nagios.cfg
    
  10. Add a broker_module definition to the file and verify that the event_broker_options directive is set to -1:
    broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg
    event_broker_options=-1

Note that the broker_module and config_file definitions should be on the same line, but event_broker_options should be on a seperate line.

With this done, the broker module is said to be successfully installed and we can move on to installing the ndo2db daemon. To install the same, follow these steps:

  1. Copy the ndo2db-4x binary into the /usr/local/nagios/bin directory:
    # cp src/ndo2db-4x /usr/local/nagios/bin/ndo2db
    
  2. Copy the sample configuration for the daemon into /usr/local/nagios/etc:
    # cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
    
  3. Make sure that it is readable only by the nagios user:
    # chown nagios.nagios /usr/local/nagios/etc/ndo2db.cfg
    # chmod 0600 /usr/local/nagios/etc/ndo2db.cfg
    
  4. Edit the configuration file as installed:
    # vi /usr/local/nagios/etc/ndo2db.cfg
    
  5. Change the values in ndo2db.cfg to reflect the database details:
    db_host=localhost
    db_port=3306
    db_name=nagios
    db_user=ndo
    db_pass=mPYxbAYqa
  6. Test the ndo2db daemon by starting it and verifying that it is running with pgrep(1):
    # /usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
    # pgrep ndo2db
    32285
    

    If it works, you should add this command into your system's init scripts so that the daemon is started at boot time. The daemon-init file in the distribution may be helpful regarding this.

  7. Validate the configuration and restart the Nagios Core server:
    # /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
    # /etc/init.d/nagios restart
    
  8. With this done, inspecting the database tables in MySQL should show that they have been filled with information from Nagios Core, for example, the nagios_services table:
    $ mysql --user=ndo --password --database=nagios
    mysql> select count(1) from nagios_services;
    +----------+
    | count(1) |
    +----------+
    |       54 |
    +----------+
    1 row in set (0.00 sec)
    

How it works...

NDOUtils is in fact a collection of components, two of which we have installed in the previous section:

  • ndomod.o: This is used as a broker module to write events and data from Nagios Core to a Unix socket in /usr/local/nagios/var/ndo.sock. It runs as a module of the Nagios Core server.
  • ndo2db: This is used as a database backend to read the events and data from the Unix socket to which ndomod writes and to apply them as MySQL database operations. It runs independently as a daemon on the system and performs the MySQL connections and transactions.

The broker_module command updates these tables as plugins are run, hosts and services change state, notifications are issued, and other Nagios Core behavior takes place. It covers most of the data of interest to us quite comprehensively. Note that it includes the following information:

  • Configuration directives
  • Details for types of Nagios Core objects
  • Properties and current states of hosts
  • Acknowledgement and scheduled downtime information
  • Notification history and complete logging

The main reason to install NDOUtils is to put Nagios Core's data into a standardized format so that it can be read and processed by external applications, whether simple table-style reports or completely new application interfaces to the Nagios Core data. This tends to be much easier than custom building a Nagios Core CGI of your own!

There's more...

NDOUtils is useful, but some administrators find the overhead of maintaining the database server for it unnecessary. For very large installations with a lot of configuration and historical data, queries on the data can also be slow or otherwise delayed. This leads many administrators to prefer the use of MK Livestatus as an alternative. This program is also discussed in this chapter in the Reading status from a UNIX socket with MK Livestatus recipe.

To get the most out of NDOUtils, it's a good idea to take a look at its documentation at https://assets.nagios.com/downloads/nagioscore/docs/ndoutils/NDOUtils.pdf.

See also

  • The Reading status from a Unix socket with MK Livestatus recipe in this chapter
  • The Writing customized Nagios Core reports recipe in this chapter
  • The Getting extra visualizations with NagVis recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.219.217