Installing Nagios on Ubuntu (or any Debian) isn’t smooth or easy; and the instructions that are on Nagios’s site don’t work. Through much trial and error and Googling I have finally managed to get a good installation working. I had the same issues with Nagios 3.5, and while there are older packages you can install from apt-get, they seem to be broken, or set up in some weird way I can’t fathom. I prefer to install from scratch as much as possible, both for compiling to my environment and for learning. This should probably still work on Ubuntu Server 12.x and Nagios 3.5 also.

Note: As usual I use vim (vi type editor, but easier to use), and in this case we are going to make ourselves root since there are so many commands, using sudo over and over again will be too much work. I also recommend starting from a fresh but completely updated box.

Here we go! Do the following commands:

sudo -i
apt-get install build-essential apache2 php5-gd libgd2-xpm libgd2-xpm-dev libapache2-mod-php5 sendmail-bin libssl-dev
cd /tmp
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.0.tar.gz
wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
tar zxvf nagios-3.4.1.tar.gz
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios
./configure --with-nagios-group=nagios --with-command-group=nagcmd -–with-mail=/usr/bin/sendmail
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If everything goes ok and there are no config errors, continue on:

cd /tmp/nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
replace /etc/init.d/nagios with (one provided)
htpasswd –c /usr/local/nagios/etc/htpasswd.users nagiosadmin

Now, the init script that comes with Nagios does not work in Debian. It was made for RedHat/CentOS. Someone kindly uploaded an older script for Nagios that does work for Debian. Do the following to replace the broken script easily:

First, delete the old file:

rm /etc/init.d/nagios

Then, make a new file:

vim /etc/init.d/nagios

and paste the contents below:

#!/bin/sh
#
# chkconfig: 345 99 01
# description: Nagios network monitor
#
# File : nagios
#
# Original Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)
#
# Description: Starts and stops the Nagios monitor
#              used to provide network services status.
#

# Load any extra environment variables for Nagios and its plugins
if test -f /etc/sysconfig/nagios; then
   . /etc/sysconfig/nagios
fi

status_nagios ()
{

   if test -x $NagiosCGI/daemonchk.cgi; then
      if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then
              return 0
      else
         return 1
      fi
   else
      if ps -p $NagiosPID > /dev/null 2>&1; then
              return 0
      else
         return 1
      fi
   fi

   return 1
}

printstatus_nagios()
{

   if status_nagios $1 $2; then
      echo "nagios (pid $NagiosPID) is running..."
   else
      echo "nagios is not running"
   fi
}

killproc_nagios ()
{

   kill $2 $NagiosPID

}

pid_nagios ()
{

   if test ! -f $NagiosRunFile; then
      echo "No lock file found in $NagiosRunFile"
      exit 1
   fi

   NagiosPID=`head -n 1 $NagiosRunFile`
}

# Source function library
# Solaris doesn't have an rc.d directory, so do a test first
if [ -f /etc/rc.d/init.d/functions ]; then
   . /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
   . /etc/init.d/functions
fi

prefix=/usr/local/nagios
exec_prefix=${prefix}
NagiosBin=${exec_prefix}/bin/nagios
NagiosCfgFile=${prefix}/etc/nagios.cfg
NagiosStatusFile=${prefix}/var/status.dat
NagiosRetentionFile=${prefix}/var/retention.dat
NagiosCommandFile=${prefix}/var/rw/nagios.cmd
NagiosVarDir=${prefix}/var
NagiosRunFile=${prefix}/var/nagios.lock
NagiosLockDir=/var/lock/subsys
NagiosLockFile=nagios
NagiosCGIDir=${exec_prefix}/sbin
NagiosUser=nagios
NagiosGroup=nagios

# Check that nagios exists.
if [ ! -f $NagiosBin ]; then
    echo "Executable file $NagiosBin not found.  Exiting."
    exit 1
fi

# Check that nagios.cfg exists.
if [ ! -f $NagiosCfgFile ]; then
    echo "Configuration file $NagiosCfgFile not found.  Exiting."
    exit 1
fi

# See how we were called.
case "$1" in

   start)
      echo -n "Starting nagios:"
      $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
      if [ $? -eq 0 ]; then
         su - $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile"
         rm -f $NagiosCommandFile
         touch $NagiosRunFile
         chown $NagiosUser:$NagiosGroup $NagiosRunFile
         $NagiosBin -d $NagiosCfgFile
         if [ -d $NagiosLockDir ]; then touch $NagiosLockDir/$NagiosLockFile; fi
         echo " done."
         exit 0
      else
         echo "CONFIG ERROR!  Start aborted.  Check your Nagios configuration."
         exit 1
      fi
      ;;

   stop)
      echo -n "Stopping nagios: "

      pid_nagios
      killproc_nagios nagios

      # now we have to wait for nagios to exit and remove its
      # own NagiosRunFile, otherwise a following "start" could
      # happen, and then the exiting nagios will remove the
      # new NagiosRunFile, allowing multiple nagios daemons
      # to (sooner or later) run - John Sellens
      #echo -n 'Waiting for nagios to exit .'
      for i in 1 2 3 4 5 6 7 8 9 10 ; do
          if status_nagios > /dev/null; then
         echo -n '.'
         sleep 1
          else
         break
          fi
      done
      if status_nagios > /dev/null; then
          echo ''
          echo 'Warning - nagios did not exit in a timely manner'
      else
          echo 'done.'
      fi

      rm -f $NagiosStatusFile $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile
      ;;

   status)
      pid_nagios
      printstatus_nagios nagios
      ;;

   checkconfig)
      printf "Running configuration check..."
      $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
      if [ $? -eq 0 ]; then
         echo " OK."
      else
         echo " CONFIG ERROR!  Check your Nagios configuration."
         exit 1
      fi
      ;;

   restart)
      printf "Running configuration check..."
      $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
      if [ $? -eq 0 ]; then
         echo "done."
         $0 stop
         $0 start
      else
         echo " CONFIG ERROR!  Restart aborted.  Check your Nagios configuration."
         exit 1
      fi
      ;;

   reload|force-reload)
      printf "Running configuration check..."
      $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
      if [ $? -eq 0 ]; then
         echo "done."
         if test ! -f $NagiosRunFile; then
            $0 start
         else
            pid_nagios
            if status_nagios > /dev/null; then
               printf "Reloading nagios configuration..."
               killproc_nagios nagios -HUP
               echo "done"
            else
               $0 stop
               $0 start
            fi
         fi
      else
         echo " CONFIG ERROR!  Reload aborted.  Check your Nagios configuration."
         exit 1
      fi
      ;;

   *)
      echo "Usage: nagios {start|stop|restart|reload|force-reload|status|checkconfig}"
      exit 1
      ;;

esac

# End of this script

Save and exit.

Note: If you are in VIM through ssh/putty, you can just right-click and it pastes in perfectly.

Next, make the file executable:

chmod a+x /etc/init.d/nagios

Finally, start the Nagios service and restart Apache2:

service nagios start
service apache2 restart

At this point you will have seen another error when apache2 restarts. See this post to fix the Apache2 Fully Qualified Name Error.

Now, you should be able to go to your machine in a web browser. Go to http://yourmachineipornamehere/nagios.

Optional but recommended:

If you would like the service to start up automatically on boot (which is recommended), just add it to rc.d:

sudo update-rc.d minidlna defaults