系統環境:CentOS release 5.8 x86_64
一:監控端安裝
1,安裝perl模組:
(1)、先安裝FCGI模組
- wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz
- tar xvzf FCGI-0.73.tar.gz
- cd FCGI-0.73
- perl Makefile.PL
- make
- make install
(2)、安裝FCGI-ProcManager模組
- wget http://mirrors.ustc.edu.cn/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz
- tar xvzf FCGI-ProcManager-0.19.tar.gz
- cd FCGI-ProcManager-0.19
- perl Makefile.PL
- make
- make install
(3)、安裝IO和IO::ALL模組
- wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
- tar zxvf IO-1.25.tar.gz
- cd IO-1.25
- perl Makefile.PL
- make
- make install
- wget http://mirrors.xmu.edu.cn/CPAN/authors/id/I/IN/INGY/IO-All-0.44.tar.gz
- tar zxvf IO-All-0.41.tar.gz
- cd IO-All-0.41
- perl Makefile.PL
- make
- make install
(4)、下載Perl指令碼
這個指令碼的目的就是產生一個PERL的FastCGI介面,讓Nginx可以以CGI方式處理Perl。注:建議把這個指令碼放在Nginx安裝目錄,修改指令碼許可權為777
- http://www.chlinux.net/perl-fcgi.zip
- unzip perl-fcgi.zip
- cp perl-fcgi.pl /usr/local/nginx/
- chmod 755 /usr/local/nginx/perl-fcgi.pl
(5)、建立一個CGI啟動/停止指令碼
這個SHELL指令碼只是為了方便管理上面的Perl指令碼。指令碼中的nobody為nginx的執行使用者,請據自己的實際情況調整。
注意事項:不能用root使用者執行(會提示). 要用與Nginx相同身份的使用者執行。否則可能會在Nginx Log中提示 Permision Denied。
- vi /usr/local/nginx/start_perl_cgi.sh
- #!/bin/bash
- #set -x
- dir=/usr/local/ nginx/
- stop ()
- {
- #pkill -f $dir/perl-fcgi.pl
- kill $(cat $dir/logs/perl-fcgi.pid)
- rm $dir/logs/perl-fcgi.pid 2>/dev/null
- rm $dir/logs/perl-fcgi.sock 2>/dev/null
- echo "stop perl-fcgi done"
- }
- start ()
- {
- rm $dir/now_start_perl_fcgi.sh 2>/dev/null
- chown nobody.nobody $dir/logs
- echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh
- chown nobody.nobody $dir/now_start_perl_fcgi.sh
- chmod u+x $dir/now_start_perl_fcgi.sh
- sudo -u nobody $dir/now_start_perl_fcgi.sh
- echo "start perl-fcgi done"
- }
- case $1 in
- stop)
- stop
- ;;
- start)
- start
- ;;
- restart)
- stop
- start
- ;;
- esac
修改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主程式和相關外掛程式包
- wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
- wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
- wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
安裝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
- tar zxvf nagios-3.2.3.tar.gz
- cd nagios-3.2.3
- ./configure --with-command-group=nagcmd
- make
- make all
- make install
- make install-init
- make install-config
- make install-commandmode
- #這裡是在Nginx下執行Nagios,這一步就不用做了
- make install-webconf
- cd ..
- 注:
- make install 用於安裝主要的程式、CGI及HTML檔案
- make install-init 用於生成init啟動指令碼
- make install-config 用於安裝示例配置檔案
- make install-commandmode 用於設定相應的目錄許可權
- 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
#以下幾項中分別加入新增的使用者,多使用者用逗號分隔。
- authorized_for_system_information=nagiosadmin,test
- authorized_for_configuration_information=nagiosadmin,test
- authorized_for_system_commands=nagiosadmin,test
- authorized_for_all_services=nagiosadmin,test
- authorized_for_all_hosts=nagiosadmin,test
- authorized_for_all_service_commands=nagiosadmin,test
- authorized_for_all_host_commands=nagiosadmin,test
2)、修改NGINX配置,以支援WEB方式訪問Nagios
方法一:以http://ip方式訪問,NGINX配置片斷如下
- server
- {
- listen 80;
- server_name 192.168.1.51;
- index index.html index.htm index.php;
- root /usr/local/nagios/share;
- auth_basic "Nagios Access";
- auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;
- location ~ .*.(php|php5)?$
- {
- #fastcgi_pass unix:/tmp/php-cgi.sock;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fcgi.conf;
- }
- location ~ .*.(cgi|pl)?$
- {
- gzip off;
- root /usr/local/nagios/sbin;
- rewrite ^/nagios/cgi-bin/(.*).cgi /$1.cgi break;
- fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;
- fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
- fastcgi_index index.cgi;
- fastcgi_read_timeout 60;
- fastcgi_param REMOTE_USER $remote_user;
- include fcgi.conf;
- auth_basic "Nagios Access";
- auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;
- }
- location /nagios
- {
- alias /usr/local/nagios/share;
- auth_basic "Nagios Access";
- auth_basic_user_file /usr/local/nagios/etc/nagiospasswd;
- }
- }
6、編譯並安裝Nagios外掛
由於Nagios主程式只是提供一個執行框架,其具體監控是靠執行在其下的外掛完成的,所以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
驗證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
- tar zxvf nrpe-2.12.tar.gz
- cd nrpe-2.12
- ./configure
- make all
- make install-plugin
- make install-daemon
- 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檔案內容如下
- define host{
- use linux-server
- host_name WEB1 #主機名隨便取,建議取一個便於記憶的名字,主機名後面最後別有空格
- alias WEB1 #主機別名
- address 192.168.1.51 #主機IP地址,我現在就暫時填寫本機的IP
- check_command check-host-alive #檢查命令,此命令來自commands.cfg,用來監控主機是否存活
- max_check_attempts 5 #檢查失敗後重試的次數
- check_period 24x7 #檢查的時間段24x7,同樣來自於我們之前在timeperiods.cfg中定義的
- contact_groups sagroup #聯絡人組,上面在contactgroups.cfg中定義的sagroup
- notification_interval 10 #提醒的間隔,每隔10秒提醒一次
- notification_period 24x7 #提醒的週期, 24x7,同樣來自於我們之前在timeperiods.cfg中定義的
- notification_options d,u,r #指定什麼情況下提醒,具體含義見之前contacts.cfg部分的介紹
- }
通過簡單的複製修改就能定義多個主機了。
可以建一個hostgroup.cfg檔案來定義主機組檔案內容如下:
- define hostgroup{
- hostgroup_name DB-servers
- #主機組名,可以隨意
- alias DB Server
- members DB1,DB2
- #成員
- }
2、建立一個service.cfg檔案來定義服務,service.cfg檔案內容如下:
- define service{
- use local-service,srv-pnp
- host_name WEB1 #主機名,這主機名必須在host.cfg檔案裡有定義
- service_description Current disk #服務說明
- check_command check_local_disk!20%!10%!/ #服務檢查命令,這個命令來自command.cfg檔案
- max_check_attempts 5 #最多檢查次數
- check_period 24x7 #服務檢查週期
- notification_interval 10 #通知時間間隔
- normal_check_interval 1 #正常檢查服務的時間間隔
- retry_check_interval 1 #重新檢查時間間隔
- notification_period 24x7 #通知時間段
- notification_options w,u,c,r,d #服務在什麼狀態下通知,d = 狀態為 DOWN , u = 狀態為 UNREACHABLE , r = 狀態恢復為 OK , f = flapping,
- contact_groups admins #聯絡人組
- }
定義聯絡人,設定聯絡人的檔名是contacts.cfg 系統預設就存在,我們只要修改就行了檔案內容如下:
- define contact{
- contact_name nagiosadmin #聯絡人名
- use generic-contact
- alias Nagios Admin #聯絡人別名,
- email nagios@localhost #聯絡人的郵件,nagios就是用這個郵件報警的,可以設定多個由逗號分開
- }
nrpe的主配置檔案是/usr/local/nagios/etc/nrpe.cfg,在command.cfg檔案中定義nrpe的命令
- define command{
- command_name check_nrpe
- command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
- }
要監控遠端主機的服務需要在被監控機的nrpe主配置檔案中定義。然後在監控伺服器的command.cfg檔案中定義命令。操作內容如下:被監控機的定義
- command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 #監控CPU的負載
- command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2 #監控硬碟第二分割槽的使用情況
- command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z #監控殭屍程式
- command[check_local_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 -P #監控活動程式
- command[check_mysql]=/usr/local/nagios/libexec/check_mysql -H localhost -u mysql使用者名稱 –p mysql密碼 –d 需要監控的資料庫
- #監控mysql資料庫。主要監控mysql的開啟資料,每秒的查詢,慢查詢,重新整理的表,開啟的表。注mysql的使用者只要給select許可權就可以
- command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10% #監控交換分割槽的使用情況,超過20%就報 w 超過10%就報 c
- command[check_nginx]=/usr/local/nagios/libexec/check_nginx.sh --url www.chlinux.net/index.php #監控nginx的狀態。可以監控403.502等等
- 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檔案配置如下:
- define service{
- use local-service,srv-pnp
- host_name WEB1
- service_description Root Partition
- check_command check_nrpe!check_disk
- check_period 24x7
- max_check_attempts 4
- normal_check_interval 1
- retry_check_interval 1
- contact_groups admins
- notification_interval 10
- notification_period 24x7
- notification_options w,u,c,r
- }
服務端配置完後需要重啟nagios服務
三、安裝pnp
Pnp是一個繪圖工具,需要依賴rrdtool軟體包
- wget http://pkgs.fedoraproject.org/repo/pkgs/pnp4nagios/pnp-0.4.13.tar.gz/20a96f81edba29dcd21215bde4af8b57/pnp-0.4.13.tar.gz
- tar zxvf pnp-0.4.13.tar.gz
- cd pnp-0.4.13
- ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-perfdata-dir=/usr/local/nagios/share/perfdata --with-rrdtool
- make all
- make install
- make install-config
- make install-init
- cd /usr/local/nagios/etc/pnp
- cp npcd.cfg-sample npcd.cfg
- cp rra.cfg-sample rra.cfg
- cp process_perfdata.cfg-sample process_perfdata.cfg
- chown nagios.nagios /usr/local/nagios/etc/pnp/ -R
修改nagios的主配置檔案 /usr/local/nagios/etc/nagios.cfg
#開啟註釋項:
- host_perfdata_command=process-host-perfdata
- service_perfdata_command=process-service-perfdata
- process_performance_data=1
5.修改 commands.cfg
- cd /usr/local/nagios/etc/objects
- vim commands.cfg
- ##新增
- # `process-host-perfdata` command definition
- define command{
- command_name process-host-perfdata
- command_line /usr/local/nagios/libexec/process_perfdata.pl
- }
- define command{
- command_name process-service-perfdata
- command_line /usr/local/nagios/libexec/process_perfdata.pl
- }
6. 新增小太陽模版,鑲嵌在nagios頁面上。
- define host {
- name host-pnp
- action_url /pnp/index.php?host=$HOSTNAME$
- register 0
- }
- define service {
- name srv-pnp
- action_url /pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
- register 0
- }
- 7,修改 hosts.cfg 和 services.cfg
- cd /usr/local/nagios/etc/eric.com
- #修改hosts.cfg
- vim hosts.cfg
- define host{
- use linux-server,host-pnp
- host_name eric.com
- alias eric.com
- address 192.168.6.101
- }
- # 修改 services.cfg
- define service{
- use local-service,srv-pnp
- host_name eric.com
- service_description PING
- check_command check_ping!100.0,20%!500.0,60%
- }
- 8.重啟 nagios 服務
- /etc/init.d/nagios restart