def callback(ch, method, properties, body): #print " [x] Received %r" % (body,) #time.sleep(random.randint(0,3)) #print dir(properties) #print properties.expiration #print properties.headers #print properties.reply_to #print properties.message_id try: #log("********************") #log(body) #log("********************") info = json.loads(body) detail ={} if MAP_INFO.has_key(method.routing_key): #(result,code,msg) = MAP_INFO[method.routing_key]["class"]().run(body) (result,code,msg,detail) = MAP_INFO[method.routing_key]["class"]().run(info) else: print "can not config method map relation:%s"%(method.routing_key) #MAP_INFO[] except Exception,e: #print e #print traceback.print_exc() result=False code = 1 msg=traceback.print_exc() info ={} detail ={} log(msg)
def run(self): try: while 1: print "start process-1" p1= ProcessMain("test-41","4") p1.start() p1.join() print "start process-5" p2= ProcessMain("test-5","5") p2.start() p2.join() # print "start process-6" p3= ProcessMain("test-6","6") p3.start() p3.join() print "start process-7" p4= ProcessMain("test-7","7") p4.start() p4.join() print "start process-8" p5= ProcessMain("test-8","8") p5.start() p5.join() print "start process-42" p6= ProcessMain("test-42","4") p6.start() p6.join() except Exception,e: print str(e) log(traceback.print_exc())
def run(self): try: while 1: pkg_run() except KeyboardInterrupt: log("[Keyboard]PkgInfoDemo stop") pass
def inner_process_ret(self,data,taskid=""): """""" (result,header_info,body_info)=self.check_params(data) if isinstance(body_info["data"]["detail"],list): detail = body_info["data"]["detail"][0] elif isinstance(body_info["data"]["detail"],dict): detail = body_info["data"]["detail"] else: log("request content:param[detail] type need be list or dict") return "request content format error" # r_method = body_info.get("method","") r_type = body_info.get("type","") str_result ="No support Method:%s"%str(r_method) if r_method.strip() == "script_run": str_result = self._run_script(detail) elif r_method.strip() == "package_run": str_result = self._run_file(detail) else: pass # (script_name,store_path)=self.convert_str_2_file(detail) # # result=exec_cmd("chmod +x "+store_path+script_name) # log("\n".join(result)) # result = exec_cmd(store_path+script_name) # str_result = "\n".join(result) # log(str_result) self._callback_result(header_info, taskid, str_result) return str_result
def __del__(self): if self.redis_obj is not None: try: log("[SyncCacheInfo]close connection.") self.redis_obj.close() except Exception,e: pass
def __init__(self): try: #pass self.redis_obj = RedisHelper(redis_ip,redis_port) except Exception,e: log(str(e)) self.redis_obj =None
def process(self,type): content ="" if type==1:#post方法,接收post参数 datas = self.rfile.read(int(self.headers['content-length'])) datas = urllib.unquote(datas).decode("utf-8", 'ignore')#指定编码方式 #print datas try: req_content=json.loads(datas) print "requeset content:",req_content #resp=json.dumps(req_content) (result,resp_result)=self._dispather(req_content) #(result,resp_result)=self._dispather(resp) #log(resp) except Exception,e: log(str(e)) print str(e) resp="Error" content =resp_result #process request #指定返回编码 enc="UTF-8" #content="Ok" content = content.encode(enc) f = io.BytesIO() f.write(content) f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html; charset=%s" % enc) self.send_header("Content-Length", str(len(content))) self.end_headers() shutil.copyfileobj(f,self.wfile)
def call_callback_api (self ,host_and_port,url,params): #print "call external api:",host_and_port,url,params #output("###############################################") #output("external request is :%s\n"%str(params)) ret_flag = True data = "" headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} try: if isinstance(params,(str,unicode)): req_data = params elif isinstance(params,(dict,list,tuple)): req_data = json.dumps(params) conn = httplib.HTTPConnection(host_and_port,timeout= self._timeout) conn.request("POST",url,req_data, headers) response = conn.getresponse() #output("[external api]response:(%s,%s)"%(str(response.status),str(response.reason))) data = json.loads(response.read()) except Exception,e: log(traceback.print_exc()) ret_flag = False if hasattr(e,"reason"): data = e.reason elif hasattr(e,"message"): data = e.message else: data = "urlopen error: unknown reson."
def run(self): try: router_obj = None process_list =[] start_run_process([PkgInfoMgr,HostBasicInfo,AgentTaskFactory],process_list,router_obj) while 1: #main_run() time.sleep(2) for p in process_list: if not p.is_alive(): #log("process [%s]is dead,pid:%s,exit code:%s"%(p.name,p.pid,p.exitcode)) log("error............") p.join() process_list.remove(p) if len(process_list)<4: terminate_all(process_list) break else: log("exist more pid info,need terminate.") terminate_all(process_list) #log("sleep time:%d"%(time.sleep(random.randint(0,10)))) #log("Random number:%d"%(random.randint(0,100))) except KeyboardInterrupt: log("[Keyboard]TimingDaemon stop") terminate_all(process_list) except Exception,e: log(str(e)) log(traceback.print_exc()) terminate_all(process_list)
def execute_cmd(self,cmd_str): try: obj = subprocess.Popen(cmd_str,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) info = obj.communicate() except Exception,e: log(str(e)) info = ("","")
def udp_report(self,info): """function:support upd report info""" try: s= socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_TTL,20) s.sendto(json.dumps(info),(self._host_ip,int(self._host_port))) except Exception,e: log("udp report error:%s"%str(e))
def _compose(self): self._info["libvirt"]=self.get_libvirt_ver() self._info["qemu"] = self.get_qemu_ver() self._info["kernel"] = self.get_kernel_ver() self._info["hostname"] = self.get_hostname() self._info["diskUsage"]=self.get_disk_usage() log(json.dumps(self._info))
def run(self): try: while 1: zmq_multiple_sub() except KeyboardInterrupt: log("[KeyBoard]MultipleZMQSubDemo stop") except Exception,e: print str(e)
def zmq_multiple_sub(): ZMQSubProcess().run() # p1= ProcessMain("ddd","4") # p1.start() while 1: log("seeeee") time.sleep(2) pass
def run(self): #support mutil-threading try: while 1: zmq_sub() except KeyboardInterrupt: log("[KeyBoard]SimpleZMQSubDemo stop") except Exception,e: print str(e)
def run(self): while 1: #step-0:sleep info interval_sec = random.randint(60,120) time.sleep(interval_sec) #step-1: self._process() log("info success...")
def run(self): try: while 1: pkg_run() except KeyboardInterrupt: log("[Keyboard]PkgInfoDemo stop") print "%%%%%%%%%%%" except Exception,e: print e
def run(self): try: while 1: zmq_pub() except KeyboardInterrupt: log("[KeyBoard]ZMQPubDemo stop") except Exception,e: print str(e)
def _run_script(self,data_detail={}): """function:run script""" (script_name,store_path)=self.convert_str_2_file(data_detail) result=exec_cmd("chmod +x "+store_path+script_name) log("\n".join(result)) result = exec_cmd(store_path+script_name) str_result = "\n".join(result) log(str_result) return str_result
def run(self): while 1: try: interval_sec = random.randint(30,60) time.sleep(interval_sec) log("[HostBasicInfo]report interval [%s] "%str(interval_sec)) self._compose() ReportInfo().run(self._info) except Exception,e: log(str(e))
def _callback_result(self,header_info={},taskid="",resp_result=""): """""" try: callback_obj = CallbackTaskInfo() result = callback_obj.run({"taskid":taskid,"result":resp_result,"host":header_info.get("ip",""), "port":header_info.get("port",""),"url":header_info.get("url","")}) log("callback...") except Exception,e: log(str(e)) print_exc_info() result = False
def get_hostname(self): """""" try: hostname_ver_list =os.popen('hostname').read() if isinstance(hostname_ver_list,(list,)) and hostname_ver_list: hostname_ver_str = hostname_ver_list[0].strip('\n') else: hostname_ver_str = hostname_ver_list.strip('\n') except Exception,e: hostname_ver_str ="unkown" log(str(e))
def get_kernel_ver(self): """""" try: kernel_ver_list =os.popen('uname -r').read() if isinstance(kernel_ver_list,(list,)) and kernel_ver_list: ker_ver_str = kernel_ver_list[0].strip('\n') else: ker_ver_str = kernel_ver_list.strip('\n') except Exception,e: ker_ver_str ="unkown" log(str(e))
def get_libvirt_ver(self): """""" try: lib_ver_list = os.popen('rpm -qa|grep libvirt').read() #log(type(lib_ver_list)) if isinstance(lib_ver_list,(list,)) and lib_ver_list: lib_ver_str = lib_ver_list[0].strip('\n') else: lib_ver_str = lib_ver_list.strip('\n') except Exception,e: lib_ver_str ="unkown" log(str(e))
def get_qemu_ver(self): """""" try: qemu_ver_list = os.popen('rpm -qa|grep qemu').read() #log(type(lib_ver_list)) if isinstance(qemu_ver_list,(list,)) and qemu_ver_list: qemu_ver_str = qemu_ver_list[0].strip('\n') else: qemu_ver_str = qemu_ver_list.strip('\n') except Exception,e: qemu_ver_str ="unkown" log(str(e))
def run(self,content): try: log(type(content)) #for i in range(random.randint(0,100)): # log(i) # self._ret_data.add_result(str("key_"+str(i)), i) #TODO:process command execute ret_obj = AgentAPI().run(content) return ret_obj.get_data() except Exception,e: print str(e) log(str(e)) self._ret_data.add_result("msg","error")
def __check_md5_info(self,pkg_name,md5_info): """""" md5_cmd = "md5sum "+self._local_dir+"/"+pkg_name info = self.execute_cmd(md5_cmd) if info[0]: tmp = info[0].split() #start compare,if equal,return True if tmp[0] == md5_info: log("md5sum info success") return True else: log(info[1]) return False
def __rename_file(self,str_cmd=""): """""" try: if str_cmd: ret_info =exec_cmd(str_cmd) if ret_info: str_info = "\n".join(ret_info) else: str_info="command already execute,but not return info." else: str_info="command content is null,please check" except Exception,e: log(str(e))
def sum_idc_host_info(self): """""" try: ret_info=[] sql = Sql(host=db_ip,port = db_port,user=db_user,passwd=db_pwd,dbname=db_name) sql_str="select count(1) as num,IdcId,ZoneId,BsiId from cmdb_host_info group by IdcId,ZoneId,BsiId;" info = sql.execute(sql_str) sql.close() for item in info: ret_info.append({"num":item[0],"idcId":item[1],"zoneId":item[2],"bsiId":item[3]}) log(ret_info) except Exception,e: log(str(e))
def _run_obj(self,path,content): '''''' app=path[0] log("path:%s"%str(path)) log(get_projname()) if app == get_projname(): pass else: ret_data = ReturnData() ret_data.set_header(code=1000, msg="[Process]Get obj path info error") log(ret_data.get_data()) return ret_data.get_data() if isinstance(path,list): try: classname=RPC_DICT[path[1]] class_obj=classname() method = path[2] obj = getattr(class_obj,method) #log(obj) return obj(content) except Exception,e: print str(e) ret_data = ReturnData() ret_data.set_header(code=1000, msg="[Process]Get obj info error") log(ret_data.get_data()) return ret_data.get_data()