Centos 7下Nagios的安裝及配置(完整版)

簡介

Nagios 是一款自動(dòng)化運(yùn)維工具,可以協(xié)助運(yùn)維人員監(jiān)控服務(wù)器的運(yùn)行狀況,并且擁有報(bào)警功能涝动。本文章將介紹其安裝方法和詳細(xì)的配置方法。

nagios 監(jiān)控服務(wù)應(yīng)用指南
本地資源:負(fù)載炬灭,CPU醋粟,磁盤,內(nèi)存重归。IO米愿,RAID,溫度鼻吮,passwd文件變化吗货,本地所有文件指紋識別
網(wǎng)絡(luò)服務(wù):端口,URL狈网,丟包,進(jìn)程笨腥,網(wǎng)絡(luò)流量
其他設(shè)備:交換機(jī)拓哺,打印機(jī),windows等脖母。
業(yè)務(wù)數(shù)據(jù):用戶登錄失敗次數(shù)士鸥,用戶登錄網(wǎng)站次數(shù),輸入驗(yàn)證碼失敗的次數(shù)谆级,某個(gè)API接口流量并發(fā)烤礁,電商網(wǎng)站訂單,支付交易的數(shù)量

nagios成員:主程序nagios肥照,插件nagios-plugins脚仔,和一些可選的客戶端nrpe,NSClient++,NSCA和NDOUtils

  • NRPE--半被動(dòng)模式
    存放位置:安裝在客戶端
    NRPE作用:在客戶端執(zhí)行相關(guān)的腳本插件來獲取數(shù)據(jù)舆绎,實(shí)現(xiàn)對客戶端主機(jī)資源的監(jiān)控鲤脏。
    存在形式:守護(hù)進(jìn)程(agent)模式,開啟的端口5666
  • NSClient++:半被動(dòng)模式,相當(dāng)于linux系統(tǒng)的nrpe
  • NDOUtils(不推薦用)
    存在位置:服務(wù)端
    作用:用于將nagios的配置信息和各event產(chǎn)生的數(shù)據(jù)存入數(shù)據(jù)庫中猎醇,以實(shí)現(xiàn)對這些數(shù)據(jù)的檢索和處理窥突。
  • NSCA:純被動(dòng)模式--->用在分布式監(jiān)控環(huán)境中
    位置:NSCA需要同時(shí)安裝在nagios的服務(wù)器端和客戶端

軟件版本

nagios版本:4.3.1
nagios-plugins版本:2.2.1
nrpe版本:3.1.0

實(shí)現(xiàn)環(huán)境

nagios運(yùn)行在LAMP環(huán)境下(如果還沒有安裝LAMP環(huán)境,可以參考下我的安裝LNMP文章硫嘶,因?yàn)槲乙呀?jīng)編譯安裝了php和Mysql阻问,本文直接采取yum 方式安裝apache,當(dāng)然也可以采取編譯方式安裝apache沦疾〕平看個(gè)人喜好和公司需求)

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軟件及插件包

下載慢的話可以去我的網(wǎng)盤下載

