Centos 7下Nagios的安装及配置(完整版)
https://www.jianshu.com/p/3476d98a4703
Nagios 是一款自动化运维工具,可以协助运维人员监控服务器的运行状况,并且拥有报警功能。本文章将介绍其安装方法和详细的配置方法。
nagios 监控服务应用指南
本地资源:负载,CPU,磁盘,内存。IO,RAID,温度,passwd文件变化,本地所有文件指纹识别
网络服务:端口,URL,丢包,进程,网络流量
其他设备:交换机,打印机,windows等。
业务数据:用户登录失败次数,用户登录网站次数,输入验证码失败的次数,某个API接口流量并发,电商网站订单,支付交易的数量
nagios成员:主程序nagios,插件nagios-plugins,和一些可选的客户端nrpe,NSClient++,NSCA和NDOUtils
- NRPE--半被动模式
存放位置:安装在客户端
NRPE作用:在客户端执行相关的脚本插件来获取数据,实现对客户端主机资源的监控。
存在形式:守护进程(agent)模式,开启的端口5666
- NSClient++:半被动模式,相当于linux系统的nrpe
- NDOUtils(不推荐用)
存在位置:服务端
作用:用于将nagios的配置信息和各event产生的数据存入数据库中,以实现对这些数据的检索和处理。
- NSCA:纯被动模式--->用在分布式监控环境中
位置:NSCA需要同时安装在nagios的服务器端和客户端
软件版本
nagios版本:4.3.1
nagios-plugins版本:2.2.1
nrpe版本:3.1.0
实现环境
nagios运行在LAMP环境下(如果还没有安装LAMP环境,可以参考下我的安装LNMP文章,因为我已经编译安装了php和Mysql,本文直接采取yum 方式安装apache,当然也可以采取编译方式安装apache。看个人喜好和公司需求)
Centos版本:CentOS Linux release 7.3.1611 (Core) 64位
Apache版本:Apache/2.4.6
Nginx版本: nginx 1.12.0
Mysql版本:mysql 5.7.18
PHP版本:php 7.1.4
下载nagios软件及插件包
下载慢的话可以去我的网盘下载
[root@nagios ~]# cd /software/
[root@nagios software]# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
[root@nagios software]# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
[root@nagios software]# wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
[root@nagios software]# ll
-rw-r--r-- 1 root root 11095797 4月 21 15:57 nagios-4.3.1.tar.gz
-rw-r--r-- 1 root root 2728818 4月 20 00:04 nagios-plugins-2.2.1.tar.gz
-rw-r--r-- 1 root root 501028 4月 17 22:36 nrpe-3.1.0.tar.gz
安装nagios-server服务端
安装依赖包
[root@nagios software]# yum -y install httpd httpd-devel gcc glibc glibc-common gd gd-devel perl-devel perl-CPAN fcgi perl-FCGI perl-FCGI-ProcManager
解压nagios源码包
[root@nagios software]# tar zxvf nagios-4.3.1.tar.gz
进入解压后的目录
[root@nagios software]# cd nagios-4.3.1/
创建ngaios用户和组(把nginx启动用户www加入到nagios相关组)
[root@nagios nagios-4.3.1]# useradd nagios -s /sbin/nologin
[root@nagios nagios-4.3.1]# id www
[root@nagios nagios-4.3.1]# groupadd nagcmd
[root@nagios nagios-4.3.1]# usermod -a -G nagcmd nagios
[root@nagios nagios-4.3.1]# usermod -a -G nagcmd www
[root@nagios nagios-4.3.1]# id -n -G nagios
[root@nagios nagios-4.3.1]# id -n -G www
配置nagios
[root@client1 nagios-4.3.1]# ./configure --with-command-group=nagcmd
编译和安装
[root@nagios nagios-4.3.1]# make all
[root@nagios nagios-4.3.1]# make install-init
[root@nagios nagios-4.3.1]# make install-commandmode
[root@nagios nagios-4.3.1]# make install-config
[root@nagios nagios-4.3.1]# make install
[root@nagios nagios-4.3.1]# cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
[root@nagios nagios-4.3.1]# chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
[root@nagios nagios-4.3.1]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
生成apache配置文件
[root@nagios nagios-4.3.1]# make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
if [ 0 -eq 1 ]; then \
ln -s /etc/httpd/conf.d/nagios.conf /etc/apache2/sites-enabled/nagios.conf; \
fi
*** Nagios/Apache conf file installed ***
生成nagios web界面的验证信息
[root@nagios nagios-4.3.1]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios
New password: ==> 输入密码,这里我输入的密码是nagios,记住这个密码
Re-type new password: ==> 确认密码
Adding password for user nagios
修改apache配置文件
vi /etc/httpd/conf/httpd.conf
Listen 80 ===> 修改为 Listen 8080
User apache ===> 修改为 User www
Group apache ===> 修改为 Group www
DirectoryIndex index.html ===> 修改为 DirectoryIndex index.php index.html
AddType application/x-gzip .gz .tgz ===> 下面添加一行内容 AddHandler application/x-httpd-php .php
# LoadModule foo_module modules/mod_foo.so ===> 下面添加一行内容
LoadModule php7_module modules/libphp7.so
apache配置文件参考
[root@nagios httpd]# grep -v '^$' /etc/httpd/conf/httpd.conf|grep -v '#'
ServerRoot "/etc/httpd"
Listen 8080
LoadModule php7_module modules/libphp7.so
Include conf.modules.d/*.conf
User www
Group www
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler application/x-httpd-php .php
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
重新编译一下php,使其直接apache(也就是编译参数加上--with-apxs2)
[root@nagios nagios-4.3.1]# cd /software/php-7.1.4/
[root@nagios php-7.1.4]# ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip --with-apxs2
[root@nagios php-7.1.4]# make && make install
[root@nagios php-7.1.4]# cd /etc/httpd/
[root@nagios httpd]# ll /etc/httpd/modules/libphp7.so
-rwxr-xr-x 1 root root 38908880 4月 24 10:34 /etc/httpd/modules/libphp7.so ===> 可以看到这个模块已经生成
启动apache
[root@nagios httpd]# systemctl start httpd
[root@nagios httpd]# systemctl enable httpd
启动nagios
chkconfig nagios on
/etc/init.d/nagios start
在浏览器输入ip:8080/nagios测试nagios-web页面是否可以打开



<br />
安装nagios-plugins插件
解压nagios-plugins源码包
[root@nagios httpd]# cd /software/
[root@nagios software]# tar zxvf nagios-plugins-2.2.1.tar.gz
进入解压后的目录进行配置
[root@nagios software]# cd nagios-plugins-2.2.1/
[root@nagios nagios-plugins-2.2.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --enable-perl-modules
编译及安装
[root@nagios nagios-plugins-2.2.1]# make && make install
<br />
安装nrpe
解压nrpe源码包
[root@nagios nagios-plugins-2.2.1]# cd /software/
[root@nagios software]# tar zxvf nrpe-3.1.0.tar.gz
进去解压后的目录进行配置
[root@nagios software]# cd nrpe-3.1.0/
[root@nagios nrpe-3.1.0]# ./configure
编译及安装
[root@nagios nrpe-3.1.0]# make all
[root@nagios nrpe-3.1.0]# make install-plugin
[root@nagios nrpe-3.1.0]# make install-daemon
[root@nagios nrpe-3.1.0]# make install-daemon-config
[root@nagios nrpe-3.1.0]# cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
安装完成后,查看下libexec下面是否有插件
[root@nagios nrpe-3.1.0]# ls /usr/local/nagios/libexec/
check_apt check_flexlm check_log check_ntp_peer check_smtp disable_active_service_checks
check_breeze check_fping check_mailq check_ntp_time check_spop disable_notifications
check_by_ssh check_ftp check_mrtg check_nwstat check_ssh distributed-monitoring
check_clamd check_http check_mrtgtraf check_oracle check_ssmtp enable_active_service_checks
check_cluster check_icmp check_mysql check_overcr check_swap enable_notifications
check_dhcp check_ide_smart check_mysql_query check_ping check_tcp eventhandlers
check_dig check_ifoperstatus check_nagios check_pop check_time negate
check_disk check_ifstatus check_nntp check_procs check_udp redundancy-scenario1
check_disk_smb check_imap check_nntps check_real check_ups submit_check_result
check_dns check_ircd check_nrpe check_rpc check_uptime urlize
check_dummy check_jabber check_nt check_sensors check_users utils.pm
check_file_age check_load check_ntp check_simap check_wave utils.sh
启动nrpe,并测试服务端本地是否可以连通
[root@nagios nrpe-3.1.0]# /usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg
[root@nagios nrpe-3.1.0]# echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
[root@nagios nrpe-3.1.0]# chmod +x /etc/rc.d/rc.local # centos 7下需要这一步, 不然/etc/rc.local中的内容开机可能不执行
[root@nagios nrpe-3.1.0]# netstat -lnput|grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 67176/nrpe
tcp6 0 0 :::5666 :::* LISTEN 67176/nrpe
[root@nagios nrpe-3.1.0]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v3.1.0-rc1
<br />
nagios客户端的安装
下载nagios-plugin和nrpe插件
[root@client1 ~]# mkdir /software/
[root@client1 ~]# cd /software/
[root@client1 software]# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
[root@client1 software]# wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
安装nagios-plugin
安装依赖包
[root@client1 software]# yum install perl-devel perl-CPAN -y
创建用户
[root@client1 software]# useradd nagios -M -s /sbin/nologin
解压nagios-plugin源码包
[root@client1 software]# tar zxvf nagios-plugins-2.2.1.tar.gz
进入解压后的目录进行配置
[root@client1 software]# cd nagios-plugins-2.2.1/
[root@client1 nagios-plugins-2.2.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
编译及安装
[root@client1 nagios-plugins-2.2.1]# make && make install
<br />
安装nrpe插件
解压nrpe源码包
[root@client1 nagios-plugins-2.2.1]# cd ..
[root@client1 software]# tar zxvf nrpe-3.1.0.tar.gz
进入解压后的目录进行配置
[root@client1 software]# cd nrpe-3.1.0/
[root@client1 nrpe-3.1.0]# ./configure
编译及安装
[root@client1 nrpe-3.1.0]# make all
[root@client1 nrpe-3.1.0]# make install-plugin
[root@client1 nrpe-3.1.0]# make install-daemon
[root@client1 nrpe-3.1.0]# make install-daemon-config
[root@client1 nrpe-3.1.0]# mkdir /usr/local/nagios/etc/
[root@client1 nrpe-3.1.0]# cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
安装完成后,查看下libexec下面是否有插件
[root@client1 nrpe-3.1.0]# ls /usr/local/nagios/libexec/
check_apt check_dummy check_imap check_nagios check_overcr check_ssh negate
check_breeze check_file_age check_ircd check_nntp check_ping check_ssmtp urlize
check_by_ssh check_flexlm check_jabber check_nntps check_pop check_swap utils.pm
check_clamd check_fping check_load check_nrpe check_procs check_tcp utils.sh
check_cluster check_ftp check_log check_nt check_real check_time
check_dhcp check_http check_mailq check_ntp check_rpc check_udp
check_dig check_icmp check_mrtg check_ntp_peer check_sensors check_ups
check_disk check_ide_smart check_mrtgtraf check_ntp_time check_simap check_uptime
check_disk_smb check_ifoperstatus check_mysql check_nwstat check_smtp check_users
check_dns check_ifstatus check_mysql_query check_oracle check_spop check_wave
启动nrpe,并测试服务端本地是否可以连通
[root@client1 nrpe-3.1.0]# /usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg
[root@client1 nrpe-3.1.0]# echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
[root@client1 nrpe-3.1.0]# chmod +x /etc/rc.d/rc.local # centos 7下需要这一步, 不然/etc/rc.local中的内容开机可能不执行
[root@client1 nrpe-3.1.0]# netstat -lnput|grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 28296/nrpe
tcp6 0 0 :::5666 :::* LISTEN 28296/nrpe
[root@client1 nrpe-3.1.0]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v3.1.0-rc1
修改配置文件
[root@client1 nrpe-3.1.0]# cd /usr/local/nagios/etc/
vi nrpe.cfg
允许服务端IP和本机访问,172.16.0.18是nagios服务端IP地址
allowed_hosts=127.0.0.1,::1 ===> 修改为 allowed_hosts=127.0.0.1,::1,172.16.0.18
注释下面几行内容
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
在nrpe.cfg文件末尾增加下面几行内容
# my custom monitor items
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_mem]=/usr/local/nagios/libexec/check_mem.pl -w 90% -c 95%
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
创建一个监控内存的perl脚本
[root@client1 etc]# vi /usr/local/nagios/libexec/check_mem.pl
添加下面内容
#! /usr/bin/perl -w
#
# $Id: check_mem.pl 8 2008-08-23 08:59:52Z rhomann $
#
# check_mem v1.7 plugin for nagios
#
# uses the output of `free` to find the percentage of memory used
#
# Copyright Notice: GPL
#
# History:
# v1.8 Rouven Homann - rouven.homann@cimt.de
# + added findbin patch from Duane Toler
# + added backward compatibility patch from Timour Ezeev
#
# v1.7 Ingo Lantschner - ingo AT boxbe DOT com
# + adapted for systems with no swap (avoiding divison through 0)
#
# v1.6 Cedric Temple - cedric DOT temple AT cedrictemple DOT info
# + add swap monitoring
# + if warning and critical threshold are 0, exit with OK
# + add a directive to exclude/include buffers
#
# v1.5 Rouven Homann - rouven.homann@cimt.de
# + perfomance tweak with free -mt (just one sub process started instead of 7)
# + more code cleanup
#
# v1.4 Garrett Honeycutt - gh@3gupload.com
# + Fixed PerfData output to adhere to standards and show crit/warn values
#
# v1.3 Rouven Homann - rouven.homann@cimt.de
# + Memory installed, used and free displayed in verbose mode
# + Bit Code Cleanup
#
# v1.2 Rouven Homann - rouven.homann@cimt.de
# + Bug fixed where verbose output was required (nrpe2)
# + Bug fixed where perfomance data was not displayed at verbose output
# + FindBin Module used for the nagios plugin path of the utils.pm
#
# v1.1 Rouven Homann - rouven.homann@cimt.de
# + Status Support (-c, -w)
# + Syntax Help Informations (-h)
# + Version Informations Output (-V)
# + Verbose Output (-v)
# + Better Error Code Output (as described in plugin guideline)
#
# v1.0 Garrett Honeycutt - gh@3gupload.com
# + Initial Release
#
use strict;
use FindBin;
FindBin::again();
use lib $FindBin::Bin;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME $PROGVER);
use Getopt::Long;
use vars qw($opt_V $opt_h $verbose $opt_w $opt_c);
$PROGNAME = "check_mem";
$PROGVER = "1.8";
# add a directive to exclude buffers:
my $DONT_INCLUDE_BUFFERS = 0;
sub print_help ();
sub print_usage ();
Getopt::Long::Configure('bundling');
GetOptions ("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"v" => \$verbose, "verbose" => \$verbose,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c);
if ($opt_V) {
print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
exit $ERRORS{'UNKNOWN'};
}
if ($opt_h) {
print_help();
exit $ERRORS{'UNKNOWN'};
}
print_usage() unless (($opt_c) && ($opt_w));
my ($mem_critical, $swap_critical);
my ($mem_warning, $swap_warning);
($mem_critical, $swap_critical) = ($1,$2) if ($opt_c =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);
($mem_warning, $swap_warning) = ($1,$2) if ($opt_w =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);
# Check if swap params were supplied
$swap_critical ||= 100;
$swap_warning ||= 100;
# print threshold in output message
my $mem_threshold_output = " (";
my $swap_threshold_output = " (";
if ( $mem_warning > 0 && $mem_critical > 0) {
$mem_threshold_output .= "W> $mem_warning, C> $mem_critical";
}
elsif ( $mem_warning > 0 ) {
$mem_threshold_output .= "W> $mem_warning";
}
elsif ( $mem_critical > 0 ) {
$mem_threshold_output .= "C> $mem_critical";
}
if ( $swap_warning > 0 && $swap_critical > 0) {
$swap_threshold_output .= "W> $swap_warning, C> $swap_critical";
}
elsif ( $swap_warning > 0 ) {
$swap_threshold_output .= "W> $swap_warning";
}
elsif ( $swap_critical > 0 ) {
$swap_threshold_output .= "C> $swap_critical";
}
$mem_threshold_output .= ")";
$swap_threshold_output .= ")";
my $verbose = $verbose;
my ($mem_percent, $mem_total, $mem_used, $swap_percent, $swap_total, $swap_used) = &sys_stats();
my $free_mem = $mem_total - $mem_used;
my $free_swap = $swap_total - $swap_used;
# set output message
my $output = "Memory Usage".$mem_threshold_output.": ". $mem_percent.'% <br>';
$output .= "Swap Usage".$swap_threshold_output.": ". $swap_percent.'%';
# set verbose output message
my $verbose_output = "Memory Usage:".$mem_threshold_output.": ". $mem_percent.'% '."- Total: $mem_total MB, used: $mem_used MB, free: $free_mem MB<br>";
$verbose_output .= "Swap Usage:".$swap_threshold_output.": ". $swap_percent.'% '."- Total: $swap_total MB, used: $swap_used MB, free: $free_swap MB<br>";
# set perfdata message
my $perfdata_output = "MemUsed=$mem_percent\%;$mem_warning;$mem_critical";
$perfdata_output .= " SwapUsed=$swap_percent\%;$swap_warning;$swap_critical";
# if threshold are 0, exit with OK
if ( $mem_warning == 0 ) { $mem_warning = 101 };
if ( $swap_warning == 0 ) { $swap_warning = 101 };
if ( $mem_critical == 0 ) { $mem_critical = 101 };
if ( $swap_critical == 0 ) { $swap_critical = 101 };
if ($mem_percent>$mem_critical || $swap_percent>$swap_critical) {
if ($verbose) { print "<b>CRITICAL: ".$verbose_output."</b>|".