在CentOS 6.2上部署nagios-3.3.1
1、nagios簡介:
Nagios是一款用於系統和網絡監控的應用程序並遵循GPL協定。它可以在你設定的條件下對主機和服務進行監控,在狀態變差和變好的時候給出告警信息
Nagios通常由一個主程序(Nagios)、一個插件程序(Nagios-plugins)和四個可選的ADDON(NRPE、NSCA、 NSClient++和NDOUtils)組成。Nagios的監控工作都是通過插件實現的,因此,Nagios和Nagios-plugins是服務器端工作所必須的組件。而四個ADDON中
(1)NRPE:用來在監控的遠程Linux/Unix主機上執行腳本插件以實現對這些主機資源的監控。
(2)NSCA:用來讓被監控的遠程Linux/Unix主機主動將監控信息發送給Nagios服務器(這在冗餘監控模式中特別要用到)。
(3)NSClient++:用來監控 Windows主機時安裝在Windows主機上的組件。
(4)NDOUtils:則用來將Nagios的配置信息和各event產生的數據存入數據庫,以實現這些數據的快速檢索和處理。
這四個ADDON中,NRPE和NSClient++工作於客戶端,NDOUtils工作於服務器端,而NSCA則需要同時安裝在服務器端和客戶端。
2、Nagios的特徵包括:
1、監控網絡服務(SMTP、POP3、HTTP、NNTP、PING 等);
2、監控主機資源(處理器負荷、磁盤利用率等);
3、簡單地插件設計使得用戶可以方便地擴展自己服務的檢測方法;
4、並行服務檢查機制;
5、具備定義網絡分層結構的能力,用"parent"主機定義來表達網絡主機間的關係,這種關係可被用來發現和明晰主機宕機或不可達狀態;
6、當服務或主機問題產生與解決時將告警發送給聯繫人(通過 EMail、短信、用戶定義方式);
7、具備定義事件句柄功能,它可以在主機或服務的事件發生時獲取更多問題定位;
8、自動的日誌回滾;
9、可以支持並實現對主機的冗餘監控;
10、可選的 WEB 界面用於查看當前的網絡狀態、通知和故障歷史、日誌文件等。
3、nagios的優缺點:
優點:
(1)良好的穩定的系統管理代碼
(2)在服務事件和主機事件之間良好的相關性
(3)命令檢查配置文件的驗證
(4)命令重新加載配置文件無需打擾Nagios的運行
(5)良好的文檔
缺點:
(1)沒有自動發現
(2)很弱的事件控制檯
(3)無OOTB採集或性能數據的閾值
(4)沒有簡單的方法來接收和解釋的SNMP TRAP
(5)沒有MIB編譯器或瀏覽器
一、安裝前的準備工作
(1)、解決安裝Nagios的依賴關係:
Nagios基本組件的運行依賴於httpd、gcc和gd。可以通過以下命令來檢查nagios所依賴的rpm包是否已經完全安裝:
# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server
說明:以上軟件包您也可以通過編譯源代碼的方式安裝,只是後面許多要用到的相關文件的路徑等需要按照您的源代碼安裝時的配置逐一修改。此外,您還得按需啓動必要的服務,如httpd等。
(2)、添加nagios運行所需要的用戶和組:
# groupadd nagcmd
# useradd -G nagcmd nagios
# passwd nagios
把apache加入到nagcmd組,以便於在通過web Interface操作nagios時能夠具有足夠的權限:# usermod -a -G nagcmd apache
二、編譯安裝nagios:10.100.10.11
# tar zxf nagios-3.3.1.tar.gz
# cd nagios-3.3.1
#./configure --with-command-group=nagcmd \
--enable-event-broker \
--with-httpd-conf=/etc/httpd/conf/ \
--prefix=/usr/local/nagios
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
(2)、爲email指定您想用來接收nagios警告信息的郵件地址,默認是本機的nagios用戶: # vi /usr/local/nagios/etc/objects/contacts.cfg
email nagios@localhost #這個是默認設置
(3)、在httpd的配置文件目錄(conf.d)中創建Nagios的Web程序配置文件:
nagios# make install-webconf
(4)、創建一個登錄nagios web程序的用戶,這個用戶帳號在以後通過web登錄nagios認證時所用:
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
以上過程配置結束以後需要重新啓動httpd:
# service httpd restart
三、編譯安裝nagios-plugins
nagios的所有監控工作都是通過插件完成的,因此,在啓動nagios之前還需要爲其安裝官方提供的插件。
nagios-plugins-1.4.16.tar.gz
# tar zxf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install
四、配置並啓動Nagios
(1)、把nagios添加爲系統服務並將之加入到自動啓動服務隊列:
# chkconfig --add nagios
# chkconfig nagios on
(2)、檢查其主配置文件的語法是否正確:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
(3)、如果上面的語法檢查沒有問題,接下來就可以正式啓動nagios服務了:
# service nagios start
(4)、配置selinux
如果您的系統開啓了selinux服務,則默認爲拒絕nagios web cgi程序的運行。您可以通過下面的命令來檢查您的系統是否開啓了selinux:
#getenforce
如果上面命令的結果顯示開啓了selinux服務,您可以通過下面的命令暫時性的將其關閉:#setenforce 0
如果您想在以後完全關閉selinux,可以通過編輯/etc/sysconfig/selinux文件,將其中的selinux後面的值“force”修改爲“disable”即可。
當然,您也可以通過以下方式將nagios的CGI程序運行於SELinux/targeted模式而不用關閉selinux:
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share
(5)、通過web界面查看nagios:
http://10.100.10.11/nagios
登錄時需要指定前面設定的web認證帳號和密碼。
此時,監控本機的服務頁面如下:
五、基於NRPE監控遠程Linux主機
1、NRPE簡介:
Nagios監控遠程主機的方法有多種,其方式包括SNMP、NRPE、SSH和NCSA等。這裏介紹其通過NRPE監控遠程Linux主機的方式。
NRPE(Nagios Remote Plugin Executor)是用於在遠端服務器上運行檢測命令的守護進程,
它用於讓Nagios監控端基於安裝的方式觸發遠端主機上的檢測命令,並將檢測結果輸出至監控端。而其執行的開銷遠低於基於SSH的檢測方式,而且檢測過程並不需要遠程主機上的系統帳號等信息,其安全性也高於SSH的檢測方式。
2、安裝配置被監控端10.100.10.10和10.100.10.12,分別在兩個機器上進行配置。
1)先添加nagios用戶
# useradd -s /sbin/nologin nagios
2)NRPE依賴於nagios-plugins,因此,需要先安裝之
nagios-plugins-1.4.16.tar.gz
# tar zxf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install
3)安裝NRPE
# tar -zxvf nrpe-2.13.tar.gz
# cd nrpe-2.13
# ./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
4)配置NRPE
# vi /usr/local/nagios/etc/nrpe.cfg
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_address=127.0.0.1
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=10.100.10.11
command_timeout=60
connection_timeout=300
debug=0
上述配置指令可以做到見名知義,因此,配置過程中根據實際需要進行修改即可。其中,需要特定說明的是allowed_hosts指令用於定義本機所允許的監控端的IP地址。
5)啓動NRPE
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
爲了便於NRPE服務的啓動,可以將如下內容定義爲/etc/init.d/nrped腳本:
# vi /etc/init.d/nrped
#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
start)
echo -n "Starting NRPE daemon..."
$NRPE -c $NRPECONF -d
echo " done."
;;
stop)
echo -n "Stopping NRPE daemon..."
pkill -u nagios nrpe
echo " done."
;;
restart)
$0 stop
sleep 2
$0 start
;;
*)
echo "Usage: $0 start|stop|restart"
;;
esac
exit 0
或者,也可以在/etc/xinetd.d目錄中創建nrpe文件,使其成爲一個基於非獨立守護進程的服務,文件內容如下:
service nrpe
{
flags = REUSE
socket_type = stream
wait = no
user = nagios
group = nagios
server = /opt/nagios/bin/nrpe
server_args = -c /etc/nagios/nrpe.cfg -i
log_on_failure += USERID
disable = no
}
此種情況下啓動NRPE進程需要通過重啓xinetd來實現。
6)配置允許遠程主機監控的對象
在被監控端,可以通過NRPE監控的服務或資源需要通過nrpe.cfg文件使用命令進行定義,定義命令的語法格式爲:command[<command_name>]=<command_to_execute>。
比如:
command[check_rootdisk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p
command[check_swap]=/usr/local/nagios/libexec/check_disk -w 40% -c 20%
command[check_sensors]=/usr/local/nagios/libexec/check_sensors
command[check_users]=/usr/local/nagios/libexec/check_users -w 10 -c 20
command[check_load]=/usr/local/nagios/libexec/check_load -w 10,8,5 -c 20,18,15
command[check_zombies]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_all_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
六、配置監控端所要監控的服務和資源
手動添加一些監控的配置文件,在監控端(10.100.10.11)的/usr/local/nagios/etc/objects/添加:
首先是添加主機和服務:
① vi /usr/local/nagios/etc/objects/host10.cfg
define host {
host_name idc010vm010
alias idc010vm010
address 10.100.10.10
check_command check-host-alive
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}
define service {
host_name idc010vm010
service_description check-host-alive
# check_command check_nrpe! check_load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name idc010vm010
service_description check-disk
check_command check_nrpe! check_df
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
# check_command check-host-alive
}
define service{
host_name idc010vm010
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm010
service_description ip_connets
check_command check_nrpe! check_ip_connets
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm010
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
② Vi /usr/local/nagios/etc/objects/host12.cfg
define host {
host_name idc010vm012
alias idc010vm012
address 10.100.10.12
check_command check-host-alive
max_check_attempts 5
notification_interval 10
notification_period 24x7
notification_options d,u,r
}
define service {
host_name idc010vm012
service_description check-host-alive
# check_command check_nrpe! check_load
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name idc010vm012
service_description check-disk
check_command check_nrpe! check_df
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups admins
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
# check_command check-host-alive
}
define service{
host_name idc010vm012
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm012
service_description ip_connets
check_command check_nrpe! check_ip_connets
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm012
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
然後是在/usr/local/nagios/etc/nagios.cfg裏添加兩行內容,如下所示:
# vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/host10.cfg
cfg_file=/usr/local/nagios/etc/objects/host12.cfg
文件保存後,先測試一下nagios是否有語法錯誤,若沒有,則重新啓動nagios。
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios restart
檢測結果如下: