shell实时统计nginx日志tps情况

有个需求,需要实时统计日志中每隔接口的tps情况。
每次使用命令写太复杂了,于是写了一个脚本

#!/bin/bash

list="queryCardList"
info="queryInfo"


while true;
do 
  sleep 1
  s_time=`date -d '5 second ago' '+%H:%M:%S'`
  now_time=`date '+%Y-%m-%d '`
  
  list_no=`tail -n 50000 /nginx/access.log |grep $s_time|grep $list|wc -l`
  info_no=`tail -n 50000 /nginx/access.log |grep $s_time|grep $info|wc -l`
  all_no=`tail -n 50000 /nginx/access.log |grep $s_time|wc -l`
  echo $now_time$s_time "list:"$list_no "info:"$info_no "all:"$all_no
  echo $now_time$s_time "list:"$list_no "info:"$info_no "all:"$all_no>> record.txt
done

输出结果

2021-01-18 10:51:07 list_no:14 info_no:17  all:76
2021-01-18 10:51:08 list_no:8 info_no:17 all:86
2021-01-18 10:51:09 list_no:4 info_no:10 all:86
2021-01-18 10:51:10 list_no:6 info_no:18 all:86
2021-01-18 10:51:11 list_no:8 info_no:14 all:86
2021-01-18 10:51:13 list_no:10 info_no:10 all:86
2021-01-18 10:51:14 list_no:13 info_no:9 all:86
2021-01-18 10:51:15 list_no:5 info_no:12 all:86
2021-01-18 10:51:16 list_no:9 info_no:20 all:86

这里还有优化的地方,重复的tail的地方比较多。比较消耗资源。暂时就不优化了。

如果访问日志量很大。一秒钟超过了1万条。那么只需要把5000修改为 真实的日志访问量即可。
不过理论上,一秒钟5000的请求。应该可以满足大部分情况了,

还可以生成json格式的文件,利用python制作一个折线图,看起来更直观。

然后利用python生成一个折线图

import pygal

time_data = []
# 查询绑卡
card_list = []
# 查询账户
acct_info = []
# 所有tps
all_tps = []


datas = '''
2021-01-18 10:51:07 list_no:14 info_no:17  all:76
2021-01-18 10:51:08 list_no:8 info_no:17 all:86
2021-01-18 10:51:09 list_no:4 info_no:10 all:86
2021-01-18 10:51:10 list_no:6 info_no:18 all:86
2021-01-18 10:51:11 list_no:8 info_no:14 all:86
2021-01-18 10:51:13 list_no:10 info_no:10 all:86
2021-01-18 10:51:14 list_no:13 info_no:9 all:86
2021-01-18 10:51:15 list_no:5 info_no:12 all:86
2021-01-18 10:51:16 list_no:9 info_no:20 all:86
'''

for line in datas.split(''):
    line_list = line.split(' ')
    if len(line_list) > 2:
        line_time = line_list[1]
        time_data.append(line_time)
        CardList = line_list[2].split(':')[1]
        card_list.append(int(CardList))
        AcctInfo = line_list[3].split(':')[1]
        acct_info.append(int(AcctInfo))
        all = line_list[6].split(':')[1]
        all_tps.append(int(all))


garph = pygal.Line()    # 创建图(叠加柱状图)

# 添加数据
garph.add('查询绑卡', card_list)
garph.add('查询账户', acct_info)
garph.add('所有tps', all_tps)

garph.x_labels = time_data            # 设置 X 轴刻度
garph.y_label_rotation = 0           # 设置Y轴的标签旋转多少度
garph.x_label_rotation = 90           # 设置Y轴的标签旋转多少度

garph.title = 'tps分析'  # 设置图标题
garph.x_title = '时间'                 # 设置 X 轴标题
garph.y_title = '并发量tps'            # 设置 Y 轴标题

garph.legend_at_bottom = True         # 设置图例位置(下面)

garph.margin = 0    # 设置页边距(margin_bottom、margin_top、margin_left、margin_right)

garph.show_x_guides = False    # 显示X轴的网格线
garph.show_y_guides = True    # 显示Y轴的网格线

garph.render_to_file('tps.svg')    # 输出到图片文件

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值