用jmeter编写脚本做实时线上监控

新路 2020-06-03

将失败的请求存入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
~

相关推荐