Build a monitoring server using Nagios

Source: Internet
Author: User

Build a monitoring server using Nagios

Chapter 4 using Nagios to build a Monitoring Server

About Nagios]

Nagios is an open-source application software for monitoring systems and networks. It is deployed on a server-client. That is to say, you must first deploy the corresponding main suite on a server, then, deploy the client program on the server to be monitored so that the server can communicate with the client to monitor various resources on the client. Nagios is very powerful. Almost all projects can be monitored, from the survival status of the server to a service (web) on the server ). These functions are implemented through custom plug-ins (or scripts.

When Nagios detects a resource exception, it notifies the user. You can connect to the SMS interface or the email interface. You can view the resources monitored by Nagios on the web page. The default Nagios server can only monitor several simple projects, other monitoring projects such as services are implemented by our own plug-ins.

 

Software to be downloaded]

Nagios-3.0.5

Nagios-plugins-1.4.13

Nrpe-2.12.tar.gz

Apache-2.2.11

// The above software versions can be different

 

Monitoring Center Server Configuration]

1. install apache (For details, refer to the relevant content in chapter 16th. You only need to install it and configure it later)

 

2. Create a nagios account

Useradd nagios

 

3. Download Software

Cd/usr/local/src/

Wget http://syslab.comsenz.com/downloads/linux/nagios-3.0.5.tar.gz

Wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz

Wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz

 

4. Compile and install nagios

Cd/usr/local/src/

Tar zxvf nagios-3.0.5.tar.gz

Cd nagios-3.0.5

./Configure -- prefix =/usr/local/nagios

Make all

Make install

Make install-init# Make nagios a running script to enable nagios to start with the system boot.

Make install-config #Copy the configuration file sample to the nagios installation directory.

Make install-commandmode #Grant External commands the permission to access the nagios configuration file

Chown-R nagios: nagios/usr/local/nagios

 

5. Compile and install nagios-plugins

Cd/usr/local/src/

Tar zxvf nagios-plugins-1.4.13.tar.gz

Cd nagios-plugins-1.4.13

./Configure -- prefix =/usr/local/nagios -- with-nagios-user = nagios -- with-nagios-group = nagios

Make & make install

To check whether the installation is successful, follow these steps:

Ls/usr/local/nagios/libexec/

Check whether there is any plug-in file in this directory.

 

6. Install nrpe

Cd/usr/local/src/

Tar zxvfNrpe-2.12.tar.gz

Cd nrpe-2.12

./Configure -- enable-ssl -- enable-command-args

Make all

Make install-plugin

Make install-daemon

Make install-daemon-config

 

7. Configure the web interface

Vim/usr/local/apache2/conf/httpd. conf

Add the following content at the end:

ScriptAlias/nagios/cgi-bin/usr/local/nagios/sbin

<Directory "=" "usr =" "local =" "nagios =" "sbin =" ">

AllowOverride AuthConfig

Options ExecCGI

Order allow, deny

Allow from all

 

Alias/nagios // usr/local/nagios/share/

Options None

AllowOverride AuthConfig

Order allow, deny

Allow from all

 

 

8. Configure nagios

Cd/usr/local/nagios/etc/

Vim cgi. cfg

Change use_authentication = 1 to use_authentication = 0, which means no user verification is required.

 

9. Start nagios

Check Before starting:

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

If the following figure is displayed, the configuration is correct.

Total Warnings: 0

Total Errors: 0

Start command:

/Etc/init. d/nagios start

Or:

/Usr/local/nagios/bin/nagios-d/usr/local/nagios/etc/nagios. cfg

 

Now you can access nagios on the web page and enter:

Http: // IP/nagios.

 

Deploy nagios on the client on the machine to be monitored]

If you open the web page, click Host Detail in the left column to view a row of data in the right column. The Host name is "localhost", the Status is "up", and green is displayed, if it is another color, it indicates that your localhost has a problem. Currently, there is only one row of data, that is, only one host in the monitoring center (localhost) is monitored. How can we add other machines to be monitored by it? Therefore, you need to deploy nagios software on the machine to be monitored.

1. Add an account

Useradd nagios

 

2. Install nrpe

Cd/usr/local/src/

Wget http://syslab.comsenz.com/downloads/linux/nrpe-2.12.tar.gz

Tar zxvfNrpe-2.12.tar.gz

Cd nrpe-2.12

./Configure -- enable-ssl -- enable-command-args

Make all

Make install-plugin

Make install-daemon

Make install-daemon-config

 

3. Install nagios-plugin

Cd/usr/local/src/

Wget http://syslab.comsenz.com/downloads/linux/nagios-plugins-1.4.13.tar.gz

