美文网首页
aws防ping服务器网络延迟测试与曲线图生成分析

aws防ping服务器网络延迟测试与曲线图生成分析

作者: SailSea | 来源:发表于2020-06-08 16:55 被阅读0次

因为最近发现aws服务器网络不是特别稳定,所以想监控一下网络的联通性和延迟。但是发现并没有可以直接用的工具。
搭建了smartping,worldping,smokeping这三个工具发现都不能使用

于是就使用tcpping来不断的将日志保存到本地文件,然后使用python解析数据生成自己需要的数据,放到echarts里面来展示。能够达到和上述的三种工具的相同效果。

部分效果:


image.png

日志默认输出格式

2020年 06月 07日 星期日 00:56:13 CST
traceroute to 54.189.110.117 (54.189.110.117), 255 hops max, 60 byte packets
seq 119049: tcp response from ec2-54-189-110-117.us-west-2.compute.amazonaws.com (54.189.110.117) <syn,ack>  186.962 ms
2020年 06月 07日 星期日 00:56:14 CST
traceroute to 54.189.110.117 (54.189.110.117), 255 hops max, 60 byte packets
seq 119050: tcp response from ec2-54-189-110-117.us-west-2.compute.amazonaws.com (54.189.110.117) <syn,ack>  183.514 ms
2020年 06月 07日 星期日 00:56:15 CST
traceroute to 54.189.110.117 (54.189.110.117), 255 hops max, 60 byte packets
seq 119051: tcp response from ec2-54-189-110-117.us-west-2.compute.amazonaws.com (54.189.110.117) <syn,ack>  183.347 ms
2020年 06月 07日 星期日 00:56:16 CST

python3日志数据分析代码

import time

"""
日期转时间戳
"""
def unix_time(dt):
    # 转换成时间数组
    timeArray = time.strptime(dt, "%Y年%m月%d日%H:%M:%S")
    # 转换成时间戳
    timestamp = int(time.mktime(timeArray))
    return timestamp

"""
时间戳转日期
"""
def custom_time(timestamp):
    # 转换成localtime
    time_local = time.localtime(timestamp)
    # 转换成新的时间格式(2016-05-05 20:28:54)
    dt = time.strftime("%Y/%m/%d %H:%M:%S", time_local)
    return dt

"""
输出日志结果
"""
def data_handle(date_key,data_dict):
    date_key_sorted = sorted(date_key)
    date = []
    data = []
    for key in date_key_sorted:
        date.append(key)
        data.append(data_dict[key])
    #print(len(data_dict))

    date_fout = open(date_file_result, 'w', encoding='utf8')
    date_fout.write(str(date))
    date_fout.close()
    #print(date)

    data_fout = open(data_file_result, 'w', encoding='utf8')
    data_fout.write(str(data))
    data_fout.close()
    #print(data)

log_file = "/Users/hfy/work/python/tools/analysis/111.log"
data_file_result = "/Users/hfy/work/python/tools/analysis/data_file_result.txt"
date_file_result = "/Users/hfy/work/python/tools/analysis/date_file_result.txt"
if __name__ == '__main__':
    file = open(log_file, 'r')
    data_dict = {}
    date_key = []
    time_str = ""
    delay = 0
    for line in file:
        if line.find("CST") != -1:
            # 时间格式转换
            date_list = line.split(" ")
            date_str = date_list[0] + date_list[1] + date_list[2] + date_list[4]
            time_str = custom_time(unix_time(date_str))

        if line.find("seq") != -1:
            line_list = line.split(" ")
            if len(line_list) < 12 and line.find("timeout") != -1:
                delay = 0
            else:
                delay = float(line_list[9])
            #收集延迟大于500和超时的日志信息
            if delay == 0 or delay > 500 :
                date_key.append(time_str)
                data_dict[time_str] = delay
            time_str = ""
            delay = 0
    data_handle(date_key,data_dict)
#echar  https://echarts.apache.org/examples/zh/editor.html?c=area-simple 数据分析

echars数据展示地址:
https://echarts.apache.org/examples/zh/editor.html
只需要将输出的data和date放到正确的位置就会自动渲染数据
demo:

var base = +new Date(1968, 9, 3);
var oneDay = 24 * 3600 * 1000;
var date = [];

var data = [Math.random() * 300];

// for (var i = 1; i < 20000; i++) {
//     var now = new Date(base += oneDay);
//     date.push([now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'));
//     data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1]));
// }

var data = [1201.895,1201.895,120.895,1201.895,12.895,1201.895,120.895,1201.895,12.895,1201.895]
var date = ['2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15','2020/06/05 15:17:15']

option = {
    tooltip: {
        trigger: 'axis',
        position: function (pt) {
            return [pt[0], '10%'];
        }
    },
    title: {
        left: 'center',
        text: '大数据量面积图',
    },
    toolbox: {
        feature: {
            dataZoom: {
                yAxisIndex: 'none'
            },
            restore: {},
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: date
    },
    yAxis: {
        type: 'value',
        boundaryGap: [0, '100%']
    },
    dataZoom: [{
        type: 'inside',
        start: 0,
        end: 10
    }, {
        start: 0,
        end: 10,
        handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
        handleSize: '80%',
        handleStyle: {
            color: '#fff',
            shadowBlur: 3,
            shadowColor: 'rgba(0, 0, 0, 0.6)',
            shadowOffsetX: 2,
            shadowOffsetY: 2
        }
    }],
    series: [
        {
            name: '网络延迟',
            type: 'line',
            smooth: true,
            symbol: 'none',
            sampling: 'average',
            itemStyle: {
                color: 'rgb(255, 70, 131)'
            },
            areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: 'rgb(255, 158, 68)'
                }, {
                    offset: 1,
                    color: 'rgb(255, 70, 131)'
                }])
            },
            data: data
        }
    ]
};

相关文章

网友评论

      本文标题:aws防ping服务器网络延迟测试与曲线图生成分析

      本文链接:https://www.haomeiwen.com/subject/rwbrtktx.html