Nagios Configuration in Linux Ubuntu


Hi see How to install nagios on ubuntu and monitor Our Infrastructure .

So first of all see my Nagios Server and client Information then go ahead .

Nagios Server.
Hostname     -ubuntu
IP address   -192.168.0.160
Linux OS     -Ubuntu 15.4

Nagios Client(Linux) .
Hostname     -ubuntu
IP address   -192.168.0.100
Linux OS     -Ubuntu 14.4

Nagios Client (Windows)
Hostname        - win7
IP address      -192.168.0.240
Windows OS      -Windows-7

Before installing nagios we must install Lamp (Linux Apache Mysql PHP) 


LAMP is a combination of operating system and open-source softwares. The acronym LAMP came from the first letters of Linux, Apache HTTP Server, MySQL or MariaDB database, and PHP/Perl/Python. This tutorial describes how to install LAMP stack on Ubuntu 14.04, and other versions such as Ubuntu 14.10/13.10 etc.

So First update our repo database .
apt-get update

1.Install Apache
Apache is an open-source multi-platform web server. It provides a full range of web server features including CGI(Common Gateway Interface) SSL(Secure Sockets Layer) and virtual domains.

To install Apache, enter:

sudo apt-get install apache2
Test Apache:

Open your web browser and navigate to http://localhost/ or http://server-ip-address/. (It will open a default page of apache)
2. Install MySQL
MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases .

sudo apt-get install mysql-server mysql-client

During installation, you’ll be asked to setup new password for  MySQL user (root) . Enter the password and click Ok.



My password -12345
Confirm      -12345
MySQL is installed now.

You can verify the MySQL server status using command:

On Ubuntu 14.10 and previous versions:

sudo service mysql status (output like below)



4. Install PHP
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.

Install PHP with following command:

sudo apt-get install php5 php5-mysql libapache2-mod-php5

To test PHP, create a sample “testphp.php” file in Apache document root folder. And add some lines as bwlow

vi /var/www/html/testphp.php
<?php
phpinfo();
?>

Save file.

On Ubuntu 14.10 and lower versions:

sudo service apache2 restart

Navigate to http://server-ip-address/testphp.php. It will display all the details about php such as version, build date and commands etc.




.....LAMP Installed.....

Now Start The installation of Nagios .

install the following prerequisites for nagios .

sudo apt-get install build-essential libgd2-xpm-dev apache2-utils unzip

Create Nagios User And Group
Create a new nagios user account:

sudo useradd -m nagios  (-m option use for create user without create home directory)
sudo passwd nagios
My Password -nagios

Create a new nagcmd group for allowing external commands to be submitted through the web interface. And Add both the nagios user and the apache user(www-data) to the group.

sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data  

Now download nagios and it’s plugin ,
I create a folder downloads and download these files inside that folder .
mkdir /downloads
cd /downloads
I download these from it’s official sites .

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.1.1.tar.gz
wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz

Go to the folder where you’ve downloaded nagios, and extract it using command:


tar xzf nagios-4.1.1.tar.gz
change to the nagios directory:

cd nagios-4.1.1/
Run the following commands one by one from the Terminal to compile and install nagios.

sudo ./configure --with-command-group=nagcmd
sudo make all
sudo make install
sudo make install-init
sudo make install-config
sudo make install-commandmode

Now Install Nagios Web interface:

Enter the following commands to compile and install nagios web interface.

sudo make install-webconf (it may give this error)



The above error message describes that nagios is trying to create the nagios.conf file inside the /etc/httpd.conf/directory. But, in Ubuntu systems the nagios.conf file should be placed in /etc/apache2/sites-enabled/directory.

So, run the following command instead of using sudo make install-webconf.

sudo /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-enabled/nagios.conf

Check if nagios.conf is placed in /etc/apache2/sites-enabled directory.

sudo ls -l /etc/apache2/sites-enabled/

Sample output:


Create a nagiosadmin account for logging into the Nagios web interface. Remember the password you assign to this account. You’ll need it while logging in to nagios web interface..

sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
My Password -123456
Restart Apache to make the new settings take effect.
sudo service apache2 restart

Now install nagios-plugin .
Go to folder where plugin tar file .

cd /downloads
tar xzf nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-2.1.1/
Run the following commands one by one to compile and install it.
sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios
sudo make
sudo make install

Wait, We are not finished yet.

Now Configure Nagios:-
Nagios sample configuration files will be found in the /usr/local/nagios/etc directory. These sample files should work fine for getting started with Nagios. However, if you want, you’ll need to put your actual email ID to receive alerts.

To do that, Edit the /usr/local/nagios/etc/objects/contacts.cfg config file with your favorite editor and change the email address associated with the nagiosadmin contact definition to the address you’d like to use for receiving alerts.

vi /usr/local/nagios/etc/objects/contacts.cfg




