系統環境:CentOS release 5.8 x86_64

一:監控端安裝

1,安裝perl模組:

(1)、先安裝FCGI模組

  1. wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz 
  2. tar xvzf FCGI-0.73.tar.gz 
  3. cd FCGI-0.73 
  4. perl Makefile.PL 
  5. make 
  6. make install 

(2)、安裝FCGI-ProcManager模組

  1. wget http://mirrors.ustc.edu.cn/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz 
  2. tar xvzf FCGI-ProcManager-0.19.tar.gz 
  3. cd FCGI-ProcManager-0.19 
  4. perl Makefile.PL 
  5. make 
  6. make install 

(3)、安裝IO和IO::ALL模組

  1. wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz 
  2. tar zxvf IO-1.25.tar.gz 
  3. cd IO-1.25 
  4. perl Makefile.PL 
  5. make 
  6. make install 
  7. wget http://mirrors.xmu.edu.cn/CPAN/authors/id/I/IN/INGY/IO-All-0.44.tar.gz 
  8. tar zxvf IO-All-0.41.tar.gz 
  9. cd IO-All-0.41 
  10. perl Makefile.PL 
  11. make 
  12. make install 

(4)、下載Perl指令碼

這個指令碼的目的就是產生一個PERL的FastCGI介面,讓Nginx可以以CGI方式處理Perl。注:建議把這個指令碼放在Nginx安裝目錄,修改指令碼許可權為777

  1. http://www.chlinux.net/perl-fcgi.zip 
  2. unzip perl-fcgi.zip 
  3. cp perl-fcgi.pl /usr/local/nginx/ 
  4. chmod 755 /usr/local/nginx/perl-fcgi.pl 

(5)、建立一個CGI啟動/停止指令碼

這個SHELL指令碼只是為了方便管理上面的Perl指令碼。指令碼中的nobody為nginx的執行使用者,請據自己的實際情況調整。

注意事項:不能用root使用者執行(會提示). 要用與Nginx相同身份的使用者執行。否則可能會在Nginx Log中提示 Permision Denied。

  1. vi /usr/local/nginx/start_perl_cgi.sh 
  2. #!/bin/bash 
  3. #set -x 
  4. dir=/usr/local/ nginx/ 
  5. stop () 
  6. #pkill  -f  $dir/perl-fcgi.pl 
  7. kill $(cat $dir/logs/perl-fcgi.pid) 
  8. rm $dir/logs/perl-fcgi.pid 2>/dev/null 
  9. rm $dir/logs/perl-fcgi.sock 2>/dev/null 
  10. echo "stop perl-fcgi done" 
  11. start () 
  12. rm $dir/now_start_perl_fcgi.sh 2>/dev/null 
  13. chown nobody.nobody $dir/logs 
  14. echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh 
  15. chown nobody.nobody $dir/now_start_perl_fcgi.sh 
  16. chmod u+x $dir/now_start_perl_fcgi.sh 
  17. sudo -u nobody $dir/now_start_perl_fcgi.sh 
  18. echo "start perl-fcgi done" 
  19. case $1 in 
  20. stop) 
  21. stop 
  22. ;; 
  23. start) 
  24. start 
  25. ;; 
  26. restart) 
  27. stop 
  28. start 
  29. ;; 
  30. esac 

修改SHELL指令碼許可權

chmod 755 /usr/local/nginx/start_perl_cgi.sh

啟動指令碼

/usr/local/nginx/start_perl_cgi.sh start

正常情況下在/usr/local/nginx/logs下生成perl-fcgi.sock這個檔案,如果沒有生成,請檢查下上面的步聚。

2、安裝Nagios和相關外掛

(1)、安裝前準備

安裝的機器上必須有一個WEB服務,本文是在Nginx環境上安裝的。下載nagios主程式和相關外掛程式包

  1. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz 
  2. wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz 
  3. wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz 

安裝GD庫(Nagios中的statusmap和trends模組必須)

yum -y install libgd2-noxpm libgd2-noxpm-devel

(2)、Nagios監控端安裝

