#!/bin/sh ret_ok=1 ret_warn=2 ret_critical=3 ret_unknown=4 info_count=`ps -aux | awk 'BEGIN {cnt=0} /[^-c] dotnet GameServer.dll/{++cnt} END {print cnt}'` process_count=$((${info_count}-1)) if [[ ${process_count} < 6 ]]; then echo ${ret_critical} else echo ${ret_ok} fi
這里還有一個帶參數的以供參考
#!/bin/bash #port on each server port=$1 #result code ret_ok=1 ret_warn=2 ret_critical=3 ret_unknown=4 #get heartbeat message #url:http://127.0.0.1:9002/heartbeat curl_result=`curl -m 3 -s http://127.0.0.1:$port/heartbeat` if [[ ${curl_result} == *"alive"* ]]; then echo ${ret_ok} else echo ${ret_critical}
ps -aux查出來進程,用awk正則匹配,然后輸出匹配行數,減去awk本身,其中進程名那里可以使用變量,這里圖省事就寫死了,反正以后可以再改
然后再在zabbix中添加
[root@zabbiclient1 zabbix_agentd.conf.d]# pwd /usr/local/zabbix/etc/zabbix_agentd.conf.d [root@zabbiclient1 zabbix_agentd.conf.d]# ll check_process.conf -rw-r--r-- 1 root root 533 Dec 26 2013 check_process.conf [root@zabbiclient1 zabbix_agentd.conf.d]# cat check_process.conf UserParameter=check.dotnet,/etc/zabbix/scripts/check_dotnet_running ##注意check.crontab必須和下面創建item時的key相對應
如果是帶參數的,就在逗號后邊的command后邊加上[]中間括着參數,比如
UserParameter=check.dotnet[*],/etc/zabbix/scripts/check_dotnet_running $1
在zabbix client機器上include 相關文件,
[root@zabbiclient1 etc]# pwd /usr/local/zabbix/etc [root@zabbiclient1 etc]# ls zabbix_agent.conf zabbix_agent.conf.d zabbix_agentd.conf zabbix_agentd.conf.d [root@zabbiclient1 etc]# cat zabbix_agentd.conf | grep -v "#" | grep -v "^$" PidFile=/tmp/zabbix_agentd.pid LogFile=/tmp/zabbix_agentd.log DebugLevel=4 Server=192.168.10.134 ServerActive=192.168.10.134 Hostname=zabbixclient1 Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/check_process.conf ###Include該文件!
3: 下面關鍵的重點是通過zabbix_get 獲取check.dotnetkey的值,這個值在創建trigger時時需要,否則
trigger表達式填寫不正確,就無法觸發trigger
當我將crond進程在134機器上stop掉的時候,在133機器上獲取值
[root@zabbiclient2 ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.10.134 -k check.dotnet
3
如果是有參數的,就是這樣測試/usr/local/zabbix/bin/zabbix_get -s 192.168.10.134 -k check.dotnet[5],並在創建模板的時候在key中加入中括號加入參數
下面就創建一個模板,我創建的模板名為apptemplate,在模板里面創建一個item,crond is running,update interval 我設置為30s,見
然后創建一個trigger,trigger名為:dotnet does not work, 表達式值為{apptemplate:check.crontab.last(0)}=3
Severity 選擇disaster,點save按鈕即可創建。見附件
4:然后就可以在134機器上手動執行service dotnet stop,看看是否觸發trigger,當然生產系統我就不完活了,你們自己慢慢玩吧
或另外一種方式:
Zabbix監控一個服務是否存在
有兩種方法:
1:數進程數
接着創建trigger:
還有一種情況,例如:iptables,盡管它啟動了,但不能通過ps –ef | grep iptables 來數進程數,這個時候就需要另外一種訪問來處理了,見附件:
接着創建trigger,如下: