Zabbix觸發器(trigger)達到閥值後會有動作(action)執行:傳送告警資訊或執行遠端命令
環境
Server:基於centos6.5 final x86_64Zabbix:zabbix-3.0.4 server/agent
注意事項
1.遠端執行命令是server端向agent端執行,不支援主動模式的agent;2.不支援代理模式;
3.zabbix使用者必須對命令具有執行許可權,可以使用sudo賦予root許可權(配置sudo無密碼方式);
4.遠端命令只是執行,執行成功與否並不檢測並確認,可在” Monitoring-->Events”中檢視action執行時,或在”Reports-->Action log”中檢視遠端命令是否執行成功(成功為” Executed”)。
zabbix-agent端的操作:
場景:監控某個伺服器的web頁面http相應碼如果連續4次不為200則觸發重啟相關服務action1.開啟客戶端遠端執行命令的開關,記得重啟zabbix-agent服務
vim /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands = 1
2.visudo開啟關於zabbix操作的命令
①新增如下內容
# allows 'zabbix' user to run all commands without password.
zabbix ALL=NOPASSWD: ALL
# allows 'zabbix' user to restart apache without password.
zabbix ALL=NOPASSWD: /bin/bash /usr/local/zabbix-agent/scripts/restart_ad_server.sh
②註釋掉如下一行,否則命令無法執行:
# Default requiretty
新增相關的指令碼:
vim /usr/local/zabbix-agent/scripts/restart_ad_server.sh
#!/bin/bash
# kill ad-server process
count_num=`ps -ef|grep ad-server-1.0.0.jar|grep -v grep|wc -l`
if [ $count_num -eq 1 ];then
ps -ef|grep ad-server-1.0.0.jar|grep -v grep|kill -9 `awk '{print $2}'`
sleep 2
fi
# start ad-server
cd /data/ad-push/ && /bin/bash /data/ad-push/start.sh start
新增執行許可權
chmod +x /usr/local/zabbix-agent/scripts/restart_ad_server.sh
zabbix-server端操作
設定Action
Configuration-->Actions-->Create action
Action
在Action選項中,
定義Name:adpush_not_200_restart_ad_server
#action name自定義即可,action選項的其餘部分可採用預設值,如下:
Conditions
在Conditions選項中新增新的條件判斷,以使判斷更有針對性,如
New condition:Trigger severity = Warning
New condition:Trigger name like ads_9010_status_not 200
#trigger name對應步驟1中定義的trigger name,如下:
Operations
在Operations選項中,新增新的”Action operation”,點選”New”,
Operation type:選擇”Remote Command”
Target list:新增target為”Current host”
#agent在本機
Type:選擇”Custom script”
Execute on:選擇”Zabbix agent”,命令為 "sudo /bin/bash /usr/local/zabbix-agent/scripts/restart_ad_server.sh"
#執行命令的賬號是zabbix賬號,非root賬號,不採用sudo命令會導致命令執行後不生效
#另外需要說明是,嘗試過使用具體的命令而非指令碼,結果是命令執行了但不生效,因為沒有具體的失敗日誌,也分析不出原因
#其餘部分採用預設值,點選”Add”即可,如下:
問題:
但是執行命令失敗,無法自動重啟服務:於是授予zabbix所有使用者許可權
# allows 'zabbix' user to restart apache without password.
zabbix ALL=(ALL) NOPASSWD: ALL
修改restart_ad_server.sh
/usr/local/zabbix-agent/scripts/restart_ad_server.sh
#!/bin/bash
# kill ad-server process
ps -ef|grep ad-server-1.0.0.jar|/bin/kill `awk '{print $2}'`
sleep 2
# start ad-server
# start.sh使用絕對路徑
cd /home/ad-push/ && /bin/bash /home/ad-push/start.sh start
#!/bin/bash
# kill ad-server process
ps -ef|grep ad-server-1.0.0.jar|/bin/kill `awk '{print $2}'`
sleep 2
ps -ef|grep ad-server-1.1.0.jar|/bin/kill `awk '{print $2}'`
sleep 2
# start ad-server
cd /data/ad-push/ && /bin/bash /data/ad-push/start.sh start
cd /data/ad-push2/ && /bin/bash /data/ad-push2/start.sh start
修改start.sh指令碼如下:
java也使用絕對路徑:
/home/java/jdk1.8.0_40/bin/java
#!/bin/bash
LANG="zh_CN.UTF-8"
APP_HOME=$(echo `pwd` | sed 's/bin//')
APPPIDFILE=$APP_HOME/app.pid
case $1 in
start)
echo "Starting server... "
HEAP_MEMORY=1024m
PERM_MEMORY=64m
JMX_PORT=1111
JMX_HOST=1.1.1.1
JAVA_OPTS="-server -XX:+UseConcMarkSweepGC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=jvm.log -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dsun.net.inetaddr.ttl=15 "
shift
ARGS=($*)
for ((i=0; i<${#ARGS[@]}; i++)); do
case "${ARGS[$i]}" in
-D*) JAVA_OPTS="${JAVA_OPTS} ${ARGS[$i]}" ;;
-Heap*) HEAP_MEMORY="${ARGS[$i+1]}" ;;
-Perm*) PERM_MEMORY="${ARGS[$i+1]}" ;;
-JmxPort*) JMX_PORT="${ARGS[$i+1]}" ;;
-JmxHost*) JMX_HOST = "${ARGS[$i+1]}" ;;
esac
done
JAVA_OPTS="${JAVA_OPTS} -Xms${HEAP_MEMORY} -Xmx${HEAP_MEMORY} -XX:PermSize=${PERM_MEMORY} -XX:MaxPermSize=${PERM_MEMORY} -XX:MaxDirectMemorySize=128m -Dcom.sun.management.jmxremote.port=${JMX_PORT} -Djava.rmi.server.hostname=${JMX_HOST} -Dapp.home=${APP_HOME}"
echo "start jvm args ${JAVA_OPTS}"
nohup <span style="color:#ff0000;">/home/java/jdk1.8.0_40/bin/java</span> $JAVA_OPTS -cp .:./ad-server-1.0.0.jar org.springframework.boot.loader.JarLauncher > /dev/null &
echo $! > $APPPIDFILE
echo STARTED
;;
stop)
echo "Stopping server ... "
if [ ! -f $APPPIDFILE ]
then
echo "error: count not find file $APPPIDFILE"
exit 1
else
kill -9 $(cat $APPPIDFILE)
rm $APPPIDFILE
echo STOPPED
fi
;;
*)
echo "Please enter start|stop ... "
;;
esac
exit 0
也可以編輯/etc/profile,加入java的相關路徑,然後在指令碼中引入
#!/bin/bash
source /etc/profile
count_num=`ps -ef|grep yunva-sdk-port-1.0.0.jar |grep -v grep|wc -l`
if [ $count_num -ge 1 ];then
ps -ef|grep yunva-sdk-port-1.0.0.jar|grep -v grep|kill -9 `awk '{print $2}'`
sleep 2
fi
cd /home/yunva/sdk-port && /bin/bash start.sh start