def addr_delay(addr): #print type(addr) #print addr if addr['dhcp_netns'] != None: cmd = "ip netns exec %s python test_delay.py %s" % (addr['dhcp_netns'], addr['addr']) ret, info = exe(cmd) else: cmd = "python test_delay.py %s" % (addr['addr']) ret, info = exe(cmd) if ret == False: SERVERLOG.info("server.func.addr_delay - cmd:%s return error, %s." % (cmd, info)) addr['performance']['error_msg'] = "can't get delay info." else: info = info[1:-1].split(',') try: info[0] = int(info[0]) info[1] = float(info[1]) info[2] = float(info[2]) except Exception, e: print "ERROR: " + info print str(e) addr['performance']['error_msg'] = "data formate error." return else:
def append(self, task): self.mutex.acquire() if task.project in self.tasks: SERVERLOG.error( "server.Tasks.append - project-%s - req_id-%s task already in tasks." % (task.project, task.req_id)) else: SERVERLOG.info( "server.Tasks.append - project-%s - req_id-%s task append tasks." % (task.project, task.req_id)) self.tasks[task.project] = task self.num += 1 self.mutex.release()
def run(self): #print "worker is running" while True: task = self.queue.get() if isinstance(task, Task): # print "get a task type" SERVERLOG.info( "server.Worker.run - project-%s - req_id-%s get a task type:%s." % (task.project, task.req_id, "Task")) ret = task.start_task() if ret: self.tasks.append(task) self.queue.task_done() else: pass elif isinstance(task, Item): SERVERLOG.info( "server.Worker.run - project-%s - req_id-%s get a task type:%s." % (task.project, task.req_id, "Item")) self.tasks.update_task(task) self.queue.task_done() elif isinstance(task, int) and task == 1: SERVERLOG.info("server.Worker.run worker exit.") self.queue.task_done() break else: SERVERLOG.error("server.Worker.run - unkown task type:%s." % (str(type(task)))) break
def update_task(self, item): self.mutex.acquire() #print item.project #print self.tasks if item.project not in self.tasks: SERVERLOG.error( "server.Tasks.update_task - project-%s - req_id-%s task not in tasks." % (item.project, item.req_id)) else: SERVERLOG.info( "server.Tasks.update_task - project-%s - req_id-%s update task success." % (item.project, item.req_id)) self.tasks[item.project].receive_item(item) if self.tasks[item.project].is_down() == True: self.tasks[item.project].stop_task() del self.tasks[item.project] self.num -= 1 self.mutex.release()
def process_item(self, item): # process item to update receive_vm_num receive_network_num # to do SERVERLOG.info( "server.Task.process_item - project-%s - req_id-%s process received item." % (self.project, self.req_id)) info = item.info # info struct # { # "vm_num": 0, # "host": "ip_addr", # "is_network_node": False, # "topo": "topo_struct" # } self.receive_vm_num += info["vm_num"] if info["node_type"] == "network": self.receive_network_num += info["vm_num"] + 1 else: self.receive_network_num += info["vm_num"] return json.dumps(info)
def receive_item(self, item): manager = Manager() if self.item_flag == False: self.receive_time = time.time() self.item_flag = True #def receive_item(self, project_name, req_id, receive_vm_num, receive_network_num, info): item = self.process_item(item) print "item len: %s" % (len(item)) ret, msg = manager.receive_item(self.project, self.req_id, self.receive_vm_num, self.receive_network_num, item) if ret == False: SERVERLOG.error( "server.Task.receive_item - project-%s - req_id-%s manager.receive_item return error:%s" % (self.project, self.req_id, msg)) self.status = "ERROR" return False SERVERLOG.info( "server.Task.receive_item - project-%s - req_id-%s receive item success." % (self.project, self.req_id)) return True
def stop_task(self, status="END"): manager = Manager() self.stop_time = time.time() self.status = status # def get_items(self, project_name, req_id): # def stop_task(self, project_name, req_id, status, result): ret, items = manager.get_items(self.project, self.req_id) # item_info struct # item_index_map = { # 'id': 0, # 'task_id': 1, # 'receive_time': 2, # 'info': 3 # } # items struct # result = { # 'item_num': item_num, # 'item_info': [] # } if ret == False: self.status = "ERROR" SERVERLOG.error( "server.Task.stop_task - project-%s - req_id-%s manager.get_items return error:%s" % (self.project, self.req_id, items)) return False SERVERLOG.info( "server.Task.stop_task - project-%s - req_id-%s manager.get_items success:%d items" % (self.project, self.req_id, items['item_num'])) self.result = self.process_items(items) ret, msg = manager.stop_task(self.project, self.req_id, self.status, self.result) if ret == False: self.status = "ERROR" SERVERLOG.error( "server.Task.stop_task - project-%s - req_id-%s manager.stop_task return error:%s" % (self.project, self.req_id, msg)) return False SERVERLOG.info( "server.Task.stop_task - project-%s - req_id-%s task stop success." % (self.project, self.req_id)) return True
def start_task(self): manager = Manager() self.start_time = time.time() self.status = "START" # send to agent msg = { 'type': 'check', 'project': self.project, 'req_id': self.req_id, 'vm_info': self.vm_info, 'network_info': self.network_info } print "monitor project %s: server send msg to agent." % ( msg['project']) ret, msg = server_to_agent_msg(msg) if ret == False: self.status = "ERROR" SERVERLOG.error( "server.Task.start_task - project-%s - req_id-%s server_to_agent_msg return error:%s" % (self.project, self.req_id, msg)) return False #def start_task(self, project_name, req_id, start_time, network_info, vm_info, network_num, vm_num): ret, msg = manager.start_task(self.project, self.req_id, format_time(self.start_time), json.dumps(self.network_info), json.dumps(self.vm_info), self.network_num, self.vm_num) if ret == False: self.status = "ERROR" SERVERLOG.error( "server.Task.start_task - project-%s - req_id-%s manager.start_task return error:%s" % (self.project, self.req_id, msg)) return False else: SERVERLOG.info( "server.Task.start_task - project-%s - req_id-%s task start success." % (self.project, self.req_id)) return True
def callback(self, ch, method, props, body): """ body:message properties:prop.reply_to """ msg_type = method.routing_key.split(".") if len(msg_type) != 2: FALOG.error("receive msg routing_key with wrong format.") if msg_type[0] == "api_to_server": # other process if msg_type[1] == "rpc": response = { "task_type": "test", "exe_res": True, "error_msg": "" } ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id = \ props.correlation_id), body=json.dumps(response)) elif msg_type[1] == "msg": SERVERLOG.info("receive api to server msg: %s" % (body)) msg = json.loads(body) # {'type': 'check', 'req_id': msg['req_id'], 'project': msg['project_name'], 'token': msg['token']} if msg['type'] == 'check': # print "check" print "receive api to server msg: monitor project %s network." % ( msg['project']) self.worker_poll.push_task(Task(msg)) else: SERVERLOG.error( "receive api to server msg: invalid msg type %s" % (msg['type'])) else: FALOG.error( "receive msg routing_key with wrong format[part 2].") elif msg_type[0] == "agent_to_server": if msg_type[1] == "rpc": response = { "task_type": "test", "exe_res": True, "error_msg": "" } ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id = \ props.correlation_id), body=json.dumps(response)) elif msg_type[1] == "msg": SERVERLOG.info("receive agent to server msg: %s" % (body)) msg = json.loads(body) print "receive agent to server msg: agent send item[%s] to server." % ( msg['project']) # {'type': 'item', 'req_id': msg['req_id'], 'project': msg['project_name'], 'info': msg['info']} if msg['type'] == 'item': self.worker_poll.push_task(Item(msg)) else: SERVERLOG.error( "receive agent to server msg: invalid msg type %s" % (msg['type'])) else: FALOG.error( "receive msg routing_key with wrong format[part 2].") # other process else: FALOG.error("receive msg routing_key with wrong format[part 1].") ch.basic_ack(delivery_tag=method.delivery_tag)