def get_task_flow(task_id=None, name=None, resource=None, **params): result = [] db = dbpools.get_local() sql = "select * from taskflow where 1=1" if task_id: sql = sql + " and task_id = '" + task_id + "'" if name: sql = sql + " and name = '" + name + "'" if resource: sql = sql + " and resource = '" + resource + "'" cur = yield db.execute(sql) tasks = cur.fetchall() if tasks: for task in tasks: resp = { "id": task.get("id"), "task_id": task.get("task_id"), "status": task.get("status"), "name": task.get("name"), "resource": task.get("resource"), "updated_at": task.get("updated_at") } pro = simplejson.loads(task.get("param")) mes = simplejson.loads(task.get("message")) res = resp.copy() res.update(pro) res.update(mes) flag = True for k, v in params.items(): if k not in res or v != res.get(k): flag = False if flag: result.append(res) raise gen.Return(result)
def execute(self): body = jsonutils.loads(self._message) if "oslo.message" in body: message = jsonutils.loads(body.get("oslo.message")) self._event = message.get("method") self._body = message['args']['instance']['nova_object.data'] if "specific_trap" in body and "var_binds" in body: yield snmp_message(body)
def execute(self): try: if isinstance(self._message, str): body = jsonutils.loads(self._message) oslo_msg = body[_MSG_BODY] oslo_msg = jsonutils.loads(oslo_msg) self._event = oslo_msg[_EVENT_TYPE] self._body = oslo_msg[_EVENT_BODY] except Exception,e: LOG.error(e) LOG.error("error messge is %s" % self._message)
def on_message(self, message): LOG.debug("the client %s connected with compute socket", self.request.remote_ip) message = json.loads(message) try: host_id = message["id"] chart = message.get("chart", None) client_info = { "target": self, "chart": chart, "id": str(host_id) } for ci in self.clients: if ci.get("target") is self: ci["chart"] = chart break else: self.clients.append(client_info) rst_data = yield self._host_meter(chart.get("counter_name"), host_id, chart.get("limit")) if rst_data: self.write_message(json.dumps({"response": rst_data, "type": "chart"})) if not self.running: HostDetailHandler.running = time.time() yield HostDetailHandler.handle() except Exception, e: LOG.error("host detail get message error is %s", e)
def on_message(self, message): """ :param message: :return: """ try: LOG.debug("the client %s connected with compute socket", self.request.remote_ip) message = json.loads(message) vm_id = message["id"] chart = message.get("chart", None) client_info = {"target": self, "chart": chart, "id": vm_id} for ci in self.clients: if ci["target"] is self: ci["chart"] = chart break else: self.clients.append(client_info) rst_data = yield self._vm_meter(chart.get("counter_name"), vm_id, chart.get("limit")) if rst_data: self.write_message( json.dumps({ "response": rst_data, "type": "chart" })) if not ComputeDetailsHandler.running: ComputeDetailsHandler.running = time.time() yield ComputeDetailsHandler.handle() except BaseException, e: LOG.error("compute detail on message %s", e)
def start(cls, cache, interval, fun, **kwargs): """ :param cls :running:运行时毫秒数 :clients:连接该进程的客户端 :param cache: 缓存区 :param interval:循环周期 :param fun:执行函数 :param kwargs:函数需要的参数 :return: """ run_time = cls.running clients = cls.clients while clients and run_time == cls.running: try: init_cache(cls) create_at = cache.get_by_id("create_at") body = cache.get_by_id("body") if cache.get_by_id("body") else {} if isinstance(body, basestring): body = jsonutils.loads(body) if (not create_at or not body) or (int(time.time()) - create_at >= interval): cache.set("create_at", int(time.time())) body = yield fun(body, **kwargs) cache.set("create_at", int(time.time())) cache.set("body", jsonutils.dumps(body)) for client in clients: response = body.get("response") _type = body.get("type") write(client, response, _type) except Exception as e: LOG.error("socket monitor or push error %s", e) yield gen.sleep(interval)
def load_json(cls, data, default_rule=None): """Allow loading of JSON rule data.""" # Suck in the JSON data and parse the rules rules = dict((k, parse_rule(v)) for k, v in jsonutils.loads(data).items()) return cls(rules, default_rule)
def get_vms_nics(vm_id=None): try: db = dbpools.get_nova() if vm_id: result = [] sql = "select instance_uuid , network_info from instance_info_caches where instance_uuid = %s" cur = yield db.execute(sql, [vm_id]) res = cur.fetchone() network_info = jsonutils.loads(res.get("network_info")) for r in network_info: result.append({ "name": r.get("network").get("label"), "ip": r.get("network").get("subnets")[0].get("ips")[0].get( "address"), "id": r.get("network").get("id"), "port_id": r.get("id") }) else: result = {} sql = "select instance_uuid , network_info from instance_info_caches where deleted = 0" cur = yield db.execute(sql) response = cur.fetchall() for res in response: network_info = jsonutils.loads(res.get("network_info")) network = [] for r in network_info: network.append({ "name": r.get("network").get("label"), "ip": r.get("network").get("subnets")[0].get("ips")[0].get( "address"), "id": r.get("network").get("id") }) result[res.get("instance_uuid")] = network except Exception as e: LOG.error("get vms nics from db error: %s" % e) raise e raise gen.Return(result)
def execute(self): message = jsonutils.loads(self._message) self._event = message.get("event") self._body = message.get("body")