Skip to content

Commit

Permalink
Add num_fds and uptime to ProcessResourcesCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
jaingaurav committed Jan 4, 2015
1 parent 2768719 commit 4120c27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/collectors/processresources/processresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import os
import re
import time

import diamond.collector
import diamond.convertor
Expand Down Expand Up @@ -148,6 +149,7 @@ def get_default_config(self):
'cpu_times',
'io_counters',
'num_threads',
'num_fds',
'memory_percent',
'ext_memory_info',
]
Expand All @@ -173,6 +175,8 @@ def collect_process_info(self, process):
pi = process_info(process, self.default_info_keys)
if cfg['count_workers']:
pi.update({'workers_count': 1})
uptime = time.time() - getattr(process, 'create_time')
pi.update({'uptime': uptime})
self.save_process_info(pg_name, pi)
except psutil.NoSuchProcess, e:
self.log.info("Process exited while trying to get info: %s", e)
Expand Down
13 changes: 10 additions & 3 deletions src/collectors/processresources/test/testprocessresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
################################################################################

import os
import time
from test import CollectorTestCase
from test import get_collector_config
from test import unittest
Expand Down Expand Up @@ -58,9 +59,10 @@ def test_import(self):
self.assertTrue(ProcessResourcesCollector)

@run_only_if_psutil_is_available
@patch.object(time, 'time')
@patch.object(os, 'getpid')
@patch.object(Collector, 'publish')
def test(self, publish_mock, getpid_mock):
def test(self, publish_mock, getpid_mock, time_mock):
process_info_list = [
# postgres processes
{
Expand Down Expand Up @@ -146,6 +148,7 @@ def __init__(self, pid, name, rss, vms, exe=None):
self.exe = exe

self.cmdline = [self.exe]
self.create_time = 0

def as_dict(self):
from collections import namedtuple
Expand All @@ -167,9 +170,9 @@ def as_dict(self):
'pid': self.pid,
'connections': None,
'cmdline': [self.exe],
'create_time': 1389294592.02,
'create_time': 0,
'ionice': ionice(ioclass=0, value=0),
'num_fds': None,
'num_fds': 10,
'memory_maps': None,
'cpu_percent': 0.0,
'terminal': None,
Expand Down Expand Up @@ -205,13 +208,17 @@ def as_dict(self):
exe=x['exe'])
for x in process_info_list)

time_mock.return_value = 1234567890

getpid_mock.return_value = self.SELFMON_PID

patch_psutil_process_iter = patch('psutil.process_iter',
return_value=process_iter_mock)
patch_psutil_process_iter.start()
self.collector.collect()
patch_psutil_process_iter.stop()
self.assertPublished(publish_mock, 'foo.uptime', 1234567890)
self.assertPublished(publish_mock, 'foo.num_fds', 10)
self.assertPublished(publish_mock, 'postgres.ext_memory_info.rss',
1000000 + 100000 + 10000 + 1000 + 100)
self.assertPublished(publish_mock, 'foo.ext_memory_info.rss', 1)
Expand Down

0 comments on commit 4120c27

Please sign in to comment.