1. 通过脚本判断线上服务是否可用
telnet 127.0.0.1 端口 #线上调用的是使用的dubbo端口
通过invoke 抓取返回的code值,脚本如下 #返回code:0则视为正常,返回其他值一概视为异常
#!/bin/sh
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo({"orderNo":"6045430f040010d1658802",
"channelCode":"0000"}) -p com.dafy.lou.loan.api.request.QueryRepayRequest";sleep 1;
echo " ";sleep 1;echo "exit")|telnet 127.0.0.1 50690 >/home/zabbix/loan/loan.txt
2. 通过zabbix监控其返回的值是否正常
每5分钟执行一次上述脚本,抓取/home/zabbix/loan/loan.txt的code值
*/5 * * * * sh /home/zabbix/loan/loan.sh
自定义监控项,zabbix安装路劲下新建一个以.conf结尾的文件,如下:
/usr/local/zabbix_agent/etc/zabbix_agentd.conf.d/loan_service.conf
UserParameter=loan, cat /home/zabbix/loan/loan.txt | grep '"code":0' | wc -l #此处返回值为1则正常,返回0则告警,通过邮件等告警及远程执行命令
需要在zabbix_agentd.conf打开以下配置项
EnableRemoteCommands=1 #允许远程执行命令
UnsafeUserParameters=0 #允许自定义监控项
Include=/usr/local/zabbix_agent/etc/zabbix_agentd.conf.d/*.conf #此处可自定义路劲
3. 如果上述返回值为0,则执行以下脚本
#!/bin/bash
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
(sleep 1;echo " ";sleep 1;echo "invoke com.dafy.letou.loan.api.service.RepayService.queryRepayByOrderNo
({"orderNo":"1604596224358802", "channelCode":"0000"}) -p com.dafy.letou.loan.api.request.QueryRepayRequest";sleep 1;echo " ";sleep 1;echo "exit")|
telnet 127.0.0.1 50690 >>/home/zabbix/loan/error.txt
sleep 3
code_err=`cat /home/zabbix/loan/error.txt | grep '"code":0' | wc -l`
loan=`ps -ef | grep letou-loan | grep -v grep | awk '{print $2}'`
if [ $code_err -lt 5 ];then
if [ ! $loan ];then
/opt/server/letou/letou-loan/bin/start.sh PRO
else
kill -9 $loan
/opt/server/letou/letou-loan/bin/start.sh PRO
fi
else
echo "重启失败"
fi
注意项:
因为生产服务的用户不是用root启动的,需要从zabbix切换到普通用户执行脚本
具体操作如下:
1. visudo 新增 zabbix ALL=(ALL) ALL
2. vim /etc/passwd 取消zabbix用户密码:即删除第二列的x
3. 在zabbix用户使用如下命令执行:sudo su deploy -s /bin/sh /home/zabbix/loan/call.sh sudo su - deploy -c 需要执行的命令(su与su -切换用户,su -会切换到用户家目录)
4. zabbix添加监控项,触发器及动作就不演示了,博客有自定义监控项的详解