標籤:nagios
nagios外掛程式程式提供兩個傳回值:一個是外掛程式的退出狀態代碼,另一個是外掛程式在控制台上列印的第一行資料。退出狀態代碼可以被nagios主程式
作為判斷被監控系統服務狀態的依據,控制台列印的第一行資料可以被nagios主程式作為被監控系統服務狀態的補充說明
會顯示在管理頁面裡面。
為了管理nagios外掛程式,nagios每查詢一個服務的狀態時,就會產生一個子進程,並且它使用來自該命令的輸出和退出狀態代碼來
確定具體的狀態。nagios主程式可識別的狀態代碼和說明如下:
OK 結束代碼 0--表示服務正常的工作
warning 結束代碼 1--表示服務處於警示狀態
critical 結束代碼 2--表示服務處於緊急,嚴重狀態
unknown 結束代碼 3--表示服務處於未知狀態
[[email protected] services]# head -7 /usr/local/nagios/libexec/utils.sh
#! /bin/sh
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
樣本一:判斷/etc/passwd檔案是否變化,利用nrpe的被動模式
原理:利用md5sum進行指紋收集 md5sum /etc/passwd > /etc/passwd.md5
利用md5sum -c /etc/passwd.md5對指紋進行判別,出現OK則沒有變化,反之則變化了
監控密碼檔案是否被更改:
先做指紋庫
md5sum /etc/passwd > /etc/passwd.md5
在client上建立指令碼vim /usr/local/nagios/libexec/check_passwd
#!/bin/bash
char=`md5sum -c /etc/passwd.md5 2>&1 |grep "OK"|wc -l`
if [ $char -eq 1 ];then
echo "passwd is OK"
exit 0
else
echo "passwd is changed"
exit 2
fi
######給指令碼執行許可權
chmod +x /usr/local/nagios/libexec/check_passwd
#####定義check_passwd命令
vim /usr/local/nagios/etc/nrpe.cfg
command[check_passwd]=/usr/local/nagios/libexec/check_passwd
#####重啟nrpe服務
######在nagios主程式先手動抓取資料
[[email protected] libexec]# ./check_nrpe -H 192.168.1.11 -c check_passwd
passwd is OK
######在nagios主程式上定義service配置
vim /usr/local/nagios/etc/objects/services.cfg(主動模式和被動模式各自的services.cfg設定檔,各自分別管理)
define service{
use generic-service
host_name client02
service_description check_passwd
check_command check_nrpe!check_passwd
}
然後在nagios服務端進行手動抓取資料:
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.11 -c check_passwd
出現資料,表明基本已經沒有問題,重啟服務,觀察web平台頁面,如:
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/86/83/wKiom1fBllqy-Lr4AAAWfikfDSQ239.jpg" title="自訂監控用戶端passwd檔案變化.jpg" alt="wKiom1fBllqy-Lr4AAAWfikfDSQ239.jpg" />
自訂監控web url,用主動模式監控
[[email protected] ~]# curl -I http://192.168.1.11/index.html 2>/dev/null|grep "OK"
HTTP/1.1 200 OK
[[email protected] ~]# curl -I http://192.168.1.11/index.html 2>/dev/null|grep "OK"|wc -l
1
1、編寫執行指令碼
cd /usr/local/nagios/libexec
vim check_web_url
#!/bin/bash
char=`curl -I http://192.168.1.11/index.html 2>/dev/null|grep "OK"|wc -l`
if [ $char -eq 1 ];then
echo "the url is OK"
exit 0
else
echo "the url is wrong"
exit 2
fi
chmod +x check_web_url
2、添加check_web_url這個命令到commands.cfg設定檔中
############define command check_web_url##########
define command{
command_name check_web_url
command_line $USER1$/check_web_url
}
3、編輯servers.cfg檔案
cd /usr/local/nagios/etc/services
vim web_url.cfg
define service{
use generic-service
host_name client02 監控的主機192.168.1.11在hosts.cfg有定義
service_description web_url
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 3
check_command check_web_url 因為是主動模式
notification_period 24x7
notification_interval 30
notification_options w,u,c,r
contact_groups admins
}
4、檢測錯誤,重啟服務
[[email protected] services]# /etc/init.d/nagios checkconfig
Running configuration check...
OK.
[[email protected] services]# /etc/init.d/nagios reload
Running configuration check...
Reloading nagios configuration...
done
成功:
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/86/83/wKioL1fBmPDCd2QiAAAXlLxo_MU338.jpg" title="自訂指令碼主動模式監控web_url.jpg" alt="wKioL1fBmPDCd2QiAAAXlLxo_MU338.jpg" />
看下整體監控效果:
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/86/83/wKioL1fBmWqCHTIEAACuhXzo46g069.jpg" title="監控介面.jpg" alt="wKioL1fBmWqCHTIEAACuhXzo46g069.jpg" />
實現郵件警示功能:
配置警示的步驟:
1、新增連絡人...和聯絡組contacts.cfg
define contact{
contact_name huang
use generic-contact ---》這裡使用的模板就是模板檔案中的contact定義
alias Nagios Admin
email [email protected]
}
將定義的contact_name添加到一個新組中
新增聯絡組:
define contactgroup{
contactgroup_name mail_users 這裡可以定義郵件組,手機簡訊組,等等
alias Nagios Administrators
members huang
}
2、添加警示的命令commands.cfg,這裡使用預設的命令,當然你也可以自己編寫shell指令碼或者其他語言指令碼
3、調整連絡人的預設範本
define contact{
name generic-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email 如果定義了手機,這裡可以加上notify-host-by-email,notify-host-by-pager,這裡使用郵件警示,所以無需設定
register 0
}
4、在hosts、services設定檔中添加警示連絡人及警示組
然後修改模板中service、host的定義,將
contact_groups admins改為
contact_groups mail_users
當然也可以不在模板中定義,在hosts、services設定檔中各自訂不同的警示方式和警示組
實驗:
將網站目錄下面的index.html檔案mv到tmp目錄下,使他warning並觸發警示
mv /var/www/html/index.html /tmp
可以看見web平台出現warning狀態,查看nagios日誌
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/86/83/wKioL1fBmyrzEzDXAAFWTU7W3v0614.jpg" title="郵件警示出錯.jpg" alt="wKioL1fBmyrzEzDXAAFWTU7W3v0614.jpg" />
然後查看郵件,發現沒有收到警示郵件,看日誌發現是找不到mail命令,於是
yum -y install mailx
由於定義的services警示參數:
service_notification_options w,u,c,r,f,s,表示監控恢複正常也會觸發郵件於是將index.html重新放到網站目錄下
mv /tmp/index.html /var/www/html
稍微過幾分鐘可以發現監控正常,查看nagios日誌
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/86/83/wKioL1fBm8-x3OhLAACVPTVsSW0641.jpg" title="警示日誌記錄.jpg" alt="wKioL1fBm8-x3OhLAACVPTVsSW0641.jpg" />
再次查看郵件,如下:
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/86/83/wKioL1fBm_OBeHYmAACCLrunGEE231.jpg" title="警示.jpg" alt="wKioL1fBm_OBeHYmAACCLrunGEE231.jpg" />
簡單基於mail警示功能實現
建立菜鳥學習交流群:584498750
自訂nagios外掛程式實現主動被動模式以及nagios基於mail的簡單警示