Skip to content

Commit

Permalink
[fix] net_io_counters used instead network_io_counters in psutil>=1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Charykov committed Jul 6, 2016
1 parent 23a2f75 commit 53d90b0
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions li_metrics_agent.py
Expand Up @@ -219,6 +219,25 @@ def get_name(state):
def is_valid(state):
return True if state in [AgentState.IDLE, AgentState.ACTIVE] else False

class PsutilAdapter(object):
"""Adapter for psutil methods calls depends on psutil current version"""

@staticmethod
def normalized_version():
# psutil versions are simple integers as x.x.x
v = psutil.__version__
return tuple(map(int, (v.split("."))))

@classmethod
def version_gte_than(self, version_tuple):
return self.normalized_version() >= version_tuple

@classmethod
def net_io_counters(self, pernic=False):
if self.version_gte_than((1,0,0)):
return psutil.net_io_counters(self, pernic=False)
else
return psutil.network_io_counters(self, pernic=False)

This comment has been minimized.

Copy link
@martinfijal

martinfijal Jul 6, 2016

Contributor

You always set pernic parameter to false here on both of these calls. Change it to pernic=pernic to forward the method parameter correctly.


class NagiosPluginExitCode(object):
"""Enum mapping process exit codes to Nagios service states."""
Expand Down Expand Up @@ -648,7 +667,7 @@ def _next_line_builtin(self, args):
interface = ""
if len(args) > 1 and args[1].lower() not in valid_metrics:
interface = args[1].replace("'", "")
counters = psutil.network_io_counters(pernic=True)
counters = PsutilAdapter.net_io_counters(pernic=True)
try:
counters = counters[interface]
except KeyError:
Expand All @@ -658,7 +677,7 @@ def _next_line_builtin(self, args):
# Format for label name in the report line below
interface = "_" + interface
else:
counters = psutil.network_io_counters(pernic=False)
counters = PsutilAdapter.net_io_counters(pernic=False)

metric = 'bps'
metric_index = 2 if interface else 1
Expand Down

0 comments on commit 53d90b0

Please sign in to comment.