def unsubscribe(context, server, *args): if len(args) < 1: return None target = args[0] msg = {"TARGET": target} result = send_rpc(context, server, "unsubscribe", **msg) return result
def aggmon_cmd(argv): ap = argparse.ArgumentParser() ap.add_argument('-C', '--cmd-port', default="tcp://127.0.0.1:5558", action="store", help="RPC command port") ap.add_argument('-l', '--log', default="info", action="store", help="logging: info, debug, ...") ap.add_argument('-v', '--verbose', default=False, action="store_true", help="verbosity") sp = ap.add_subparsers(dest='cmd_group', help="Subcommand help") tagp = sp.add_parser('tag', help="Tagging commands") tagp.add_argument('--add', nargs=argparse.REMAINDER, metavar=('tagname', 'tagvalue', '[key value]'), action="store", help="add a tagging condition") tagp.add_argument('--remove', '--del', nargs=2, metavar=('tagname', 'tagvalue'), action="store", help="remove a tagging condition") tagp.add_argument('--show', default=False, action="store_true", help="show tags") subp = sp.add_parser('sub', help="Subscribe commands") subp.add_argument('--add', nargs=argparse.REMAINDER, metavar=('target', '[key value]'), action="store", help="add a subscription to target and optional key value match conditions") subp.add_argument('--remove', '--del', nargs=1, metavar='target', action="store", help="remove a target from the subscriptions") subp.add_argument('--show', default=False, action="store_true", help="show subscriptions") rawp = sp.add_parser('raw', help="Raw commands") rawp.add_argument('args', nargs='+', help="raw command and arguments, arguments coming as key value pairs. Example: test_rpc key1 val1 key2 val2") pargs = ap.parse_args(argv) log_level = eval("logging."+pargs.log.upper()) FMT = "%(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s" logging.basicConfig( stream=sys.stderr, level=log_level, format=FMT ) context = zmq.Context() server = pargs.cmd_port if pargs.cmd_group == "tag": if pargs.add is not None: result = tag_add(context, server, *pargs.add) elif pargs.remove is not None: result = tag_remove(context, server, *pargs.remove) elif pargs.show: result = tags_show(context, server) elif pargs.cmd_group == "sub": if pargs.add is not None: result = subscribe(context, server, *pargs.add) elif pargs.remove is not None: result = unsubscribe(context, server, *pargs.remove) elif pargs.show: result = show_subscriptions(context, server) elif pargs.cmd_group == "raw": if pargs.args is not None: rpc_cmd = pargs.args[0] rpc_args = dict_from_args(*pargs.args[1:]) result = send_rpc(context, server, rpc_cmd, **rpc_args) print json.dumps(result) context.destroy()
def tag_remove(context, server, *args): if len(args) < 2: return None tag_key = args[0] tag_val = args[1] msg = {"TAG_KEY": tag_key, "TAG_VALUE": tag_val} result = send_rpc(context, server, "remove_tag", **msg) return result
def subscribe(context, server, *args): if len(args) < 1: return None target = args[0] rest = args[1:] msg = dict_from_args(*rest) msg["TARGET"] = target result = send_rpc(context, server, "subscribe", **msg) return result
def tag_add(context, server, *args): if len(args) < 2: return None tag_name = args[0] tag_value = args[1] rest = args[2:] msg = dict_from_args(*rest) msg["TAG_KEY"] = tag_name msg["TAG_VALUE"] = tag_value result = send_rpc(context, server, "add_tag", **msg) return result
def request_resend(self, state): res = False if "cmd_port" in state: # send "quit" cmd over RPC log.info("requesting resend_state from %s" % state["cmd_port"]) reply = send_rpc(self.zmq_context, state["cmd_port"], "resend_state") if reply is not None: res = True elif "listen" in state: # send "quit" cmd over PULL port send_agg_command(self.zmq_context, state["listen"], "resend_state") res = True return res
def send_state_update(self): try: send_rpc(self.zmq_context, self.dispatcher, "set_component_state", **self.state) except RPCNoReplyError as e: log.warning("Server did not reply, ignoring... %r" % e)
def kill_component(self, service, group_path, __CALLBACK=None, __CALLBACK_ARGS=[], **kwds): """ Kill a component. First attempt is by sending it a "quit" command. This sets the "soft-fill" flag in the component state. When this flag is found at a subsequent kill attempt, the kill will attempt to kill the process of the component (hard kill). """ msg = {"component": service, "group": group_path} msg.update(kwds) res = False group = group_name(group_path) state = self.get_state(msg) if state is None: log.warning("component '%s' state not found. Don't know how to kill it." % service) return False # register callback if __CALLBACK is not None: key = service + ":" + component_key(self.config["services"][service]["component_key"], kwds) self.component_kill_cb[key] = {"cb": __CALLBACK, "args": __CALLBACK_ARGS} if "cmd_port" not in state: log.error("cmd_port not found in state: %r" % state) return False # is the process running? check with "ps" status = self.process_status(state) if status == "not found": res = self.del_state(msg) log.debug("kill_component deleting state %r" % res) return True elif status == "unknown": log.error("process status found as '%s'. Aborting kill." % status) return False # send "quit" cmd over RPC try: reply = send_rpc(self.zmq_context, state["cmd_port"], "quit") res = True except RPCNoReplyError as e: log.warning("Component did not reply, maybe it is dead already? %r" % e) reply = None # wait ... seconds and check if process has disappeared start_wait = time.time() while time.time() - start_wait < KILL_WAIT_TIMEOUT: status = self.process_status(state) # # This wait loop is disabled right now (by the -1000). # It took too long for the processes to die. # break if status == "running": time.sleep(1) elif status == "not found": log.info("status changed to 'not found'") break elif status == "unknown": return False log.info("status for pid=%d is '%s'" % (state["pid"], status)) if status == "running": # kill process using the remembered pid. This could be dangerous as we could kill another process. res = True try: log.info("attempting hard-kill of pid %d" % state["pid"]) exec_cmd = self.config["global"]["remote_kill"] % state out = subprocess.check_output(exec_cmd, stderr=subprocess.STDOUT, shell=True) # send_rpc(self.zmq_context, self.dispatcher, "del_component_state", **msg) log.debug("kill_component (kill) res=%r" % res) except Exception as e: log.error("subprocess error when running '%s' : '%r'" % (exec_cmd, e)) if res: res = self.del_state(msg) log.debug("kill_component deleting state %r" % res) return res
def tags_show(context, server): result = send_rpc(context, server, "show_tags") return result
def tags_reset(context, server): result = send_rpc(context, server, "reset_tags") return result
def show_subscriptions(context, server): result = send_rpc(context, server, "show_subs") return result