-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_query.py
135 lines (119 loc) · 4.23 KB
/
system_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
BASE_DIRECTORY="/var/lib/collectd/csv/"
CPU_HISTORY=10
class SystemInfo:
def __init__(self,system_name):
self.base_folder=BASE_DIRECTORY+system_name
self.prev_op_r_sum=0
self.prev_op_r_avgcount=0
self.prev_op_sum=0
self.prev_op_avgcount=0
self.prev_op_w_avgcount=0
self.prev_op_w_sum=0
def read(self,file_name):
f = open(file_name, 'rU')
for line in f:
fields=line.split(",")
try:
float(fields[0])
except ValueError:
continue
return fields
def get_total_cpu(self):
if(os.path.isdir(self.base_folder)):
folders=os.walk(self.base_folder).next()[1]
else:
print "No folder exists "+self.base_folder
return -1
cpus=[x for x in folders if x.find('cpu') != -1]
print "Total no of cpus ",len(cpus)
return len(cpus)
def get_load(self):
folder_name=self.base_folder+"/load/"
if(os.path.isdir(folder_name)):
files=os.walk(folder_name).next()[2]
else:
print "No folder exists "+folder_name
return []
abs_path=[folder_name+x for x in files]
latest_file = max(abs_path, key=os.path.getmtime)
values=self.read(latest_file)
#print "Avg in last 1 min ",values[1]
#print "Avg in last 5 min ",values[2]
#print "Avg in last 10 min ",values[3]
result_set=[]
for val in values:
result_set.append(float(val))
return result_set
def get_latest_file(self,folder_name,filter_name):
if(os.path.isdir(folder_name)):
files=os.walk(folder_name).next()[2]
else:
print "No folder exists "+folder_name
return []
filtered_files= [x for x in files if x.find(filter_name) != -1]
abs_path=[folder_name+x for x in filtered_files]
latest_file = max(abs_path, key=os.path.getmtime)
return latest_file
def get_osd_latency(self,osd_id):
folder_name=self.base_folder+"/ceph-ceph-"+str(osd_id)+"/"
latest_file_avgcount= self.get_latest_file(folder_name,'op_latency-avgcount')
latest_file_sum= self.get_latest_file(folder_name,'op_latency-sum')
latest_file_r_avgcount= self.get_latest_file(folder_name,'op_r_latency-avgcount')
latest_file_r_sum= self.get_latest_file(folder_name,'op_r_latency-sum')
latest_file_w_avgcount= self.get_latest_file(folder_name,'op_w_latency-avgcount')
latest_file_w_sum= self.get_latest_file(folder_name,'op_w_latency-sum')
result_set=[]
value_avgcount=float((self.read(latest_file_avgcount))[1])
value_sum=float((self.read(latest_file_sum))[1])
temp_avgcount=value_avgcount
temp_sum = value_sum
value_avgcount= value_avgcount - self.prev_op_avgcount
value_sum = value_sum - self.prev_op_sum
self.prev_op_sum = temp_sum
self.prev_op_avgcount = temp_avgcount
#print "Overall count value",value_avgcount
#print "Overall sum value",value_sum
if value_avgcount == 0:
latency = 0
else:
latency = value_sum/value_avgcount
result_set.append(latency)
#print "Overall latency ",latency
value_avgcount=float((self.read(latest_file_r_avgcount))[1])
value_sum=float((self.read(latest_file_r_sum))[1])
temp_avgcount=value_avgcount
temp_sum = value_sum
#print "Read count value",value_avgcount
#print "Read sum value",value_sum
value_avgcount= value_avgcount - self.prev_op_r_avgcount
value_sum = value_sum - self.prev_op_r_sum
self.prev_op_r_sum = temp_sum
self.prev_op_r_avgcount = temp_avgcount
if value_avgcount == 0:
latency = 0
else:
latency = value_sum/value_avgcount
result_set.append(latency)
#print "Read latency ",latency
value_avgcount=float((self.read(latest_file_w_avgcount))[1])
value_sum=float((self.read(latest_file_w_sum))[1])
temp_avgcount=value_avgcount
temp_sum = value_sum
#print "Write count value",value_avgcount
#print "Write sum value",value_sum
value_avgcount= value_avgcount - self.prev_op_w_avgcount
value_sum = value_sum - self.prev_op_w_sum
self.prev_op_w_sum = temp_sum
self.prev_op_w_avgcount = temp_avgcount
if value_avgcount == 0:
latency = 0
else:
latency = value_sum/value_avgcount
result_set.append(latency)
#print "Write latency ",latency
return result_set
if __name__ == "__main__":
info=SystemInfo("bd-1-3.ubuntu")
print info.get_total_cpu()
print info.get_load()