Zabbix通过curl命令判断Web服务是否正常并自动重启

Zabbix通过curl命令判断Web服务是否正常并自动重启服务主要思路:
通过curl命令获取服务器响应码,如果正常返回200,不正常返回000
具体命令:
curl -I -s -w "%{http_code}" -o /dev/null

1.编辑zabbix-agent客户端的配置文件
①vim /etc/zabbix/zabbix_agentd.conf

# 添加命令参数
UserParameter=script_status,/usr/bin/curl -I -s -w "%{http_code}" -o /dev/null :9735/check
# 打开远程命令调用
EnableRemoteCommands = 1

重启服务生效
service zabbix-agent restart

②visudo打开关于zabbix操作的命令
a. 添加zabbix权限
zabbix ALL=(ALL) NOPASSWD: ALL

b.注释掉如下一行,否则命令无法执行:
# Default requiretty

2.添加需要被触发的脚本
vim /usr/local/zabbix-agent/scripts/restart_script.sh

#!/bin/bash # killyunva-script 关闭yunva-script这个Java进程 ps -ef|grep yunva-script-1.0.0.jar|/bin/kill `awk '{print $2}'` sleep 3 # start yunva-script 重新启动yunva-script这个java脚本 cd /home/script/ && /bin/bash start.sh start chmod +x /usr/local/zabbix-agent/scripts/restart_script.sh


--------------------------------------
start.sh脚本内容:


#!/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=8911 JMX_HOST=1.1.1.1 JAVA_OPTS="-server -Djava.nio.channels.spi.SelectorProvider=sun.nio.ch.EPollSelectorProvider -XX:+HeapDumpOnOutOfMemoryError -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dio.netty.leakDetectionLevel=advanced" 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} -Dcom.sun.management.jmxremote.port=${JMX_PORT} -Djava.rmi.server.hostname=${JMX_HOST} -Dapp.home=${APP_HOME}" echo "start jvm args ${JAVA_OPTS}" nohup /home/java/jdk1.8.0_40/bin/java -classpath .:./yunva-script-1.0.0.jar:$CLASSPATH $JAVA_OPTS com.yunva.script.ScriptServer& echo $! > $APPPIDFILE echo STARTED ;; stop) echo "Stopping server ... " if [ ! -f $APPPIDFILE ] then echo "error: count not find file $APPPIDFILE" exit 1 else kill -15 $(cat $APPPIDFILE) rm $APPPIDFILE echo STOPPED fi ;; *) echo "Please enter start|stop ... " ;; esac exit 0


--------------------------------------

3.zabbix-server的web配置

测试远程命令是否ok
# zabbix_get -s 1.1.1.1 -p 10050 -k script_status
200

①添加监控项item
Confuguration --> Hosts --> 找到对应的主机,点开 Items --> Create item
Name: script_check
# key需要和zabbix-agentd.conf中定义的Parameter参数一致
Key: script_status 


②创建对该监控项的触发器
Confuguration --> Hosts --> 找到对应的主机,点开 Triggers --> Create trigger

Name: yunva_scripts_port_9735_not_200
Expression: {iZ23xtdqvgmZ:script_status.last()}<>200


③设置Action
Configuration-->Actions-->Create action


创建Action动作,即返回值不是200的时候重启服务
在Action选项中,
定义Name:yunva_scripts_port_9735_not_200
#action name自定义即可,action选项的其余部分可采用默认值,如下:


Conditions
在Conditions选项中添加新的条件判断,以使判断更有针对性,如
New condition:Trigger severity = Warning
New condition:Trigger name like yunva_scripts_port_9735_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_script.sh"

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/755e889e6e6dc8825ac0fb331aefc529.html