Friday, May 8, 2015

How to install Nagios on CentOS 7

The easiest way to install Nagios on CentOS /RHEL 7 is using yum. You can find Nagios rpm file in EPEL repository so the 1st step is enabling EPEL on your server:
# yum install epel-release
Now you can use yum to install Nagios rpm:
# yum install nagios
Now Nagios is ready to start. Let’s start Nagios and Apache webserver:
# systemctl start nagios.service 
# systemctl start htppd.service
If you want to monitor some other services on the nagios server, you have to install the appropriate plugins. So for monitoring apache web server:
# yum install nagios-pluins-http
Or simply install all available plugins in the yum repository: 
# yum install nagios-plugins-all
To enable Nagios and apache to start at boot time:
# systemctl enable nagios
The nagios service can not be enabled by systemctl similar to httpd. It could be controlled by legacy chkconfig as you can see in the following screenshot it is already enabled in runlevel 3 so we are fine and nagios will start automatically at boot time. 



Now the nagios is ready to use but if you want to access admin web page via a web browser you may receive an error message because port 80 in blocked by default. To open http port:
# firewall-cmd --zone=public --add-port=80/tcp --permanent
And then reload the firewall:
# firewall-cmd --reload
It should work right now but to double check, run:
# iptables -L | grep http

Open the web browser and go to your Nagios web admin page:

    http://<server-name-or-ip-address>/nagios

And enter the default login credentials. The default username is nagiosadmin and the password is nagiosadmin as well. If you want to replace the username and password, enter the following command:
# htpasswd -c /etc/nagios/passwd behnam
To add another user for Nagios simply use the same command without the c switch:
# htpasswd /etc/nagios/passwd ben
After opening the web admin page, you can select Hosts from the left panel. As you see the it monitors itself only.

Next step is Adding other targets/clients to Nagios server. Assume that the target has the same OS i.e. CentOS 7 or RHEL 7:
# yum install epel-release# yum install nrpe nagios-plugins-all
Open /etc/nagios/nrpe.cfg on the target machine and add the Nagios server IP address or FQDN:

  allowed_hosts=127.0.0.1 172.16.51.34

And start and enable the nrpe service on the target machine:
# systemctl start nrpe# chkconfig nrpe on
Now add the client(s) to server config file by opening /etc/nagios/nagios.cfg and uncomment the following line:

  cfg_dir=/etc/nagios/servers

Create the above-mentioned directory:
# mkdir /etc/nagios/servers
And create config file for the clients to be monitored in that directory:
# vi /etc/nagios/servers/clients.cfg
And put the following lines:

  define host{
  use                             linux-server
  host_name                       client
  alias                           client
  address                         172.16.51.101
  max_check_attempts              5
  check_period                    24x7
  notification_interval           30
  notification_period             24x7
  }

Then restart Nagios service on the server. 

Wait for a moment, and refresh Nagios web admin page then navigate to Hosts and see the clinet. 

We added monitoring target/host so far. Let's a service like ssh service. To do that, add the following lines to the end of clients.cfg file:

  define service {
        use                             generic-service
        host_name                       client
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }

Then restart Nagios service on the server. 

Wait for a moment, and refresh Nagios web admin page then navigate to Services and see the ssh service.