Build nagios with Lnmp in centos6.3

Source: Internet
Author: User
Tags rrdtool stop script

Environment Introduction:

System: centos6.3 mini X64

IP: 172.16.1.200

Integrated Environment: Lnmp

------------------------------

Installation start:

1. Install the FCGI Module

# Yum install-y perl-devel

# Wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz

# Tar xvzf FCGI-0.73.tar.gz

# Cd FCGI-0.73

# Perl Makefile. PL

# Make

# Make install

2. Install the FCGI-ProcManager Module

# Wget http://mirrors.ustc.edu.cn/CPAN/authors/id/ B /BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz

# Tar xvzf FCGI-ProcManager-0.19.tar.gz

# Cd FCGI-ProcManager-0.19

# Perl Makefile. PL

# Make & make install

3. Install IO and IO: ALL Module

# Wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz

# Tar zxvf IO-1.25.tar.gz

# Cd IO-1.25

# Perl Makefile. PL

# Make

# Make install

# Wgethttp: // cpan.metacpan.org/authors/id/ I /IN/INGY/IO-All-0.44.tar.gz

# Tar zxvf IO-All-0.41.tar.gz

# Cd IO-All-0.41

# Perl Makefile. PL

# Make

# Make install

4. Download the Perl script

The purpose of this script is to generate a FastCGI interface of PERL, so that Nginx can process Perl in CGI Mode.

# Wget http://www.chlinux.net/perl-fcgi.zip

# Unzip perl-fcgi.zip

# Cp perl-fcgi.pl/usr/local/nginx/Note: it is recommended to put this script in the Nginx installation directory.

Modify script Permissions

# Chmod 755/usr/local/nginx/perl-fcgi.pl

5. Create a CGI startup/Stop script

This SHELL script is only used to facilitate the management of the above Perl script. The nobody in the script is the nginx running user. Please adjust it according to your actual situation.

Note: it cannot be executed by the root user (a prompt will be displayed). It must be executed by a user with the same identity as Nginx. Otherwise, Permision Denied may be prompted in the Nginx Log.

# Vi/usr/local/nginx/start_perl_cgi.sh

#! /Bin/bash

# Set-x

Dir =/usr/local/nginx/

Stop ()

{

# Pkill-f $ dir/perl-fcgi.pl

Kill $ (cat $ dir/logs/perl-fcgi.pid)

Rm $ dir/logs/perl-fcgi.pid 2>/dev/null

Rm $ dir/logs/perl-fcgi.sock 2>/dev/null

Echo "stop perl-fcgi done"

}

Start ()

{

Rm $ dir/now_start_perl_fcgi.sh 2>/dev/null

Chown www. www $ dir/logs

Echo "$ dir/perl-fcgi.pl-l $ dir/logs/perl-fcgi.log-pid $ dir/logs/perl-fcgi.pid-S $ dir/logs/perl-fcgi.sock"> $ dir/now_start_perl_fcgi.sh

Chown www. www $ dir/now_start_perl_fcgi.sh

Chmod u + x $ dir/now_start_perl_fcgi.sh

Sudo-u nobody $ dir/now_start_perl_fcgi.sh

Echo "start perl-fcgi done"

}

Case $1 in

Stop)

Stop

;;

Start)

Start

;;

Restart)

Stop

Start

;;

Esac

Save and exit

Modify SHELL script Permissions

# Chmod 755/usr/local/nginx/start_perl_cgi.sh

Start script

#/Usr/local/nginx/start_perl_cgi.sh start

Under normal circumstances, the perl-fcgi.sock file is generated under/usr/local/nginx/logs. If not, check the preceding step aggregation.

Ii. Install Nagios

A. Preparations before installation

The installed machine must have a WEB service. This article is installed in the Nginx environment.

Download the main nagios program and related plug-in packages

# Wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz

# Wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz

# Wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz

Install the GD library (required for statusmap and trends modules in Nagios)

# Yum-y install libgd2-noxpm libgd2-noxpm-devel

B. Install Nagios monitoring

1. Create Nagios users and groups

Create a Nagios account

#/Usr/sbin/useradd-m-s/sbin/nologin nagios

2. Create a user group named nagcmd to execute External commands from the web interface. Add Nagios users and Nginx users to the group.

# Groupadd nagcmd

# Usermod-a-G nagcmd nagios

# Usermod-a-G nagcmd www

Note: The above www is the group to which Nginx users belong. If there are differences, please adjust it by yourself.

3. Compile and install Nagios

# Tar zxvf nagios-3.2.3.tar.gz
# Cd nagios-3.2.3
#./Configure-prefix =/usr/local/nagios-with-command-group = nagcmd
# Make all
# Make install
# Make install-config
# Make install-commandmode
# Make install-webconf
# Cd ..

  • Note:
  1. Make install is used to install main programs, CGI and HTML files
  2. Make install-init is used to generate the init Startup Script
  3. Make install-config is used to install the sample configuration file
  4. Make install-commandmode is used to set the corresponding directory permissions.
  5. Make install-webconf is used to install the Apache configuration file

