2016-11-09 18:06:05 i-642gg331t 0 37.064 0.353 62.503 0.080 5131751952
2016-11-09 18:06:05 i-642gg331t 1 36.490 0.431 63.002 0.077 5131751952
2016-11-09 18:06:05 i-642gg331t 2 42.522 0.432 56.661 0.385 5131751952
2016-11-09 18:06:05 i-642gg331t 3 55.079 0.351 43.491 1.078 5131751952
2016-11-09 18:06:05 i-641c7vsd7 0 34.191 0.730 64.914 0.165 5131751952
2016-11-09 18:06:05 i-641c7vsd7 1 58.079 0.548 41.294 0.079 5131751952
2016-11-09 18:06:05 i-641c7vsd7 2 28.740 0.799 69.563 0.898 5131751952
2016-11-09 18:06:05 i-641c7vsd7 3 33.892 1.298 64.709 0.101 5131751952
2016-11-09 18:06:10 i-642gg331t 0 47.942 0.249 51.711 0.098 5205043312
2016-11-09 18:06:10 i-642gg331t 1 24.982 0.422 74.525 0.071 5205043312
2016-11-09 18:06:10 i-642gg331t 2 23.231 0.442 76.261 0.067 5205043312
2016-11-09 18:06:10 i-642gg331t 3 40.668 0.339 58.921 0.072 5205043312
{
"i-642gg331t":
{
"2016-11-09 18:06:05":
{
"0":[37.064,0.353,62.503,0.080],
"1":[36.490,0.431,63.002,0.077],
"2":[42.522,0.432,56.661,0.385],
"3":[55.079,0.351,43.491,1.078]
},
"2016-11-09 18:06:10":
{
"0":[47.942,0.249,51.711,0.098],
"1":[24.982,0.422,74.525,0.071],
"2":[23.231,0.442,76.261,0.067],
"3":[40.668,0.339,58.921,0.072]
},
},
"i-642gg331t":
{
"2016-11-09 18:06:05":
{
"0":[37.064,0.353,62.503,0.080],
"1":[36.490,0.431,63.002,0.077],
"2":[42.522,0.432,56.661,0.385],
"3":[55.079,0.351,43.491,1.078]
}
}
}
from collections import defaultdict
def get_minute_perf_data(perf_data):
"""
输出当前最近一分钟时间性能
minute_perf_data 是一个四重嵌套列表的字典:
字典的索引是虚拟机名字
字典的索引是采样时间
字典的索引是vm的vcpu编号
列表内容是每个vm的每个采样时间的每个vcpu的性能值(user,steal,idle,offline)
"""
minute_perf_data = defaultdict(dict)
vm_time_perf_data = defaultdict(dict)
time_vcpu_perf_data = defaultdict(list)
with open(perf_data,'r') as f:
debug_count=0
for line in f:
items = line.split()
vm_name = items[2]
print "vm_name: %s" % vm_name
time_str = items[1].split(':')[2]
print "time_str: %s" % time_str
vcpu_str = items[3]
print "vcpu_str: %s" % vcpu_str
vcpu_perf_data = []
for i in range(4,8):
vcpu_perf_data.append(float(items[i]))
print "vcpu_perf_data: %s" % vcpu_perf_data
time_vcpu_perf_data[vcpu_str] = vcpu_perf_data
print "time_vcpu_perf_data: %s" % time_vcpu_perf_data
vm_time_perf_data[time_str] = time_vcpu_perf_data
print "vm_time_perf_data: %s" % vm_time_perf_data
minute_perf_data[vm_name] = vm_time_perf_data
print "minute_perf_data: %s" % minute_perf_data
print minute_perf_data
print "=============================="
return minute_perf_data
from collections import defaultdict
def get_minute_perf_data(perf_data):
minute_perf_data = defaultdict(dict)
vm_time_perf_data = defaultdict(dict)
time_vcpu_perf_data = defaultdict(list)
vcpu_perf_data = []
vm_name = ""
time_str = ""
vcpu_str = ""
minute_perf_data[vm_name] = vm_time_perf_data
vm_time_perf_data[time_str] = time_vcpu_perf_data
time_vcpu_perf_data[vcpu_str] = vcpu_perf_data
with open(perf_data,'r') as f:
for line in f:
items = line.split()
vm_name = items[2]
print "vm_name: %s" % vm_name
time_str = items[1].split(':')[2]
print "time_str: %s" % time_str
vcpu_str = items[3]
print "vcpu_str: %s" % vcpu_str
for i in range(4,8):
vcpu_perf_data.append(float(items[i]))
print "vcpu_perf_data: %s" % vcpu_perf_data
minute_perf_data[vm_name][time_str][vcpu_str] = vcpu_perf_data
defaultdict(<type 'dict'>, {'': defaultdict(<type 'dict'>, {'': defaultdict(<type 'list'>, {'': []})})})
vm_name: i-64bcnnqdc
time_str: 04
vcpu_str: 0
vcpu_perf_data: [27.986, 0.308, 71.114, 0.592]
Traceback (most recent call last):
File "./cpu_steal.bak1", line 313, in <module>
main()
File "./cpu_steal.bak1", line 304, in main
minute_perf_data = get_minute_perf_data(g_perf_data)
File "./cpu_steal.bak1", line 180, in get_minute_perf_data
minute_perf_data[vm_name][time_str][vcpu_str] = vcpu_perf_data
KeyError: '04'
from collections import defaultdict
def get_minute_perf_data(perf_data):
mydict = lambda: defaultdict(mydict)
minute_perf_data = mydict()
with open(perf_data,'r') as f:
for line in f:
items = line.split()
vm_name = items[2]
time_str = items[1].split(':')[2]
vcpu_str = items[3]
vcpu_perf_data = []
for i in range(4,8):
vcpu_perf_data.append(float(items[i]))
print "vcpu_perf_data: %s" % vcpu_perf_data
minute_perf_data[vm_name][time_str][vcpu_str] = vcpu_perf_data