mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Create compatability psutil.
psutil 3.0 drops 1.0 API, but we still support old psutil versions.
This commit is contained in:
parent
982f87316d
commit
93ee411fd5
5 changed files with 81 additions and 6 deletions
|
@ -48,7 +48,7 @@ import salt.ext.six as six
|
|||
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
|
||||
HAS_PSUTIL = False
|
||||
try:
|
||||
import psutil
|
||||
import salt.utils.psutil_compat as psutil
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
pass
|
||||
|
@ -520,7 +520,7 @@ class SaltLoadModules(ioflo.base.deeding.Deed):
|
|||
)
|
||||
modules_max_memory = True
|
||||
old_mem_limit = resource.getrlimit(resource.RLIMIT_AS)
|
||||
rss, vms = psutil.Process(os.getpid()).get_memory_info()
|
||||
rss, vms = psutil.Process(os.getpid()).memory_info()
|
||||
mem_limit = rss + vms + self.opts.value['modules_max_memory']
|
||||
resource.setrlimit(resource.RLIMIT_AS, (mem_limit, mem_limit))
|
||||
elif self.opts.value.get('modules_max_memory', -1) > 0:
|
||||
|
|
|
@ -42,7 +42,7 @@ except ImportError:
|
|||
|
||||
HAS_PSUTIL = False
|
||||
try:
|
||||
import psutil
|
||||
import salt.utils.psutil_compat as psutil
|
||||
HAS_PSUTIL = True
|
||||
except ImportError:
|
||||
pass
|
||||
|
@ -924,7 +924,7 @@ class Minion(MinionBase):
|
|||
log.debug('modules_max_memory set, enforcing a maximum of {0}'.format(self.opts['modules_max_memory']))
|
||||
modules_max_memory = True
|
||||
old_mem_limit = resource.getrlimit(resource.RLIMIT_AS)
|
||||
rss, vms = psutil.Process(os.getpid()).get_memory_info()
|
||||
rss, vms = psutil.Process(os.getpid()).memory_info()
|
||||
mem_limit = rss + vms + self.opts['modules_max_memory']
|
||||
resource.setrlimit(resource.RLIMIT_AS, (mem_limit, mem_limit))
|
||||
elif self.opts.get('modules_max_memory', -1) > 0:
|
||||
|
|
|
@ -17,7 +17,7 @@ from salt.exceptions import SaltInvocationError, CommandExecutionError
|
|||
|
||||
# Import third party libs
|
||||
try:
|
||||
import psutil
|
||||
import salt.utils.psutil_compat as psutil
|
||||
|
||||
HAS_PSUTIL = True
|
||||
PSUTIL2 = psutil.version_info >= (2, 0)
|
||||
|
|
75
salt/utils/psutil_compat.py
Normal file
75
salt/utils/psutil_compat.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Version agnostic psutil hack to fully support both old (<2.0) and new (>=2.0) psutil versions.
|
||||
The old <1.0 psutil API is dropped in psutil 3.0
|
||||
|
||||
Should be removed once support for psutil <2.0 is dropped. (eg RHEL 6)
|
||||
|
||||
Built off of http://grodola.blogspot.com/2014/01/psutil-20-porting.html
|
||||
'''
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
#log = logging.getLogger(__name__)
|
||||
|
||||
# No exception handling, as we want ImportError if psutil doesn't exist
|
||||
import psutil
|
||||
|
||||
if psutil.__version__ >= (2, 0):
|
||||
from psutil import *
|
||||
else:
|
||||
# Import hack to work around bugs in old psutil's
|
||||
# Psuedo "from psutil import *"
|
||||
_globals = globals()
|
||||
for attr in psutil.__all__:
|
||||
_temp = __import__('psutil', globals(), locals(), [attr], -1)
|
||||
try:
|
||||
_globals[attr] = getattr(_temp, attr)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
# Alias new module functions
|
||||
def boot_time():
|
||||
return psutil.BOOT_TIME
|
||||
|
||||
def cpu_count():
|
||||
return psutil.NUM_CPUS
|
||||
|
||||
# Alias renamed module functions
|
||||
pids = psutil.get_pid_list
|
||||
users = psutil.get_users
|
||||
|
||||
# Alias renamed Process functions
|
||||
_PROCESS_FUNCTION_MAP = {
|
||||
"children": "get_children",
|
||||
"connections": "get_connections",
|
||||
"cpu_affinity": "get_cpu_affinity",
|
||||
"cpu_percent": "get_cpu_percent",
|
||||
"cpu_times": "get_cpu_times",
|
||||
"io_counters": "get_io_counters",
|
||||
"ionice": "get_ionice",
|
||||
"memory_info": "get_memory_info",
|
||||
"memory_info_ex": "get_ext_memory_info",
|
||||
"memory_maps": "get_memory_maps",
|
||||
"memory_percent": "get_memory_percent",
|
||||
"nice": "get_nice",
|
||||
"num_ctx_switches": "get_num_ctx_switches",
|
||||
"num_fds": "get_num_fds",
|
||||
"num_threads": "get_num_threads",
|
||||
"open_files": "get_open_files",
|
||||
"rlimit": "get_rlimit",
|
||||
"threads": "get_threads",
|
||||
"cwd": "getcwd",
|
||||
|
||||
"cpu_affinity": "set_cpu_affinity",
|
||||
"ionice": "set_ionice",
|
||||
"nice": "set_nice",
|
||||
"rlimit": "set_rlimit",
|
||||
}
|
||||
|
||||
for new, old in _PROCESS_FUNCTION_MAP.iteritems():
|
||||
try:
|
||||
setattr(Process, new, psutil.Process.__dict__[old])
|
||||
except KeyError:
|
||||
pass
|
||||
|
|
@ -16,7 +16,7 @@ HAS_PSUTIL = ps.__virtual__()
|
|||
HAS_PSUTIL_VERSION = False
|
||||
|
||||
if HAS_PSUTIL:
|
||||
import psutil
|
||||
import salt.utils.psutil_compat as psutil
|
||||
from collections import namedtuple
|
||||
|
||||
PSUTIL2 = psutil.version_info >= (2, 0)
|
||||
|
|
Loading…
Add table
Reference in a new issue