1、建立Nagios使用者及組

建立Nagios賬號

/usr/sbin/useradd -m -s /sbin/nologin nagios

2、建立一個名為nagcmd的使用者組,用於從web介面執行外部命令。將Nagios使用者和Nginx使用者加入組中。

groupadd nagcmd

usermod -a -G nagcmd nagios

usermod -a -G nagcmd www

注:上面的www是Nginx使用者所屬的組,如有不同請自行調整。

3、編譯安裝Nagios

  1. tar zxvf nagios-3.2.3.tar.gz 
  2. cd nagios-3.2.3 
  3. ./configure --with-command-group=nagcmd 
  4. make 
  5. make all 
  6. make install 
  7. make install-init 
  8. make install-config 
  9. make install-commandmode 
  10. #這裡是在Nginx下執行Nagios,這一步就不用做了 
  11. make install-webconf 
  12. cd .. 
  13. 注: 
  14. make install 用於安裝主要的程式、CGI及HTML檔案 
  15. make install-init 用於生成init啟動指令碼 
  16. make install-config 用於安裝示例配置檔案 
  17. make install-commandmode 用於設定相應的目錄許可權 
  18. make install-webconf 用於安裝Apache配置檔案 

4、驗證程式是否被正確安裝

切換目錄到安裝路徑,這裡是/usr/local/nagios,看是否存在etc、bin、 sbin、 share、 var這五個目錄,如果存在則可以表明程式被正確的安裝到系統了。

ls  /usr/local/nagios/

bin/     etc/     sbin/    share/   var/

注;bin–Nagios執行程式所在目錄,其中的nagios檔案即為主程式。

etc–Nagios配置檔案位置

sbin–Nagios cgi檔案所在目錄,也就是執行外部命令所需檔案所在的目錄

Share–Nagios網頁檔案所在的目錄

var–Nagios日誌檔案、spid 等檔案所在的目錄

var/archives–日誌歸檔目錄

var/rw–用來存放外部命令檔案

5、配置NGINX

1)、配置Nagios Web介面登陸帳號及密碼

htpasswd -c /usr/local/nagios/etc/nagiospasswd test

如果你沒有htpasswd(這個工具由Apache安裝包所提供),可線上生成需要加密資料。

a)、訪問http://www.4webhelp.net/us/password.php生成需要加密資料

b)、建立加密驗證檔案

vi /usr/local/nagios/etc/nagiospasswd

#加入生成的加密資料,冒號前是使用者名稱,後面是加密後的密碼

test:25JB.R7mXY96o

修改Nagios配置檔案,給新增的使用者增加訪問許可權

vi /usr/local/nagios/etc/cgi.cfg

#以下幾項中分別加入新增的使用者,多使用者用逗號分隔。

  1. authorized_for_system_information=nagiosadmin,test 
  2. authorized_for_configuration_information=nagiosadmin,test 
  3. authorized_for_system_commands=nagiosadmin,test 
  4. authorized_for_all_services=nagiosadmin,test 
  5. authorized_for_all_hosts=nagiosadmin,test 
  6. authorized_for_all_service_commands=nagiosadmin,test 
  7. authorized_for_all_host_commands=nagiosadmin,test 

2)、修改NGINX配置,以支援WEB方式訪問Nagios

