ok
Direktori : /proc/self/root/lib/fm-agent/plugins/ |
Current File : //proc/self/root/lib/fm-agent/plugins/process.py |
import os import sys import agent_util try: import psutil except: psutil = None import re class ProcessPlugin(agent_util.Plugin): textkey = "process" label = "Process" @classmethod def get_metadata(self, config): status = agent_util.SUPPORTED msg = None if "aix" in sys.platform: status = agent_util.SUPPORTED msg = None elif "darwin" in sys.platform: status = agent_util.SUPPORTED msg = None elif "vmware" in sys.platform: status = agent_util.SUPPORTED msg = None elif psutil is None: # Unable to import psutil self.log.info( "Unable to import psutil library, no process metrics available" ) status = agent_util.UNSUPPORTED msg = "Unable to import psutil library, please install and rebuild metadata" elif not os.path.exists("/proc"): self.log.info("/proc not found") status = agent_util.UNSUPPORTED msg = "Enable procfs." return {} if "vmware" in sys.platform: metadata = { "process.named_count": { "label": "Number of processes - name", "options": None, "status": status, "error_message": msg, "unit": "processes", "option_string": True, }, "process.named_count.full": { "label": "Number of processes - full command line", "options": None, "status": status, "error_message": msg, "unit": "processes", "option_string": True, }, "process.exists": { "label": "Process is running", "options": None, "status": status, "error_message": msg, "unit": "boolean", "option_string": True, }, "process.exists.full": { "label": "Process is running - full command line", "options": None, "status": status, "error_message": msg, "unit": "boolean", "option_string": True, }, } else: metadata = { "process.running_count": { "label": "Number of processes running", "options": None, "status": status, "error_message": msg, "unit": "processes", }, "process.named_count": { "label": "Number of processes - name", "options": None, "status": status, "error_message": msg, "unit": "processes", "option_string": True, }, "process.named_memory_percentage": { "label": "Memory percentage of processes - name", "options": None, "status": status, "error_message": msg, "unit": "percent", "option_string": True, }, "process.named_cpu_percentage": { "label": "CPU percentage of processes - name", "options": None, "status": status, "error_message": msg, "unit": "percent", "option_string": True, }, "process.exists": { "label": "Process is running", "options": None, "status": status, "error_message": msg, "unit": "boolean", "option_string": True, }, "process.thread_count": { "label": "Process Thread Count - executable name only", "options": None, "status": status, "error_message": msg, "unit": "threads", "option_string": True, }, "process.named_count.full": { "label": "Number of processes - full command line", "options": None, "status": status, "error_message": msg, "unit": "processes", "option_string": True, }, "process.named_memory_percentage.full": { "label": "Memory percentage of processes - full command line", "options": None, "status": status, "error_message": msg, "unit": "percent", "option_string": True, }, "process.named_memory_raw_mb.full": { "label": "MB of memory used by processes - full command line", "options": None, "status": status, "error_message": msg, "unit": "MB", "option_string": True, }, "process.named_cpu_percentage.full": { "label": "CPU percentage of processes - full command line", "options": None, "status": status, "error_message": msg, "unit": "percent", "option_string": True, }, "process.exists.full": { "label": "Process is running - full command line", "options": None, "status": status, "error_message": msg, "unit": "boolean", "option_string": True, }, "process.thread_count.full": { "label": "Process Thread Count - executable name and args", "options": None, "status": status, "error_message": msg, "unit": "threads", "option_string": True, }, } return metadata def check(self, textkey, data, config): if "aix" in sys.platform or "sunos" in sys.platform: # ps_cmd = "ps -eo %p' '%a" if "aix" in sys.platform: ps_cmd = "ps axww" elif "sunos" in sys.platform: ps_cmd = "ps -eo pid' 'args" retcode, output = agent_util.execute_command(ps_cmd) output = output.split("\n") if textkey.endswith(".full"): textkey = textkey.rstrip(".full") if textkey == "process.running_count": return len(output) - 1 else: count = 0 pids = [] for line in output[1:]: if data in line: count += 1 pids.append(line.split()[0]) if textkey == "process.named_count": return count elif textkey == "process.exists": if count: return 1 else: return 0 elif textkey in [ "process.named_memory_percentage", "process.named_cpu_percentage", "named_memory_raw_mb", ]: all_cpu = 0 all_mem = 0 all_raw_kb = 0 for pid in pids: if "aix" in sys.platform: ps = "ps -fp %s -o pcpu,pmem,rss" % pid elif "sunos" in sys.platform: ps = "ps -fp %s -o pcpu,pmem,rss" % pid ret, output = agent_util.execute_command(ps) output = output.strip().split("\n") if len(output) < 2: continue fields = output[1].split() cpu = float(fields[0]) mem = float(fields[1]) raw_kb = float(fields[2]) all_cpu += cpu all_mem += mem all_raw_kb += raw_kb if textkey == "process.named_memory_percentage": return all_mem if textkey == "process.named_memory_raw_mb": return float(all_raw_kb) / 1024 else: return all_cpu # Unknown AIX/Solaris textkey return None # vmware logic elif "vmware" in sys.platform: pgrep = agent_util.which("pgrep") cmd = "" if not pgrep: self.log.error("Unable to find 'pgrep'! Unable to check for processes") return None if textkey.endswith(".full"): cmd = "%s -f" % (pgrep) else: cmd = "%s" % (pgrep) cmd += " %s" % data ret, output = agent_util.execute_command(cmd) out = output.split("\n") if textkey.startswith("process.named_count"): return len(out) - 1 if textkey.startswith("process.exists"): if len(out) - 1 > 0: return 1 else: return 0 if psutil is None: # Unable to import psutil, log and move on self.log.info( "Unable to import psutil library, no process metrics available" ) return None # Default Linux/FreeBSD logic search = "name" if textkey.endswith(".full"): textkey = textkey.rstrip(".full") search = "cmdline_str" # updated to use psutil library to get all processes and pull the name, pid and cmdline # this gets all the processes as objects process_objs = psutil.process_iter() processes = [] data = str(data) if textkey.startswith("process.named_cpu_percentage") and search == "name": for proc in process_objs: try: if re.search(data, proc.name()): processes.append( proc.as_dict(attrs=["pid", "cmdline", "name", "cpu_times"]) ) except psutil.NoSuchProcess: self.log.exception("Unable to get process.") continue elif ( textkey.startswith("process.named_cpu_percentage") and search == "cmdline_str" ): for proc in process_objs: try: if re.search(data, " ".join(proc.cmdline())): processes.append( proc.as_dict(attrs=["pid", "cmdline", "name", "cpu_times"]) ) except psutil.NoSuchProcess: self.log.exception("Unable to get process.") continue else: # this iterates through and shaves off infomation that isn't needed so we can filter on it later for proc in process_objs: try: processes.append( proc.as_dict( attrs=[ "pid", "cmdline", "name", "cpu_percent", "memory_percent", "memory_info", "num_threads", ] ) ) except psutil.NoSuchProcess: self.log.exception("Unable to get process.") continue # setting up a process list for us to transform the split cmdline entires into cmdline_str for proc in processes: if not proc["cmdline"]: proc["cmdline_str"] = "" continue proc["cmdline_str"] = " ".join(proc["cmdline"]) self.log.debug("All running processes:\n%s\n" % processes) if textkey == "process.running_count" or textkey == "count": return len(psutil.pids()) if textkey == "process.named_count": found_procs = [] self.log.info("Searching processes for '%s'" % data) for proc in processes: if proc[search] is not None and re.search(data, proc[search]): found_procs.append(proc) self.log.debug(found_procs) return len(found_procs) elif textkey == "process.exists": found_procs = [] self.log.info("Searching processes for '%s'" % data) for proc in processes: if proc[search] is not None and re.search(data, proc[search]): found_procs.append(proc) self.log.debug(found_procs) if found_procs: return 1 else: return 0 elif textkey in [ "process.named_memory_percentage", "process.named_memory_raw_mb", "process.thread_count", ]: found_procs = [] self.log.info("Searching processes for '%s'" % data) for proc in processes: if proc[search] is not None and re.search(data, proc[search]): found_procs.append(proc) self.log.debug(found_procs) self.log.debug( "Found matching processes: %s" % [p[search] for p in found_procs] ) # return 0 if no procs found if not found_procs: return 0 all_cpu = 0 all_mem = 0 all_raw_mem = 0 all_thread_count = 0 if "darwin" == sys.platform: rv = self.findDarwinProcInfo(found_procs) all_cpu = rv["cpu_percent"] all_mem = rv["memory_percent"] all_raw_mem = rv["memory_info"] all_thread_count = rv["num_threads"] else: for pid in found_procs: cpu = pid["cpu_percent"] mem = pid["memory_percent"] mem_raw = pid["memory_info"].rss thread_count = pid["num_threads"] all_cpu += cpu all_mem += mem all_raw_mem += mem_raw all_thread_count += thread_count all_raw_mem = float(all_raw_mem) / (1024 * 1024) if textkey == "process.named_memory_percentage": return all_mem if textkey == "process.named_memory_raw_mb": return all_raw_mem if textkey == "process.thread_count": return all_thread_count else: return all_cpu elif textkey in [ "process.named_cpu_percentage", "process.named_cpu_percentage.full", ]: if processes: user_sum = sum( [ p.get("cpu_times").user + p.get("cpu_times").system for p in processes ] ) else: return 0.0 last_result = self.get_cache_results(textkey, data) if not last_result: self.cache_result(textkey, data, user_sum) return None delta, previous = last_result[0] time_used_result = (user_sum - previous) / delta self.cache_result(textkey, data, user_sum) number_of_cores = psutil.cpu_count() if not number_of_cores: number_of_cores = 1 return (time_used_result / number_of_cores) * 100 return 0 def findDarwinProcInfo(self, found_procs): """ On OSX, psutil will not report process information on processes belonging to other users, unless the requesting process is privileged. https://github.com/giampaolo/psutil/issues/883 """ pids = [] for fp in found_procs: pids.append(str(fp["pid"])) rc, output = agent_util.execute_command("ps uM -p {}".format(",".join(pids))) lines = output.split("\n") procLines = lines[1:] rv = { "cpu_percent": float(0), "memory_percent": float(0), "memory_info": float(0), "num_threads": len(procLines), } for l in procLines: info = l.split() if len(info) == 14: # Full info line for process rv["cpu_percent"] += float(info[2]) rv["memory_percent"] += float(info[3]) rv["memory_info"] += float(info[5]) m = rv["memory_info"] rv["memory_info"] = float(m) / 1024 return rv