Skip to content

Commit

Permalink
Merge pull request #646 from morskoyzmey/patch-1
Browse files Browse the repository at this point in the history
Add CPU percent and threads number metrics
  • Loading branch information
remh committed Sep 5, 2013
2 parents ee78f43 + f9d8806 commit b191e3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
33 changes: 21 additions & 12 deletions checks.d/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand All @@ -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])
1 change: 1 addition & 0 deletions conf.d/process.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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:
#
Expand Down

0 comments on commit b191e3e

Please sign in to comment.