Monitoring Web Services With Nagios
Nagios is one of my all time favorite open source projects, mainly because it never lets me down. Its interface can be a bit confusing at first, but once you get used to the flow of config files, it becomes very easy to maintain.
One great use for Nagios is to monitor web services to ensure that the service is up, and that it’s doing the right thing. Here’s a quick walk through of setting up Nagios to monitor a basic web service from Ubuntu.
The easiest way to get started is to run sudo apt-get install nagios3 from the command line. This will install Nagios, the plugins, Apache, and all of the required libraries. Once everything is installed and running, you’ll find the config files in /etc/nagios3. Each config file is important, and worth a look, but for today, we’ll just start with nagios.cfg.
Inside of nagios.cfg, you’ll find a directive like this:
This line tells Nagios to look inside of the conf.d directory and load all files it finds ending in .cfg. Create a new file named google.cfg, and drop this into it:
The IP address is just the first one that resolved for Google for me, and it works well for a quick rundown. As it sits right now, Nagios will monitor Google, and display red in the web interface if it finds Google down. Since we don’t want to be watching the web UI seven days a week, we’ll add an email alert. Open up the file contacts_nagios2.cfg in your favorite text editor, which will obviously be vi, right? You’ll find that Ubuntu has setup a very decent config skeleton for Nagios, and is sending all email alerts to the root user. Simply change the email address, and restart Nagios. Nagios will now send you an email when it finds a problem.
The beauty behind this setup is that it is easily reproducible, and easily scriptable. It is a little restrictive though. If we want to be even more proactive, we can check for a string inside the response we get back from the web host. To do that, we need to make two changes. First, open up /etc/nagios-plugins/config/http.cfg and add the following to the bottom of the file:
command_line /usr/lib/nagios/plugins/check_http -H '$HOSTADDRESS$' -I '$HOSTADDRESS$' -s '$ARG1'
Next, modify your newly created /etc/nagios3/conf.d/google.cfg file and add this to the bottom of the file:
This tells the check_http plugin to check for the string “google” in the response it gets back. To see exactly what the plugin is parsing, you can run
/usr/lib/nagios/plugins/check_http -H 126.96.36.199 -vv
With this basic setup, you can expand and customize your Nagios web service monitoring environment to suit your needs. Nagios may have a reputation for being complicated and difficult to set up, but once you understand the basics it becomes like second nature. I also have to commend the work that Ubuntu has done on the config files. Installing Nagios from the Ubuntu repositories now gives you a sane and expandable base to build your monitoring system on.