-
Notifications
You must be signed in to change notification settings - Fork 107
/
run.py
294 lines (264 loc) · 10 KB
/
run.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
# -*- coding: utf8 -*-
import os,sys,logging
from multiprocessing import Process, Queue, current_process
from pymongo import MongoClient
#from pymongo import MongoClient
import datetime
import yaml
import urllib2
import json
import time
from datadiff import diff
cur_dir = os.path.dirname(os.path.abspath(__file__))
settings = {
'config': os.path.join(cur_dir,"config.yaml"),
'logfile': os.path.join(cur_dir,"log","system.log"),
'mongodb': 'localhost',
'mongodb_port': 27017,
'db': 'LvsMonitor',
}
try:
logging.basicConfig(level=logging.NOTSET,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=settings['logfile'],
filemode='a')
logging = logging.getLogger()
#st = logging.StreamHandler()
#st.setLevel(logging.INFO)
#st.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s %(message)s'))
#logging.addHandler(st)
except AttributeError as err:
print("Error: %s %s" %(err, settings['logfile']))
sys.exit(2)
class MongoSession():
def __init__(self):
self.conn = MongoClient(settings['mongodb'],settings['mongodb_port'])
#self.conn = MongoClient(settings['mongodb'],settings['mongodb_port'],auto_start_request=True)
self.db = self.conn[settings['db']]
def close(self):
try:
self.conn.end_request()
self.conn.close()
logging.info('close mongo connection Success !')
except Exception, e:
logging.warning('close mongo connection fail !')
def insert(self,id,data_type,data):
_time = time.time()
collection = self.db[data_type]
insert_data = {"id":id,"time":_time,"data": data}
try:
_id = collection.insert(insert_data)
self.conn.end_request()
logging.info('Insert data (ID:%s) Success !' % id)
return id
except Exception, e:
return e
def insert_demo(self,data_type,insert_data):
_time = time.time()
collection = self.db[data_type]
try:
_id = collection.insert(insert_data)
self.conn.end_request()
logging.info('Method: Insert | table: %s | result: Success ' % (data_type))
return id
except Exception, e:
logging.warning('Method: Insert | table: %s | result: %s' % (data_type,e))
def upsert(self,data_type,find,update):
collection = self.db[data_type]
try:
_id = collection.update(find,update,True)
self.conn.end_request()
logging.info('Method: Upsert | table: %s | result: Success ' % (data_type))
return id
except Exception, e:
logging.warning('Method: Upsert | table: %s | result: %s' % (data_type,e))
def update(self,data_type,find,update):
collection = self.db[data_type]
try:
_id = collection.update(find,update)
self.conn.end_request()
logging.info('Method: Update | table: %s | result: Success ' % (data_type))
return id
except Exception, e:
logging.warning('Method: Update | table: %s | result: %s' % (data_type,e))
def find_one(self,data_type,find):
collection = self.db[data_type]
try:
_id = collection.find_one(find)
self.conn.end_request()
logging.info('Method: FindOne | table: %s | result: Success ' % (data_type))
return _id
except Exception, e:
logging.warning('Method: FindOne | table: %s | result: %s' % (data_type,e))
def getmonitordata(agent,port,data_type):
url = 'http://%s:%s/node/%s/' % (agent,port,data_type)
fails = 0
retry = 5
while True:
try:
if fails > retry:
logging.warning('Get Monitor Data Failed | %s' % url)
return None
res = urllib2.urlopen(url, timeout=2)
return json.loads(res.read())
except Exception, e:
fails += 1
else:
logging.info('Get Monitor Data Success | %s ' % url)
break
def getlvstrffic(agent_ip,port):
url = 'http://%s:%s/node/GetLvsTraffic/' % (agent_ip,port)
fails = 0
retry = 5
while True:
try:
if fails > retry:
logging.warning('Get Monitor Data Failed | %s' % url)
return None
res = urllib2.urlopen(url, timeout=2)
return json.loads(res.read())
except Exception, e:
fails += 1
else:
logging.info('Get Monitor Data Success | %s ' % url)
break
def lvstraffic_handler(id,agent_ip,agent_port,cluster,_time):
mongo_task = MongoSession()
sum_dict = {}
inpkts_sum = 0
outpkts_sum = 0
inbytes_sum = 0
outbytes_sum = 0
conns_sum = 0
data = getlvstrffic(agent_ip,agent_port)
for vip_dict in data:
vip_dict["id"] = id
vip_dict["time"] = _time
inpkts_sum += vip_dict['inpkts_sum_per']
outpkts_sum += vip_dict['outpkts_sum_per']
inbytes_sum += vip_dict['inbytes_sum_per']
outbytes_sum += vip_dict['outbytes_sum_per']
conns_sum += vip_dict['conns_sum_per']
vip_dict["cluster"] = cluster
results = mongo_task.insert_demo('GetLvsTraffic_demo',vip_dict)
find = {"cluster":cluster,"time":_time,"vip":vip_dict["vip"]}
update = update = {"$inc": {"inpkts_sum_per":vip_dict["inpkts_sum_per"],"outpkts_sum_per":vip_dict["outpkts_sum_per"] ,"inbytes_sum_per":vip_dict["inbytes_sum_per"],"outbytes_sum_per":vip_dict["outbytes_sum_per"],"conns_sum_per":vip_dict["conns_sum_per"]}}
mongo_task.upsert('GetLvsTraffic_cluster',find,update)
sum_dict["inpkts_sum"] = inpkts_sum
sum_dict["outpkts_sum"] = outpkts_sum
sum_dict["inbytes_sum"] = inbytes_sum
sum_dict["outbytes_sum"] = outbytes_sum
sum_dict["conns_sum"] = conns_sum
sum_dict["time"] = _time
sum_dict["id"] = id
sum_dict["cluster"] = cluster
mongo_task.insert_demo('GetLvsTraffic_sum',sum_dict)
find = {"cluster":cluster,"time":_time}
update = {"$inc": {"inpkts_sum":inpkts_sum ,"outpkts_sum":outpkts_sum ,"inbytes_sum":inbytes_sum,"outbytes_sum":outbytes_sum,"conns_sum":conns_sum}}
mongo_task.upsert('GetLvsTraffic_cluster_sum',find,update)
mongo_task.close()
return None
def getlvsstatus(agent_ip,port):
url = 'http://%s:%s/node/GetLvsStatus/' % (agent_ip,port)
fails = 0
retry = 5
while True:
try:
if fails > retry:
logging.warning('Get Monitor Data Failed | %s' % url)
return None
res = urllib2.urlopen(url, timeout=2)
return json.loads(res.read())
except Exception, e:
fails += 1
else:
logging.info('Get Monitor Data Success | %s ' % url)
def lvsstatus_handler(id,agent_ip,agent_port,cluster,_time):
mongo_task = MongoSession()
data = getlvsstatus(agent_ip,agent_port)
last_data = mongo_task.find_one('GetLvsConn_status',{"id":id})
now_status = data
if last_data:
last_status = last_data['node']
_diff = diff(now_status,last_status)
if _diff:
dict_diff = {"id":id,"time":_time,"diff":str(_diff),"cluster":cluster}
mongo_task.insert_demo('GetLvsConn_diff',dict_diff)
find = {"id":id}
dict = {"id":id,"time":_time,"cluster":cluster,"node":data}
mongo_task.upsert('GetLvsConn_status',find,dict)
mongo_task.close()
def worker(input, output):
for func in iter(input.get,'STOP'):
#解析出config.yaml里面的agent
agent_dict = func['agent']
pid = os.getpid()
logging.info('tasks.get | pid: %s tasks: %s is connection' %(pid,agent_dict['id']))
_time = func['time']
id = agent_dict['id']
agent = agent_dict['ipadd']
port = agent_dict['port']
cluster = agent_dict['cluster']
#for data_type in agent_dict['data_type']:
# #data = getmonitordata(agent,port,data_type)
# #mongo_task = MongoSession()
# #results = mongo_task.insert(id,data_type,data)
# #mongo_task.close()
# output.put('ok')
_timenow = time.time()
lvstraffic_handler(id,agent,port,cluster,_time)
lvsstatus_handler(id,agent,port,cluster,_time)
_timenow1 = time.time() - _timenow
logging.warning(_timenow1)
#for i in range(50):
# mongo_task = MongoSession()
# mongo_task.insert_demo('Test',{"test":1})
# time.sleep(1)
# mongo_task.close()
# output.put('ok')
def main():
logging.info('##################Task Begining##################')
#load config
try:
config = yaml.load(open(settings['config']))
logging.info('load config.yaml Success')
except Exception, e:
print 'load config error!!'
#进程数
#NUMBER_OF_PROCESSES = config['base']['NUMBER_OF_PROCESSES']
#agent 列表
agent_list = config['agent']
# Create queues 消息用于进程通信
task_queue = Queue()
done_queue = Queue()
#agent有多少就开启多少个进程数
NUMBER_OF_PROCESSES = len(agent_list)
_time = time.time()
logging.info('Agentlist: %s' % [a['id'] for a in agent_list])
#submit tasks
for agent in agent_list:
dict = {}
dict["time"] = _time
dict["agent"] = agent
task_queue.put(dict)
logging.info('tasks.put | tasks: %s ' %(agent['id']))
#进程开启
record = []
for i in range(NUMBER_OF_PROCESSES):
process = Process(target=worker, args=(task_queue, done_queue))
process.start()
record.append(process)
#打印结果
#for i in range(len(agent_list)):
# logging.info(done_queue.get())
#告诉子进程要结束了
for i in range(NUMBER_OF_PROCESSES):
task_queue.put('STOP')
logging.info("Stopping Process #%s" % i)
#告诉父进程要结束了
for process in record:
process.join()
if __name__ == "__main__":
main()