diff --git a/checks.d/process.py b/checks.d/process.py index 184761f370..799317cf94 100644 --- a/checks.d/process.py +++ b/checks.d/process.py @@ -6,7 +6,7 @@ class ProcessCheck(AgentCheck): def find_pids(self, search_string, psutil, exact_match=True): """ Create a set of pids of selected processes. - Search for search_string + Search for search_string """ found_process_list = [] for proc in psutil.process_iter(): @@ -35,41 +35,46 @@ def find_pids(self, search_string, psutil, exact_match=True): self.log.error('Access denied to %s process' % string) self.log.error('Error: %s' % e) raise - + if found or string == 'All': found_process_list.append(proc.pid) - + return set(found_process_list) - - def get_process_memory_size(self, pids, psutil, extended_metrics=False): + + def get_process_memory_size(self, pids, psutil, cpu_check_interval, extended_metrics=False): rss = 0 vms = 0 + cpu = 0 + thr = 0 if extended_metrics: real = 0 else: real = None for pid in set(pids): try: + p = psutil.Process(pid) if extended_metrics: - mem = psutil.Process(pid).get_ext_memory_info() + mem = p.get_ext_memory_info() real += mem.rss - mem.shared else: - mem = psutil.Process(pid).get_memory_info() + mem = p.get_memory_info() rss += mem.rss vms += mem.vms - + thr += p.get_num_threads() + cpu += p.get_cpu_percent(cpu_check_interval) + # Skip processes dead in the meantime except psutil.NoSuchProcess: self.warning('Process %s disappeared while scanning' % pid) pass #Return value in Byte - return (rss, vms, real) + return (thr, cpu, rss, vms, real) def psutil_older_than_0_6_0(self, psutil): return psutil.version_info[1] >= 6 - + def check(self, instance): try: import psutil @@ -79,19 +84,23 @@ def check(self, instance): name = instance.get('name', None) exact_match = instance.get('exact_match', True) search_string = instance.get('search_string', None) + cpu_check_interval = instance.get('cpu_check_interval',0.1) if name is None: raise KeyError('The "name" of process groups is mandatory') if search_string is None: raise KeyError('The "search_string" is mandatory') - + pids = self.find_pids(search_string, psutil, exact_match=exact_match) + self.log.debug('ProcessCheck: process %s analysed' % name) self.gauge('system.processes.number', len(pids), tags=[name]) - rss, vms, real = self.get_process_memory_size(pids, psutil, + thr, cpu, rss, vms, real = self.get_process_memory_size(pids, psutil, cpu_check_interval, extended_metrics=self.psutil_older_than_0_6_0(psutil)) self.gauge('system.processes.mem.rss', rss, tags=[name]) self.gauge('system.processes.mem.vms', vms, tags=[name]) + self.gauge('system.processes.cpu.pct', cpu, tags=[name]) + self.gauge('system.processes.threads', thr, tags=[name]) if real is not None: self.gauge('system.processes.mem.real', real, tags=[name]) diff --git a/conf.d/process.yaml.example b/conf.d/process.yaml.example index 79aa8cd359..645f5dbba5 100644 --- a/conf.d/process.yaml.example +++ b/conf.d/process.yaml.example @@ -6,6 +6,7 @@ instances: # return the counter of all the processes that contain the string # exact_match: (optional) True/False, default to False, if you want to look for an arbitrary # string, use exact_match: False, unless use the exact base name of the process +# cpu_check_interval: (optional) CPU percent check interval: 0.1 - 1.0 sec. More time - more precise # # Examples: #