Nginx用户请求响应时间监控与微信预警

时间:2024-04-03 20:44:42

功能:能够在用户反馈(用户体验:访问速度慢)之前就能掌握用户访问情况,快人一步,可以掌握事态发展!

nginx日志格式统一:

log_format main '$remote_addr $request_time $upstream_response_time $remote_user [$time_local] $upstream_addr $request'
                    ' $status $body_bytes_sent $request_body $http_referer'
                    ' $http_user_agent $http_x_forwarded_for';

脚本:

#!/bin/bash
# -*- coding: utf-8 -*-
###SCRIPT_NAME:weixin.sh###
###send message from weixin for monitoring###
###满世界跑的小运维###
###V1-2018-06-14


#!/bin/sh


expireTime=7200


dbFile="db.json"


# "我的企业" 最下面
corpid='xxxx'


# "企业应用" 第一行
agentid="1"


# "企业应用" 第二行
corpsecret='xxxxxxxx'


# "通讯录"->"成员详情" 的帐号
touser="xxxx"


# "通讯录" 左侧部门最右边的三个点里的ID
toparty="1"


#日志文件目录
applog=/home/data/logs/nginx/business/app.log


log=/home/logs/monitor


last_minutes=120


#开始时间
start_time=`date -d"$last_minutes minutes ago" +"%H:%M:%S"`


#结束时间
stop_time=`date +"%H:%M:%S"`


#获取规定的时间段的TENGINE日志文件


# s 为秒,m 为 分钟,h 为小时,d 为日数  
## 发送报警信息
sendMsg(){
    if [ ! -f "$dbFile" ];then
            touch "$dbFile"
    fi


    # 获取token
    req_time=`jq '.req_time' $dbFile`
    current_time=$(date +%s)
    refresh=false
    if [ ! -n "$req_time" ];then
            refresh=true
    else
            if [ $((current_time-req_time)) -gt $expireTime ];then

                refresh=true

            fi
    fi
    if $refresh ;then
        req_access_token_url=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid\&corpsecret=$corpsecret
        access_res=$(curl -s -G $req_access_token_url | jq -r '.access_token')


        ## 保存文件
        echo "" > $dbFile
        echo -e "{" > $dbFile
        echo -e "\t\"access_token\":\"$access_res\"," >> $dbFile
        echo -e "\t\"req_time\":$current_time" >> $dbFile
        echo -e "}" >> $dbFile
        echo $dbfile
        echo ">>>刷新Token成功<<<"
    fi


    ## 发送消息
    content="请注意--线上Nginx接口超时{响应大于四秒}:$(cat -s /home/logs/monitor/wewant.log)"
    msg_body="{\"touser\":\"$touser\",\"toparty\":\"$toparty\",\"msgtype\":\"text\",\"agentid\":$agentid,\"text\":{\"content\":\"$content\"}}"
    echo $msg_body > /home/logs/monitor/bodytxt
    access_token=`jq -r '.access_token' $dbFile`
    req_send_msg_url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token"
    req_msg=$(curl -s -H "Content-Type: application/json" -X POST -d @/home/logs/monitor/bodytxt  $req_send_msg_url | jq -r '.errmsg')
    echo $req_msg
    echo "触发报警发送动作,返回信息为:" $req_msg    


}




loopMonitor(){
    cat $applog | awk -v st="$start_time" -v et="$stop_time" '{t=substr($5,RSTART+14,21);if(t>=st && t<=et) {print $0} }' | sort | uniq -c | sort -nr > $log/date-filter.log
    cat $log/date-filter.log |awk '{if($4 > 4) print $0}'|awk '{split($2 "  " $3 "  " $4 "  " $6 "  " $8 "  " $9 "  "$10 "  " $14,b,"?");COUNT[b[1]]++;}END{for(a in COUNT) print  COUNT[a], a}'|sort -k 1 -nr >$log/wewant.log
    filename=/home/logs/monitor/wewant.log
    filesize=`ls -l $filename | awk '{ print $5 }'`


    c=$(echo "$filesize>0" | bc)


    if [ $c -eq 1  ];then
         sendMsg
    fi
}


loopMonitor;

预警形态如下【有疑问可以联系我:[email protected] or qq:1583864778】:

Nginx用户请求响应时间监控与微信预警