4. Check whether the verification program is correctly installed

Switch the directory to the installation path, which is/usr/local/nagios. Check whether the five directories including etc, bin, sbin, share, and var exist, if yes, it indicates that the program has been correctly installed to the system.

# Ls/usr/local/nagios/

Bin/etc/sbin/share/var/

Note: The directory where the bin-Nagios executable program is located. The nagios file is the main program.

Etc-Nagios configuration file location

The directory where the sbin-Nagios cgi file is located, that is, the directory where the file needed to execute the external command is located.

Share-Nagios webpage file directory

Directory where var-Nagios log files and spid files are located

Var/archives-log archiving directory

Var/rw-used to store external command files

5. Configure NGINX

1) configure the Logon account and password on the Nagios Web Interface

# Htpasswd-c/usr/local/nagios/etc/nagiospasswd test

If you do not have htpasswd (provided by the Apache installation package), you can generate encrypted data online.

A) Access http://www.4webhelp.net/us/password.phpto generate encrypted data

B) Create an encrypted authentication File

# Vi/usr/local/nagios/etc/nagiospasswd

# Add the generated encrypted data. The user name is prior to the colon and the encrypted password is later.

Test: 25JB. R7mXY96o

Modify the Nagios configuration file to grant new users access permissions.

# Vi/usr/local/nagios/etc/cgi. cfg

# Add new users to the following items. Multiple users are separated by commas.

  1. Authorized_for_system_information = nagiosadmin, test
  2. Authorized_for_configuration_information = nagiosadmin, test
  3. Authorized_for_system_commands = nagiosadmin, test
  4. Authorized_for_all_services = nagiosadmin, test
  5. Authorized_for_all_hosts = nagiosadmin, test
  6. Authorized_for_all_service_commands = nagiosadmin, test
  7. Authorized_for_all_host_commands = nagiosadmin, test

2) Modify NGINX configuration to support WEB access to Nagios

Because I bind domain names to multiple virtual hosts, server_name is the domain name. If a single host directly writes an IP address, you can.

The NGINX configuration snippets are as follows:

  1. Server
  2. {
  3. Listen 80;
  4. Server_name nagios.xx.com;
  5. Index index.html index.htm index. php;
  6. Root/usr/local/nagios/share;
  7. Auth_basic "Nagios Access ";
  8. Auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
  9. Location ~ . * \. (Php | php5 )? $
  10. {
  11. # Fastcgi_pass unix:/tmp/php-cgi.sock;
  12. Fastcgi_pass 127.0.0.1: 9000;
  13. Fastcgi_index index. php;
  14. Fcinclude gi. conf;
  15. }
  16. Location ~ . * \. (Cgi | pl )? $
  17. {
  18. Gzip off;
  19. Root/usr/local/nagios/sbin;
  20. Rewrite ^/nagios/cgi-bin/(. *) \. cgi/$ 1.cgi break;
  21. Fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
  22. Fastcgi_param SCRIPT_FILENAME/usr/local/nagios/sbin $ fastcgi_script_name;
  23. Fastcgi_index index. cgi;
  24. Fastcgi_read_timeout 60;
  25. Fastcgi_param REMOTE_USER $ remote_user;
  26. Fcinclude gi. conf;
  27. Auth_basic "Nagios Access ";
  28. Auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
  29. }
  30. Location/nagios
  31. {
  32. Alias/usr/local/nagios/share;
  33. Auth_basic "Nagios Access ";
  34. Auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
  35. }
  36. }

6. Compile and install the Nagios plugin

Because the main program of Nagios only provides a runtime framework, its specific monitoring is completed by the plug-ins running under it, so the Nagios plug-in must be installed.

# Tar zxvf nagios-plugins-1.4.15.tar.gz
# Cd nagios-plugins-1.4.15
#./Configure-prefix =/usr/local/nagios-with-nagios-user = nagios-with-nagios-group = nagios
# Make & make install

Verify that the Nagios plug-in is correctly installed

# Ls/usr/local/nagios/libexec

The installed plug-in file is displayed, that is, all plug-ins are installed in the libexec directory.

7. Start the service

Check whether the configuration file is correct before starting.

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

If no error is reported, you can start the Nagios service.

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

View Nagios running status

#/Usr/local/nagios/bin/nagiostats

8. Install NRPE

Nagios can only monitor some local conditions of its host, such as cpu load, memory usage, and hard disk usage. If you want to monitor these local conditions on the monitored server, you need to use NRPE. NRPE (Nagios Remote Plugin Executor) is an extension of Nagios. It is used on the monitored server to provide some local information about the server to the Nagios monitoring platform. NRPE can be called the Linux client of Nagios.

