Posts Tagged ‘How imports Nagios alerts into the mysql database’

How imports historical Nagios log files into the mysql database

March 5th, 2010

NDO Utilities:-

The NDO utilities add-on, written by Nagios developer Ethan Galstad, is designed to output events and data from Nagios to standard files or to a Unix socket. It also comes with a module called NDO2DB that allows Nagios data to be written to a MySQL or PostgreSQL database The add-on is made up of the NDOMOD Event Broker module, which is loaded by Nagios at runtime. It dumps all events and data from Nagios to a regular file or a Unix domain socket. It also contains the ndo2db daemon, which reads data that has been sent for the NDOMOD module to a Unix domain socket and dumps it into a MySQL or PostgreSQL database. You can dump into multiple databases and have multiple instances of the NDOMOD module writing to the same domain socket. There is also a utility called FILE2SOCK, which reads data from a standard file and dumps it into a Unix domain socket. Suggested uses are to dump data from
NDOMOD that has been stored in a standard file into a Unix domain socket. Or if your Nagios server is remote from your database server, you can dump data into a standard file from NDOMOD, send the file via SSH or SFTP to the database server, and then dump the data into a Unix domain socket and from there into a database. Finally, there is the LOG2NDO utility, which imports historical Nagios log files into the ndo2db daemon and sends them to a Unix domain socket or to standard output.

To install the NDO add-on, you first need to download and unpack the module from the Nagios Sourceforge site at http://sourceforge.net/project/showfiles.php?group_id=26589&package_id=173832, as you can see here:
puppy# wget http://optusnet.dl.sourceforge.net/sourceforge/nagios/ndoutils-12272005.tar.gz

manoj# tar -zxf ndoutils-1.4b9.tar.gz
manoj# cd ndoutils-1.4b9

By default NDO utilities use Nagios user, but my requirements was diffrent, i want to compile NDO utilities with manoj user. So i have used the following options during the compile time
manoj#./configure –with-ndo2db-user=manoj –with-ndo2db-group=manoj –enable-mysql –disable-pgsql –with-mysql-lib=/usr/lib/mysql
manoj# make

You must grant the user you create, in this case nagios, the SELECT, INSERT, UPDATE, and DELETE privileges to the nagios database. Replace ‘password’ with an appropriate password for the database.

manoj#create user ‘nagios@%’ identified by ‘password’;
OR
manoj#create user nagios@’localhost’ identified by ‘password’;
manoj#grant insert,delete,select,update on *.* to nagios@’%’ ;
OR
manoj#grant all on *.*  to nagios@’%’ ;
manoj#grant all on *.* to nagios@’localhost’ ;
manoj#create database nagios_db
Change Mysql Root password
manoj#update user set password=PASSWORD(“new password”) where User=’root’;
manoj#flush privileges;

The NDO add-on contains a script to populate this newly created database with the required tables. For MySQL, it is called ndo-mysql.sql, and it is located in the db directory in the root of the package:
manoj# cd ndoutils-1.4b9
manoj(ndoutils-1.4b9/db)#./installdb  -u user -p password -h hostname -d database

To install the NDO module itself, install the compiled ndomod.omodule file located in the src directory. I recommend copying it into the Nagios bin directory, usually /usr/local/nagios/bin:

manoj# cp src/ndomod-3x.o /usr/local/nagios/bin/ndomod.o

You also need to copy the sample configuration file for the module, ndomod.cfg. It is located in the config directory in the NDO utilities package. I recommend installing it to the Nagios etc directory, usually /usr/local/nagios/etc:

manoj# cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg

You also need to install the ndo2db daemon and its configuration file. They are also located in the src and config directories, respectively, and I suggest you copy them to the same locations in your Nagios installation:

manoj# cp src/ndo2db-3x /usr/local/nagios/bin/ndo2db
manoj# cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg

Next, you need to modify your Nagios configuration file, nagios.cfg, to load the NDO module when Nagios starts. Add the following line to your nagios.cfg configuration file, usually located in /usr/local/nagios/etc:
broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg
(It should be on single line entry)

This configuration directive will load the ndomod.o NEB module when Nagios is started. You will need to restart Nagios to make the module active. The config_file part of the directive must be modified to specify the location of the module configuration file. You should ensure that the ownership and permissions of all these files is appropriate. They should generally all be owned by the user and group used by the Nagios server process and the configuration files only readable by that user:

manoj# chown prod:prod /usr/local/nagios/bin/ndo2db /usr/local/nagios/bin/ndomod.o
/usr/local/nagios/etc/ndo2db.cfg /usr/local/nagios/etc/ndomod.cfg
manoj# chmod 0600 /usr/local/nagios/etc/ndo2db.cfg /usr/local/nagios/etc/ndo2db.cfg

You may also want to modify the two configuration files, ndo2db.cfg and ndomod.cfg. By default, the ndomod.o NEB module outputs data to a Unix domain socket, /usr/local/nagios/var/ndo.sock, which is created by the ndo2db daemon when it is started. You will also need to modify the ndo2db.cfg configuration file to update it with the correct database name, username, and password to allow the ndo2db daemon to write to the database.

Now you can start ndo2db by using the following command
manoj# /usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg

The daemon is launched with one command-line option, the location of the ndo2db daemon’s configuration file ndo2db.cfg. The daemon will create the Unix domain socket, /usr/local/nagios/var/ndo.sock. As you can see, I used the su command to change to the user nagios before launching. You should run the ndo2db daemon as the nagios user to allow the Unix domain socket to be created with the correct ownership. This will allow the ndomod.o module, which is run with the ownership and permissions of the Nagios server process, to write to that domain socket.

The module logs events and errors in the default Nagios log file, usually /usr/local/nagios/var/nagios.log OR /usr/local/nagios/var/ndo2db.debug. Check this file for errors and messages.

Thanks
Manoj Chauhan