怎么測試到amazonaws全球各可用區(qū)域時(shí)延
2022-08-04 10:51:14 - 米境通跨境電商
因?yàn)樽罱l(fā)現(xiàn)aws服務(wù)器網(wǎng)絡(luò)不是特別穩(wěn)定,所以想監(jiān)控一下網(wǎng)絡(luò)的聯(lián)通性和延遲。但是發(fā)現(xiàn)并沒有可以直接用的工具。
搭建了smartping,worldping,smokeping這三個(gè)工具發(fā)現(xiàn)都不能使用
于是就使用tcpping來不斷的將日志保存到本地文件,然后使用python解析數(shù)據(jù)生成自己需要的數(shù)據(jù),放到echarts里面來展示。能夠達(dá)到和上述的三種工具的相同效果。
部分效果:
image.png
日志默認(rèn)輸出格式
2020年06月07日星期日00:56:13CST
tracerouteto54.189.110.117(54.189.110.117),255hopsmax,60bytepackets
seq119049:tcpresponsefromec2-54-189-110-117.us-west-2.compute.amazonaws.com(54.189.110.117)
186.962ms
2020年06月07日星期日00:56:14CST
tracerouteto54.189.110.117(54.189.110.117),255hopsmax,60bytepackets
seq119050:tcpresponsefromec2-54-189-110-117.us-west-2.compute.amazonaws.com(54.189.110.117)183.514ms
2020年06月07日星期日00:56:15CST
tracerouteto54.189.110.117(54.189.110.117),255hopsmax,60bytepackets
seq119051:tcpresponsefromec2-54-189-110-117.us-west-2.compute.amazonaws.com(54.189.110.117)183.347ms
2020年06月07日星期日00:56:16CST
python3日志數(shù)據(jù)分析代碼
importtime
"""
日期轉(zhuǎn)時(shí)間戳
"""
defunix_time(dt):
#轉(zhuǎn)換成時(shí)間數(shù)組
timeArray=time.strptime(dt,"%Y年%m月%d日%H:%M:%S")
#轉(zhuǎn)換成時(shí)間戳
timestamp=int(time.mktime(timeArray))
returntimestamp
"""
時(shí)間戳轉(zhuǎn)日期
"""
defcustom_time(timestamp):
#轉(zhuǎn)換成localtime
time_local=time.localtime(timestamp)
#轉(zhuǎn)換成新的時(shí)間格式(2016-05-0520:28:54)
dt=time.strftime("%Y/%m/%d%H:%M:%S",time_local)
returndt
"""
輸出日志結(jié)果
"""
defdata_handle(date_key,data_dict):
date_key_sorted=sorted(date_key)
date=[]
data=[]
forkeyindate_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
forlineinfile:
ifline.find("CST")!=-1:
#時(shí)間格式轉(zhuǎn)換
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))
ifline.find("seq")!=-1:
line_list=line.split("")
iflen(line_list)<12andline.find("timeout")!=-1:
delay=0
else:
delay=float(line_list[9])
#收集延遲大于500和超時(shí)的日志信息
ifdelay==0ordelay>500:
date_key.append(time_str)
data_dict[time_str]=delay
time_str=""
delay=0
data_handle(date_key,data_dict)
#echarhttps://echarts.apache.org/examples/zh/editor.html?c=area-simple數(shù)據(jù)分析
echars數(shù)據(jù)展示地址:
https://echarts.apache.org/examples/zh/editor.html
只需要將輸出的data和date放到正確的位置就會自動渲染數(shù)據(jù)
demo:
varbase=+newDate(1968,9,3);
varoneDay=24*3600*1000;
vardate=[];
vardata=[Math.random()*300];
//for(vari=1;i<20000;i++){
//varnow=newDate(base+=oneDay);
//date.push([now.getFullYear(),now.getMonth()+1,now.getDate()].join('/'));
//data.push(Math.round((Math.random()-0.5)*20+data[i-1]));
//}
vardata=[1201.895,1201.895,120.895,1201.895,12.895,1201.895,120.895,1201.895,12.895,1201.895]
vardate=['2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15','2020/06/0515:17:15']
option={
tooltip:{
trigger:'axis',
position:function(pt){
return[pt[0],'10%'];
}
},
title:{
left:'center',
text:'大數(shù)據(jù)量面積圖',
},
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.9zM13.3,24.4H6.7V23h6.6V24.4zM13.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:'網(wǎng)絡(luò)延遲',
type:'line',
smooth:true,
symbol:'none',
sampling:'average',
itemStyle:{
color:'rgb(255,70,131)'
},
areaStyle:{
color:newecharts.graphic.LinearGradient(0,0,0,1,[{
offset:0,
color:'rgb(255,158,68)'
},{
offset:1,
color:'rgb(255,70,131)'
}])
},
data:data
}
]
};