-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_check_v2.py
240 lines (193 loc) · 10.5 KB
/
time_check_v2.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import datetime
import calendar
import time
import xml_check_v2
import subprocess
import shlex
def make_xml_2(start, end):
args = "/usr/local/rrdtool/bin/rrdtool xport --start " + str(start) + " --end " + str(end) + " DEF:test='/usr/local/cacti-0.8.8a/rra/10lan-testnw-01_traffic_in_82726.rrd':'traffic_in':AVERAGE CDEF:test2='test,8,*' XPORT:test2:'test is '"
seperator = ','
seperator.join(args)
args = shlex.split(args)
output = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
return output
#return args #-> TEST
def make_xml(ip_name, start=0, end=0):
start_point = "/usr/local/rrdtool/bin/rrdtool xport --start " + str(start) + " --end " + str(end)
if(ip_name=="l1"):
# print "L1!!!!"
l1_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game-new_traffic_in_20122.rrd':'traffic_in':AVERAGE"
l1_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game-new_traffic_in_20502.rrd':'traffic_in':AVERAGE"
l1_3 = " CDEF:c='a,b,+'"
l1_4 = " CDEF:result='c,8,*'"
l1_5 = " XPORT:result:'Result is '"
# print start_point + l1_1 + l1_2 + l1_3 + l1_4 + l1_5
args = start_point + l1_1 + l1_2 + l1_3 + l1_4 + l1_5
elif(ip_name=="l2"):
# print "L2!!!!"
l2_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game-new_traffic_in_20124.rrd':'traffic_in':AVERAGE"
l2_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game-new_traffic_in_20504.rrd':'traffic_in':AVERAGE"
l2_3 = " CDEF:c='a,b,+'"
l2_4 = " CDEF:result='c,8,*'"
l2_5 = " XPORT:result:'Result is '"
# print start_point + l2_1 + l2_2 + l2_3 + l2_4 + l2_5
args = start_point + l2_1 + l2_2 + l2_3 + l2_4 + l2_5
elif(ip_name=="aion"):
# print "AION!!!"
aion_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game-new_traffic_in_20114.rrd':'traffic_in':AVERAGE"
aion_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game-new_traffic_in_20117.rrd':'traffic_in':AVERAGE"
aion_3 = " DEF:c='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game-new_traffic_in_20494.rrd':'traffic_in':AVERAGE"
aion_4 = " DEF:d='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game-new_traffic_in_20497.rrd':'traffic_in':AVERAGE"
aion_5 = " CDEF:e='a,b,+,c,d,+,+'"
aion_6 = " CDEF:result='e,8,*'"
aion_7 = " XPORT:result:'Result is '"
# print start_point + aion_1 + aion_2 + aion_3 + aion_4 + aion_5 + aion_6 + aion_7
args = start_point + aion_1 + aion_2 + aion_3 + aion_4 + aion_5 + aion_6 + aion_7
elif(ip_name=="bs"):
# print "B&S!!!"
bs_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game-new_traffic_in_20107.rrd':'traffic_in':AVERAGE"
bs_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game-new_traffic_in_20110.rrd':'traffic_in':AVERAGE"
bs_3 = " DEF:c='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game-new_traffic_in_20487.rrd':'traffic_in':AVERAGE"
bs_4 = " DEF:d='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game-new_traffic_in_20490.rrd':'traffic_in':AVERAGE"
bs_5 = " CDEF:e='a,b,+,c,d,+,+'"
bs_6 = " CDEF:result='e,8,*'"
bs_7 = " XPORT:result:'Result is '"
# print start_point+bs_1+bs_2+bs_3+bs_4+bs_5+bs_6+bs_7
args = start_point+bs_1+bs_2+bs_3+bs_4+bs_5+bs_6+bs_7
elif(ip_name=="lm"):
# print "LM!!!!"
lm_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-n7k-1-game_201511_traffic_in_62895.rrd':'traffic_in':AVERAGE"
lm_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-n7k-2-game_201511_traffic_in_62513.rrd':'traffic_in':AVERAGE"
lm_3 = " CDEF:c='a,b,+'"
lm_4 = " CDEF:result='c,8,*'"
lm_5 = " XPORT:result:'Result is '"
# print start_point+lm_1+lm_2+lm_3+lm_4+lm_5
args = start_point+lm_1+lm_2+lm_3+lm_4+lm_5
elif(ip_name=="plaync"):
#print "ETC!!!!!!"
nc_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-gpgame-new_traffic_in_41846.rrd':'traffic_in':AVERAGE"
nc_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-gpgame-new_traffic_in_41847.rrd':'traffic_in':AVERAGE"
nc_3 = " CDEF:c='a,b,+'"
nc_4 = " CDEF:result='c,8,*'"
nc_5 = " XPORT:result:'Result is '"
# print start_point+lm_1+lm_2+lm_3+lm_4+lm_5
args = start_point+nc_1+nc_2+nc_3+nc_4+nc_5
#args =
elif(ip_name=="web"):
#print "WEB!!!!!!!"
web_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-n7k-1-web-new_traffic_in_22360.rrd':'traffic_in':AVERAGE"
web_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-n7k-2-web-new_traffic_in_22210.rrd':'traffic_in':AVERAGE"
web_3 = " CDEF:c='a,b,+'"
web_4 = " CDEF:result='c,8,*'"
web_5 = " XPORT:result:'Result is '"
# print start_point+lm_1+lm_2+lm_3+lm_4+lm_5
args = start_point+web_1+web_2+web_3+web_4+web_5
elif(ip_name=="talk"):
#print "TALK!!!!!"
talk_1 = " DEF:a='/usr/local/cacti-0.8.8a/rra/public-nctalk-new_traffic_in_22071.rrd':'traffic_out':AVERAGE"
talk_2 = " DEF:b='/usr/local/cacti-0.8.8a/rra/public-nctalk-new_traffic_in_22072.rrd':'traffic_out':AVERAGE"
talk_3 = " DEF:c='/usr/local/cacti-0.8.8a/rra/public-nctalk-new_traffic_in_22073.rrd':'traffic_out':AVERAGE"
talk_4 = " DEF:d='/usr/local/cacti-0.8.8a/rra/public-nctalk-new_traffic_in_22074.rrd':'traffic_out':AVERAGE"
talk_5 = " DEF:e='/usr/local/cacti-0.8.8a/rra/public-nctalk-new_traffic_in_22075.rrd':'traffic_out':AVERAGE"
talk_6 = " DEF:f='/usr/local/cacti-0.8.8a/rra/public-nctalk-new_traffic_in_22076.rrd':'traffic_out':AVERAGE"
talk_7 = " CDEF:g='a,b,+,c,d,+,+,e,f,+,+'"
talk_8 = " CDEF:result='g,8,*'"
talk_9 = " XPORT:result:'Result is '"
args = start_point + talk_1 + talk_2 + talk_3 + talk_4 + talk_5 + talk_6 + talk_7 + talk_8 + talk_9
#args =
# elif(ip_name=="rk"):
# print "Lineage RK!"
else:
print "Input should be L1, L2, AION, BS, LM, MOBILE, WEB, RK"
# args = start_point + " DEF:test='/usr/local/cacti-0.8.8a/rra/10lan-testnw-01_traffic_in_82726.rrd':'traffic_in':AVERAGE CDEF:test2='test,8,*' XPORT:test2:'test is '"
seperator = ','
seperator.join(args)
args = shlex.split(args)
# #
output = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
return output
# return 0
def time_convert(time_str):
# if(n_month=='10' or n_month=='11' or n_month=='12'):
# n_month = n_month
# else:
time_format = "%Y-%m-%d %H:%M:%S"
#time_str = n_year+"-"+n_month+"-"+n_day+" 00:00:00"
#print(time_str)
if(type(time_str) == datetime.datetime):
n_time = time_str
else:
n_time = datetime.datetime(*time.strptime(time_str, time_format)[:6])
#n_time = datetime.datetime.strptime(time_str, time_format) - version 3
time_tuple = n_time.timetuple()
utc_time = time.mktime(time_tuple)
utc_time = int(utc_time)
#print (utc_time)
#print "UTC TIME IS : " , utc_time
return utc_time
def time_division(ip_name, t_start, t_end):
day_array = []
# print "TIME _ DIVISION - START : " + t_start
# print "TIME _ DIVISION - END : " + t_end
time_format = "%Y-%m-%d %H:%M:%S"
if(time_convert(t_start) >= time_convert(t_end)):
print ("Start time should be earlier than End time")
return -1
print "=====================" + ip_name + "============================="
#start = datetime.strptime(t_start, time_format) - version3
start = datetime.datetime(*time.strptime(t_start, time_format)[:6]) #version2
start_day = start.day
#end = datetime.strptime(t_end, time_format) - version3
end = datetime.datetime(*time.strptime(t_end, time_format)[:6])
end_day = end.day
for a in range(start_day, end_day+1):
start = start.replace(day=a)
start_finish = start.replace(day=a)
if(a != start_day):
start = start.replace(hour=00, minute=00, second=00)
if(a == end_day):
start_finish = start.replace(hour = end.time().hour, minute = end.time().minute, second = end.time().second)
else:
start_finish = start.replace(hour=23, minute=59, second=59)
# print a, ": ", start, " | ", start_finish
xml_result = make_xml(ip_name, time_convert(start), time_convert(start_finish))
t_value, d_value = xml_check_v2.find_TV(xml_result)
temp_max = xml_check_v2.day_max(d_value)
# print "Maximum in one day is :", xml_check_v2.day_max(d_value)
#day_array.append(d_value) #For getting day's average.
day_array.append(temp_max)
# print day_array
avg_result, avg_result2 = xml_check_v2.get_average(day_array)
# print "Average is : ", avg_result
return avg_result, avg_result2
if __name__=="__main__":
print("Make XML from CACTI. Input 'Start, End' (24-hour-clock)")
#ip_name = input("Input Game Name to Extract XML from CACTI (L1, L2, AION, BS, LM ... ) : ")
# ip_name = raw_input("Input Game Name to Extract XML from CACTI (L1, L2, AION, BS, LM ... ) : ") # -> version2
# ip_name = ip_name.lower()
start_date = raw_input("Input START Time(YYYY-MM-DD HH:MM:SS) : ") #version 2
end_date = raw_input("Input END Time(YYYY-MM-DD HH:MM:SS) : ")
# start_date = input("Input From Time(YYYY-MM-DD HH:MM:SS) : ") - version 3
# end_date = input("Input From Time(YYYY-MM-DD HH:MM:SS) : ")
#print(start_date.month)
#print(start_date.day)
#print ("UTC START TIME:", time_convert(start_date))
#print ("UTC END TIME:", time_convert(end_date))
maximum=0
a, a_1 = time_division("l1", start_date, end_date)
b, b_1 = time_division("l2", start_date, end_date)
c, c_1 = time_division("lm", start_date, end_date)
d, d_1 = time_division("aion", start_date, end_date)
e, e_1 = time_division("bs", start_date, end_date)
f, f_1 = time_division("talk", start_date, end_date)
g, g_1 = time_division("web", start_date, end_date)
h, h_1 = time_division("plaync", start_date, end_date)
# time_division("rk", start_date, end_date) # For Lineage RK
print "\n\n\n\n"
maximum = a_1+b_1+c_1+d_1+e_1+f_1+g_1+h_1
print maximum
# __array_file = 'rrdtool_xport.txt'
# a, b = xml_check.find_TV(__array_file)
#
# print("Maximum in one day is : ", xml_check.day_max(b))