Nagios Setup
1.下載nagios-4.4.2.tar.gz
2.yum install gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix
No package gd-devel available.
下載gd-devel
https://centos.pkgs.org/6/centos-x86_64/gd-devel-2.0.35-11.el6.x86_64.rpm.html
[[email protected] downloads]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm
error: Failed dependencies:
fontconfig-devel is needed by gd-devel-2.0.35-11.el6.x86_64
[[email protected] downloads]# yum install fontconfig-devel freetype-devel libX11-devel libXpm-devel libgd.so.2 libjpeg-devel libpng-devel pkgconfig zlib-devel
依賴包都安裝完畢後,再次安裝gd-devel
[[email protected] downloads]# rpm -ivh gd-devel-2.0.35-11.el6.x86_64.rpm
package gd-devel-2.0.35-11.el6.x86_64 is already installed
此時可以繼續安裝nagios-core了。
3.解壓,安裝
[[email protected] downloads]# tar zxvf nagios-4.4.2.tar.gz
[[email protected] downloads]# cd nagios-4.4.2
[[email protected] nagios-4.4.2]# ./configure
4.Create User And Group
[[email protected] nagios-4.4.2]# make install-groups-users
groupadd -r nagios
useradd -g nagios nagios
5.Install Binaries
[[email protected] nagios-4.4.2]# make install
6.Install Service / Daemon
[[email protected] nagios-4.4.2]# make install-daemoninit
httpd自啟動由於已經設置完畢,這裏就不重復設定了。
7.Install Command Mode
[[email protected] nagios-4.4.2]# make install-commandmode
8.Install Configuration Files
[[email protected] nagios-4.4.2]# make install-config
9.Install Apache Config Files
[[email protected] nagios-4.4.2]# make install-webconf
10.Configure Firewall
防火墻80端口已經放行。所以這裏就不配置了。
否則配置以下命令(RHEL6)
iptables -I INPUT -p tcp --destination-port 80 -j ACCEPT
service iptables save
ip6tables -I INPUT -p tcp --destination-port 80 -j ACCEPT
service ip6tables save
11.Create nagiosadmin User Account
[[email protected] nagios-4.4.2]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
12.啟動服務
[[email protected] nagios-4.4.2]# /etc/init.d/nagios start
Starting nagios: Running configuration check... done.
13.此時遇到一個問題,由於我設定了兩個VirtualHost,貌似現在無法使用http://IP/nagios或是http://IP:8080/nagios進行訪問。
首先確認/etc/httpd/conf/httpd.conf裏,
LoadModule cgi_module modules/mod_cgi.so被開啟。
然後發現是沒有重啟Httpd服務導致的。重啟後就能正常訪問nagios頁面了。
需要註意的是,彈出的需要輸入用戶名和密碼的框,是輸入剛才設定的nagiosadmin和他的密碼。
不要輸入成自己設定的http基礎認證了。
而且經過測試,不論是80端口還是8080端口,只要開放了的端口都可以被用作nagios的訪問端口
URL:https://blog.51cto.com/jianye/1364716
14.接下來安裝Nagios Plugin
下載 nagios-plugins-2.2.1.tar.gz(錯誤!)這裏註意要下載的是nagios-plugins-release-2.2.1。否則後面的./tools/setup不存在。
[[email protected] downloads]# tar zxvf nagios-plugins-release-2.2.1.tar.gz
[[email protected] nagios-plugins-2.2.1]# yum install gcc glibc glibc-common make gettext automake wget openssl-devel net-snmp net-snmp-utils epel-release
全都已經安裝。
無網絡環境下,千萬不要按照官方文檔安裝epel-release-latest-6.noarch.rpm。否則自建的光盤repo會爆炸。
[[email protected] nagios-plugins-2.2.1]# yum install -y perl-Net-SNMP
No package perl-Net-SNMP available.
URL:https://centos.pkgs.org/6/repoforge-i386/perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch.rpm.html
perl(Net::SNMP) = perl-Net-SNMP
這安裝perl-Net-SNMP遇到了問題。在沒有網絡的情況下,好像沒法簡單的用perl來安裝Net-SNMP。
下載perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch.rpm
[[email protected] downloads]# rpm -ivh perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch.rpm
error: Failed dependencies:
perl(Crypt::DES) is needed by perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch
perl(Digest::HMAC) is needed by perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch
perl(Digest::SHA1) is needed by perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch
perl(Socket6) >= 0.19 is needed by perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch
下載並安裝依賴包
[[email protected] downloads]# rpm -ivh perl-Crypt-DES-2.05-3.2.el6.rf.x86_64.rpm
[[email protected] downloads]# rpm -ivh perl-Digest-SHA1-2.12-2.el6.x86_64.rpm
[[email protected] downloads]# rpm -ivh perl-Digest-HMAC-1.01-22.el6.noarch.rpm
[[email protected] downloads]# rpm -ivh perl-Socket6-0.23-4.el6.x86_64.rpm
再安裝perl-Net-SNMP
[[email protected] downloads]# rpm -ivh perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch.rpm
warning: perl-Net-SNMP-5.2.0-1.2.el6.rf.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEY
Preparing... ########################################### [100%]
1:perl-Net-SNMP ########################################### [100%]
成功了。
15.繼續安裝Nagios-Plugin
[[email protected] downloads]# cd nagios-plugins-release-2.2.1
[[email protected] nagios-plugins-release-2.2.1]# ./tools/setup
[[email protected] nagios-plugins-release-2.2.1]# ./configure
[[email protected] nagios-plugins-release-2.2.1]# make
[[email protected] nagios-plugins-release-2.2.1]# make install
經驗證,現在nagios裏的localhost已經up了。
16.添加開機自啟動
[[email protected] nagios-plugins-release-2.2.1]# chkconfig --add nagios
[[email protected] nagios-plugins-release-2.2.1]# chkconfig --level 2345 nagios on
檢測監控配置是否有錯誤。
[[email protected] nagios-plugins-release-2.2.1]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
17.命令行測試nagios插件的可用性
[[email protected] ~]# /usr/local/nagios/libexec/check_ping -H 10.116.6.193 -w 3000.0,80% -c 5000.0,100% -p 5
PING OK - Packet loss = 0%, RTA = 31.18 ms|rta=31.181999ms;3000.000000;5000.000000;0.000000 pl=0%;80;100;0
18.修改contact信息
[[email protected] etc]# vi objects/contacts.cfg
email [email protected] ;
19.復制一個windows模板出來
[[email protected] objects]# cp windows.cfg windows.cfg.bak
[[email protected] objects]# vi windows.cfg
修改定義Host。
define host {
use windows-server ; Inherit default values from a template
host_name CyberArk ; The name we‘re giving to this host
alias My Windows Server ; A longer name associated with the host
address 10.116.6.193 ; IP address of the host(這裏修改IP)
}
另外記得把下面默認定義的所有針對Windows的Service模板裏的host_name全部註釋掉,並且都增加一行"hostgroup_name windows-servers ;"
否則配置文件會出錯。
並添加如下Ping的服務,這個在windows裏沒有,但其實也可以不定義。因為默認在網頁的Host裏就是對PING的驗證了。
##################Added by Chris###################
Define a service to "ping" the local machine
define service {
use generic-service ; Name of service template to use
host_name winserver
hostgroup_name windows-servers ; The name of the hostgroup
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
##################Added by Chris###################
但是重啟nagios後還是沒有,這是因為除了localhost外,默認其他模板不生效.
需要到nagios.cfg裏去取掉#註釋。
[[email protected] objects]# vi ../nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/windows.cfg
註意在重啟服務前,記得用這個命令先驗證一下配置文件。
[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
[[email protected] objects]# /etc/init.d/nagios restart
20.發現包括C盤空間監控的一系列服務拒絕連接。
connect to address 10.116.6.193 and port 12489: 拒絕連接
原因是,這些命令是采用NRPE,NSClient等第三方agent區實現監控的。
我想用SNMP實現,所以需要在Nagios服務器上新增監控用的插件。
URL:http://nagios.manubulon.com/snmp_windows.html
點開download鏈接,是一段perl腳本。
直接到本地Libexec文件夾,設定755屬性保存。發現執行./check_snmp_windows.pl -h沒反應。
URL:https://blog.csdn.net/winsonyuan/article/details/54863667
[[email protected] libexec]# yum install perl*
再次執行pl腳本還是不執行,我發現一個問題,腳本內容由於直接從網上復制下來的,好像格式之間沒有換行。也許是這個原因?
於是我右鍵保存目標target,下載下來是一個htm文件,用word打開,發現裏面的格式是正確的,修改後綴名為pl後,最後用rz復制到linux系統上。
我下載的是這個:
URL: http://nagios.manubulon.com/snmp_windows.html
[[email protected] libexec]# chmod 755 check_snmp_win.pl
果然就可以執行了!
[[email protected] libexec]# ./check_snmp_win.pl -H 10.116.6.193 -C public -n HTTP -N 2
1 services active (matching "HTTP") : CRITICAL
[[email protected] libexec]# ./check_snmp_win.pl -H 10.116.6.193 -C public -n dns
1 services active (matching "dns") : OK
Linux系統不行,因為這個pl本身就是針對windows的。
並且需要註意,在頁面上已經說明了,這個pl只能用於snmpv1,v3,不支持v2。所以在操作前,先驗證v1的可用性。
[[email protected] libexec]# snmpwalk -v2c -c public 10.116.6.193
這個頁面可以參考配置define command,service,
URL:http://nagios.manubulon.com/index_commands.html#windows
[[email protected] libexec]# vi ../etc/objects/commands.cfg
加入以下這段。
define command {
command_name check_win
command_line $USER1$/check_snmp_win.pl -H $HOSTADDRESS$ -C $USER7$ -n $ARG1$
comment community service(s) name
}
[[email protected] objects]# vi windows.cfg
加入以下這段。
define service {
use generic-service
hostgroup_name windows-servers ; The name of the hostgroup
service_description HTTP service
check_command check_win!http
}
註意,按照URL中的文檔添加會出錯,我們還是要參考當前環境進行設置。
[[email protected] objects]# /etc/init.d/nagios restart
然而此時Status Information顯示Put snmp login info!
檢查commands.cfg 發現裏面有個$USER7$,不僅在前面漏了-C, 這個是用來定義Community的。目前還沒在resource.cfg裏定義。
command_line $USER1$/check_snmp_win.pl -H $HOSTADDRESS$ $USER7$ -n $ARG1$
[[email protected] etc]# vi resource.cfg
Sets $USER7$ to be the SNMP community name
$USER7$=public
}
然後重啟服務,一切恢復正常。
21.接下來配置check_snmp_storage.pl,用來監控windows的磁盤空間
同樣的方法,復制到libexec目錄,賦予755權限.
然後,網頁文檔裏沒有描述如何監控windows磁盤.但是在script -h裏有。
Browse storage list : <script> -C <community> -H <host> -m <anything> -w 1 -c 2 -v
the -m option allows regexp in perl format :
Test drive C,F,G,H,I on Windows : -m ^[CFGHI]:
看到這段文字。
[[email protected] libexec]# ./check_snmp_storage.pl -C public -H 10.116.6.193 -m ^[CD]: -w 1 -c 2 -v
這條命令可以成功執行。
根據這條語句,可以想象出定義的command為:
[[email protected] objects]# vi commands.cfg
define command {
command_name check_snmp_stor
command_line $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ -C $ARG1$ -m $ARG2$ -w $ARG3$ -c $ARG4$
}
[[email protected] objects]# vi windows.cfg
define service {
use generic-service
hostgroup_name windows-servers ; The name of the hostgroup
service_description C:\ Disk Space
check_command check_snmp_stor!public!C:!70!90
}
22.接著監控進程
下載check_snmp_process.pl。修改權限755復制到libexec目錄下。
check_snmp_process.pl
[[email protected] libexec]# ./check_snmp_process.pl -C SMBC -H 10.116.6.193 -n CAPSM.exe -v
1 process matching CAPSM.exe (> 0)
[[email protected] objects]# vi commands.cfg
define command{
command_name check_snmp_process_v1
command_line $USER1$/check_snmp_process.pl -C $ARG1$ -H $HOSTADDRESS$ -n $ARG2$ -w $ARG3$ -c $ARG4$
}
ARG1 : community
ARG2 : regexp of process name
ARG3 : warning level(s)
ARG4 : critical level(s)
[[email protected] objects]# vi windows.cfg
define service {
use generic-service
hostgroup_name windows-servers ; The name of the hostgroup
service_description AppProvider.exe
check_command check_snmp_process_v1!public!"AppProvider.exe"!0!0
}
重啟服務後查看效果。
經過驗證,如果warning和critical都設置為0,則會報critical而不是warning.
check_command check_snmp_process_v1!public!"PsExec.exe"!0!0
23.郵件發送設置
[[email protected] objects]# yum install sendmail
[[email protected] objects]# /etc/init.d/sendmail start
[[email protected] objects]# echo "test" | mail [email protected]
停了防火墻和selinux後還是收不到。
查看日誌,發現原來是因為那是個外部郵箱。而我的server是局域網的。
這當然收不到了。
=======================================================
Nagios Setup