Because NRPE transmits data on the monitored and monitored hosts through SSL, you must first install the ssl-related software package.

Compile and install NRPE

# Tar zxvf nrpe-2.12.tar.gz
# Cd nrpe-2.12
#./Configure
# Make all
# Make install-plugin
# Make install-daemon
# Make install-daemon-config

Note: you only need to make install-plugin this step on the monitoring host. On the monitoring machine, you only need a check_nrpe plug-in to connect to the daemon of the monitored nrpe.

Start NRPE

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

Verify that NRPE is correctly installed

/Usr/local/nagios/libexec/check_nrpe-H localhost

NOTE: If successful, the NRPE version is returned.

5. Modify the NRPE configuration file so that the monitored host can access the NRPE of the monitored host.

By default, only the local access to the NRPE Daemon is allowed in the NRPE configuration file.

# Vi/usr/local/nagios/etc/nrpe. cfg

# The default value is 127.0.0.1, which can be accessed only on the local machine.

Allowed_hosts = 192.168.1.108

6. Restart nrpe

Killall nrpe

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

Configure nagios

Ii. Define host and Host group

1. Create a host. cfg to define the host

The content of the Host. cfg file is as follows:

Define host {

  • Use linux-server
  • Host_name WEB1 # The host name can be retrieved at will. We recommend that you use a name that is easy to remember. There is no space at the end of the host name.
  • Alias WEB1 # host alias
  • Address 192.168.1.51 # Host IP address. Now I will enter the IP address of the local machine temporarily
  • Check_command check-host-alive # check command, which is from commands. cfg and used to monitor whether the host is alive
  • Max_check_attempts 5 # Number of Retries after check failure
  • Check_period 24 × 7 # The check period is 24 × 7, also from the time range defined in timeperiods. cfg.
  • Contact_groups sagroup # Contact Group, which is defined in contactgroups. cfg.
  • Icationication_interval 10 # reminder interval, which is triggered every 10 seconds
  • N
  • Otification_period 24 × 7 # reminder cycle, 24 × 7, also from the one we previously defined in timeperiods. cfg
  • Notification_options d, u, r # specify the conditions for alerts. For more information, see the description in contacts. cfg.
  • }

You can define multiple hosts by simply copying and modifying them.

You can create a hostgroup. cfg file to define the Host group.

The file content is as follows:

Define hostgroup {

  • Hostgroup_name DB-servers # Host group name.
  • Alias DB Server # Server alias
  • Members DB1, DB2 # Member
  • }

2. Create a service. cfg file to define the service

The content of the service. cfg file is as follows:

Define service {

  • Use local-service, srv-pnp
  • Host_name WEB1 # host name, which must be defined in the host. cfg file
  • Service_description Current disk # service description
  • Check_command check_local_disk! 20%! 10%! /# Service check command, which is from the command. cfg file
  • Max_check_attempts 5 # maximum number of checks
  • Check_period 24 × 7 # service check cycle
  • Notification_interval 10 # notification interval
  • Normal_check_interval 1 # Time Interval for normal service check
  • Retry_check_interval 1 # Re-check the interval
  • Icationication_period 24 × 7 # Notification Period
  • Icationication_options w, u, c, r, d # when the service is notified, d = when the status is DOWN, u = when the status is UNREACHABLE, r = when the status is OK, f = flapping,
  • Contact_groups admins # Contact Group
  • }

Define the contact and set the contact file name to contacts. cfg by default. You only need to modify the file content as follows:

  1. Define contact {
  2. Contact_name nagiosadmin # contact name
  3. Use generic-contact
  4. Alias Nagios Admin # contact alias,
  5. Email nagios @ localhost # The contact's email. nagios uses this email to send an alarm. You can set multiple emails separated by commas (,).
  6. }

The main configuration file of nrpe is/usr/local/nagios/etc/nrpe. cfg.

Define the nrpe command in the command. cfg file

  1. Define command {
  2. Command_name check_nrpe
  3. Command_line $ USER1 $/check_nrpe-H $ HOSTADDRESS $-c $ ARG1 $
  4. }

Services to monitor remote hosts must be defined in the nrpe master configuration file of the monitored host. Then define the command in the command. cfg file of the monitoring server. The operation is as follows:

