Module: check_mk
Branch: master
Commit: c637d14961bc73851bb6a238a443f44001a54199
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c637d14961bc73…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Jun 5 15:57:37 2018 +0200
Removed an SNMP-related import cycle.
This brings the number of cycles down to 13 (from 14).
Change-Id: I980e31e89239b1773467e75844ffe2fd1bcc9e4a
---
cmk_base/check_api.py | 18 ++++++++--------
cmk_base/snmp.py | 57 +++++++-------------------------------------------
cmk_base/snmp_utils.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 59 deletions(-)
diff --git a/cmk_base/check_api.py b/cmk_base/check_api.py
index 6fc064c..6470c4b 100644
--- a/cmk_base/check_api.py
+++ b/cmk_base/check_api.py
@@ -97,7 +97,7 @@ import cmk_base.console as _console
import cmk_base.config as _config
import cmk_base.rulesets as _rulesets
import cmk.defines as _defines
-import cmk_base.snmp as _snmp
+import cmk_base.snmp_utils as _snmp_utils
import cmk_base.item_state as _item_state
import cmk_base.prediction as _prediction
@@ -138,15 +138,15 @@ core_state_names = _defines.short_service_state_names()
# Symbolic representations of states in plugin output
state_markers = ["", "(!)", "(!!)", "(?)"]
-BINARY = _snmp.BINARY
-CACHED_OID = _snmp.CACHED_OID
+BINARY = _snmp_utils.BINARY
+CACHED_OID = _snmp_utils.CACHED_OID
-OID_END = _snmp.OID_END
-OID_STRING = _snmp.OID_STRING
-OID_BIN = _snmp.OID_BIN
-OID_END_BIN = _snmp.OID_END_BIN
-OID_END_OCTET_STRING = _snmp.OID_END_OCTET_STRING
-binstring_to_int = _snmp.binstring_to_int
+OID_END = _snmp_utils.OID_END
+OID_STRING = _snmp_utils.OID_STRING
+OID_BIN = _snmp_utils.OID_BIN
+OID_END_BIN = _snmp_utils.OID_END_BIN
+OID_END_OCTET_STRING = _snmp_utils.OID_END_OCTET_STRING
+binstring_to_int = _snmp_utils.binstring_to_int
# Management board checks
MGMT_PRECEDENCE = "mgmt_precedence" # Use management board address/credentials
when it's a SNMP host
diff --git a/cmk_base/snmp.py b/cmk_base/snmp.py
index cdcaa32..bcf09c4 100644
--- a/cmk_base/snmp.py
+++ b/cmk_base/snmp.py
@@ -40,6 +40,7 @@ import cmk_base.ip_lookup as ip_lookup
import cmk_base.agent_simulator
from cmk_base.exceptions import MKSNMPError
import cmk_base.cleanup
+import cmk_base.snmp_utils
try:
import cmk_base.cee.inline_snmp as inline_snmp
@@ -47,12 +48,6 @@ except ImportError:
inline_snmp = None
-OID_END = 0 # Suffix-part of OID that was not specified
-OID_STRING = -1 # Complete OID as string ".1.3.6.1.4.1.343...."
-OID_BIN = -2 # Complete OID as binary string
"\x01\x03\x06\x01..."
-OID_END_BIN = -3 # Same, but just the end part
-OID_END_OCTET_STRING = -4 # yet same, but omit first byte (assuming that is the length
byte)
-
_enforce_stored_walks = False
# TODO: Replace this by generic caching
@@ -136,44 +131,6 @@ def _clear_other_hosts_oid_cache(hostname):
#.
-# .--CheckHelpers--------------------------------------------------------.
-# | ____ _ _ _ _ _ |
-# | / ___| |__ ___ ___| | _| | | | ___| |_ __ ___ _ __ ___ |
-# | | | | '_ \ / _ \/ __| |/ / |_| |/ _ \ | '_ \ / _ \ '__/ __| |
-# | | |___| | | | __/ (__| <| _ | __/ | |_) | __/ | \__ \ |
-# | \____|_| |_|\___|\___|_|\_\_| |_|\___|_| .__/ \___|_| |___/ |
-# | |_| |
-# +----------------------------------------------------------------------+
-# | Some SNMP related check helping code. Offered to the checks by the |
-# | check API. |
-# '----------------------------------------------------------------------'
-
-def BINARY(oid):
- """Tell Check_MK to process this OID as binary data to the
check."""
- return "binary", oid
-
-
-def CACHED_OID(oid):
- """Use this to mark OIDs as being cached for regular checks,
- but not for discovery"""
- return "cached", oid
-
-
-def binstring_to_int(binstring):
- """Convert a string to an integer.
-
- This is done by consideren the string to by a little endian byte string.
- Such strings are sometimes used by SNMP to encode 64 bit counters without
- needed COUNTER64 (which is not available in SNMP v1)."""
- value = 0
- mult = 1
- for byte in binstring[::-1]:
- value += mult * ord(byte)
- mult *= 256
- return value
-
-
-#.
# .--Generic SNMP--------------------------------------------------------.
# | ____ _ ____ _ _ __ __ ____ |
# | / ___| ___ _ __ ___ _ __(_) ___ / ___|| \ | | \/ | _ \ |
@@ -223,7 +180,7 @@ def get_snmp_table(access_data, check_plugin_name, oid_info,
use_snmpwalk_cache)
# in later. If the column is OID_STRING or OID_BIN we do something
# similar: we fill in the complete OID of the entry, either as
# string or as binary UTF-8 encoded number string
- if column in [ OID_END, OID_STRING, OID_BIN, OID_END_BIN,
OID_END_OCTET_STRING ]:
+ if column in [ cmk_base.snmp_utils.OID_END, cmk_base.snmp_utils.OID_STRING,
cmk_base.snmp_utils.OID_BIN, cmk_base.snmp_utils.OID_END_BIN,
cmk_base.snmp_utils.OID_END_OCTET_STRING ]:
if index_column >= 0 and index_column != colno:
raise MKGeneralException("Invalid SNMP OID specification in
implementation of check. "
"You can only use one of OID_END, OID_STRING, OID_BIN,
OID_END_BIN and OID_END_OCTET_STRING.")
@@ -245,15 +202,15 @@ def get_snmp_table(access_data, check_plugin_name, oid_info,
use_snmpwalk_cache)
# Take end-oids of non-index columns as indices
fetchoid, max_column, value_encoding = columns[max_len_col]
for o, _unused_value in max_column:
- if index_format == OID_END:
+ if index_format == cmk_base.snmp_utils.OID_END:
index_rows.append((o, _extract_end_oid(fetchoid, o)))
- elif index_format == OID_STRING:
+ elif index_format == cmk_base.snmp_utils.OID_STRING:
index_rows.append((o, o))
- elif index_format == OID_BIN:
+ elif index_format == cmk_base.snmp_utils.OID_BIN:
index_rows.append((o, _oid_to_bin(o)))
- elif index_format == OID_END_BIN:
+ elif index_format == cmk_base.snmp_utils.OID_END_BIN:
index_rows.append((o, _oid_to_bin(_extract_end_oid(fetchoid, o))))
- else: # OID_END_OCTET_STRING:
+ else: # cmk_base.snmp_utils.OID_END_OCTET_STRING:
index_rows.append((o, _oid_to_bin(_extract_end_oid(fetchoid,
o))[1:]))
columns[index_column] = fetchoid, index_rows, value_encoding
diff --git a/cmk_base/snmp_utils.py b/cmk_base/snmp_utils.py
new file mode 100644
index 0000000..1bec64b
--- /dev/null
+++ b/cmk_base/snmp_utils.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+
+OID_END = 0 # Suffix-part of OID that was not specified
+OID_STRING = -1 # Complete OID as string ".1.3.6.1.4.1.343...."
+OID_BIN = -2 # Complete OID as binary string
"\x01\x03\x06\x01..."
+OID_END_BIN = -3 # Same, but just the end part
+OID_END_OCTET_STRING = -4 # yet same, but omit first byte (assuming that is the length
byte)
+
+
+def BINARY(oid):
+ """Tell Check_MK to process this OID as binary data to the
check."""
+ return "binary", oid
+
+
+def CACHED_OID(oid):
+ """Use this to mark OIDs as being cached for regular checks,
+ but not for discovery"""
+ return "cached", oid
+
+
+def binstring_to_int(binstring):
+ """Convert a string to an integer.
+
+ This is done by consideren the string to by a little endian byte string.
+ Such strings are sometimes used by SNMP to encode 64 bit counters without
+ needed COUNTER64 (which is not available in SNMP v1)."""
+ value = 0
+ mult = 1
+ for byte in binstring[::-1]:
+ value += mult * ord(byte)
+ mult *= 256
+ return value