方法一:以http://ip方式訪問,NGINX配置片斷如下

  1. server 
  2.   { 
  3.     listen       80; 
  4.     server_name  192.168.1.51; 
  5.     index index.html index.htm index.php; 
  6.     root  /usr/local/nagios/share; 
  7.     auth_basic "Nagios Access"
  8.     auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; 
  9.     location ~ .*.(php|php5)?$ 
  10.     {     
  11.       #fastcgi_pass  unix:/tmp/php-cgi.sock; 
  12.       fastcgi_pass  127.0.0.1:9000; 
  13.       fastcgi_index index.php; 
  14.       include fcgi.conf; 
  15.     } 
  16.     location ~ .*.(cgi|pl)?$ 
  17.     { 
  18.     gzip off
  19.    root   /usr/local/nagios/sbin; 
  20.     rewrite ^/nagios/cgi-bin/(.*).cgi /$1.cgi break; 
  21.     fastcgi_pass  unix:/usr/local/nginx/logs/perl-fcgi.sock; 
  22.     fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name; 
  23.     fastcgi_index index.cgi; 
  24.     fastcgi_read_timeout   60; 
  25.     fastcgi_param  REMOTE_USER        $remote_user; 
  26.     include fcgi.conf; 
  27.     auth_basic "Nagios Access"
  28.     auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; 
  29.     } 
  30.     location /nagios 
  31.     { 
  32.     alias /usr/local/nagios/share; 
  33.     auth_basic "Nagios Access"
  34.     auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; 
  35.       } 
  36.      } 

6、編譯並安裝Nagios外掛

由於Nagios主程式只是提供一個執行框架,其具體監控是靠執行在其下的外掛完成的,所以Nagios外掛是必須安裝的。

  1. tar zxvf nagios-plugins-1.4.15.tar.gz 
  2. cd nagios-plugins-1.4.15 
  3. ./configure --with-nagios-user=nagios --with-nagios-group=nagios 
  4. make 
  5. make install 

驗證Nagios外掛是否正確安裝

ls /usr/local//nagios/libexec

顯示安裝的外掛檔案,即所有的外掛都安裝在libexec這個目錄下。

7、啟動服務

啟動前先檢查下配置檔案是否正確

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

如果沒有報錯,可以啟動Nagios服務

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

檢視Nagios執行狀態

/usr/local/nagios/bin/nagiostats

8、安裝NRPE

由於Nagios只能監測自己所在的主機的一些本地情況,例如,cpu負載、記憶體使用、硬碟使用等等。如果想要監測被監控的伺服器上的這些本地情況,就要用到NRPE。NRPE(Nagios Remote Plugin Executor)是Nagios的一個擴充套件,它被用於被監控的伺服器上,向Nagios監控平臺提供該伺服器的一些本地的情況。NRPE可以稱為Nagios的Linux客戶端。

由於NRPE是通過SSL方式在監控和被監控主機上進行資料傳輸的,所以必須先安裝ssl相關的軟體包。

編譯安裝NRPE

  1. tar zxvf nrpe-2.12.tar.gz 
  2. cd nrpe-2.12 
  3. ./configure 
  4. make all 
  5. make install-plugin 
  6. make install-daemon 
  7. make install-daemon-config 

注:監控主機上只需要make install-plugin這一步就可以了。監控機上只要有一個check_nrpe外掛用於連線被監控端nrpe的daemon就行了。

啟動NRPE

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

驗證NRPE是否正確安裝

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

注:如果成功,會返回NRPE的版本號.

三、Nagios被控端安裝配置

1、建立Nagios使用者及組

建立Nagios賬號

/usr/sbin/useradd -m -s /sbin/nologin nagios

2、編譯並安裝Nagios外掛

tar zxvf nagios-plugins-1.4.15.tar.gz

cd nagios-plugins-1.4.15

./configure –with-nagios-user=nagios –with-nagios-group=nagios

make

make install

cd ..

驗證程式是否被正確安裝:

ls /usr/local/nagios/libexec

顯示安裝的外掛檔案,即所有的外掛都安裝在libexec這個目錄下。

3、安裝NRPE

tar zxvf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config

cd ..

4、啟動NRPE

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

驗證NRPE是否正確安裝

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

注:如果成功,會返回NRPE的版本號。

5、修改NRPE配置檔案,讓監控主機可以訪問被監控主機的NRPE。

預設NRPE配置檔案中只允許本機訪問NRPE的Daemon

vi /usr/local/nagios/etc/nrpe.cfg

#預設為127.0.0.1,只能本機訪問

allowed_hosts=192.168.1.108

6、重啟nrpe的方法

killall nrpe

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

5、配置nagios

定義主機和主機組

1、建立一個host.cfg用來定義主機

