Configure Simple Nagios Service Checks
Once you have followed the Nagios monitoring system quick setup guide, you have a working Nagios system to experiment with. Now we will describe how to add your service checks so you can put Nagios to real use.
Just to recap, we assume $NAGIOS is the Nagios base installation
directory. If you follow our earlier guide, it points to /opt/dev/nagios
and you can add the following line to your $HOME/.bashrc file so
later we can just reference $NAGIOS.
export NAGIOS=/opt/dev/nagios
Let's start with MySQL server monitoring. By default all Nagios service check
plugins are installed in the $NAGIOS/libexec directory. It is also
what the Nagios macro $USER1$ points to. Usually you can find out how
to use the plugins by running them with the -h flag. One of the
plugins to check MySQL server is $NAGIOS/libexec/check_mysql. After
checking its help with -h, probably only the -s matters
to us here. It accepts an argument of the socket file that check_mysql
should use.
If you have followed our
guide on creating a local LAMP (Linux, Apache, MySQL and PHP/Perl) development
environment, then the socket file for the local MySQL server is
/tmp/mysql.sock. You can try the following command:
$NAGIOS/libexec/check_mysql -s /tmp/mysql.sock
It should output something like:
Uptime: 29541 Threads: 1 Questions: 101 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.003
If you check the exit code immediately with echo $?, it should be 0.
Now run the following command and type in MySQL root user's password to shut down the MySQL server temporarily
mysqladmin -S /tmp/mysql.sock -u root -p shutdown
Now you run check_mysql again and should get error like this
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
and the exit code is 2. So this is how Nagios plugin works in general.
Now we are ready to define the Nagios command and service for checking
local MySQL server. Open the
$NAGIOS/etc/objects/commands.cfg file which defines all
the Nagios commands and add the following lines:
define command {
command_name check_mysqld
command_line $USER1$/check_mysql -s $ARG1$
}
In fact, you can find plenty of examples in the sample file provided by
Nagios distribution. As we have mentioned earlier, $USER1$
is a macro defined in Nagios' configuration file
$NAGIOS/etc/resources.cfg that points to
the $NAGIOS/libexec directory.
In contrast, $ARG1$ is the first argument you pass to the
check_mysqld command when you define your MySQL service check.
Now open the
$NAGIOS/etc/objects/localhost.cfg file where all the service
checks for the localhost are defined. Similarly, you can add
the following lines to the end of the file.
define service{
use local-service ; Name of service template to use
host_name localhost
service_description MySQL server
check_command check_mysqld!/tmp/mysql.sock
notifications_enabled 0
}
Please note here the exclamation mark (!) is used to separate the arguments from the command. Now you can run
$NAGIOS/bin/nagios -v $NAGIOS/etc/nagios.conf
to verify if these configuration files are correct. If so, then you can send
a HUP signal to the Nagios daemon to reread the configuration
files. You can find the process ID (PID) of the Nagios daemon with the
ps command or
with $NAGIOS/bin/nagiostats and then run the command:
kill -HUP <nagios_pid>
Now point your browser to
http://<host>:<port>/nagios/
and choose "Service Detail" under the "Monitoring" heading from the
left-side navigation menu, you will see the MySQL server
appears under localhost. You can stop and start
your MySQL server multiple times to see what happens to the status
of the service.
References
-
Nagios 3.x documentation:
This is the online manual that you may need to reference from time to time.
After Nagios installation, a local copy should also be available
from
http://<host>:<port>/nagios/docs/index.html.