Monday, September 27, 2010

How to install nagios on cpanel or linux server?

Guys,

Nagios is a powerful monitoring system that enables organizations to identify and resolve IT infrastructure problems before they affect critical business processes.

Nagios is an open source application that monitors for computer network problems so that they can be fixed more quickly. Nagios was designed for use with the Linux operating system, but it will also work under Unix and most Unix-based systems.

Nagios runs periodic checks on user-specified resources and services. Resources that can be monitored include memory usage, disk usage, microprocessor load, the number of currently running processes, and log files. Services that can be monitored include Simple Mail Transfer Protocol (SMTP), Post Office Protocol 3 (POP3), Hypertext Transfer Protocol (HTTP), and other common network protocols. The program can monitor environmental variables such as temperature, humidity, or barometric pressure in conjunction with the appropriate measuring hardware. A user-friendly Web-based graphical user interface is provided. An authorization system allows the administrator to restrict access as necessary.

I have referred following steps to install and configure the nagios.

Before proceeding to mention the steps let me tell some points. Here I have installed nagios on monitoring server. So, this server will monitor other remote servers. Here are the required tools for them.

[A] For monitoring server(Who will monitor others) : a)nagios b) nagios-plugin c) nrpe daemon
[B] For monitored remote server(who will be monitored) : a) nagios-plugin c) nrpe daemon
[C] Configuring monitoring server to monitor remote hosts.


Let IP of monitoring server is 64.34.203.8. and 76.74.238.165 is the IP of remote server.

Now please make it sure that following packages has installed on the server.

-------
php-mysql php-gd php-ldap php-xml gd gd-devel gcc glibc glibc-common libstdc++-devel gcc-c++perl-DBI perl-DBD-MySQL perl-Config-IniFiles rrdtool perl-rrdtool

Note : For cpanel server all the modules might have installed. You can also enable them using /scripts/easyapache or yum install mod_name
-------
============================================================================

I am mentioning step by step here :

[A] Installing and configuring the monitoring tools on monitoring server(Who will monitor others) : a)nagios b) nagios-plugin c) nrpe daemon :-

a)nagios :-

------------------
1. Login into the server via ssh and gain root access.
2. useradd nagios
3. passwd nagios
4. groupadd nagcmd
5. /usr/sbin/usermod -a -G nagcmd nagios
6. /usr/sbin/usermod -a -G nagcmd nobody

Note : here nobody is the apache user on my server(cpanel linux server). You can get the user by executing the following command :

root@server [/usr/local/src]# grep '^User' /usr/local/apache/conf/httpd.conf
User nobody
UserDir public_html
root@server [/usr/local/src]#

7. cd /usr/local/src/
8. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
9. tar -xvzf nagios-3.2.1.tar.gz
10. cd nagios-3.2.1
11. ./configure --with-command-group=nagcmd
12. make all
13. make install
14. make install-init
15. make install-config
16. make install-commandmode
17. make install-webconf

Note: During executing "make install-webconf" you'll face following error message.

``````````````````````````````````````````````````````````
root@server [/usr/local/src/nagios-3.2.1]# 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

I hope you got the clue from this error message. So, please make a directory and link with cpanel apache conf.d like :

root@server [/usr/local/src/nagios-3.2.1]# mkdir -p /etc/httpd/conf.d
root@server [/usr/local/src/nagios-3.2.1]# ln -s /etc/httpd/conf.d /usr/local/apache/conf.d

Then execute the above webconf once again. It'll create nagios.conf file in that conf.d directory. That's it. This is for integrating the nagios with webserver.
``````````````````````````````````````````````````````````

18. htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //provide password here.

That's it for nagios. For my case here are the content in conf.d

````````
root@server [/etc/httpd/conf.d]# ll
total 12
drwxr-xr-x 2 root root 4096 Sep 26 23:22 ./
drwxr-xr-x 18 root root 4096 Sep 26 23:17 ../
lrwxrwxrwx 1 root root 6 Sep 26 23:20 conf.d -> conf.d
-rw-r--r-- 1 root root 1010 Sep 26 23:22 nagios.conf
root@server [/etc/httpd/conf.d]#
````````

===================================

b) nagios-plugin installation :-