Save the file.
Enable Apache’s rewrite and cgi modules:
sudo a2enmod rewrite
sudo a2enmod cgi
And Restart apache service.
sudo systemctl restart apache2
Or,

sudo service apache2 restart

Check nagios,conf file for any syntax errors:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


If there are no errors, start nagios service and make it to start automatically on every boot if any error read below .

sudo service nagios start

sudo ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios

Note: In Ubuntu 15.04, you will see the following error message while starting nagios service.




To fix this error, copy /etc/init.d/skeleton to /etc/init.d/nagios using the following command:

sudo cp /etc/init.d/skeleton /etc/init.d/nagios

Edit file /etc/init.d/nagios and add these lines as below:

vi /etc/init.d/nagios
DESC="Nagios"
NAME=nagios
DAEMON=/usr/local/nagios/bin/$NAME
DAEMON_ARGS="-d /usr/local/nagios/etc/nagios.cfg"
PIDFILE=/usr/local/nagios/var/$NAME.lock

Save and close the file.

Finally you need to change the permissions of the file

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

Now, you can start nagios service using command:

sudo /etc/init.d/nagios start

Now Access Nagios Web Interface
Open up your web browser and navigate to http://nagios-server-ip/nagios and enter the username as nagiosadmin and its password which we created in the earlier steps.
This is how Nagios administrative console looked:




Add Monitoring targets to Nagios server
Now, let us add some clients to monitor by Nagios server.

Now we have to install nrpe and nagios-plugins in our monitoring targets.
My client Linux   - ubuntu -14.1
IP Address         - 192.168.0.100
Hostname           - ubuntu
On The Client side.

sudo apt-get install nagios-nrpe-server nagios-plugins

Edit nrpe config file and add nagios server ip
vi /etc/nagios/nrpe.cfg
allowed_hosts=127.0.0.1 192.168.0.160

Save file .
Restart nrpe service 
sudo /etc/init.d/nagios-nrpe-server restart

Now go to Server Side.


and uncomment the following lines for active the servers directory .
vi /usr/local/nagios/etc/nagios.cfg

## Find and uncomment the following line ##
cfg_dir=/usr/local/nagios/etc/servers

Save file .
Create a directory called “servers” under “/usr/local/nagios/etc/”.

sudo mkdir /usr/local/nagios/etc/servers

Create config file for monitoring target (clients):
I am copying nagios default localhost file then modify it accordingly.

Cd /usr/local/nagios/etc/objects/

cp localhost.cfg /usr/local/nagios/etc/servers/

And now disable default localhost.cfg entry from nagios config file for avoid confliction.

vi /usr/local/nagios/etc/nagios.cfg
And comment the line by #.
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

Save file.
cd /usr/local/nagios/etc/servers/
Now rename file name as you comfortable.
mv localhost.cfg linux-servers.cfg


vi /usr/local/nagios/etc/servers/linux-servers.cfg







Save file.

Here, 192.168.1.100 is my nagios client IP address and ubuntu is the client system’s hostname.

Finally, restart nagios service.

sudo /etc/init.d/nagios restart
Or
sudo service nagios restart

Wait for few seconds, and refresh nagios admin console in the browser and navigate to “Hosts” section in the left pane. Now, You will see the newly added client will be visible there. Click on the host to see if there is anything wrong or any alerts it has.

Define services
We have just defined the monitoring host. Now, let us add some services of the monitoring host. For example, to monitor the ssh service, add the following lines shown in bold in the“/usr/local/nagios/etc/servers/linux-servers.cfg” file.

Vi /usr/local/nagios/etc/servers/linux-servers.cfg








Save and close the file. Restart Nagios service.

sudo /etc/init.d/nagios restart

Or,

sudo service nagios restart
Wait for few seconds (90 seconds by default), and check for the added services (i.e ssh) in the nagios web interface. Navigate to Services section on the left side bar, you’ll see the ssh service there.



Make firwall disable for testing purpose if everything ok then allow nagios port in firewall .



Now Monitor Windows machine by nagios.
Step 1: Download and Install NSClient++
Download the latest version of NSClient from its official site and start installation as below. The software will be named NSCP-xxxxxx.msi. During installation process, you would require to enter nagios server to allow server to connect nsclient through nrpe.













Now go to Nagios server and copy windows.cfg file template to server directory earlier we create .
Varify the connectivity between nagios-server and windows host-client .

/usr/local/nagios/libexec/use tab 192.168.0.240

cp /usr/local/nagios/etc/objects/windows.cfg /usr/local/nagios/etc/servers/windows-severs.cfg

Cd /usr/local/nagios/etc/servers/
vi windows-servers.cfg


  





Modify file as you have multiple servers and save file
Restart nagios service.

And now Login to your Nagios Web-Interface and see the result ,Your Nagios is working fine if still any issue then commnet below or write me at :- satishrawat92@gmail.com