国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

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

wawor4827 / 3021人閱讀

簡介

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

    AllowOverride none
    Require all denied

DocumentRoot "/var/www/html"

    AllowOverride None
    Require all granted


    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted


    DirectoryIndex index.php index.html


    Require all denied

ErrorLog "logs/error_log"
LogLevel warn

    LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
    LogFormat "%h %l %u %t "%r" %>s %b" common
    
      LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
    
    CustomLog "logs/access_log" combined


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"


    AllowOverride None
    Options None
    Require all granted


    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

AddDefaultCharset UTF-8

    MIMEMagicFile conf/magic

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頁面是否可以打開


安裝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


安裝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



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


安裝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."% 
"; $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
"; $verbose_output .= "Swap Usage:".$swap_threshold_output.": ". $swap_percent."% "."- Total: $swap_total MB, used: $swap_used MB, free: $free_swap MB
"; # 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 "CRITICAL: ".$verbose_output."|".$perfdata_output." ";} else { print "CRITICAL: ".$output."|".$perfdata_output." ";} exit $ERRORS{"CRITICAL"}; } elsif ($mem_percent>$mem_warning || $swap_percent>$swap_warning) { if ($verbose) { print "WARNING: ".$verbose_output."|".$perfdata_output." ";} else { print "WARNING: ".$output."|".$perfdata_output." ";} exit $ERRORS{"WARNING"}; } else { if ($verbose) { print "OK: ".$verbose_output."|".$perfdata_output." ";} else { print "OK: ".$output."|".$perfdata_output." ";} 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 -c [-v] [-h] "; exit $ERRORS{"UNKNOWN"} unless ($opt_h); } sub print_help () { print_revision($PROGNAME,"$Revision: ".$PROGVER." $"); print "Copyright (c) 2005 Garrett Honeycutt/Rouven Homann/Cedric Temple "; print " "; print_usage(); print " "; print "-w , = Memory and Swap usage to activate a warning message (eg: -w 90,25 ) . "; print "-c , = Memory and Swap usage to activate a critical message (eg: -c 95,50 ). "; print "-v = Verbose Output. "; print "-h = This screen. "; support(); }

給腳本增加執行權限

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

重啟nrpe服務

# 方法一
[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}"`

在本機執行兩個命令看下效果

[root@client1 etc]# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_mem
CRITICAL: Memory Usage (W> 10, C> 3): 29% 
Swap Usage (W> 100, C> 100): 12%
|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


服務端配置

nrpe連接客戶端機器測試是否可以連通,然后執行一個監控命令(如果不通的話可能是因為selinux和防火墻沒關閉而造成的)

