Module: check_mk
Branch: master
Commit: 64d9889a4fddf19b8aa9f1a1139e8ddf2c491219
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=64d9889a4fddf1…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Mon Sep 2 12:28:49 2013 +0200
Further process in new snmp handling
---
modules/check_mk.py | 119 ++++++++++-----------------------------------------
modules/snmp.py | 20 ++++++---
2 files changed, 36 insertions(+), 103 deletions(-)
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 1c40eed..f022468 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -578,69 +578,6 @@ def aggregated_service_name(hostname, servicedesc):
# | |
# +----------------------------------------------------------------------+
-# Returns command lines for snmpwalk and snmpget including
-# options for authentication. This handles communities and
-# authentication for SNMP V3. Also bulkwalk hosts
-def snmp_walk_command(hostname):
- return snmp_base_command('walk', hostname) + " -Cc"
-
-# Constructs the basic snmp commands for a host with all important information
-# like the commandname, SNMP version and credentials.
-# This function also changes snmpbulkwalk to snmpwalk for snmpv1.
-def snmp_base_command(what, hostname):
- # if the credentials are a string, we use that as community,
- # if it is a four-tuple, we use it as V3 auth parameters:
- # (1) security level (-l)
- # (2) auth protocol (-a, e.g. 'md5')
- # (3) security name (-u)
- # (4) auth password (-A)
- # And if it is a six-tuple, it has the following additional arguments:
- # (5) privacy protocol (DES|AES) (-x)
- # (6) privacy protocol pass phrase (-X)
-
- credentials = snmp_credentials_of(hostname)
- if what == 'get':
- command = 'snmpget'
- elif what == 'getnext':
- command = 'snmpgetnext -Cf'
- else:
- command = 'snmpbulkwalk'
-
- # Handle V1 and V2C
- if type(credentials) == str:
- if is_bulkwalk_host(hostname):
- options = '-v2c'
- else:
- if what == 'walk':
- command = 'snmpwalk'
- if is_snmpv2c_host(hostname):
- options = '-v2c'
- else:
- options = '-v1'
- options += " -c '%s'" % credentials
-
- # Handle V3
- else:
- if len(credentials) == 6:
- options = "-v3 -l '%s' -a '%s' -u '%s' -A
'%s' -x '%s' -X '%s'" % tuple(credentials)
- elif len(credentials) == 4:
- options = "-v3 -l '%s' -a '%s' -u '%s' -A
'%s'" % tuple(credentials)
- else:
- raise MKGeneralException("Invalid SNMP credentials '%r' for host
%s: must be string, 4-tuple or 6-tuple" % (credentials, hostname))
-
- # Do not load *any* MIB files. This save lot's of CPU.
- options += " -m '' -M ''"
-
- # Configuration of timing and retries
- settings = snmp_timing_of(hostname)
- if "timeout" in settings:
- options += " -t %d" % settings["timeout"]
- if "retries" in settings:
- options += " -r %d" % settings["retries"]
-
- return command + ' ' + options
-
-
# Determine SNMP community for a specific host. It the host is found
# int the map snmp_communities, that community is returned. Otherwise
# the snmp_default_community is returned (wich is preset with
@@ -738,11 +675,11 @@ def snmp_get_oid(hostname, oid):
# In case of .*, check if prefix is the one we are looking for
value = None
- elif value == 'NULL':
+ elif value == 'NULL' or var.type in [ 'NOSUCHINSTANCE',
'NOSUCHOBJECT' ]:
value = None
- elif value is not None and value[0] == '"' and value[0] ==
'"':
- value = value[1:-1]
+ elif value is not None:
+ value = strip_snmp_value(value)
if opt_verbose and opt_debug:
sys.stdout.write("=> [%r] %s\n" % (value, var.type))
@@ -3953,42 +3890,30 @@ def do_snmpwalk(hostnames):
def do_snmpwalk_on(hostname, filename):
if opt_verbose:
sys.stdout.write("%s:\n" % hostname)
- ip = lookup_ipaddress(hostname)
- portspec = snmp_port_spec(hostname)
- cmd = snmp_walk_command(hostname) + " -On -Ob -OQ -Ot %s%s " % (ip,
portspec)
- if opt_debug:
- print 'Executing: %s' % cmd
+
out = file(filename, "w")
- for oid in [ "", "1.3.6.1.4.1" ]: # SNMPv2-SMI::enterprises
- oids = []
- values = []
+ for oid in [
+ ".1.3.6.1.2.1", # SNMPv2-SMI::mib-2
+ ".1.3.6.1.4.1" # SNMPv2-SMI::enterprises
+ ]:
if opt_verbose:
- sys.stdout.write("%s..." % (cmd + oid))
+ sys.stdout.write("Walk on \"%s\"..." % oid)
sys.stdout.flush()
- count = 0
- f = os.popen(cmd + oid)
- while True:
- line = f.readline()
- if not line:
- break
- parts = line.split("=", 1)
- if len(parts) != 2:
- continue
- oid, value = parts
- value = value.rstrip("\n")
- if value.lstrip().startswith('"'):
- while value[-1] != '"':
- value += f.readline().rstrip("\n")
-
- if not oid.startswith("."):
- oid = "." + oid
- oids.append(oid)
- values.append(value)
- for oid, value in zip(oids, values):
+ results = snmpwalk_on_suboid(hostname, oid, strip_values = False)
+ for oid, value in results:
+ # FIXME: Need to parse some datatypes to have a compatible data format
+ # with older versions:
+ # .1.3.6.1.2.1.1.3.0 0:1:43:32.82
+ # ->
+ # .1.3.6.1.2.1.1.3.0 620972
+ #
+ # .1.3.6.1.2.1.1.8.0 0:0:00:00.00
+ # ->
+ # .1.3.6.1.2.1.1.8.0 0
out.write("%s %s\n" % (oid, value.strip()))
- count += 1
+
if opt_verbose:
- sys.stdout.write("%d variables.\n" % count)
+ sys.stdout.write("%d variables.\n" % len(results))
out.close()
if opt_verbose:
diff --git a/modules/snmp.py b/modules/snmp.py
index a6bbd42..638b4f5 100644
--- a/modules/snmp.py
+++ b/modules/snmp.py
@@ -93,7 +93,8 @@ def init_snmp_host(hostname):
priv_proto = 'DEFAULT'
priv_pass = ''
else:
- raise MKGeneralException("Invalid SNMP credentials '%r' for host
%s: must be string, 4-tuple or 6-tuple" % (credentials, hostname))
+ raise MKGeneralException("Invalid SNMP credentials '%r' for host
%s: "
+ "must be string, 4-tuple or 6-tuple" %
(credentials, hostname))
try:
ipaddress = lookup_ipaddress(hostname)
@@ -119,10 +120,11 @@ def init_snmp_host(hostname):
g_snmp_sessions[hostname] = s
return s
-def snmpwalk_on_suboid(hostname, ip, oid):
+def snmpwalk_on_suboid(hostname, oid, strip_values = True):
s = init_snmp_host(hostname)
- # FIXME: handle bulkwalk/getnext walk
+ # FIXME: handle bulkwalk/getnext walk. At the moment it seems only
+ # a getnext walk is done in all cases.
# Remove trailing .0 for walks
# .1.3.6.1.2.1.1.5.0 but receive the value for 1.3.6.1.2.1.1.6.0.
@@ -130,7 +132,7 @@ def snmpwalk_on_suboid(hostname, ip, oid):
oid = oid[:-2]
if opt_debug:
- sys.stdout.write("Executing SNMPWALK of %s on %s\n" % (oid, hostname))
+ sys.stdout.write("Executing SNMPWALK of \"%s\" on %s\n" %
(oid, hostname))
var_list = netsnmp.VarList(netsnmp.Varbind(oid))
res = s.walk(var_list)
@@ -142,8 +144,14 @@ def snmpwalk_on_suboid(hostname, ip, oid):
results = []
for var in var_list:
+ if var.iid is None:
+ continue
+
this_oid = var.tag + '.' + var.iid
- value = strip_snmp_value(var.val)
+ if strip_values:
+ value = strip_snmp_value(var.val)
+ else:
+ value = var.val
if opt_verbose and opt_debug:
sys.stdout.write("%s => [%s] %r\n" % (this_oid, value,
var.type))
@@ -217,7 +225,7 @@ def get_snmp_table(hostname, ip, oid_info):
if opt_use_snmp_walk or is_usewalk_host(hostname):
rowinfo = get_stored_snmpwalk(hostname, fetchoid)
else:
- rowinfo = snmpwalk_on_suboid(hostname, ip, fetchoid)
+ rowinfo = snmpwalk_on_suboid(hostname, fetchoid)
columns.append((fetchoid, rowinfo))
number_rows = len(rowinfo)