def populate_process_metrics(proc): ACCESS_DENIED = '' try: pinfo = proc.as_dict(ad_value=ACCESS_DENIED) except psutil.NoSuchProcess: collectd.exception('process disappeared') # it went away return None,None pid = pinfo['pid'] ppid = pinfo['ppid'] pruntime = time.time() - pinfo['create_time'] pcpupct = pinfo['cpu_percent'] pmempct = round(pinfo['memory_percent'], 1) if (DEBUG_DO_FILTER_PROCESSES == False or (pruntime > MIN_RUN_INTERVAL and (pcpupct > MIN_CPU_USAGE_PERCENT or pmempct > MIN_MEM_USAGE_PERCENT))): pmap = {} set_process_metric_val(pmap, 'PM_CPU_PCT', pcpupct) set_process_metric_val(pmap, 'PM_MEM_PCT', pmempct) set_process_metric_val(pmap, 'PM_RUN_SECS', pruntime) set_process_metric_val(pmap, 'PM_PID', str(pid)) set_process_metric_val(pmap, 'PM_PPID', str(ppid)) pio = pinfo.get('io_counters', ACCESS_DENIED) if pio != ACCESS_DENIED: set_process_metric_val(pmap, 'PM_BYTES_READ', pio.read_bytes) set_process_metric_val(pmap, 'PM_BYTES_WRITE', pio.write_bytes) pname = mk_process_name(pinfo['name'], pid) return pname,pmap return None,None
def get_docker_container_name(pid): dcname = '' dc_id = get_docker_container_id(pid) if dc_id != None: collectd.debug('pid ' + str(pid) + ': ' + \ 'looking up container id: ' + dc_id) try: dcinfo = dclnt.inspect_container(dc_id) except: collectd.exception('error getting container info:'\ ' check your script user/group'\ 'is OK in collectd.conf') return dcname dcname = dcinfo['Name'][1:] # strip beginning '/' collectd.debug('pid ' + str(pid) + ': ' + \ 'returning container name: ' + dcname) return dcname