-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_io.py
More file actions
123 lines (99 loc) · 4.07 KB
/
Copy pathtest_io.py
File metadata and controls
123 lines (99 loc) · 4.07 KB
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
import os
import errno
import datetime
import json
import re
import logging
class TestIO(object):
def __init__(self):
self.rootDir = 'Not Defined'
self.logEnable = False
#self.metrics_entry = {}
self.output_list = []
self.bwOutput_list = []
def dateStr(self):
dt = datetime.datetime.now()
return str(dt.year) + '_' + str(dt.month) + '_' + str(dt.day)
def timeStamp(self):
dt = datetime.datetime.now()
return str(dt.day) + '_' + str(dt.hour) + '_' + str(dt.minute)
def readConfig(self):
path = os.path.dirname(os.path.realpath(__file__))
try:
data = json.loads(open(path + "/" +"CONFIG.json").read())
except:
logging.info('No CONIFG file is found, exiting')
exit(1)
self.rootDir = data["rootDir"]
if data["log"] == "enable":
self.logEnable = True
for e in data["CFM output"]:
self.output_list.append(e)
for bw in data["BW output"]:
self.bwOutput_list.append(bw)
logging.info("output format: " + ','.join(self.output_list))
def checkEnvironment(self):
if not os.path.exists(self.rootDir):
logging.info('creating rootDir' + self.rootDir)
os.makedirs(self.rootDir)
#check host file
if not os.path.exists(self.rootDir + "/hosts"):
logging.info('Cannot find the hosts file, terminating')
exit(1)
#check account file
if not os.path.exists(self.rootDir +"/accounts"):
logging.info('Cannot find the accounts file, terminating')
exit(1)
print("checking Environment completed")
def reportFiles(self, ip, cpe):
files = {}
if not os.path.exists(self.rootDir +'/' + ip):
print('creating rootDir' + self.rootDir)
os.makedirs(self.rootDir +'/' + ip)
if not os.path.exists(self.rootDir +'/' + ip + '/' + cpe):
os.makedirs(self.rootDir +'/' + ip + '/' + cpe)
files["latest"] = open(self.rootDir +'/' + ip + '/' + cpe + '/cfmreport', 'w+')
#files["daily"] = open(self.rootDir +'/' + ip + '/' + cpe + '/report_' + self.dateStr(), 'a+')
return files
def createReports(self, **testMetrics):
#open the report files
files = self.reportFiles(testMetrics['ip'], testMetrics['cpe'])
#build the line:
line = ''
for entry in self.output_list:
if entry in testMetrics:
line = line + str(testMetrics[entry]) + ','
else:
logging.info('measurement ' + entry + 'does not exist!')
line = line[:-1] + '\n'
#write the metircs to both latest report and daily report
files['latest'].write(line)
files['latest'].close()
#files['daily'].write(line)
#files['daily'].close()
#close the file and error handling
def reportBWFiles(self, ip, cpe):
files = {}
if not os.path.exists(self.rootDir +'/' + ip):
print('creating rootDir' + self.rootDir)
os.makedirs(self.rootDir +'/' + ip)
if not os.path.exists(self.rootDir +'/' + ip + '/' + cpe):
os.makedirs(self.rootDir +'/' + ip + '/' + cpe)
files["bwr"] = open(self.rootDir +'/' + ip + '/' + cpe + '/bandwidth', 'w+')
#files["daily"] = open(self.rootDir +'/' + ip + '/' + cpe + '/report_' + self.dateStr(), 'a+')
return files
def createBWReports(self, **testMetrics):
#open the report files
files = self.reportBWFiles(testMetrics['ip'], testMetrics['cpe'])
#build the line:
line = ''
for entry in self.bwOutput_list:
if entry in testMetrics:
line = line + str(testMetrics[entry]) + ','
else:
logging.info('bw measurement ' + entry + 'does not exist!')
line = line[:-1] + '\n'
#write the metircs to both latest report and daily report
files["bwr"].write(line)
files["bwr"].close()
#close the file and error handling