Nagios on nginx & Ubuntu 12.04

Before you begin installing Nagios, you must first install several prerequisites. Issue the following command:
apt-get install libperl-dev libpng12-dev libgd2-xpm-dev build-essential php5-gd wget libgd2-xpm
You will need to create a new system user account to run Nagios under. Issue the following commands:
adduser --system --no-create-home --disabled-login --group nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -a -G nagcmd www-data
Now get the sources, be sure to take the latest stable: http://www.nagios.org/download/
cd /opt
mkdir nagios
cd nagios
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.4.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.16.tar.gz
tar xvf nagios-3.4.4.tar.gz
tar xvf nagios-plugins-1.4.16.tar.gz
cd nagios
Configure
./configure --prefix /opt/nagios/nagios \
--sysconfdir=/etc/nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--with-command-user=nagios \
--with-command-group=nagcmd
Compile
make all
make install
make install-init
make install-config
make install-commandmode
create a password file, for we run nginx we don't have apache2-utils and use a python package http://trac.edgewall.org/browser/trunk/contrib/htpasswd.py
cd /usr/local/bin
wget wget http://trac.edgewall.org/export/10791/trunk/contrib/htpasswd.py
chmod +x htpasswd.py
htpasswd.py -c -b /etc/nagios/htpasswd.users nagiosadmin password
Edit the config file for contact and change the email.
nano /etc/nagios/objects/contacts.cfg
set logging to /var/log/nagios
mkdir /var/log/nagios
touch /var/log/nagios/nagios.log
chown nagios:nagios /var/log/nagios
nano /etc/nagios/nagios.cfg
and add the logging file, then install the plugins
cd /opt/nagios/nagios-plugins-1.4.16
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
Issue the following commands to ensure that Nagios is started when your system boots:
chmod +x /etc/init.d/nagios
/usr/sbin/update-rc.d -f nagios defaults
Check config for errors
/opt/nagios/nagios/bin/nagios -v /etc/nagios/nagios.cfg
If its ok, not red start the service:
/etc/init.d/nagios start
we also need fcgi
apt-get install spawn-fcgi fcgiwrap
Configure nginx:
nano /etc/nginx/sites-available/nagios
server {
        server_name example.com;
        access_log  /var/log/nginx/example.com.access.log;
        error_log   /var/log/nginx/example.com.error.log;
 
  auth_basic            "Restricted Nagios Area!";
  auth_basic_user_file  /etc/nagios/htpasswd.users;     
 
        root    /var/www;
        index   index.php index.html;
 
   location / {
    try_files $uri $uri/ index.php;
  }
 
  location /nagios {
    alias /opt/nagios/share/;
  }
 
  location ~ ^/nagios/(.*\.php)$ {
    alias /opt/nagios/share/$1;
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }
 
  location ~ \.cgi$ {
    root /opt/nagios/sbin/;
    rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
    fastcgi_param AUTH_USER $remote_user;
    fastcgi_param REMOTE_USER $remote_user;    
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
  }  
 
  location ~ \.php$ {
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
  }
}
Configuring Basic IRC Notifications You can also configure Nagios to send notifications to an IRC channel through a bot. Issue the following commands to download, extract, and build the IRC bot:
cd /opt/nagios
wget http://www.vanheusden.com/nagircbot/nagircbot-0.0.33.tgz
tar -zxvf nagircbot-0.0.33.tgz
cd nagircbot-0.0.33b
make
make install
You will need to provide a few parameters to the IRC bot such as its nickname and server in order to use it. For a list of parameters, issue the following command:
nagircbot -h
To start the Nagios IRC bot issue the nagircbot command. See the following example as a starting point. Replace irc.ducklington.net with the address of your IRC network, and #bucknell with the name of the IRC channel that the bot should send notifications to. Replace nagircbot with the desired nickname for your bot, and ident and realname with the ident and real name strings for the IRC bot.
nagircbot -f /opt/nagios/nagios/var/status.dat -s irc.server.net:6667 -c \#channel -C -n nagircbot -u ident -U realname -I 900
In the above example, "-f /usr/local/nagios/var/status.dat" tells the bot where to get status updates. The "-C" flag will allow the bot to send colored messages to the channel depending on the status of the service. When services are down, red status messages are displayed. These messages turn green when the service has recovered. Warnings are displayed in yellow, but do not typically represent a critical issue. The "-I 900" parameter tells the bot to send a status message to the channel every 900 seconds (or 15 minutes). For example, the bot may send something like "Critical: 0, warning: 1, ok: 6, up: 2, down: 0, unreachable: 0, pending: 0", which indicates that there are no critical messages and 1 warning. Nagios contains numerous features that are beyond the scope of this document. You are encouraged to explore the resources listed below and the administrative interface for more information regarding the setup and configuration of Nagios. Congratulations on your new Nagios monitoring and notification system!

Installing Prerequisites to send Mail Alerts

apt-get install mailutils postfix
When the installation process prompts you to define the type of mail setup you're running, select "Internet Site". You will also want to specify the machine specific hostname for this server during the installation process. Next, you'll need to update the path to the mail binary in the Nagios command file. Change both references from /bin/mail to /usr/bin/mail. The relevant section of this file should look like this: File:/usr/local/nagios/etc/objects/commands.cfg
define command{
  command_name    notify-host-by-email
  command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
 
# 'notify-service-by-email' command definition
define command{
 command_name    notify-service-by-email
 command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
In order for these changes to take effect, you will need to restart Nagios:
/etc/init.d/nagios restart

 


Proud member of DA.

Bloemfonteinstraat 62, 
    room 1,2,9
5642 EH Eindhoven
phone: +316-81888456