1. cd /usr/local/src/
2. wget wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-
plugins-1.4.11.tar.gz
3. tar -xvzf nagios-plugins-1.4.11.tar.gz
4. cd nagios-plugins-1.4.11
5. ./configure --with-nagios-user=nagios --with-nagio-group=nagios
6. make
7. make install
8. chkconfig --add nagios
9. chkconfig nagios on
10. chown nagios.nagios /usr/local/nagios
11. chown -R nagios.nagios /usr/local/nagios/libexec
12. /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

````````````````
10th command will check the configuration whether it's correct or not. If it's correct it'll show following :
..............
Total Warnings: 0
Total Errors: 0

Things look okay - No serious problems were detected during the pre-flight
check

If there is no error it means your nagios configuration is correct and you can
start nagios service.
.............

`````````````````

13. /etc/init.d/nagios start
14. ps aux | grep nagios //check whether nagios is running or not.

that's it for nagios plugin :)

===================================

c) nrpe daemon installation : --

1. cd /usr/local/src/
2. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
3. tar -xvzf nrpe-2.12.tar.gz
4. cd nrpe-2.12

......
Make sure openssl-devel has installed on the server. Check like

root@server [/usr/local/src/nrpe-2.12]# rpm -qa | grep openssl-devel
openssl-devel-0.9.8e-12.el5_4.6
root@server [/usr/local/src/nrpe-2.12]#

Else execute : yum install openssl*
......

5. ./configure
6. make all
7. make install-plugin
8. make install-daemon
9. make install-daemon-config
10. make install-xinetd

.......
Note : Make sure that xinetd has installed on the server. Else install it like yum install xinetd
.......

11. vi /etc/xinetd.d/nrpe and disable “only_from” directive. So, the content of the file will look like :

.......
root@server [~]# cat /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
service nrpe
{
flags = REUSE
socket_type = stream
port = 5666
wait = no
user = nagios
group = nagios
server = /usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
# only_from = 127.0.0.1
}
root@server [~]#
.......

12. echo "nrpe 5666/tcp # NRPE" >> /etc/services
13. Make it sure that you have opened the above port at the firewall.
14. chkconfig xinetd on
15. service xinetd restart

op will be like :

.....
root@server [/usr/local/src/nrpe-2.12]# chkconfig --list | grep xinetd
xinetd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
xinetd based services:
root@server [/usr/local/src/nrpe-2.12]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
root@server [/usr/local/src/nrpe-2.12]#
.....

That's it.

Test the NRPE daemon locally :

16. netstat -at | grep nrpe

Here is the o/p :
........
tcp 0 0 *:nrpe *:* LISTEN

root@server [/usr/local/src/nrpe-2.12]# netstat -plan | grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:*
LISTEN 31130/xinetd
root@server [/usr/local/src/nrpe-2.12]#
........

17. /usr/local/nagios/libexec/check_nrpe -H localhost

Here is the o/p :

........
root@server [/usr/local/src/nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
root@server [/usr/local/src/nrpe-2.12]#
........

===================================
==========================================================================================

[B] Installing and configuring the monitoring tools on monitored server (who will be monitored) : a) nagios-plugin c) nrpe daemon :----

Login into the remote server via ssh and gain root access.

a) : nagios-plugin installation :

1. cd /usr/local/src/
2. /usr/sbin/useradd nagios
3. passwd nagios
4. wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
5. tar -xvzf nagios-plugins-1.4.15.tar.gz
6. cd nagios-plugins-1.4.15
7. ./configure
8. make
9. make install
11. chown nagios.nagios /usr/local/nagios
12. chown -R nagios.nagios /usr/local/nagios/libexec
13. Make sure that xinetd has installed on the server. Else install using : yum install xinetd

That's it :)
===================================

b) nrpe daemon installation :-


1. cd /usr/local/src/
2. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
3. tar -xvzf nrpe-2.12.tar.gz
4. cd nrpe-2.12

......
Make sure openssl-devel has installed on the server. Check like

root@server [/usr/local/src/nrpe-2.12]# rpm -qa | grep openssl-devel
openssl-devel-0.9.8e-12.el5_4.6
root@server [/usr/local/src/nrpe-2.12]#

Else execute : yum install openssl*
......

5. ./configure
6. make all
7. make install-plugin
8. make install-daemon
9. make install-daemon-config
10. make install-xinetd

.......
Note : Make sure that xinetd has installed on the server. Else install it like yum install xinetd
.......

11. vi /etc/xinetd.d/nrpe and change the IP at the “only_from” directive. It'll be monitoring server's(who will monirot) IP. For my case it was 64.34.203.8. So, the content of the file will look like :

.......
root@server [/etc/xinetd.d]# hostname
server.loveme.com
root@server [/etc/xinetd.d]# cat /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
service nrpe
{
flags = REUSE
socket_type = stream
port = 5666
wait = no
user = nagios
group = nagios
server = /usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
only_from = 64.34.203.8
}
root@server [/etc/xinetd.d]#
.......

12. echo "nrpe 5666/tcp # NRPE" >> /etc/services
13. Make it sure that you have opened the above port at the firewall.
14. chkconfig xinetd on
15. service xinetd restart

op will be like :

.....
root@server [/usr/local/src/nrpe-2.12]# chkconfig --list | grep xinetd
xinetd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
xinetd based services:
root@server [/usr/local/src/nrpe-2.12]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
root@server [/usr/local/src/nrpe-2.12]#
.....


Test the NRPE daemon locally :

16. netstat -at | grep nrpe

Here is the o/p :
........
tcp 0 0 *:nrpe *:* LISTEN

root@server [/usr/local/src/nrpe-2.12]# netstat -plan | grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:*
LISTEN 31130/xinetd
root@server [/usr/local/src/nrpe-2.12]#
........

17. /usr/local/nagios/libexec/check_nrpe -H localhost

Here is the o/p :

........
root@server [/usr/local/src/nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
root@server [/usr/local/src/nrpe-2.12]#
........

That's it.

================================================================================


[C] Configuring monitoring server to monitor remote hosts :-

Login into the monitoring server(who will monitor) via ssh and gain root access.

1. Create a command definition: /usr/local/nagios/etc/commands.cfg

and add following entries in that file :
---
root@server [/usr/local/nagios/etc/objects]# cat /usr/local/nagios/etc/commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

root@server [/usr/local/nagios/etc/objects]
---

2. chown nagios. /usr/local/nagios/etc/commands.cfg
3. create the file /usr/local/nagios/etc/objects/linux-box-remote.cfg and add
following entries in that file.

........
root@server [/usr/local/nagios/etc/objects]# cat /usr/local/nagios/etc/objects/linux-box-remote.cfg
define host{
name linux-box-remote ; Name of this
template
use generic-host ; Inherit default values
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 10
check_command check-host-alive
notification_period 24x7
notification_interval 30
notification_options d,r
contact_groups admins
register 0 ; DONT REGISTER THIS - ITS A
TEMPLATE
}

define host{
use linux-box-remote ; Inherit default values from a
template
host_name server.ubeea2.com ; The name we're giving to this
server
alias ubeea2 ; A longer name for the server
address 76.74.238.165 ; IP address of the server
}

define service{
use generic-service
host_name server.ubeea2.com
service_description CPU Load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name server.ubeea2.com
service_description Current Users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name server.ubeea2.com
service_description /dev/hda1 Free Space
check_command check_nrpe!check_hda1
}
define service{
use generic-service
host_name server.ubeea2.com
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use generic-service
host_name server.ubeea2.com
service_description Zombie Processes
check_command check_nrpe!check_zombie_procs
}
root@server [/usr/local/nagios/etc/objects]#
..........

Note : Change the IP and hostname. Then call this file in the nagios.cfg file. Just add
like

4. cfg_file=/usr/local/nagios/etc/objects/linux-box-remote.cfg

Example :
........
root@server [/usr/local/nagios/etc]# grep linux-box-remote.cfg /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/linux-box-remote.cfg
........

5. chown nagios. /usr/local/nagios/etc/objects/linux-box-remote.cfg
6. check config like :
....
root@server [/usr/local/nagios/etc/objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

Make sure warning and error are zero.
....

7. service nagios restart
8. You can add multple remote host in the linux-box-remote.cfg file. Just add another entry like second defined "host" to last one(only you have to change the hostname and services those you want to monitor).

That's it. Try :)

===================================================================================

6 comments:

  1. geez! thanks for the documentation, it save me big time!

    ReplyDelete
  2. Thanks for sharing the article :)

    ReplyDelete
  3. Really superb tutorial you have shared here and it's very easy to understand through this post about this concept.Website Hosting India

    ReplyDelete