Tuesday, June 15, 2010

Misc 34

Today we'll touch up a little on our Linux. We'll do a little bit of network monitoring through an open source monitoring system known as Nagios. Nagios would alert users when certain critical conditions are triggered and again when things become better. To install Nagios in Debian, use:apt-get install nagios3

Nagios is managed through a web interface. However, to log into Nagios's web interface, we'll need to create an account and make it an administrator. By default, the account named "root" would be administrator after it's created. However, to show you how to make another account an administrator, I'll use the account name of "kelvin". To create such an account, type:
htpasswd -c htpasswd.users kelvin

The -c parameter in htpasswd makes the tool create a new file. The file created is "htpasswd.users" and the user "kelvin" is created. After the command is typed, you would be prompted for a password. Enter anything you want and it would create a hash of the password in the htpasswd.users file. The htpasswd.users file is actually referenced from within /etc/nagios3/apache2.conf under the line:
AuthUserFile /etc/nagios3/htpasswd.users

Now, since "kelvin" is not an administrator, we'll need to browse into the configuration folder to make changes to some configuration file:
cd conf.d

The file we're interested in is the contacts_nagios2.cfg file. This file defines the contacts ("Administrative Users") which manage the Nagios system. This is so that we can easily manage what users are contacted when different events are triggered. Right now, we'll need to create a contact for Kelvin. To do this, we'll turn the root account's contact into a template. To do this, add in the "name" attribute as shown:
define contact{
name generic-contact
contact_name root
alias Root
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email root@localhost

Now that we've made the root contact as a contact template named "generic-contact", we can create the kelvin contact using the generic-contact. We do this by using the "use" parameter:
define contact{
use generic-contact
contact_name kelvin
alias Kelvin
email kelvin@localhost

Now that we have a contact for "kelvin", we'll need to put him under the administrators group. To do this, add him as a member in the "admins" contact group at the bottom of the file as shown:

define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members root, kelvin

When you're done, do a restart of the nagios3 service as shown:
/etc/init.d/nagios3 restart

You should now be able to access the web interface through:

Now we'll do some simple monitoring on two systems called Kelvin-Laptop-1 and Kelvin-Laptop-2 at and respectively. We'll check if the systems are pingable. If they aren't, a notification will be sent to the emails of the contacts. We first need to specify a host. To do this, we'll need to add hosts into the generic-host_nagios2.cfg file. Notice that in the file we have a generic host template specified. This is the template we will reference when creating our new hosts. To create the two hosts, append these to the bottom of the file:
define host{
use generic-host
host_name Kelvin-Laptop

define host{
use generic-host
host_name Kelvin-Laptop-2

Now that we have the two hosts, it's time to explain how the services are applied to the hosts. Services can be applied to either hosts or hostgroups. Services are actually defined in services.nagios2.cfg. Notice that the services in there have a "hostgroup_name" parameter. This specifies which group of hosts the services should be applied to. The hostgroups are specified in hostgroups_nagios2.cfg. Another way to apply services is to apply it directly to a host. In this case, we use the "host_name" parameter instead, as shown in localhost_nagios2.cfg.

What we are doing now is that we want to apply the check_ping service to our hosts. If we scroll down, we'll notice that the check_ping service is applied to all hosts in the hostgroup "ping-servers". Therefore, we are now going to add our new hosts into the ping-servers hostgroup. To do this, modify hostgroups_nagios2.cfg as shown:
define hostgroup{
hostgroup_name ping-servers
alias Pingable Servers
members gateway, Kelvin-Laptop, Kelvin-Laptop-2

Take note that the "members" parameter refer to the "host_name" of the hosts and it is case sensitive. Do a restart on the nagios3 service and we should have the services coming up in the Service Details tab. If it all goes well, we'll see an OK beside each test. You can add services to the hosts by adding them into the appropriate groups.

No comments :

Post a Comment