Posts here are mostly step-by-step guides on how to replicate something I have set up in the past. Read over my About page to see how I show commands/output and read the disclaimer.
I am just starting to upgrade / install Ubuntu 14.04 on my servers, so it’s time to create a guide to install Nagios 4 on Ubuntu 14.04. Apparently Nagios Core install scripts wern’t well tested for Debian systems. But in any case, it can be installed with a couple modifications.
This guide is going to be very similar to my other Nagios install guides.
s10-nagios
. Change at will, but adjust the commands accordingly.Base OS Installation
Install Ubuntu Server 14.04. If you are provisioning a VM, a 24GB drive will be plenty.
Set a root password. Yeah, it’s best to use sudo for the one-off commands. But sometimes being root is just easier.
[user]$ sudo passwd root
Be sure we are on the latest updates:
[root]$ apt-get update [root]$ apt-get dist-upgrade
Some basic reminders: Set account passwords, bash prompts, etc.
Nagios & Plugins Install
Prepare the Nagios User & Group
[root]$ useradd -m -s /bin/bash nagios [root]$ passwd nagios
Create the group needed for submitting commands from the web.
[root]$ groupadd nagcmd [root]$ usermod -a -G nagcmd nagios [root]$ usermod -a -G nagcmd www-data
Download / Extract Archives
[root]$ cd ~ [root]$ wget http://downloads.sourceforge.net/project/nagios/nagios-4.x/nagios-4.1.1/nagios-4.1.1.tar.gz [root]$ tar -xzvf nagios-4.1.1.tar.gz [root]$ wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz [root]$ tar -xzvf nagios-plugins-2.1.1.tar.gz
By the way, both of those tar command were without referencing the docs.
Compile and Install Nagios
Install some required packages.
[root]$ apt-get install libgd2-xpm-dev apache2 php5 apache2-utils build-essential unzip
[root]$ cd ~/nagios-4.1.1 [root]$ ./configure --with-command-group=nagcmd [root]$ make all
After the compiling is complete, the following options are presented:
– make install – This installs the main program, CGIs, and HTML files
– make install-init – This installs the init script in /etc/init.d
– make install-commandmode – This installs and configures permissions on the directory for holding the external command file
– make install-config – This installs SAMPLE config files in /usr/local/nagios/etc You’ll have to modify these sample files before you can use Nagios. Read the HTML documentation for more info on doing this. Pay particular attention to the docs on object configuration files, as they determine what/how things get monitored!
– make install-webconf – This installs the Apache config file for the Nagios web interface
– make install-exfoliation – This installs the Exfoliation theme for the Nagios web interface
– make install-classicui – This installs the classic theme for the Nagios web interface
I ran the following installations:
[root]$ make install [root]$ make install-init [root]$ make install-commandmode [root]$ make install-config [root]$ make install-webconf
Check out the output from this last command:
[root]$ make install-webconf /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf /usr/bin/install: cannot create regular file โ/etc/httpd/conf.d/nagios.confโ: No such file or directory make: *** [install-webconf] Error 1
Silly Nagios installer, this is a Debian box! Let’s install this Apache2 config manually.
[root]$ /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/conf-available/nagios.conf [root]$ ln -s /etc/apache2/conf-available/nagios.conf /etc/apache2/conf-enabled/nagios.conf
Don’t attempt to start Nagios yet.
Compile and Install Nagios Plugins
We first need to install some prerequisites.
[root]$ apt-get install libnet-snmp-perl libperl5.18 libpq5 libradius1 libsensors4 libsnmp-base libsnmp30 libtalloc2 libtdb1 libwbclient0 samba-common samba-common-bin smbclient snmp whois libmysqlclient-dev libssl-dev
Now install it
[root]$ cd ~/nagios-plugins-2.1.1 [root]$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios [root]$ make [root]$ make install
NRPE Plugin Make & Install
Note the steps below carefully. DO NOT run “make install”. All we want to do is copy out the check_nrpe plugin, which is apparently not included in the original plugin pack.
[root]$ cd ~ [root]$ wget http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz [root]$ tar -xzvf nrpe-2.14.tar.gz [root]$ cd nrpe-2.14 [root]$ ./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu [root]$ make
Instead of installing this whole package, we just want the plugin exec.
[root]$ cp src/check_nrpe /usr/local/nagios/libexec/
Configuration
Create the Apache login credentials.
[root]$ htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Configure your Nagios services / hosts. For now, I recommend that you only setup a couple service checks for localhost
while we build out the rest of the configuration.
BONUS TIP: Bash Alias
Create this bash alias to make it easier to run checks. For the rest of this guide, I will use the command nverify
to run a check assuming that you created this alias.
[root]$ vim ~/.bash_aliases
alias nverify="/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg"
Don’t forget to log out and then back in to this box to see the new alias.
Check the configuration.
[root]$ nverify
Hold your breath & start the services.
[root]$ service apache2 restart [root]$ service nagios start
After starting up Nagios, attempt to navigate to http://s10-nagios/nagios. Provide the necessary credentials (remember, username is nagiosadmin).
You will notice that there is a red X icon stating that Nagios is not running, and clicking on a CGI link will download the appropriate file – not the intended result.
The fix is simple – enable CGI!
[root]$ a2enmod cgi [root]$ service apache2 restart
Now configure Nagios to run at startup:
[root]$ ln -s /etc/init.d/nagios /etc/rc2.d/S20nagios
Checkpoint One
- Reboot
s10-nagios
- Verify that the service automatically started. Correct any startup config issues.
- Attempt to navigate to
http://s10-nagios/nagios
from a computer on the same LAN. - Login using credentials previously specified.
Nagiosgraph
Install some more prerequisites.
[root]$ apt-get install libcgi-pm-perl librrds-perl libgd-gd2-perl
Download and extract the files.
[root]$ cd ~
[root]$ wget http://downloads.sourceforge.net/project/nagiosgraph/nagiosgraph/1.5.2/nagiosgraph-1.5.2.tar.gz [root]$ tar -xzvf nagiosgraph-1.5.2.tar.gz [root]$ cd nagiosgraph-1.5.2
Verify that we have a working environment.
[root]$ ./install.pl --check-prereq
If everything looks good, continue with the install. I think it’s best to keep everything separate from the Nagios install to make upgrades easier.
[root]$ ./install.pl --layout standalone --prefix /usr/local/nagiosgraph
Most of the questions should be left at the default, except the last few. Questions that are not default are emphasized.
Destination directory (prefix)? [/usr/local/nagiosgraph] Location of configuration files (etc-dir)? [/usr/local/nagiosgraph/etc] Location of executables? [/usr/local/nagiosgraph/bin] Location of CGI scripts? [/usr/local/nagiosgraph/cgi] Location of documentation (doc-dir)? [/usr/local/nagiosgraph/doc] Location of examples? [/usr/local/nagiosgraph/examples] Location of CSS and JavaScript files? [/usr/local/nagiosgraph/share] Location of utilities? [/usr/local/nagiosgraph/util] Location of state files (var-dir)? [/usr/local/nagiosgraph/var] Location of RRD files? [/usr/local/nagiosgraph/var/rrd] Location of log files (log-dir)? [/usr/local/nagiosgraph/var] Path of log file? [/usr/local/nagiosgraph/var/nagiosgraph.log] Path of CGI log file? [/usr/local/nagiosgraph/var/nagiosgraph-cgi.log] URL of CGI scripts? [/nagiosgraph/cgi-bin] URL of CSS file? [/nagiosgraph/nagiosgraph.css] URL of JavaScript file? [/nagiosgraph/nagiosgraph.js] Path of Nagios performance data file? [/tmp/perfdata.log] URL of Nagios CGI scripts? [/nagios/cgi-bin] username or userid of Nagios user? [nagios] username or userid of web server user? [www-data] Modify the Nagios configuration? [n] y Path of Nagios configuration file? [/usr/local/nagios/etc/nagios.cfg] Path of Nagios commands file? /usr/local/nagios/etc/objects/commands.cfg Modify the Apache configuration? [n] y
Restart some services.
[root]$ service apache2 restart [root]$ service nagios restart
Graphs can now be accessed here: http://s10-nagios/nagiosgraph/cgi-bin/show.cgi
Template Hacks…err…Integration
The graphs can be integrated into the templates with a couple hacks. They are a bit cringeworthy, but they work.
To activate the pop-up graphs that we will include later, create this file.
[root]$ vim /usr/local/nagios/share/ssi/common-header.ssi
<script type="text/javascript" src="/nagiosgraph/nagiosgraph.js"></script>
To include icons for a specific service, you will need this line in your service definition:
action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$
I also have a special graph saved for ping tests. I use this to show RTA and packet loss. We are going to utilize both actionurl
and notesurl
for this.
action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit notes_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit
Since we make use of the notes icon, lets give it the graph icon as well.
[root]$ mv /usr/local/nagios/share/images/notes.gif /usr/local/nagios/share/images/notes.bak.gif [root]$ cp /usr/local/nagios/share/images/action.gif /usr/local/nagios/share/images/notes.gif
Go into your host configuration files and add the graph templates to your service definitions. Only add them to services that are returning performance data.
Here are some example services you can change in the default configuration files.
[root]$ vim /usr/local/nagios/etc/objects/localhost.cfg
Delete the service definition for Current Load
and replace with this.
define service { use local-service host_name localhost service_description Current Load check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0 action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$ }
Delete the service definition for Ping
and replace with this.
define service { use local-service host_name localhost service_description PING check_command check_ping!100.0,20%!500.0,60% action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=pl,data&db=pl,warn&db=pl,crit notes_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit&geom=1000x200' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&db=rta,data&db=rta,warn&db=rta,crit }
Check and restart Nagios.
[root]$ nverify [root]$ service nagios restart
Now reload your Nagios homepage to see the graph icons.
Checkpoint Two
You may have to let Nagios run for about 30 minutes to give the graphs a chance to collect some data.
- Navigate to
http://s10-nagios/nagios
from a computer on the same LAN. - Login using credentials previously specified.
- View the services page, and note that ping and other services should have graph icons next to them.
- Mouseover the icons, confirm that graphs are popping up.
- Click on the icons, verify that you are being led to the page that shows graphs.
Monitoring Nagios / MRTG
So you now have Nagios running to ensure all your servers and gadgets are running as expected. But…how do you know that Nagios is healthy? We should monitor that too! We are going to use MRTG to graph stats about how Nagios is running.
[root]$ apt-get install mrtg
Copy the default config file for Nagios stats.
[root]$ cp ~/nagios-4.1.1/sample-config/mrtg.cfg /usr/local/nagios/etc/ [root]$ mkdir /usr/local/nagios/share/stats
Configure the working directory.
[root]$ vim /usr/local/nagios/etc/mrtg.cfg
Add this to the top.
WorkDir: /usr/local/nagios/share/stats
Make the initial run.
[root]$ env LANG=C mrtg /usr/local/nagios/etc/mrtg.cfg
Configure the HTML page.
[root]$ indexmaker /usr/local/nagios/etc/mrtg.cfg --output=/usr/local/nagios/share/stats/index.html
Create a scheduled cron job to keep the status up to date.
[root]$ vim /etc/cron.d/nagiostats
*/5 * * * * root env LANG=C /usr/bin/mrtg /usr/local/nagios/etc/mrtg.cfg
Checkpoint Three
You may have to let Nagios run for about 30 minutes to give the graphs a chance to collect some data.
- Navigate to
http://s10-nag-01/nagios/stats/
from a computer on the same LAN. - Verify that you see a bunch of pretty graphs.
Nagios Template Tweaks
There are several additions / tweaks that are recommended for a more efficient experience.
Set the default page to the services detail page (may want to limit results or show a different page on a large installation)
[root]$ vim /usr/local/nagios/share/index.php
Find where $url
is defined, and change it to something more useful.
$url="cgi-bin/status.cgi?host=all&limit=0";
Also lets add some links to the sidebar – specifically the graphs.
[root]$ vim /usr/local/nagios/share/side.php
Add this section wherever you see fit.
<div class="navsection"> <div class="navsectiontitle">External Tools</div> <div class="navsectionlinks"> <ul class="navsectionlinks"> <li><a href="/nagios/stats" target="<?php echo $link_target;?>">Nagiostats</a></li> <li><a href="/nagiosgraph/cgi-bin/show.cgi" target="<?php echo $link_target;?>">Nagiosgraph</a></li> </ul> </div> </div>
Conclusion
You now have a working base Nagios install. Now you are off to build your service checks, find more plugins, and tweak your alerts. I can almost feel your anticipation!
Just want to thank you for this great tutorial. Really appreciated!
Thanks for this tutorial! Did anyone else get a 403 forbidden error trying to access http://servername/nagiosgraph? I did and I am trying to figure out why.
Ah yes, forgot about this. This has something to do with how permissions differ in Apache 2.4.7.
Relevant upgrade notes: http://httpd.apache.org/docs/2.4/upgrading.html
I found that this section is causing this denied error message:
You could technically change that to
Require all granted
, but that would create security issues. I would create a directory permissions specific to the nagiosgraph directory. I am currently unable to test how to do it, but I will update the article as soon as I can. In the meantime, if guides you to a solution, please post back. Thanks!by the way, the last version of nagiosgraph is 1.5.1 http://sourceforge.net/projects/nagiosgraph/files/nagiosgraph/1.5.1/
Also do you have any experience of using pnp4nagios?
Your guide was very helpful! I also got the security error with NagiosGraph, so I would really like to get a fix in place for it. I am a Linux novice, so getting around to do all of this stuff has been incredibly challenging for me. At least I’m getting a better understanding of the OS for it, though. ๐
Is there already a manual to fix the 403 forbidden error on the nagiosgraphs? Or is there somebody that can tell me where to edit:
Options FollowSymLinks
AllowOverride None
Require all denied
Your very first step is to set a sudo password, but if all you’re trying to do is run many commands as root, why not just leave the sudo account alone (recommended) and enter
sudo su
to elevate your prompt for the remainder of the session? I believe this is the recommended way, and generally people will freak out when you tell them to set a root password.
Yes, some people don’t like that. It’s a matter of preference though. Other distros have setting a root password as part of the setup process. I also get root passwords by default when provisioning Ubuntu VMs on cloud services. In the scope of this article, running
sudo su
would accomplish thing.That depends on what you consider “nothing.” Educating users about best practices for security on the operating system they are using seems a ways away from that to me. Additionally, some people may not be aware of the fact that you can elevate an entire session without knowing the root password. Sure, it may not always be applicable, but good security practices should be practiced as often as possible.
Either way, I thought the article was great with lots of helpful tips, like the bash alias. I have been running Nagios for years, and haven’t ever thought of that. Talk about doing things the hard way!
In Nagios 4.0.7, this template tweak no longer works, “Find where $corewindow is defined, and change it to something more useful.”
What works for me is change the line
$url = ‘main.php’;
to
$url = “cgi-bin/status.cgi?host=all&limit=0”;
I’m getting: “CRITICAL – Plugin timed out while executing system call” when trying to use check_snmp plugin. Anyone have a tip for me to fix this?
Sven – I would run the command in the terminal to see how long it actually takes. Nagios has a built-in timeout value when executing plugins, so that will probably have to be increased.
I found the solutions. This has to be done before nagios plugin is installed:
apt-get install snmp snmp-mibs-downloader
If anyone is still getting the 402 Forbidden error, I fixed it on mine.
In my nagiosgraph.conf I set it to:
Options ExecCGI
AllowOverride All
Require all granted
Note, I’m not 100% sure of the security impact by doing this, my server is in a closed environment so security is not an issue for me.
Andrew, thanks for this article, it was very helpful.
Hi Andrew,
I have multiple hosts to be monitored. I have installed NRPE client on them and configured. How can I add hosts to be monitored in my NAGIOS server machine. Could you please point me to such sample configurations
Regards,
Malintha
You can always use “sudo -s” instead of giving root a password.
I followed all the instructions on NagiosGraph but I get the
Not Found
The requested URL /nagiosgraph/cgi-bin/show.cgi was not found on this server.
I was looking somewhere else and they said I need to add some code to /usr/local/nagiosgraph/etc/nagiosgraph-apache.conf
but that file doesn’t exist. I even tried making a new file but that didn’t work. I’m very new to Nagios but how can I tell Apache about Nagios
Thank You
I even tried making changes to the /etc/apache2/sites-available/nagios.conf to point to /nagiosgraph/cgi but that just ended up breaking nagios so set it back:
This is the code I am putting in my nagios.cfg in apache2/sites-available but didn’t work. Am I suppose to edit this file? If not, then how can I get it configured? I can’t even view a simple: https://server/nagiosgraph/cgi-bin/show.cgi
# enable nagiosgraph CGI scripts
ScriptAlias /nagiosgraph/cgi-bin “/usr/local/nagiosgraph/cgi”
Options ExecCGI
AllowOverride None
Require all granted
# enable nagiosgraph CSS and JavaScript
Alias /nagiosgraph “/usr/local/nagiosgraph/share”
Options None
AllowOverride None
Require all granted
It`s the most useful tutorial that I found. Thank you!
the best guide that I’ve seen, thank you!