Fixes process checker using older psutil versions

Generally, psutil is restricted to versions older than 2.0.0
in OpenStack [1].

Because of this, there can exist nodes with psutil older than 2.0.0.

The data from Process object are accessed differently, depending on
the version. For example, in older versions, this is valid:

process.name()
process.pid
process.username()
process.cmdline()

While in older versions than 2.0.0, those are not methods:

process.name
process.pid
process.username
process.cmdline

Transforming the Process object into a dict solves the issue, as it
behaves the same across different psutil versions.

[1] https://github.com/openstack/requirements/blob/master/global-requirements.txt#L142

Closes-Bug: #1592695

Change-Id: I9b0ff7a666ac9900809f8e4dc07d2b2afbe43516
This commit is contained in:
Claudiu Belu 2016-06-01 00:30:29 +03:00
parent d798c9e45c
commit c88de30c1b
3 changed files with 22 additions and 14 deletions

View File

@ -151,17 +151,20 @@ class ProcessCheck(checks.AgentCheck):
for process in psutil.process_iter():
try:
p = ProcessStruct(name=process.name(),
pid=process.pid,
username=process.username(),
cmdline=' '.join(process.cmdline()))
process_dict = process.as_dict(
attrs=['name', 'pid', 'username', 'cmdline'])
p = ProcessStruct(name=process_dict['name'],
pid=process_dict['pid'],
username=process_dict['username'],
cmdline=' '.join(process_dict['cmdline']))
self._current_process_list.append(p)
except psutil.NoSuchProcess:
# No way to log useful information here so just move on
pass
except psutil.AccessDenied as e:
process_dict = process.as_dict(attrs=['name'])
self.log.info('Access denied to process {0}: {1}'.format(
process.name(), e))
process_dict['name'], e))
def check(self, instance):
try:

View File

@ -11,7 +11,7 @@ gevent>=1.1.1
httplib2>=0.7.5,<=0.9.2
netaddr>=0.7.12,<=0.7.18,!=0.7.16
ntplib>=0.3.2,<0.4
psutil>=3.0.0,<3.1.0
psutil<3.1.0
pymongo>=3.0.2,<=3.2.2,!=3.1
python-memcached>=1.56,<=1.58
python-monascaclient>=1.0.30 # Apache-2.0

View File

@ -11,12 +11,13 @@ class TestSimpleProcess(unittest.TestCase):
self.mock_process_iter = self.psutil_process_iter_patcher.start()
process_attrs = {
'name.return_value': 'process_name',
'name': 'process_name',
'pid': 1234,
'username.return_value': 'user',
'cmdline.return_value': '/usr/bin/process_name'
'username': 'user',
'cmdline': '/usr/bin/process_name'
}
process = mock.Mock(**process_attrs)
process = mock.Mock()
process.as_dict.return_value = process_attrs
self.mock_process_iter.return_value = [process]
config = {'init_config': {},
@ -44,11 +45,14 @@ class TestDetailedProcess(unittest.TestCase):
self.mock_process = self.psutil_process_patcher.start()
self.mock_process_iter = self.psutil_process_iter_patcher.start()
process_attrs = {
'name.return_value': 'process_name',
process_attrs_as_dict = {
'name': 'process_name',
'pid': 1234,
'username.return_value': 'user',
'cmdline.return_value': '/usr/bin/process_name',
'username': 'user',
'cmdline': '/usr/bin/process_name',
}
process_attrs = {
'memory_info_ex.return_value': mock.Mock(rss=1048576),
'num_threads.return_value': 1,
'num_fds.return_value': 1,
@ -59,6 +63,7 @@ class TestDetailedProcess(unittest.TestCase):
'write_bytes': 1024})
}
process = mock.Mock(**process_attrs)
process.as_dict.return_value = process_attrs_as_dict
self.mock_process_iter.return_value = [process]
self.mock_process.return_value = process