[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服務(wù)端

安裝依賴包

[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 

進(jìn)入解壓后的目錄

[root@nagios software]# cd nagios-4.3.1/

創(chuàng)建ngaios用戶和組(把nginx啟動(dòng)用戶www加入到nagios相關(guān)組)

[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界面的驗(yàn)證信息

[root@nagios nagios-4.3.1]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios
New password:   ==> 輸入密碼,這里我輸入的密碼是nagios曹鸠,記住這個(gè)密碼
Re-type new password:  ==> 確認(rèn)密碼
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    ===>    下面添加一行內(nèi)容  AddHandler application/x-httpd-php .php
# LoadModule foo_module modules/mod_foo.so    ===>    下面添加一行內(nèi)容   
 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(也就是編譯參數(shù)加上--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    ===>    可以看到這個(gè)模塊已經(jīng)生成

啟動(dòng)apache

[root@nagios httpd]# systemctl start httpd
[root@nagios httpd]# systemctl enable httpd

啟動(dòng)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

進(jìn)入解壓后的目錄進(jìn)行配置

[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 

進(jìn)去解壓后的目錄進(jìn)行配置

[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

啟動(dòng)nrpe彻桃,并測試服務(wù)端本地是否可以連通

[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中的內(nèi)容開機(jī)可能不執(zhí)行
[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

創(chuàng)建用戶

[root@client1 software]# useradd nagios -M -s /sbin/nologin

解壓nagios-plugin源碼包

[root@client1 software]# tar zxvf nagios-plugins-2.2.1.tar.gz

進(jìn)入解壓后的目錄進(jìn)行配置

[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 

進(jìn)入解壓后的目錄進(jìn)行配置

[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

啟動(dòng)nrpe邻眷,并測試服務(wù)端本地是否可以連通

[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中的內(nèi)容開機(jī)可能不執(zhí)行
[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

允許服務(wù)端IP和本機(jī)訪問,172.16.0.18是nagios服務(wù)端IP地址

allowed_hosts=127.0.0.1,::1    ===>    修改為    allowed_hosts=127.0.0.1,::1,172.16.0.18 

注釋下面幾行內(nèi)容

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文件末尾增加下面幾行內(nèi)容

# 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%

創(chuàng)建一個(gè)監(jiān)控內(nèi)存的perl腳本

[root@client1 etc]# vi /usr/local/nagios/libexec/check_mem.pl

添加下面內(nèi)容

#! /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>|".$perfdata_output."\n";}
    else { print "<b>CRITICAL: ".$output."</b>|".$perfdata_output."\n";}
    exit $ERRORS{'CRITICAL'};
} elsif ($mem_percent>$mem_warning || $swap_percent>$swap_warning) {
    if ($verbose) { print "<b>WARNING: ".$verbose_output."</b>|".$perfdata_output."\n";}
    else { print "<b>WARNING: ".$output."</b>|".$perfdata_output."\n";}
    exit $ERRORS{'WARNING'};
} else {
    if ($verbose) { print "OK: ".$verbose_output."|".$perfdata_output."\n";}
    else { print "OK: ".$output."|".$perfdata_output."\n";}
    exit $ERRORS{'OK'};
}

sub sys_stats {
    my @memory = split(" ", `free -mt`);
    my $mem_total = $memory[7];
    my $mem_used;
    if ( $DONT_INCLUDE_BUFFERS) { $mem_used = $memory[15]; }
    else { $mem_used = $memory[8];}
    my $swap_total = $memory[18];
    my $swap_used = $memory[19];
    my $mem_percent = ($mem_used / $mem_total) * 100;
    my $swap_percent;
    if ($swap_total == 0) {
  $swap_percent = 0;
    } else {
  $swap_percent = ($swap_used / $swap_total) * 100;
    }
    return (sprintf("%.0f",$mem_percent),$mem_total,$mem_used, sprintf("%.0f",$swap_percent),$swap_total,$swap_used);
}

sub print_usage () {
    print "Usage: $PROGNAME -w <warn> -c <crit> [-v] [-h]\n";
    exit $ERRORS{'UNKNOWN'} unless ($opt_h);
}

sub print_help () {
    print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
    print "Copyright (c) 2005 Garrett Honeycutt/Rouven Homann/Cedric Temple\n";
    print "\n";
    print_usage();
    print "\n";
    print "-w <MemoryWarn>,<SwapWarn> = Memory and Swap usage to activate a warning message (eg: -w 90,25 ) .\n";
    print "-c <MemoryCrit>,<SwapCrit> = Memory and Swap usage to activate a critical message (eg: -c 95,50 ).\n";
    print "-v = Verbose Output.\n";
    print "-h = This screen.\n\n";
    support();
}

給腳本增加執(zhí)行權(quán)限

[root@client1 etc]# chmod 755 /usr/local/nagios/libexec/check_mem.pl

重啟nrpe服務(wù)

# 方法一
[root@client1 etc]# killall nrpe               
[root@client1 etc]# /usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg
# 方法二
[root@client1 etc]# kill -HUP `ps -ef|grep nrpe|awk 'NR==1{print $2}'`

在本機(jī)執(zhí)行兩個(gè)命令看下效果

[root@client1 etc]# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_mem
<b>CRITICAL: Memory Usage (W> 10, C> 3): 29% <br>Swap Usage (W> 100, C> 100): 12%</b>|MemUsed=29%;10;3 SwapUsed=12%;100;100
[root@client1 etc]# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_disk
DISK OK - free space: / 4201 MB (24.15% inode=97%);| /=13192MB;13915;15654;0;17394

<br />
服務(wù)端配置
nrpe連接客戶端機(jī)器測試是否可以連通肆饶,然后執(zhí)行一個(gè)監(jiān)控命令(如果不通的話可能是因?yàn)閟elinux和防火墻沒關(guān)閉而造成的)

[root@nagios nrpe-3.1.0]# /usr/local/nagios/libexec/check_nrpe -H 172.16.0.20
NRPE v3.1.0-rc1
[root@nagios nrpe-3.1.0]# /usr/local/nagios/libexec/check_nrpe -H 172.16.0.20 -c check_disk
DISK OK - free space: / 4201 MB (24.15% inode=97%);| /=13192MB;13915;15654;0;17394

因?yàn)閚agios默認(rèn)把全部的權(quán)限給nagiosadmin改衩,所以可以通過修改cgi.cfg文件賦予nagios權(quán)限,切換到/usr/local/nagios/etc目錄下

[root@nagios nrpe-3.1.0]# cd /usr/local/nagios/etc
[root@nagios etc]# ll
總用量 144
-rw-rw-r-- 1 nagios nagios 12999 4月  21 17:00 cgi.cfg
-rw-r--r-- 1 root   root      45 4月  24 09:28 htpasswd.users
-rw-rw-r-- 1 nagios nagios 44831 4月  21 17:00 nagios.cfg
-rw-r--r-- 1 root   root   10765 4月  24 12:32 nrpe.cfg
drwxrwxr-x 2 nagios nagios   336 4月  21 17:00 objects
-rw-rw---- 1 nagios nagios  1312 4月  21 17:00 resource.cfg
[root@nagios etc]# grep nagiosadmin cgi.cfg
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
[root@nagios etc]# sed -i 's/nagiosadmin/nagiosadmin,nagios/g' cgi.cfg
[root@nagios etc]# grep nagiosadmin cgi.cfg
authorized_for_system_information=nagiosadmin,nagios
authorized_for_configuration_information=nagiosadmin,nagios
authorized_for_system_commands=nagiosadmin,nagios
authorized_for_all_services=nagiosadmin,nagios
authorized_for_all_hosts=nagiosadmin,nagios
authorized_for_all_service_commands=nagiosadmin,nagios
authorized_for_all_host_commands=nagiosadmin,nagios

修改nagios.cfg(自定義一些配置)

[root@nagios etc]# vi nagios.cfg +34
#注釋掉下面這行
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
#添加下面兩行內(nèi)容
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

創(chuàng)建hosts.cfg和services.cfg這兩個(gè)文件

[root@nagios etc]# cd objects/
[root@nagios objects]# pwd
/usr/local/nagios/etc/objects
[root@nagios objects]# touch services.cfg
[root@nagios objects]# head -51 localhost.cfg  > hosts.cfg
[root@nagios objects]# chown -R nagios.nagios *

修改nagios檢查語法腳本

[root@nagios objects]# vim /etc/init.d/nagios +181
#check_config
$NagiosBin -v $NagiosCfgFile;

vi commands.cfg 進(jìn)入后按shift+g切到結(jié)尾加入下面內(nèi)容驯镊。

# 'check_nrpe' command definition
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

# 'check_ping' command definition
define command{
        command_name    check-ping
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 100.0,20% -c 200.0,50% -p 3 -t 2
        }

# 'check_http' command definition
define command{
        command_name    check-weburl
        command_line    $USER1$/check_http -H $HOSTADDRESS$ $ARG1$ -w 5 -c 10
        }

# 'check_tcp' command definition
define command{
        command_name    check-tcp
        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ -w 0.02 -c 0.1
        }

查看有哪些cfg文件

[root@nagios objects]# pwd
/usr/local/nagios/etc/objects
[root@nagios objects]# ll
總用量 100
-rw-rw-r-- 1 nagios nagios  7860 4月  24 16:53 commands.cfg
-rw-rw-r-- 1 nagios nagios  2138 4月  21 17:00 contacts.cfg
-rw-r--r-- 1 nagios nagios  1843 4月  24 16:46 hosts.cfg
-rw-rw-r-- 1 nagios nagios  5379 4月  21 17:00 localhost.cfg
-rw-rw-r-- 1 nagios nagios  3070 4月  21 17:00 printer.cfg
-rw-r--r-- 1 nagios nagios     0 4月  24 16:46 services.cfg
-rw-rw-r-- 1 nagios nagios  3252 4月  21 17:00 switch.cfg
-rw-rw-r-- 1 nagios nagios 10595 4月  21 17:00 templates.cfg
-rw-rw-r-- 1 nagios nagios  3180 4月  21 17:00 timeperiods.cfg
-rw-rw-r-- 1 nagios nagios  3991 4月  21 17:00 windows.cfg

常用對象介紹

  • 聯(lián)系人
    contact
    出了問題像誰報(bào)告?一般當(dāng)然是系統(tǒng)管理員了
  • 監(jiān)控時(shí)間段
    timeperiod
    7X24小時(shí)不間斷還是周一至周五,或是自定義的其他時(shí)間段
  • 被監(jiān)控主機(jī)
    host
    所需要監(jiān)控的服務(wù)器,當(dāng)然可以是監(jiān)控機(jī)自己
  • 監(jiān)控命令
    command
    nagios發(fā)出的哪個(gè)指令來執(zhí)行某個(gè)監(jiān)控,這也是自己定義的
  • 被監(jiān)控的服務(wù)
    service
    例如主機(jī)是否存活,80端口是否開,磁盤使用情況或者自定義的服務(wù)等

contacts.cfg文件介紹

  • service_notification_period 24x7
    服務(wù)出了狀況通知的時(shí)間段,這個(gè)時(shí)間段就是上面在timeperiods.cfg中定義的.
  • host_notification_period 24x7
    主機(jī)出了狀況通知的時(shí)間段, 這個(gè)時(shí)間段就是上面在timeperiods.cfg中定義的
  • service_notification_options w,u,c,r
    當(dāng)服務(wù)出現(xiàn)w—報(bào)警(warning),u—未知(unkown),c—嚴(yán)重(critical),或者r—從異常情況恢復(fù)正常,在這四種情況下通知聯(lián)系人.
  • host_notification_options d,u,r
    當(dāng)主機(jī)出現(xiàn)d----—當(dāng)機(jī)(down),u—返回不可達(dá)(unreachable),r—從異常情況恢復(fù)正常,在這3種情況下通知聯(lián)系人
  • service_notification_commands notify- service -by-email
    服務(wù)出問題通知采用的命令notify-by-email,這個(gè)命令是在commands.cfg中定義的,作用是給聯(lián)系人發(fā)郵件.至于commands.cfg之后將專門介紹
  • host_notification_commands host-notify-by-email notify- host--by-email
    同上,主機(jī)出問題時(shí)采用的也是發(fā)郵件的方式通知聯(lián)系人
  • pager 1338757xxxx

聯(lián)系人的手機(jī),如果支持短信的通知的話,這個(gè)就很有用了.

  • alias是聯(lián)系人別名,address是地址 .

contactgroups.cfg文件介紹

define contactgroup{
contactgroup_name 組名 //聯(lián)系人組的名稱
alias 別名 //別名
members 用戶名 //組的成員,來自于上面定義的contacts.cfg,如果有多個(gè)聯(lián)系人則以逗號相隔
}


主機(jī)模板介紹(hosts.cfg)
> ```# host 
define host{
       host_name                       主機(jī)名  //被監(jiān)控主機(jī)的名稱,最好別帶空格nagios-server
       alias                           別名
       address                         IP  //被監(jiān)控主機(jī)的IP地址
       check_command                   check-host-alive  //監(jiān)控的命令check-host-alive,這個(gè)命令來自commands.cfg,用來監(jiān)控主機(jī)是否存活
       max_check_attempts              5  //檢查失敗后重試的次數(shù)
       check_period                    24x7  //檢查的時(shí)間段24x7,同樣來自于我們之前在    timeperiods.cfg中定義的
       contact_groups                  組名  //聯(lián)系人組,上面在contactgroups.cfg中定義的組名
       notification_interval           10  //提醒的間隔,每隔10秒提醒一次
       notification_period             24x7  //提醒的周期, 24x7,同樣來自于我們之前在timeperiods.cfg中定義的
       notification_options            d,u,r  //指定什么情況下提醒,具體含義見之前contacts.cfg部分的介紹
       }

主機(jī)組模板介紹(hosts.cfg)

define hostgroup{
hostgroup_name 主機(jī)組名
alias 別名
members 主機(jī)名 //組的成員主機(jī),多個(gè)主機(jī)以逗號相隔,必須是上面hosts.cfg中定義的
}


服務(wù)模板介紹(services.cfg)
> ```# service definition
define service{
       host_name               主機(jī)名  //被監(jiān)控的主機(jī),hosts.cfg中定義的
       service_description     check-host-alive  //這個(gè)監(jiān)控項(xiàng)目的描述(也可以說是這個(gè)項(xiàng)目的名稱),可以空格,我們這里定義的是監(jiān)控這個(gè)主機(jī)是不是存活
       check_command           check-host-alive  //所用的命令,是commands.cfg中定義的
       max_check_attempts      5
       normal_check_interval   3
       retry_check_interval    2
       check_period            24x7  //監(jiān)控的時(shí)間段,是timeperiods.cfg中定義的
       notification_interval   10
       notification_period     24x7  //通知的時(shí)間段, ,是timeperiods.cfg中定義的
       notification_options    w,u,c,r  //在監(jiān)控的結(jié)果是wucr時(shí)通知聯(lián)系人,具體含義看前文.
       contact_groups          組名  //聯(lián)系人組,是contactgroups.cfg中定義的
       }

主機(jī)模板配置

[root@nagios objects]# vi hosts.cfg +21
刪除下面11行內(nèi)容
添加下面內(nèi)容

# Define some hosts

###########172.16.0.18##################

define host {
        use                      linux-server
        host_name                nagios
        alias                    nagios
        address                  172.16.0.18
        check_command            check-host-alive
        max_check_attempts        3
        normal_check_interval     2
        retry_check_interval      2
        check_period              24x7
        notification_interval     300
        notification_period       24x7
        notification_options      d,u,r
        contact_groups            admins
        process_perf_data         1
}

###########172.16.0.18##################

define host {
        use                      linux-server
        host_name                client1
        alias                    client1
        address                  172.16.0.20
        check_command            check-host-alive
        max_check_attempts        3
        normal_check_interval     2
        retry_check_interval      2
        check_period              24x7
        notification_interval     300
        notification_period       24x7
        notification_options      d,u,r
        contact_groups            admins
        process_perf_data         1
}

把監(jiān)控的主機(jī)添加到主機(jī)組里面
[root@nagios objects]# vi hosts.cfg +76
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         nagios,client1     ; Comma separated list of hosts that belong to this group
        }

服務(wù)模板配置

[root@nagios objects]# pwd
/usr/local/nagios/etc/objects
[root@nagios objects]# vi services.cfg 
添加下面內(nèi)容

###########172.16.0.18##################

define service{
        use                     generic-service
        host_name               nagios
        service_description     Load
        check_command           check_nrpe!check_load       #這里的check_nrpe不是服務(wù)端/usr/local/nagios/libexec/check_nrpe,而是command.cfg里定義的命令
        }

define service{
        use                     generic-service
        host_name               nagios
        service_description     Disk
        check_command           check_nrpe!check_disk
        }

define service{
        use                     generic-service
        host_name               nagios
        service_description     memory
        check_command           check_nrpe!check_mem
        }
define service{
        use                     generic-service
        host_name               nagios
        service_description     Ping
        check_command           check-ping!172.16.0.18
}

define service{
        use                     generic-service
        host_name               nagios
        service_description     port_3306
        check_command           check-tcp!3306
}

###########172.16.0.20##################

define service{
        use                     generic-service
        host_name               client1
        service_description     Load
        check_command           check_nrpe!check_load
        }

define service{
        use                     generic-service
        host_name               client1
        service_description     Disk
        check_command           check_nrpe!check_disk
        }

define service{
        use                     generic-service
        host_name               client1
        service_description     memory
        check_command           check_nrpe!check_mem
        }
define service{
    use             generic-service
        host_name               client1
        service_description     Ping
        check_command           check-ping!172.16.0.20
}
define service{
        use                     generic-service
        host_name               client1
        service_description     port_3306
        check_command           check-tcp!3306
}

重啟nagios服務(wù)

[root@nagios objects]# /etc/init.d/nagios restart

打開瀏覽器訪問


點(diǎn)擊hosts



點(diǎn)擊services


ok葫督,我們的配置告一段落了。

出圖部分
下載pnp4nagios源碼包

[root@nagios objects]# cd /software/
[root@nagios software]# wget https://jaist.dl.sourceforge.net/project/pnp4nagios/PNP-0.6/pnp4nagios-0.6.25.tar.gz

安裝依賴包

[root@nagios software]# yum install cairo pango perl-rrdtool rrdtool librrds-perl zlib zlib-devel freetype freetype-devel gd gd-devel -y

解壓pnp4nagios源碼包

[root@nagios software]# tar zxf pnp4nagios-0.6.25.tar.gz 

進(jìn)入解壓后的目錄配置png

[root@nagios pnp4nagios-0.6.25]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios

編譯及安裝

[root@nagios pnp4nagios-0.6.25]# make all
[root@nagios pnp4nagios-0.6.25]# make install
[root@nagios pnp4nagios-0.6.25]# make install-webconf
[root@nagios pnp4nagios-0.6.25]# make install-config
[root@nagios pnp4nagios-0.6.25]# make install-init
[root@nagios pnp4nagios-0.6.25]# cd sample-config/
[root@nagios sample-config]# make install-webconf

配置pnp4nagios

[root@nagios sample-config]# cd /usr/local/pnp4nagios/etc/
[root@nagios etc]# mv misccommands.cfg-sample misccommands.cfg
[root@nagios etc]#  mv rra.cfg-sample rra.cfg
[root@nagios etc]# mv nagios.cfg-sample nagios.cfg
[root@nagios etc]# cd pages/
[root@nagios pages]# mv web_traffic.cfg-sample web_traffic.cfg
[root@nagios pages]#  cd ../check_commands/
[root@nagios check_commands]# mv check_all_local_disks.cfg-sample check_all_local_disks.cfg
[root@nagios check_commands]#  mv check_nrpe.cfg-sample check_nrpe.cfg
[root@nagios check_commands]# mv check_nwstat.cfg-sample check_nwstat.cfg
[root@nagios check_commands]# systemctl enable npcd
[root@nagios check_commands]# systemctl start npcd

配置Nagios數(shù)據(jù)輸出接口(以BULK模式運(yùn)行)詳情參考官網(wǎng)https://docs.pnp4nagios.org/pnp-0.6/config#bulk_mode

[root@nagios check_commands]# vi /usr/local/nagios/etc/nagios.cfg
process_performance_data=1 #默認(rèn)為0板惑,修改為1

并在該文件中添加下面的內(nèi)容

#

# service performance data

#

service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata

service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$

service_perfdata_file_mode=a

service_perfdata_file_processing_interval=15

service_perfdata_file_processing_command=process-service-perfdata-file

#

# host performance data starting with Nagios 

#

host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata

host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$

host_perfdata_file_mode=a

host_perfdata_file_processing_interval=15

host_perfdata_file_processing_command=process-host-perfdata-file

配置command.cfg

[root@nagios check_commands]#  vi /usr/local/nagios/etc/objects/commands.cfg
在該文件中添加下面的內(nèi)容

define command{
      command_name    process-service-perfdata-file
      command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/service-perfdata

}

define command{
      command_name    process-host-perfdata-file
      command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/host-perfdata

}

在模板配置文件中添加圖表圖標(biāo)模板:

[root@nagios check_commands]# vi /usr/local/nagios/etc/objects/templates.cfg
在該文件中添加下面的內(nèi)容

define host {
  name      host-pnp
  action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
  register  0
}

define service {
  name      service-pnp
  action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
  register  0

}

在監(jiān)控主機(jī)和服務(wù)中調(diào)用圖表模板(在主機(jī)和服務(wù)后面添加新的模板):

[root@nagios check_commands]# vi /usr/local/nagios/etc/objects/hosts.cfg 
主機(jī)內(nèi)容修改如下
###########172.16.0.18##################

define host {
        use                      linux-server,host-pnp
        host_name                nagios
        alias                    nagios
        address                  172.16.0.18
        check_command            check-host-alive
        max_check_attempts        3
        normal_check_interval     2
        retry_check_interval      2
        check_period              24x7
        notification_interval     300
        notification_period       24x7
        notification_options      d,u,r
        contact_groups            admins
        process_perf_data         1
}

###########172.16.0.18##################

define host {
        use                      linux-server,host-pnp
        host_name                client1
        alias                    client1
        address                  172.16.0.20
        check_command            check-host-alive
        max_check_attempts        3
        normal_check_interval     2
        retry_check_interval      2
        check_period              24x7
        notification_interval     300
        notification_period       24x7
        notification_options      d,u,r
        contact_groups            admins
        process_perf_data         1
}

[root@nagios check_commands]# vi /usr/local/nagios/etc/objects/services.cfg 
服務(wù)內(nèi)容修改如下
###########172.16.0.18##################

define service{
        use                     generic-service,service-pnp
        host_name               nagios
        service_description     Load
        check_command           check_nrpe!check_load       #這里的check_nrpe不是服務(wù)端/usr/local/nagios/libexec/check_nrpe,而是command.cfg里定義的命令
        }

define service{
        use                     generic-service,service-pnp
        host_name               nagios
        service_description     Disk
        check_command           check_nrpe!check_disk
        }

define service{
        use                     generic-service,service-pnp
        host_name               nagios
        service_description     memory
        check_command           check_nrpe!check_mem
        }
define service{
        use                     generic-service,service-pnp
        host_name               nagios
        service_description     Ping
        check_command           check-ping!172.16.0.18
}

define service{
        use                     generic-service,service-pnp
        host_name               nagios
        service_description     port_3306
        check_command           check-tcp!3306
}

###########172.16.0.20##################

define service{
        use                     generic-service,service-pnp
        host_name               client1
        service_description     Load
        check_command           check_nrpe!check_load
        }

define service{
        use                     generic-service,service-pnp
        host_name               client1
        service_description     Disk
        check_command           check_nrpe!check_disk
        }

define service{
        use                     generic-service,service-pnp
        host_name               client1
        service_description     memory
        check_command           check_nrpe!check_mem
        }
define service{
    use                     generic-service,service-pnp
        host_name               client1
        service_description     Ping
        check_command           check-ping!172.16.0.20
}
define service{
        use                     generic-service,service-pnp
        host_name               client1
        service_description     port_3306
        check_command           check-tcp!3306
}

圖表展示
重啟nagios和apache

[root@nagios check_commands]# /etc/init.d/nagios checkconfig 
[root@nagios check_commands]# /etc/init.d/nagios restart
[root@nagios check_commands]# systemctl restart httpd

訪問nagios界面即可看到圖表小圖標(biāo):


點(diǎn)擊圖標(biāo)會(huì)顯示pnp4nagios測試頁面:



全是綠色代表配置正常橄镜,如果不是全綠,要逐個(gè)解決錯(cuò)誤冯乘。然后移除或修改install.php文件:

[root@nagios ~]# rm -rf /usr/local/pnp4nagios/share/install.php

再次點(diǎn)擊圖標(biāo)就會(huì)顯示當(dāng)前監(jiān)控服務(wù)由pnp4nagios生成的圖表了


郵件報(bào)警配置

查看sendmail郵件系統(tǒng)是否安裝

[root@nagios ~]# rpm -qa|grep sendmail

如果沒有安裝洽胶,可以yum安裝一下

[root@nagios ~]# yum -y install sendmail

啟動(dòng)sendmail

[root@nagios ~]# systemctl enable sendmail
[root@nagios ~]# systemctl start sendmail

測試發(fā)送郵件:

[root@nagios ~]# echo "test" | mail zhengxinlei@test.com.cn

發(fā)送外部郵件的時(shí)候,這種本地形式的發(fā)件人和發(fā)件地址容易被誤認(rèn)為垃圾郵件而拒收裆馒。
我們這里定義郵件發(fā)送smtp信息姊氓。
首先安裝mailx12.5

wget http://fossies.org/linux/misc/old/mailx-12.5.tar.gz
tar zxf mailx-12.5.tar.gz
cd mailx-12.5
make
make install UCBINSTALL=/usr/bin/install
mv /bin/mail /bin/mail_old
ln -s /usr/local/bin/mailx /bin/mail
mail -V

接下來配置外部smtp發(fā)件信息

vi /etc/nail.rc 
添加如下內(nèi)容:
set bsdcompat
set from=yunwei@yasn.com.cn
set smtp=smtp.yasn.com.cn
set smtp-auth-user=yunwei@yasn.com.cn
set smtp-auth-password=123456
set smtp-auth=login

測試郵件發(fā)送

echo 'mail content'|mail -s test zhengxinlei@test.com.cn

發(fā)送郵件測試,可以看到發(fā)件人是我們設(shè)定的外部郵件發(fā)件人


編輯contacts.cfg喷好,定義郵件聯(lián)系人和組

[root@nagios ~]# cd /usr/local/nagios/etc/objects/
[root@nagios objects]# ls
commands.cfg  localhost.cfg~  services.cfg  templates.cfg    hosts.cfg    printer.cfg     
switch.cfg   windows.cfg contacts.cfg   localhost.cfg  timeperiods.cfg  
[root@nagios objects]# vi contacts.cfg              // 修改內(nèi)容如下
define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user

        #email                           nagios@localhost       ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
        email                           3166@vip.qq.com,xinlei@126.com
        }


define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin
        }

編輯commands.cfg文件翔横,定義郵件發(fā)送命令

[root@nagios objects]# vi commands.cfg                // 查看下面內(nèi)容是否存在,不存在則添加
# 'notify-host-by-email' command definition
define command{
  command_name    notify-host-by-email
  command_line    /usr/bin/printf "%b" "***** Nagios ***** Notification Type: $NOTIFICATIONTYPE$ Host: $HOSTNAME$ State: $HOSTSTATE$ Address: $HOSTADDRESS$ Info: $HOSTOUTPUT$ Date/Time: $LONGDATETIME$ " | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
  }

# 'notify-service-by-email' command definition
define command{
  command_name    notify-service-by-email
  command_line    /usr/bin/printf "%b" "***** Nagios ***** Notification Type: $NOTIFICATIONTYPE$ Service: $SERVICEDESC$ Host: $HOSTALIAS$ Address: $HOSTADDRESS$ State: $SERVICESTATE$ Date/Time: $LONGDATETIME$ Additional Info: $SERVICEOUTPUT$ " | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
  }

修改一下監(jiān)控的閥值梗搅,模擬報(bào)警棕孙。



重啟nagios服務(wù)和sendmail服務(wù)

[root@nagios ~]# systemctl restart nagios
[root@nagios ~]# systemctl stop sendmail
[root@nagios ~]# ps -ef|grep sendmail
[root@nagios ~]# systemctl start sendmail
[root@nagios ~]# ps -ef|grep sendmail

查看報(bào)警的郵件,來幾張圖吧:



這是報(bào)警恢復(fù)后的郵件:


ok,搞定了蟀俊。
<br />

常見郵件發(fā)送報(bào)錯(cuò):

報(bào)錯(cuò)1



解決方法:
修改hosts為域名形式


[root@nagios objects]# cat /etc/hosts
172.16.0.18 nagios nagios.com 

<br />


報(bào)錯(cuò)2(發(fā)送外部郵件的時(shí)候钦铺,這種本地形式的發(fā)件人和發(fā)件地址容易被誤認(rèn)為垃圾郵件而拒收。)



解決方法肢预,見上面配置mail發(fā)件人smtp信息矛洞。

<br />

●監(jiān)視物理組件的高級 Linux 命令(僅供參考)

內(nèi)存:top free、vmstat烫映、mpstat沼本、iostat、sar
CPU:top vmstat锭沟、mpstat抽兆、iostat、sar
I/O:vmstat族淮、mpstat辫红、iostat、sar
進(jìn)程:ipcs祝辣、ipcrm
負(fù)載:uptime


<br />
一鍵安裝nagios命令參考

#############nagios-server-install########################
mkdir /software/
# mv nagios-4.3.1.tar.gz nagios-plugins-2.2.1.tar.gz nrpe-3.1.0.tar.gz pnp4nagios-0.6.25.tar.gz /software/
cd /software/
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
wget https://jaist.dl.sourceforge.net/project/pnp4nagios/PNP-0.6/pnp4nagios-0.6.25.tar.gz
yum -y install httpd httpd-devel gcc glibc glibc-common gd gd-devel perl-devel perl-CPAN fcgi perl-FCGI perl-FCGI-ProcManager
tar zxvf nagios-4.3.1.tar.gz
cd nagios-4.3.1/
useradd nagios -s /sbin/nologin 
id www
groupadd nagcmd
usermod -a -G nagcmd nagios 
usermod -a -G nagcmd www
id -n -G nagios
id -n -G www
./configure --with-command-group=nagcmd
make all
make install-init
make install-commandmode
make install-config
make install
sleep 2    
cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
make install-webconf
echo 'nagios:$apr1$UigX6LG0$29RugFJZTSxRjnX4NIH3E0' > /usr/local/nagios/etc/htpasswd.users
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
sed -i 's/User apache/User www/g' /etc/httpd/conf/httpd.conf
sed -i 's/Group apache/Group www/g' /etc/httpd/conf/httpd.conf
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /etc/httpd/conf/httpd.conf
grep 'Listen' /etc/httpd/conf/httpd.conf
grep 'User' /etc/httpd/conf/httpd.conf 
grep 'Group' /etc/httpd/conf/httpd.conf 
grep 'DirectoryIndex' /etc/httpd/conf/httpd.conf
cd /software/php-7.1.4/
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --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
make && make install
sleep 1

cd /etc/httpd/
ll /etc/httpd/modules/libphp7.so
systemctl start httpd
systemctl enable httpd
chkconfig nagios on
/etc/init.d/nagios start
cd /software/
tar zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1/
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --enable-perl-modules
make && make install

cd /software/
tar zxvf nrpe-3.1.0.tar.gz
cd nrpe-3.1.0/
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
ls /usr/local/nagios/libexec/
/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg 
echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local 
netstat -lnput|grep 5666
/usr/local/nagios/libexec/check_nrpe -H localhost


#######################nagios-client-install###########################
mkdir /software/
# mv nagios-plugins-2.2.1.tar.gz nrpe-3.1.0.tar.gz /software/
cd /software/
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
yum install perl-devel perl-CPAN -y
useradd nagios -M -s /sbin/nologin
tar zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1/
./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
make && make install
sleep 1
cd ..
tar zxvf nrpe-3.1.0.tar.gz
cd nrpe-3.1.0/
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
mkdir /usr/local/nagios/etc/
cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
ls /usr/local/nagios/libexec/
/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg 
echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local           
netstat -lnput|grep 5666
sed -i 's/allowed_hosts=127.0.0.1,::1/allowed_hosts=127.0.0.1,::1,172.16.0.1/g' /usr/local/nagios/etc/nrpe.cfg
sed -i 's/^command\[check/\#command\[check/g' /usr/local/nagios/etc/nrpe.cfg
cat >> /usr/local/nagios/etc/nrpe.cfg << EOF
# 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%
EOF
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_disk

總結(jié):
多多查看nagios日志和mail日志贴妻,進(jìn)行排錯(cuò)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蝙斜,一起剝皮案震驚了整個(gè)濱河市名惩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌孕荠,老刑警劉巖娩鹉,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異稚伍,居然都是意外死亡弯予,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門槐瑞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人阁苞,你說我怎么就攤上這事困檩。” “怎么了那槽?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵悼沿,是天一觀的道長。 經(jīng)常有香客問我骚灸,道長糟趾,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮义郑,結(jié)果婚禮上蝶柿,老公的妹妹穿的比我還像新娘。我一直安慰自己非驮,他們只是感情好交汤,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著劫笙,像睡著了一般芙扎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上填大,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天戒洼,我揣著相機(jī)與錄音,去河邊找鬼允华。 笑死圈浇,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的例获。 我是一名探鬼主播汉额,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼榨汤!你這毒婦竟也來了蠕搜?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤收壕,失蹤者是張志新(化名)和其女友劉穎妓灌,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蜜宪,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡虫埂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了圃验。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掉伏。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖澳窑,靈堂內(nèi)的尸體忽然破棺而出斧散,到底是詐尸還是另有隱情,我是刑警寧澤摊聋,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布鸡捐,位于F島的核電站,受9級特大地震影響麻裁,放射性物質(zhì)發(fā)生泄漏箍镜。R本人自食惡果不足惜源祈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望色迂。 院中可真熱鬧香缺,春花似錦、人聲如沸脚草。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽馏慨。三九已至埂淮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間写隶,已是汗流浹背倔撞。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留慕趴,地道東北人痪蝇。 一個(gè)月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像冕房,于是被迫代替她去往敵國和親躏啰。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容