Tar zxvf nagios-plugins-1.4.13.tar.gz

Cd nagios-plugins-1.4.13

./Configure -- prefix =/usr/local/nagios -- with-nagios-user = nagios -- with-nagios-group = nagios

Make & make install

Even if the installation is complete, check whether there are four directories in the/usr/local/nagios/directory: bin etc libexec share also contains many files starting with check _ in the libexec directory. If your machine does not exist, reinstall it.

 

4. Configuration

Vim/usr/local/nagios/etc/nrpe. cfg

Change "allowed_hosts = 127.0.0.1" to "allowed_hosts = 127.0.0.1, 10.0.4.67"

// The following IP address is the IP address of the server.

Change "dont_blame_nrpe = 0" to "dont_blame_nrpe = 1"

 

5. Start nrpe

/Usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe. cfg-d

 

Add monitored hosts in the monitoring center]

To add a host, you must go to the server (Monitoring Center) to modify the configuration file.

1. Modify the main configuration file

Cd/usr/local/nagios/etc/

Vim nagios. cfg

Added content:

Export _dir =/usr/local/nagios/etc/services # define a directory and put all the newly added host information files here

 

2. Add monitored host information

Mkdir/usr/local/nagios/etc/services

Cd/usr/local/nagios/etc/services

Vim 10.0.4.56.cfgAdd the following content:

define host{use linux-serverhost_name 10.0.4.56alias 10.0.4.56address 10.0.4.56}define service{use generic-servicehost_name 10.0.4.56service_description check_pingcheck_command check_ping!100.0,20%!200.0,50%max_check_attempts 5normal_check_interval 1}define service{use generic-servicehost_name 10.0.4.56service_description check_sshcheck_command check_sshmax_check_attempts 5normal_check_interval 1}define service{use generic-servicehost_name 10.0.4.56service_description check_httpcheck_command check_httpmax_check_attempts 5normal_check_interval 1}

// Note that the IP address here is the client IP address. The monitored items include three ping, ssh, and http. In fact, the scripts used by these three projects are local scripts. That is to say, even if nagios and nrpe are not installed on the remote host, these projects can be monitored. However, if you want to monitor load, disk, and so on, you need to use the nrpe service. The principle is very simple. Both load and disk need to log on to the remote host to obtain information, while ping, ssh, http is not required. The process of obtaining relevant information from the remote host is completed by nrpe. If the nrpe service is not started on your client, we cannot obtain the load and disk information of the remote host. The following describes how to use nrpe to monitor remote hosts.

Edit/usr/local/nagios/etc/objects/commands. cfg on the server

Vim/usr/local/nagios/etc/objects/commands. cfg# Add the following content at the end

define command{command_name check_nrpecommand_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$}

Then edit 10.0.4.56.cfg (or server)

Cd/usr/local/nagios/etc/servicesvim 10.0.4.56.cfg # Add the following content: define service {use generic-servicehost_name 10.0.4.56service _ description check_loadcheck_command check_nrpe! Check_loadmax_check_attempts 5normal_check_interval 1} define service {use generic-servicehost_name 10.0.4.56service _ description check_disk_hda1check_command check_nrpe! Check_hda1max_check_attempts 5normal_check_interval 1} define service {use generic-servicehost_name 10.0.4.56service _ description check_disk_hda2check_command check_nrpe! Check_hda2max_check_attempts 5normal_check_interval 1}

// Here, we need to explain the related "check_command". Let's take a look at this "check_nrpe! Check_load "the check_nrpe here is the above/Usr/local/nagios/etc/objects/commands. cfgAs defined in, check_load is a Command Script defined on the remote host. Where to define it. Why add "!" in the middle "!", This is a unique form of nagios and does not need to be concerned. The script used above needs to be defined on the remote host.

Edit on remote host/Usr/local/nagios/etc/nrpe. cfgFile

Vim/usr/local/nagios/etc/nrpe. cfg(Client)

Change the line "command [check_hda1]":

Command [check_hda1] =/usr/local/nagios/libexec/check_disk-w 20%-c 10%-p/dev/hda1

Then add another row:

Command [check_hda2] =/usr/local/nagios/libexec/check_disk-w 20%-c 10%-p/dev/hda2

// Here, check_hda1 and check_hda2 are both customized and correspond to check_command in the service defined on the server side. That is to say, if a service (in nrpe mode) is defined on the server side, the corresponding script must be defined in nrpe. cfg on the client side. After saving the file, you need to restart the nrpe service.

Killall nrpe;/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe. cfg-d(Client)

 

3. Restart the nagios Service

