Module: check_mk
Branch: master
Commit: 9784cb4292615dd2ef77eaff23b691028f4ff208
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9784cb4292615d…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Sun Oct 25 18:42:33 2015 +0100
Code cleanup: get rid of import struct in checks
---
checks/fc_port | 5 +++--
checks/mcdata_fcport | 10 +---------
modules/check_mk_base.py | 12 ++++++++++++
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/checks/fc_port b/checks/fc_port
index e416f4b..69e6831 100644
--- a/checks/fc_port
+++ b/checks/fc_port
@@ -86,14 +86,15 @@ def fc_port_getitem(num_ports, index, portname):
itemname += portname.strip()
return itemname
+
def fc_parse_counter(value):
# The counters are sent via SNMP as OCTETSTR, which is converted to
# a byte string by Check_MKs SNMP code. The counters seem to be
# 64 bit big endian values, which are converted to integers here
if len(value) == len("00 00 00 0C 8F 70 DD 74"):
value = "".join(map(chr, [ eval("0x" + v) for v in
value.split() ]))
- import struct
- return int(struct.unpack('>Q', value)[0])
+ return binstring_to_int(value)
+
def inventory_fc_port(info):
if not info:
diff --git a/checks/mcdata_fcport b/checks/mcdata_fcport
index 4715ca9..fbe0394 100644
--- a/checks/mcdata_fcport
+++ b/checks/mcdata_fcport
@@ -28,15 +28,7 @@ mcdata_fcport_speedbits = { "2": '1000000000',
"3": '2000000000' }
mcdata_fcport_opstatus = { "1" : "1", "2": "2",
"3": "testing", "4": "faulty" }
def mcdata_bin_to_64(bin):
- # McData encodes 64 bit counters as big endian HEX strings,
- # e.g. Hex-STRING: 00 00 00 01 AC C2 8C EE. Check_MK converts
- # such strings into binary Python strings
- value = 0
- mult = 1
- for byte in bin[::-1]:
- value += mult * ord(byte)
- mult *= 256
- return str(value)
+ return str(binstring_to_int(bin))
def mcdata_fcport_convert_to_if64(info):
return map(mcdata_fcport_convert_line_to_if64, info)
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 9986616..6d743ca 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -2119,6 +2119,18 @@ def savefloat(f):
except:
return 0.0
+# 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)
+def binstring_to_int(binstring):
+ value = 0
+ mult = 1
+ for byte in binstring[::-1]:
+ value += mult * ord(byte)
+ mult *= 256
+ return value
+
+
# Takes bytes as integer and returns a string which represents the bytes in a
# more human readable form scaled to GB/MB/KB
# The unit parameter simply changes the returned string, but does not interfere