Host.cfg檔案內容如下

  1. define host{  
  2.      use                 linux-server                
  3.      host_name           WEB1       #主機名隨便取,建議取一個便於記憶的名字,主機名後面最後別有空格  
  4.      alias                WEB1        #主機別名  
  5.      address              192.168.1.51       #主機IP地址,我現在就暫時填寫本機的IP  
  6.      check_command       check-host-alive      #檢查命令,此命令來自commands.cfg,用來監控主機是否存活  
  7.       max_check_attempts              5      #檢查失敗後重試的次數  
  8.       check_period                    24x7      #檢查的時間段24x7,同樣來自於我們之前在timeperiods.cfg中定義的  
  9.       contact_groups                  sagroup      #聯絡人組,上面在contactgroups.cfg中定義的sagroup  
  10.       notification_interval           10     #提醒的間隔,每隔10秒提醒一次  
  11.       notification_period             24x7     #提醒的週期, 24x7,同樣來自於我們之前在timeperiods.cfg中定義的  
  12.       notification_options            d,u,r      #指定什麼情況下提醒,具體含義見之前contacts.cfg部分的介紹  
  13.      }  

通過簡單的複製修改就能定義多個主機了。

可以建一個hostgroup.cfg檔案來定義主機組檔案內容如下:

  1. define hostgroup{ 
  2.      hostgroup_name     DB-servers 
  3. #主機組名,可以隨意 
  4.      alias          DB Server 
  5.      members          DB1,DB2 
  6. #成員 

2、建立一個service.cfg檔案來定義服務,service.cfg檔案內容如下:

  1. define service{ 
  2.         use                     local-service,srv-pnp 
  3.         host_name               WEB1   #主機名,這主機名必須在host.cfg檔案裡有定義 
  4.          service_description            Current disk   #服務說明 
  5.          check_command                   check_local_disk!20%!10%!/   #服務檢查命令,這個命令來自command.cfg檔案 
  6.          max_check_attempts           5    #最多檢查次數 
  7.         check_period                 24x7   #服務檢查週期 
  8.         notification_interval            10   #通知時間間隔 
  9.         normal_check_interval       1    #正常檢查服務的時間間隔 
  10.         retry_check_interval        1     #重新檢查時間間隔 
  11.         notification_period    24x7    #通知時間段 
  12.         notification_options    w,u,c,r,d   #服務在什麼狀態下通知,d = 狀態為 DOWN , u = 狀態為 UNREACHABLE , r = 狀態恢復為 OK , f = flapping,  
  13.         contact_groups             admins    #聯絡人組 
  14.         } 

定義聯絡人,設定聯絡人的檔名是contacts.cfg 系統預設就存在,我們只要修改就行了檔案內容如下:

  1. define contact{ 
  2.         contact_name                    nagiosadmin    #聯絡人名   
  3.         use                            generic-contact               
  4. alias                           Nagios Admin          #聯絡人別名, 
  5.         email                           nagios@localhost          #聯絡人的郵件,nagios就是用這個郵件報警的,可以設定多個由逗號分開 

nrpe的主配置檔案是/usr/local/nagios/etc/nrpe.cfg,在command.cfg檔案中定義nrpe的命令

  1. define command{ 
  2.         command_name    check_nrpe 
  3.         command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ 
  4.         } 

要監控遠端主機的服務需要在被監控機的nrpe主配置檔案中定義。然後在監控伺服器的command.cfg檔案中定義命令。操作內容如下:被監控機的定義

  1. command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20    #監控CPU的負載 
  2. command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2    #監控硬碟第二分割槽的使用情況 
  3. command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z   #監控殭屍程式 
  4. command[check_local_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 -P   #監控活動程式 
  5. command[check_mysql]=/usr/local/nagios/libexec/check_mysql  -H localhost  -u mysql使用者名稱 –p mysql密碼 –d 需要監控的資料庫 
  6. #監控mysql資料庫。主要監控mysql的開啟資料,每秒的查詢,慢查詢,重新整理的表,開啟的表。注mysql的使用者只要給select許可權就可以 
  7. command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%   #監控交換分割槽的使用情況,超過20%就報 w  超過10%就報 c 
  8. command[check_nginx]=/usr/local/nagios/libexec/check_nginx.sh --url www.chlinux.net/index.php   #監控nginx的狀態。可以監控403.502等等 
  9. command[check_traffic]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H 127.0.0.1 -I 2 -w 300,300 -c 500,600 -K –B   #監控網路卡流量 

被監控機配置好後需要重啟nrpe

nrpe重啟方法

killall  -9  nrpe

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

[root@localhost ~]# /usr/local/nagios/libexec/check_nrpe -H localhost

NRPE v2.12   檢查是否有類似的輸出,如果有就是啟動成功,沒有就檢查配置是否正確

監控機的配置

在service.cfg檔案配置如下:

  1. define service{ 
  2.         use                             local-service,srv-pnp       
  3.         host_name                       WEB1 
  4.         service_description             Root Partition 
  5.         check_command                   check_nrpe!check_disk 
  6.         check_period            24x7 
  7.         max_check_attempts      4 
  8.         normal_check_interval   1 
  9.         retry_check_interval    1 
  10.         contact_groups          admins 
  11.         notification_interval   10 
  12.         notification_period     24x7 
  13.         notification_options    w,u,c,r 
  14.         } 

服務端配置完後需要重啟nagios服務

三、安裝pnp

Pnp是一個繪圖工具,需要依賴rrdtool軟體包

  1. wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.13.tar.gz/20a96f81edba29dcd21215bde4af8b57/pnp-0.4.13.tar.gz 
  2. tar zxvf  pnp-0.4.13.tar.gz 
  3. cd pnp-0.4.13 
  4. ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-perfdata-dir=/usr/local/nagios/share/perfdata --with-rrdtool 
  5. make all 
  6. make install 
  7.   make install-config 
  8. make install-init 
  9.   cd /usr/local/nagios/etc/pnp 
  10. cp npcd.cfg-sample npcd.cfg 
  11. cp rra.cfg-sample rra.cfg 
  12. cp process_perfdata.cfg-sample process_perfdata.cfg 
  13. chown nagios.nagios /usr/local/nagios/etc/pnp/ -R 

修改nagios的主配置檔案 /usr/local/nagios/etc/nagios.cfg

#開啟註釋項:

  1. host_perfdata_command=process-host-perfdata 
  2. service_perfdata_command=process-service-perfdata 
  3. process_performance_data=1 

5.修改 commands.cfg

  1. cd /usr/local/nagios/etc/objects 
  2. vim commands.cfg 
  3. ##新增 
  4. `process-host-perfdata` command definition 
  5. define command{ 
  6.         command_name    process-host-perfdata 
  7.         command_line    /usr/local/nagios/libexec/process_perfdata.pl 
  8.         } 
  9. define command{ 
  10.         command_name    process-service-perfdata 
  11.         command_line    /usr/local/nagios/libexec/process_perfdata.pl 
  12.         } 

6. 新增小太陽模版,鑲嵌在nagios頁面上。

  1. define host { 
  2. name       host-pnp 
  3. action_url /pnp/index.php?host=$HOSTNAME$ 
  4. register   0 
  5. define service { 
  6. name       srv-pnp 
  7. action_url /pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ 
  8. register   0 
  9. 7,修改 hosts.cfg 和 services.cfg 
  10. cd /usr/local/nagios/etc/eric.com 
  11. #修改hosts.cfg 
  12. vim hosts.cfg 
  13. define host{ 
  14.         use                     linux-server,host-pnp  
  15.         host_name         eric.com 
  16.         alias                     eric.com 
  17.         address                 192.168.6.101 
  18.         } 
  19.  
  20.  
  21. # 修改 services.cfg 
  22. define service{ 
  23.         use                                     local-service,srv-pnp      
  24.         host_name                       eric.com 
  25.         service_description        PING 
  26.         check_command            check_ping!100.0,20%!500.0,60% 
  27. 8.重啟 nagios 服務 
  28. /etc/init.d/nagios restart