def __call__(self, srv_com, cur_ns): # s_time = time.time() if cur_ns.arguments: name_list = cur_ns.arguments if "cron" in name_list: name_list.append("crond") else: name_list = [] _p_dict = {} if PLATFORM_SYSTEM_TYPE == PlatformSystemTypeEnum.WINDOWS: attr_list = [ "pid", "ppid", "name", "exe", "cmdline", "status", "ppid", "cpu_affinity" ] elif PLATFORM_SYSTEM_TYPE == PlatformSystemTypeEnum.LINUX: attr_list = [ "pid", "ppid", "uids", "gids", "name", "exe", "cmdline", "status", "ppid", "cpu_affinity", ] else: attr_list = ["pid", "name"] for key, value in process_tools.get_proc_list( proc_name_list=name_list).items(): try: if value.is_running(): _p_dict[key] = value.as_dict(attrs=attr_list) except psutil.NoSuchProcess: pass if cur_ns.arguments: # try to be smart about cron / crond t_dict = { key: value for key, value in _p_dict.items() if value["name"] in cur_ns.arguments } if not t_dict and cur_ns.arguments[0] == "cron": t_dict = { key: value for key, value in _p_dict.items() if value["name"] in ["crond"] } _p_dict = t_dict srv_com["process_tree"] = server_command.compress(_p_dict, json=True) srv_com["process_tree"].attrib["format"] = "2"
def kill_previous(): # check for already running rrdcached processes and kill them proc_dict = process_tools.get_proc_list( proc_name_list=["rrdcached", "collectd"]) if proc_dict: for _key in proc_dict.iterkeys(): try: os.kill(_key, 15) except: pass time.sleep(0.5) for _key in proc_dict.iterkeys(): try: os.kill(_key, 9) except: pass
def __call__(self, srv_com, cur_ns): srv_com["psutil"] = "yes" srv_com["num_cores"] = psutil.cpu_count(logical=True) srv_com["process_tree"] = process_tools.compress_struct( process_tools.get_proc_list(attrs=[ "pid", "ppid", "uids", "gids", "name", "exe", "cmdline", "status", "ppid", "cpu_affinity", ]))
def __call__(self, srv_com, cur_ns): # s_time = time.time() if cur_ns.arguments: name_list = cur_ns.arguments if "cron" in name_list: name_list.append("crond") else: name_list = [] _p_dict = {} for key, value in process_tools.get_proc_list( proc_name_list=name_list).iteritems(): try: if value.is_running(): _p_dict[key] = value.as_dict(attrs=[ "pid", "ppid", "uids", "gids", "name", "exe", "cmdline", "status", "ppid", "cpu_affinity", ]) except psutil.NoSuchProcess: pass if cur_ns.arguments: # try to be smart about cron / crond t_dict = { key: value for key, value in _p_dict.iteritems() if value["name"] in cur_ns.arguments } if not t_dict and cur_ns.arguments[0] == "cron": t_dict = { key: value for key, value in _p_dict.iteritems() if value["name"] in ["crond"] } _p_dict = t_dict srv_com["process_tree"] = process_tools.compress_struct(_p_dict) srv_com["process_tree"].attrib["format"] = "2"
def update_machine_vector(self, mv): pdict = process_tools.get_proc_list() if self.feed_affinity: self.af_struct.feed(pdict) pids = list(pdict.keys()) n_dict = {key: 0 for key in process_tools.PROC_INFO_DICT.keys()} # mem_mon_procs = [] # mem_found_procs = {} for p_stuff in list(pdict.values()): try: if p_stuff.status() in n_dict: n_dict[p_stuff.status()] += 1 else: self.log( "*** unknown process state '{}' for process {} (pid {:d})" .format(p_stuff.status(), p_stuff.name(), p_stuff.pid()), logging_tools.LOG_LEVEL_ERROR) except psutil.NoSuchProcess: pass for key, value in n_dict.items(): mv["proc.{}".format(key)] = value mv["proc.total"] = len(pids)
def __call__(self, srv_com, cur_ns): def priv_check(key, what): _name, _uid = (what.name(), what.uids()[0]) if include_list: if _name in include_list or "{:d}".format( what.pid) in include_list: # take it and everything beneath return 1 else: # do not take it return 0 if _name in exclude_list: # do not take leaf and stop iteration return -1 else: if _uid >= cur_ns.minuid and _uid <= cur_ns.max_uid: # take it return 1 else: # do not take it return 0 # check arguments exclude_list = [ _entry for _entry in cur_ns.exclude.split(",") if _entry.strip() ] include_list = [ _entry for _entry in cur_ns.arguments if _entry.strip() ] srv_com["signal_list"] = [] if not include_list and not exclude_list: self.log("refuse to operate without include or exclude list", logging_tools.LOG_LEVEL_ERROR) else: sig_str = "signal {:d}[{}] (uid {:d}:{:d}), exclude_list is {}, include_list is {}".format( cur_ns.signal, self.get_signal_string(cur_ns.signal), cur_ns.min_uid, cur_ns.max_uid, ", ".join(exclude_list) or "<empty>", ", ".join(include_list) or "<empty>") self.log(sig_str) pid_list = find_pids(process_tools.get_proc_list(), priv_check) for struct in pid_list: try: _name = struct.name() _cmdline = struct.cmdline() # print struct, cur_ns.signal os.kill(struct.pid, cur_ns.signal) except: info_str, is_error = (process_tools.get_except_info(), True) else: info_str, is_error = ("sent {:d} to {:d} ({})".format( cur_ns.signal, struct.pid, _name), False) self.log( "{:d}: {}".format(struct.pid, info_str), logging_tools.LOG_LEVEL_ERROR if is_error else logging_tools.LOG_LEVEL_OK) srv_com["signal_list"].append( srv_com.builder("signal", _name, error="1" if is_error else "0", result=info_str, cmdline=" ".join(_cmdline))) srv_com["signal_list"].attrib.update( {"signal": "{:d}".format(cur_ns.signal)})
def update_proc_dict(self): self.__act_proc_dict = process_tools.get_proc_list()