用jmeter编写脚本做实时线上监控
将失败的请求存入xml格式的文件中
选择想要的信息,信息太多是无用的,处理也不方便
#!/bin/bash # 文件路径 dir="/opt/monitorScript/mtl_pc.jmx" # jmter路径 jmterdir="/opt/jmeter5.1.1" ## 日志存储地址 log="/tmp" ## 设置时间 time=`date +%Y%m%d%H%M%S` ## 设置操作函数 function operate(){ /opt/jmeter5.1.1/bin/jmeter.sh -n -t $dir>>/tmp/mtl.log } ## 判断xml日志文件是否存在,不存在, if [ -f $log/mtl_pc.xml ];then mv $log/mtl_pc.xml $log/mtl_pc_$time.xml operate else operate fi ## 定义接口名称 lb=`cat $log/mtl_pc.xml |xml2|grep "@lb"|awk -F "=" '{print $2}'|tail -1` ## 获取接口地址 URL=`cat $log/mtl_pc.xml|xml2|grep "java.net.URL" |tail -1` url=`echo ${URL#*=}` ## 获取异常信息 failureMessage=`cat $log/mtl_pc.xml|xml2|grep "failureMessage"|awk -F '=' '{print $2}'|sed "s/'//g"|tail -2` ## failureMessagenum=`cat $log/mtl_pc.xml|xml2|grep "failureMessage"|wc -l` ## 设置函数,发送钉钉群 function SendMessageToDingding(){ Dingding_Url="https://oapi.dingtalk.com/robot/send?access_token=8ba7c120854ac253d7affa1f0b10e1e34aa5ad3ccd467694148533797f471ec9" curl "${Dingding_Url}" -H 'Content-Type: application/json' -d "{ 'msgtype': 'text', 'text': { 'content': '接口场景:摩天轮线上业务监控\n接口名称:$lb\n接口地址:$url\n接口异常:$failureMessage\n' } }" } if [ $failureMessagenum -ge 3 ];then SendMessageToDingding else echo "成功" fi ~
调取Python脚本进行发送
Last login: Wed Jun 3 14:09:16 on ttys000 heixiaoyadeMacBook-Pro:~ jinyan$ shh root@129.211.29.204's password: Last login: Wed Jun 3 13:50:50 2020 from 180.167.200.10 [root@VM_0_10_centos ~]# cd /opt/monitorScript/ [root@VM_0_10_centos monitorScript]# ll 总用量 5020 -rwxr-xr-x 1 root root 1505 5月 21 11:54 Dingding4piaoxingqiu.sh -rwxr-xr-x 1 root root 1469 3月 24 10:10 Dingding4sn.sh -rwxrwxrwx 1 root root 1456 5月 6 20:10 Dingding.sh -rwxr-xr-x 1 root root 1237 6月 3 14:06 JmeterAlert.sh -rw-r--r-- 1 root root 5895 6月 3 14:06 jmeter.log -rw-r--r-- 1 root root 436866 5月 11 20:56 mtl_pc.jmx -rwxrwxrwx 1 root root 435016 2月 17 15:43 mtl_pc.jmx_2020-03-01 -rw-r--r-- 1 root root 434123 3月 1 17:07 mtl_pc.jmx_2020-03-08 -rw-r--r-- 1 root root 434124 3月 8 18:54 mtl_pc.jmx_2020-03-10 -rw-r--r-- 1 root root 434425 3月 10 12:17 mtl_pc.jmx_2020-03-12 -rw-r--r-- 1 root root 434424 3月 12 19:16 mtl_pc.jmx_2020_03_122 -rw-r--r-- 1 root root 433974 5月 6 18:12 mtl_pc.jmx_2020_05_08 -rw-r--r-- 1 root root 415581 5月 8 10:32 mtl_pc.jmx_2020_05_09 -rw------- 1 root root 451521 5月 21 12:11 piaoxingqiu.jmx -rw------- 1 root root 451642 5月 21 10:47 piaoxingqiu.jmx_2020_05_21 -rwxrwxrwx 1 root root 451642 5月 21 11:11 piaoxingqiu.jmx_2020_05_22 -rwxrwxrwx 1 root root 1809 6月 3 13:39 ProdAlert.py -rwxrwxrwx 1 root root 1731 6月 3 13:32 ProdAlert.py_20200601 -rw-r--r-- 1 root root 268288 2月 17 22:45 shownow.jmx [root@VM_0_10_centos monitorScript]# rm -rf *.jmx_2020* [root@VM_0_10_centos monitorScript]# ll 总用量 1168 -rwxr-xr-x 1 root root 1505 5月 21 11:54 Dingding4piaoxingqiu.sh -rwxr-xr-x 1 root root 1469 3月 24 10:10 Dingding4sn.sh -rwxrwxrwx 1 root root 1456 5月 6 20:10 Dingding.sh -rwxr-xr-x 1 root root 1237 6月 3 14:06 JmeterAlert.sh -rw-r--r-- 1 root root 5895 6月 3 14:06 jmeter.log -rw-r--r-- 1 root root 436866 5月 11 20:56 mtl_pc.jmx -rw------- 1 root root 451521 5月 21 12:11 piaoxingqiu.jmx -rwxrwxrwx 1 root root 1809 6月 3 13:39 ProdAlert.py -rwxrwxrwx 1 root root 1731 6月 3 13:32 ProdAlert.py_20200601 -rw-r--r-- 1 root root 268288 2月 17 22:45 shownow.jmx [root@VM_0_10_centos monitorScript]# mv mv: 缺少了文件操作数 Try 'mv --help' for more information. [root@VM_0_10_centos monitorScript]# mv mtl_pc.jmx mtl_pc.jmx_222 [root@VM_0_10_centos monitorScript]# ll 总用量 1596 -rwxr-xr-x 1 root root 1505 5月 21 11:54 Dingding4piaoxingqiu.sh -rwxr-xr-x 1 root root 1469 3月 24 10:10 Dingding4sn.sh -rwxrwxrwx 1 root root 1456 5月 6 20:10 Dingding.sh -rwxr-xr-x 1 root root 1237 6月 3 14:06 JmeterAlert.sh -rw-r--r-- 1 root root 5895 6月 3 14:06 jmeter.log -rw-r--r-- 1 root root 436866 6月 3 14:13 mtl_pc.jmx -rw-r--r-- 1 root root 436866 5月 11 20:56 mtl_pc.jmx_222 -rw------- 1 root root 451521 5月 21 12:11 piaoxingqiu.jmx -rwxrwxrwx 1 root root 1809 6月 3 13:39 ProdAlert.py -rwxrwxrwx 1 root root 1731 6月 3 13:32 ProdAlert.py_20200601 -rw-r--r-- 1 root root 268288 2月 17 22:45 shownow.jmx [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 01用户登录 /testResults/httpSample/java.net.URL=https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591164812338&src=web&source=web Value expected to match regexp 200, but it did not match: 514 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi Dingding.sh [root@VM_0_10_centos monitorScript]# cd /opt/monitorScript/ [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 01用户登录 /testResults/httpSample/java.net.URL=https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591165308781&src=web&source=web Value expected to match regexp 200, but it did not match: 515 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh JmeterAlert.sh: command substitution:行34: 寻找匹配的 `'' 是遇到了未预期的文件结束符 JmeterAlert.sh: command substitution:行35: 语法错误: 未预期的文件结尾 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591165551845&src=web&source=web 6 本次需要发送alert Traceback (most recent call last): File "/opt/monitorScript/ProdAlert.py", line 49, in <module> ProdAlert(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) IndexError: list index out of range [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591165745398&src=web&source=web Value expected to match regexp 200, but it did not match: 514 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# $aaa= 111 222 -bash: =: 未找到命令 [root@VM_0_10_centos monitorScript]# echo $aaa [root@VM_0_10_centos monitorScript]# aaa= 222 333 -bash: 222: 未找到命令 [root@VM_0_10_centos monitorScript]# s = "111 222" -bash: s: 未找到命令 [root@VM_0_10_centos monitorScript]# s= "11" -bash: 11: 未找到命令 [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591166157484&src=web&source=web Value expected to match regexp 200, but it did not match: 514 Value expected to match regexp 200, but it did not match: 514 Value expected to match regexp 200 but it did not match: 514 Value expected to match regexp 200 but it did not match: 514 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh JmeterAlert.sh:行38: = 123 456 : 未找到命令 JmeterAlert.sh:行40: =: 未找到命令 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591166299124&src=web&source=web Value expected to match regexp 200, but it did not match: 514 Value expected to match regexp 200, but it did not match: 514 Value expected to match regexp 200 but it did not match: 514 Value expected to match regexp 200 but it did not match: 514 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 123 456 123 456 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591166353342&src=web&source=web Value expected to match regexp 200, but it did not match: 514 Value expected to match regexp 200, but it did not match: 514 Value expected to match regexp 200 but it did not match: 514 Value expected to match regexp 200 but it did not match: 514 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 123 456 123 456 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591166426850&src=web&source=web Value expected to match regexp 200, but it did not match: 515 Value expected to match regexp 200, but it did not match: 515 Value expected to match regexp 200 but it did not match: 515 Value expected to match regexp 200 but it did not match: 515 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh JmeterAlert.sh:行41: =123_456: 未找到命令 123 456 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591166869341&src=web&source=web Value expected to match regexp 200, but it did not match: 515 Value expected to match regexp 200, but it did not match: 515 Value expected to match regexp 200 but it did not match: 515 Value expected to match regexp 200 but it did not match: 515 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 123 456 123_456 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591166903985&src=web&source=web Value expected to match regexp 200, but it did not match: 515 Value expected to match regexp 200, but it did not match: 515 Value expected to match regexp 200 but it did not match: 515 Value expected to match regexp 200 but it did not match: 515 6 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 123 456 123_456 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591167010553&src=web&source=web Value expected to match regexp 200, but it did not match: 515 Value_expected_to_match_regexp_200,_but_it_did_not_match:_515 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh [root@VM_0_10_centos monitorScript]# sh JmeterAlert.sh 01用户登录 https://www.moretickets.com/userapi/pub/verify_mobile_token?cellphone=18900000008&token=1111&nonce=TldRM1pqUmhaamsyTXpFMk1XSTNOR0l3T0RRME5qYzBfOl8xNTY4NjIzMzUzMjY3&time=1591167059067&src=web&source=web Value_expected_to_match_regexp_200,_but_it_did_not_match:_515 本次需要发送alert [root@VM_0_10_centos monitorScript]# vi JmeterAlert.sh #!/bin/bash # 文件路径 dir="/opt/monitorScript/mtl_pc.jmx" # jmter路径 jmterdir="/opt/jmeter5.1.1" ## 日志存储地址 log="/tmp" ## 设置时间 time=`date +%Y%m%d%H%M%S` ## 设置操作函数 function operate(){ /opt/jmeter5.1.1/bin/jmeter.sh -n -t $dir>>/tmp/mtl.log } ## 判断xml日志文件是否存在,不存在, if [ -f $log/mtl_pc.xml ];then mv $log/mtl_pc.xml $log/mtl_pc_$time.xml operate else operate fi ## 定义接口名称 lb=`cat $log/mtl_pc.xml |xml2|grep "@lb"|awk -F "=" '{print $2}'|tail -1` ## 获取接口地址 URL=`cat $log/mtl_pc.xml|xml2|grep "java.net.URL" |tail -1` url=`echo ${URL#*=}` ## 获取异常信息 failureMessage=`cat $log/mtl_pc.xml|xml2|grep "failureMessage"|awk -F '=' '{print $2}'|sed "s/'//g"|tail -2` failureMessage=`echo $failureMessage | sed 's/ /_/g'` ##计量一下报错的所有条数 failureMessagenum=`cat $log/mtl_pc.xml|xml2|grep "failureMessage"|wc -l` echo "$lb" echo "$url" echo "$failureMessage" ## 设置函数,发送警报群组 if test -z "$URL" then #python3 /opt/monitorScript/ProdAlert.py $lb $URL $failureMessage $failureMessagenum echo "不需要发送alert" else echo "本次需要发送alert" python3 /opt/monitorScript/ProdAlert.py $lb $url $failureMessage $failureMessagenum fi ~ ~ "JmeterAlert.sh" 52L, 1299C
------------------------- A little Progress a day makes you a big success... ----------------------------