If you modify the configuration, restart the service to make the added monitoring host take effect.

/Etc/init. d/nagios restart(Server)

Go to the web page and check whether there is an additional IP address 10.0.4.56.

 

Custom monitoring script on nagios Client]

At the beginning, I mentioned that we can customize the write monitoring step. From the above example, we can also see that the disk status is monitored based on the disk partition. This is troublesome because the disk partitions of each host are different (the same is good), and how many partitions need to be defined. Therefore, I will customize a shell script to monitor all the disk partitions:

1. Create a script on the client/Usr/local/nagios/libexec/check_disk.sh

Vim/usr/local/nagios/libexec/check_disk.shWrite the following content: (client)

#!/bin/bashrow=`df -h -P|wc -l`status=0for i in `seq 2 $row`dospare=`df -h -P|sed -n "$i"p|awk '{print $4}'`use_percentage=`df -h -P|sed -n "$i"p|sed -n "s/\%//"p|awk '{print $5}'`spare_percentage=`expr 100 - $use_percentage`partition_name=`df -h -P|sed -n "$i"p|awk '{print $6}'`if [ "$spare_percentage" -lt "3" ];thenecho -n "$partition_name CRITICAL ${spare_percentage}% $spare "status[$i]=2elif [ "$spare_percentage" -lt "5" ];thenecho -n "$partition_name WARNING! ${spare_percentage}% $spare "status[$i]=1elseecho -n "$partition_name OK ${spare_percentage}% $spare "status[$i]=0fidonezhuangtai=0for j in `seq 2 $row`doif [ "${status[$j]}" -gt "$zhuangtai" ];thenzhuangtai=${status[$j]}fidoneexit $zhuangtai

2. Save the script and modify its permissions.

Chmod + x/usr/local/nagios/libexec/check_disk.sh(Client)

3. Edit the/usr/local/nagios/etc/nrpe. cfg file.

Vim/usr/local/nagios/etc/nrpe. cfg# Add a line: (client)

Command [check_disk] =/usr/local/nagios/libexec/check_disk.sh

Save and restart the nrpe Service

Killall nrpe;/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe. cfg-d(Client)

4. to check whether the script is running normally, run the following command on the server:

/Usr/local/nagios/libexec/check_nrpe-H 10.0.4.56-c check_disk(Server)

If it is normal, a row of disk detection data will be output; otherwise, an error may be reported.

5. Add the corresponding service to the server

Cd/usr/local/nagios/etc/services(Server)

Vim 10.0.4.56.cfg# Add the following content:

define service{use generic-servicehost_name 10.0.4.56service_description check_diskcheck_command check_nrpe!check_diskmax_check_attempts 5normal_check_interval 1}

6. Restart the nagios service.

/Etc/init. d/nagios restart(Server)

 

Configure nagios alarm email]

At present, the 139 mailbox has the function of sending text messages by the way, so when there is an alarm, you only need to send it to your 139 mailbox and you will receive an alarm message. The advantage of doing so is that you don't have to buy the SMS gateway any more, saving a lot of money.

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

Change "email nagios @ localhost" to "email your 139 mailbox"

Vim/usr/local/nagios/etc/objects/templates. cfg

Find:

Define service {

Name generic-service

The reason for reading this section is that10.0.4.56.cfgMany generic-services are defined, so pay attention to the configuration in this section.

define service{name generic-serviceactive_checks_enabled 1passive_checks_enabled 1parallelize_check 1obsess_over_service 1check_freshness 0notifications_enabled 1event_handler_enabled 1flap_detection_enabled 1failure_prediction_enabled 1process_perf_data 1retain_status_information 1retain_nonstatus_information 1is_volatile 0check_period 24x7max_check_attempts 3normal_check_interval 10retry_check_interval 2ontact_groups adminsnotification_options w,u,c,rnotification_interval 60notification_period 24x7register 0}

Note the following parameters:

Notifications_enabled: whether to enable the reminder function. 1 is enabled, 0 is disabled. Generally, this option is defined in the main configuration file (nagios. cfg) and the effect is the same.

Notification_interval: the shortest interval between repeated notifications. The default interval is 60 minutes. If this value is set to 0, a duplicate reminder is not sent.

Icationication_period: the time period when the reminder is sent. I define a very important host (Service) as 7 × 24, and a general host (Service) as the start time. If you do not specify a time period, no notification is sent for any problem.

Icationication_options: this parameter defines the conditions for sending reminders: d = the status is DOWN, u = the status is UNREACHABLE, r = the status is OK, f = flapping ., N = do not send a reminder.

To send emails correctly, you must configure the preceding parameters properly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.