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 ..
- Make install is used to install main programs, CGI and HTML files
- Make install-init is used to generate the init Startup Script
- Make install-config is used to install the sample configuration file
- Make install-commandmode is used to set the corresponding directory permissions.
- 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.
- Authorized_for_system_information = nagiosadmin, test
- Authorized_for_configuration_information = nagiosadmin, test
- Authorized_for_system_commands = nagiosadmin, test
- Authorized_for_all_services = nagiosadmin, test
- Authorized_for_all_hosts = nagiosadmin, test
- Authorized_for_all_service_commands = nagiosadmin, test
- 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:
- Server
- {
- Listen 80;
- Server_name nagios.xx.com;
- Index index.html index.htm index. php;
- Root/usr/local/nagios/share;
- Auth_basic "Nagios Access ";
- Auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
- Location ~ . * \. (Php | php5 )? $
- {
- # Fastcgi_pass unix:/tmp/php-cgi.sock;
- Fastcgi_pass 127.0.0.1: 9000;
- Fastcgi_index index. php;
- Fcinclude gi. conf;
- }
- Location ~ . * \. (Cgi | pl )? $
- {
- Gzip off;
- Root/usr/local/nagios/sbin;
- Rewrite ^/nagios/cgi-bin/(. *) \. cgi/$ 1.cgi break;
- Fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
- Fastcgi_param SCRIPT_FILENAME/usr/local/nagios/sbin $ fastcgi_script_name;
- Fastcgi_index index. cgi;
- Fastcgi_read_timeout 60;
- Fastcgi_param REMOTE_USER $ remote_user;
- Fcinclude gi. conf;
- Auth_basic "Nagios Access ";
- Auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
- }
- Location/nagios
- {
- Alias/usr/local/nagios/share;
- Auth_basic "Nagios Access ";
- Auth_basic_user_file/usr/local/nagios/etc/nagiospasswd;
- }
- }
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 {
- 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.
- 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:
- Define contact {
- Contact_name nagiosadmin # contact name
- Use generic-contact
- Alias Nagios Admin # contact alias,
- Email nagios @ localhost # The contact's email. nagios uses this email to send an alarm. You can set multiple emails separated by commas (,).
- }
The main configuration file of nrpe is/usr/local/nagios/etc/nrpe. cfg.
Define the nrpe command in the command. cfg file
- Define command {
- Command_name check_nrpe
- Command_line $ USER1 $/check_nrpe-H $ HOSTADDRESS $-c $ ARG1 $
- }
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
- Command [check_load] =/usr/local/nagios/libexec/check_load-w 15,10, 5-c 30,25, 20
- # Monitor CPU load
- Command [check_disk] =/usr/local/nagios/libexec/check_disk-w 20%-c 10%-p/dev/sda2
- # Monitor the usage of the second partition of the hard disk
- Command [check_zombie_procs] =/usr/local/nagios/libexec/check_procs-w 5-c 10-s Z
- # Monitor zombie Processes
- Command [check_local_procs] =/usr/local/nagios/libexec/check_procs-w 150-c 200-P
- # Monitor active processes
- Command [check_mysql] =/usr/local/nagios/libexec/check_mysql-H localhost-u mysql username-p mysql password-d database to be monitored
- # 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.
- Command [check_swap] =/usr/local/nagios/libexec/check_swap-w 20%-c 10%
- # 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.
- Command [check_nginx] =/usr/local/nagios/libexec/check_nginx.sh-url www.chlinux.net/index.php
- # Monitor the nginx status. Can monitor 403.502 and so on
- 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
- # 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:
- Define service {
- Use local-service, srv-pnp
- Host_name WEB1
- Service_description Root Partition
- Check_command check_nrpe! Check_disk
- Check_period 24 × 7
- Max_check_attempts 4
- Normal_check_interval 1
- Retry_check_interval 1
- Contact_groups admins
- Icationication_interval 10
- Icationication_period 24 × 7
- Notification_options w, u, c, r
- }
After the server is configured, restart the nagios service.
Iii. Install pnp
Pnp is a drawing tool that depends on the rrdtool software package.
- Wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.13.tar.gz/20a96f81edba29dcd21215bde4af8b57/pnp-0.4.13.tar.gz
- Tar zxvf pnp-0.4.13.tar.gz
- Cd pnp-0.4.13
- ./Configure-with-nagios-user = nagios-with-nagios-group = nagios-with-perfdata-dir =/usr/local/nagios/share/perfdata-with-rrdtool
- Make all
- Make install
- Make install-config
- Make install-init
- Cd/usr/local/nagios/etc/pnp
- Cp npcd. cfg-sample npcd. cfg
- Cp rra. cfg-sample rra. cfg
- Cpprocess_perfdata.cfg-sample process_perfdata.cfg
- 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
- Define command {
- Command_name process-host-perfdata
- Command_line/usr/local/nagios/libexec/process_perfdata.pl
- }
- Define command {
- Command_name process-service-perfdata
- Command_line/usr/local/nagios/libexec/process_perfdata.pl
- }
6. Add a small Sun template and embed it on the nagios page.
Vim templates. cfg
# Add at the end
- Define host {
- Name host-pnp
- Action_url/pnp/index. php? Host = $ HOSTNAME $
- Register 0
- }
- Define service {
- Name srv-pnp
- Action_url/pnp/index. php? Host = $ HOSTNAME $ & srv = $ SERVICEDESC $
- Register 0
- }
7. Modify hosts. cfg and services. cfg.
Cd/usr/local/nagios/etc/eric.com
# Modify hosts. cfg
- Vim hosts. cfg
- Define host {
- Use linux-server, host-pnp
- Host_name eric.com
- Alias eric.com
- Address 192.168.6.101
- }
# Modify services. cfg
- Define service {
- Use local-service, srv-pnp
- Host_name eric.com
- Service_description PING
- Check_command check_ping! 100.0, 20%! 500.0, 60%
- }
8. Restart the nagios service.
/Etc/init. d/nagios restart