Definition of monitored Machine

  1. Command [check_load] =/usr/local/nagios/libexec/check_load-w 15,10, 5-c 30,25, 20
  2. # Monitor CPU load
  3. Command [check_disk] =/usr/local/nagios/libexec/check_disk-w 20%-c 10%-p/dev/sda2
  4. # Monitor the usage of the second partition of the hard disk
  5. Command [check_zombie_procs] =/usr/local/nagios/libexec/check_procs-w 5-c 10-s Z
  6. # Monitor zombie Processes
  7. Command [check_local_procs] =/usr/local/nagios/libexec/check_procs-w 150-c 200-P
  8. # Monitor active processes
  9. Command [check_mysql] =/usr/local/nagios/libexec/check_mysql-H localhost-u mysql username-p mysql password-d database to be monitored
  10. # Monitor the mysql database. It mainly monitors mysql's enabled data, queries per second, slow queries, refreshed tables, and opened tables. Note: mysql users only need to grant select permission.
  11. Command [check_swap] =/usr/local/nagios/libexec/check_swap-w 20%-c 10%
  12. # Monitor the usage of swap partitions. If the value exceeds 20%, the system reports that the value of w exceeds 10%. If the value is greater than, the system reports the value of c.
  13. Command [check_nginx] =/usr/local/nagios/libexec/check_nginx.sh-url www.chlinux.net/index.php
  14. # Monitor the nginx status. Can monitor 403.502 and so on
  15. Command [check_traffic] =/usr/local/nagios/libexec/check_traffic.sh-V 2c-C public-H 127.0.0.1-I 2-w 300,300-c 500,600-K-B
  16. # Network card Traffic Monitoring

After being configured, You need to restart nrpe.

How to restart nrpe

Killall-9 nrpe

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

[Root @ localhost ~] #/Usr/local/nagios/libexec/check_nrpe-H localhost

NRPE v2.12 check whether similar output exists. If yes, the startup is successful. If no, check whether the configuration is correct.

Monitoring machine configuration

Configure the following in the service. cfg file:

  1. Define service {
  2. Use local-service, srv-pnp
  3. Host_name WEB1
  4. Service_description Root Partition
  5. Check_command check_nrpe! Check_disk
  6. Check_period 24 × 7
  7. Max_check_attempts 4
  8. Normal_check_interval 1
  9. Retry_check_interval 1
  10. Contact_groups admins
  11. Icationication_interval 10
  12. Icationication_period 24 × 7
  13. Notification_options w, u, c, r
  14. }

After the server is configured, restart the nagios service.

Iii. Install pnp

Pnp is a drawing tool that depends on the rrdtool software package.

  1. Wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.13.tar.gz/20a96f81edba29dcd21215bde4af8b57/pnp-0.4.13.tar.gz
  2. Tar zxvf pnp-0.4.13.tar.gz
  3. Cd pnp-0.4.13
  4. ./Configure-with-nagios-user = nagios-with-nagios-group = nagios-with-perfdata-dir =/usr/local/nagios/share/perfdata-with-rrdtool
  5. Make all
  6. Make install
  7. Make install-config
  8. Make install-init
  9. Cd/usr/local/nagios/etc/pnp
  10. Cp npcd. cfg-sample npcd. cfg
  11. Cp rra. cfg-sample rra. cfg
  12. Cpprocess_perfdata.cfg-sample process_perfdata.cfg
  13. Chown nagios. nagios/usr/local/nagios/etc/pnp/-R

Modify the main configuration file/usr/local/nagios/etc/nagios. cfg of nagios

# Open comments:

Host_perfdata_command = process-host-perfdata

Service_perfdata_command = process-service-perfdata

Process_performance_data = 1

5. Modify commands. cfg

Cd/usr/local/nagios/etc/objects

Vim commands. cfg

# Add

# 'Process-host-perfdata' command definition

  1. Define command {
  2. Command_name process-host-perfdata
  3. Command_line/usr/local/nagios/libexec/process_perfdata.pl
  4. }
  5. Define command {
  6. Command_name process-service-perfdata
  7. Command_line/usr/local/nagios/libexec/process_perfdata.pl
  8. }

6. Add a small Sun template and embed it on the nagios page.

Vim templates. cfg

# Add at the end

  1. Define host {
  2. Name host-pnp
  3. Action_url/pnp/index. php? Host = $ HOSTNAME $
  4. Register 0
  5. }
  6. Define service {
  7. Name srv-pnp
  8. Action_url/pnp/index. php? Host = $ HOSTNAME $ & srv = $ SERVICEDESC $
  9. Register 0
  10. }

7. Modify hosts. cfg and services. cfg.

Cd/usr/local/nagios/etc/eric.com

# Modify hosts. cfg

  1. Vim hosts. cfg
  2. Define host {
  3. Use linux-server, host-pnp
  4. Host_name eric.com
  5. Alias eric.com
  6. Address 192.168.6.101
  7. }

# Modify services. cfg

  1. Define service {
  2. Use local-service, srv-pnp
  3. Host_name eric.com
  4. Service_description PING
  5. Check_command check_ping! 100.0, 20%! 500.0, 60%
  6. }

8. Restart the nagios service.

/Etc/init. d/nagios restart

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.