[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

因為nagios默認把全部的權限給nagiosadmin,所以可以通過修改cgi.cfg文件賦予nagios權限,切換到/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
#添加下面兩行內容
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

創建hosts.cfg和services.cfg這兩個文件

[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 進入后按shift+g切到結尾加入下面內容。

# "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
常用對象介紹

聯系人
contact
出了問題像誰報告?一般當然是系統管理員了

監控時間段
timeperiod
7X24小時不間斷還是周一至周五,或是自定義的其他時間段

被監控主機
host
所需要監控的服務器,當然可以是監控機自己

監控命令
command
nagios發出的哪個指令來執行某個監控,這也是自己定義的

被監控的服務
service
例如主機是否存活,80端口是否開,磁盤使用情況或者自定義的服務等

contacts.cfg文件介紹

service_notification_period ? ? 24x7
服務出了狀況通知的時間段,這個時間段就是上面在timeperiods.cfg中定義的.

host_notification_period ? ? ? ?24x7
主機出了狀況通知的時間段, 這個時間段就是上面在timeperiods.cfg中定義的

service_notification_options ? ?w,u,c,r
當服務出現w—報警(warning),u—未知(unkown),c—嚴重(critical),或者r—從異常情況恢復正常,在這四種情況下通知聯系人.

host_notification_options ? ? ? d,u,r
當主機出現d----—當機(down),u—返回不可達(unreachable),r—從異常情況恢復正常,在這3種情況下通知聯系人

service_notification_commands ? notify- service -by-email
服務出問題通知采用的命令notify-by-email,這個命令是在commands.cfg中定義的,作用是給聯系人發郵件.至于commands.cfg之后將專門介紹

host_notification_commands ? ? ?host-notify-by-email notify- host--by-email
同上,主機出問題時采用的也是發郵件的方式通知聯系人

email ? ? ? ? ? ? ? ? ? ? ? ? ??123456@qq.com
很明顯,聯系的人email地址

pager ? ? ? ? ? ? ? ? ? ? ? ? ? 1338757xxxx
聯系人的手機,如果支持短信的通知的話,這個就很有用了.

alias是聯系人別名,address是地址 .

contactgroups.cfg文件介紹

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

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

define hostgroup{
     hostgroup_name          主機組名  
     alias                   別名  
     members                 主機名  //組的成員主機,多個主機以逗號相隔,必須是上面hosts.cfg中定義的
     }
服務模板介紹(services.cfg)
> ```# service definition
define service{
       host_name               主機名  //被監控的主機,hosts.cfg中定義的
       service_description     check-host-alive  //這個監控項目的描述(也可以說是這個項目的名稱),可以空格,我們這里定義的是監控這個主機是不是存活
       check_command           check-host-alive  //所用的命令,是commands.cfg中定義的
       max_check_attempts      5
       normal_check_interval   3
       retry_check_interval    2
       check_period            24x7  //監控的時間段,是timeperiods.cfg中定義的
       notification_interval   10
       notification_period     24x7  //通知的時間段, ,是timeperiods.cfg中定義的
       notification_options    w,u,c,r  //在監控的結果是wucr時通知聯系人,具體含義看前文.
       contact_groups          組名  //聯系人組,是contactgroups.cfg中定義的
       }

主機模板配置

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

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

把監控的主機添加到主機組里面
[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
        }

服務模板配置

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

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

define service{
        use                     generic-service
        host_name               nagios
        service_description     Load
        check_command           check_nrpe!check_load        #這里的check_nrpe不是服務端/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服務

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

打開瀏覽器訪問


點擊hosts

點擊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 

進入解壓后的目錄配置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數據輸出接口(以BULK模式運行)詳情參考官網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 #默認為0,修改為1

并在該文件中添加下面的內容

#

# service performance data

#

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

service_perfdata_file_template=DATATYPE::SERVICEPERFDATA	TIMET::$TIMET$	HOSTNAME::$HOSTNAME$	SERVICEDESC::$SERVICEDESC$	SERVICEPERFDATA::$SERVICEPERFDATA$	SERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$	HOSTSTATE::$HOSTSTATE$	HOSTSTATETYPE::$HOSTSTATETYPE$	SERVICESTATE::$SERVICESTATE$	SERVICESTATETYPE::$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	TIMET::$TIMET$	HOSTNAME::$HOSTNAME$	HOSTPERFDATA::$HOSTPERFDATA$	HOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$	HOSTSTATE::$HOSTSTATE$	HOSTSTATETYPE::$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
在該文件中添加下面的內容

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

}

在模板配置文件中添加圖表圖標模板:

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

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

}

在監控主機和服務中調用圖表模板(在主機和服務后面添加新的模板):

[root@nagios check_commands]# vi /usr/local/nagios/etc/objects/hosts.cfg 
主機內容修改如下
###########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 
服務內容修改如下
###########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不是服務端/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界面即可看到圖表小圖標:

點擊圖標會顯示pnp4nagios測試頁面:

全是綠色代表配置正常,如果不是全綠,要逐個解決錯誤。然后移除或修改install.php文件:

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

再次點擊圖標就會顯示當前監控服務由pnp4nagios生成的圖表了

郵件報警配置

查看sendmail郵件系統是否安裝

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

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

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

啟動sendmail 

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

測試發送郵件:  

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

  

發送外部郵件的時候,這種本地形式的發件人和發件地址容易被誤認為垃圾郵件而拒收。
我們這里定義郵件發送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發件信息

vi /etc/nail.rc 
添加如下內容:
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

測試郵件發送

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

發送郵件測試,可以看到發件人是我們設定的外部郵件發件人

編輯contacts.cfg,定義郵件聯系人和組

[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              // 修改內容如下
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文件,定義郵件發送命令

[root@nagios objects]# vi commands.cfg                // 查看下面內容是否存在,不存在則添加
# "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$
  }

修改一下監控的閥值,模擬報警。

重啟nagios服務和sendmail服務

[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

查看報警的郵件,來幾張圖吧:

這是報警恢復后的郵件:


ok,搞定了。

常見郵件發送報錯:

報錯1

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

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



報錯2(發送外部郵件的時候,這種本地形式的發件人和發件地址容易被誤認為垃圾郵件而拒收。)

解決方法,見上面配置mail發件人smtp信息。


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

內存:top  free、vmstat、mpstat、iostat、sar
CPU:top vmstat、mpstat、iostat、sar
I/O:vmstat、mpstat、iostat、sar
進程:ipcs、ipcrm
負載:uptime


一鍵安裝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

總結:
多多查看nagios日志和mail日志,進行排錯。

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/8028.html

相關文章

  • Nagios 安裝微信短信提醒

    摘要:或參考集成安裝文檔通過微信報警提供腳本命令插件,通過新增用戶通知的方式,調用命令將告警通知發送至云告警平臺,根據通知策略,分派和通知到用戶,提供微信短信電話和郵件方式。 引言 Nagios 作為業界非常強大的一款開源監視系統。 監控網絡服務(SMTP、POP3、HTTP、NNTP、PING 等); 監控主機資源(處理器負荷、磁盤利用率等); 簡單地插件設計使得用戶可以方便地擴展自己服...

    MoAir 評論0 收藏0

發表評論

0條評論

wawor4827

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<