#!/bin/python#encoding=utf-8#Author By:BenjaminYang
importcalendarimporttimeimportos
date=str(input('请输入你要查询的年月:\n(格式如:201811)\n----------------\n'))print('###############################################################################################')
year=int(date[0:4]) #数据切片取得年
month=int(date[4:6]) #数据切片取得月
total_days=calendar.monthrange(year,month)[1] #根据年月组合获取该月共有多少天
start_time=time.time()defreport_check():
i=0#定义开始时间#遍历每一天的tsar监控数据
while i
i=i+1date=str(year)+str(month)+str(0)+str(i)else:
i=i+1date=str(year)+str(month)+str(i)
os.environ['date']=date#将获取到的数据写入文件
cpu=os.popen("tsar -d $date|grep MEAN|awk '{print $2}'").read()
mem=os.popen("tsar -d $date|grep MEAN|awk '{print $3}'").read()
tcp=os.popen("tsar -d $date|grep MEAN|awk '{print $4}'").read()
bytein=os.popen("tsar -d $date|grep MEAN|awk '{print $5}'").read()
byteout=os.popen("tsar -d $date|grep MEAN|awk '{print $6}'").read()
sda=os.popen("tsar -d $date|grep MEAN|awk '{print $7}'").read()
sdb=os.popen("tsar -d $date|grep MEAN|awk '{print $8}'").read()
load=os.popen("tsar -d $date|grep MEAN|awk '{print $9}'").read()printdateprint os.popen("tsar -d $date|grep MEAN").read()
with open("data.csv","a+") as f:if cpu.strip()=='':
f.write(str(date)+','+'have null data'+'\n')else:
f.write(str(date+','+cpu+','+mem+','+tcp+','+bytein+','+byteout+','+sda+','+sdb+','+load).replace("\n","")+"\n")
os.system('>data.csv')
ag=['adsag','dockerag','ecsag','odpsag','opsag','ossag','rdsag','slbag']
with open("data.csv","a+") as f:
f.write(str('check_date'+','+'cpu'+','+'mem'+','+'tcp'+','+'bytein'+','+'byteout'+','+'sda'+','+'sdb'+','+'load').replace("\n","")+"\n")
report_check()
end_time=time.time()
total_time=end_time-start_timeprint "此次生成报表总共耗时:",total_time