Module: check_mk
Branch: master
Commit: e0cebeb39db1e3938f0f010cdf1267827529b82e
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e0cebeb39db1e3…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Tue Oct 15 01:22:47 2013 +0200
FIX: fixed error handling in SNMP scan
---
ChangeLog | 2 ++
modules/check_mk.py | 46 ++++++++++++++++++++++++++++++----------------
2 files changed, 32 insertions(+), 16 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d2f562c..36d1b3c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,8 @@
* FIX: continue inventory, if one check does not support it
* FIX: fix configuration of explicit SNMP community, allow unicode
* FIX: avoid invalid cache of 2nd and up hosts in bulk inventory
+ * FIX: fixed error handling in SNMP scan, inventory check fails now
+ if SNMP agent is not responding
* inventory check of SNMP devices now does scan per default (configurable)
Multisite:
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 3d7d7b0..d29e890 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -765,6 +765,11 @@ def snmp_get_oid(hostname, ipaddress, oid):
snmp_process = os.popen(command, "r")
line = snmp_process.readline().strip()
+ if not line:
+ if opt_debug:
+ sys.stdout.write("Error in response to snmpget.\n")
+ return None
+
item, value = line.split("=", 1)
value = value.strip()
if opt_debug:
@@ -828,11 +833,13 @@ def snmp_scan(hostname, ipaddress):
sys.stdout.write("Scanning host %s(%s) for SNMP checks..." % (hostname,
ipaddress))
sys.stdout.flush()
if not in_binary_hostlist(hostname, snmp_without_sys_descr):
- sys_descr = get_single_oid(hostname, ipaddress, ".1.3.6.1.2.1.1.1.0")
+ sys_descr_oid = ".1.3.6.1.2.1.1.1.0"
+ sys_descr = get_single_oid(hostname, ipaddress, sys_descr_oid)
if sys_descr == None:
- if opt_verbose:
- sys.stderr.write("no SNMP answer\n")
- return []
+ raise MKSNMPError("Cannot fetch system description OID %s" %
sys_descr_oid)
+ # if opt_verbose:
+ # sys.stderr.write("no SNMP answer\n")
+ # return []
found = []
for check_type, check in check_info.items():
@@ -2488,7 +2495,14 @@ def do_snmp_scan(hostnamelist, check_only=False,
include_state=False):
except:
sys.stdout.write("Cannot resolve %s into IP address. Skipping.\n" %
hostname)
continue
- checknames = snmp_scan(hostname, ipaddress)
+ try:
+ checknames = snmp_scan(hostname, ipaddress)
+ except Exception, e:
+ if opt_debug:
+ raise
+ sys.stdout.write("SNMP scan for %s failed: %s\n" % (hostname, e))
+ continue
+
for checkname in checknames:
if opt_debug:
sys.stdout.write("Trying inventory for %s on %s\n" %
(checkname, hostname))
@@ -2715,19 +2729,16 @@ def check_inventory(hostname):
is_snmp = is_snmp_host(hostname)
is_tcp = is_tcp_host(hostname)
check_table = get_check_table(hostname)
- if is_snmp and inventory_check_do_scan:
- try:
+
+ try:
+ if is_snmp and inventory_check_do_scan:
ipaddress = lookup_ipaddress(hostname)
snmp_checktypes = snmp_scan(hostname, ipaddress)
- except:
- if opt_debug:
- raise
- pass
- else:
- snmp_checktypes = []
+ else:
+ snmp_checktypes = []
+
+ hosts_checktypes = set([ ct for (ct, item), params in check_table.items() ])
- hosts_checktypes = set([ ct for (ct, item), params in check_table.items() ])
- try:
for ct in inventorable_checktypes("all"):
if check_uses_snmp(ct) and not is_snmp:
continue # Skip SNMP checks on non-SNMP hosts
@@ -2761,7 +2772,10 @@ def check_inventory(hostname):
# Honor rule settings for "Status of the Check_MK service". In case of
# a problem we assume a connection error here.
spec = exit_code_spec(hostname)
- what = isinstance(e, MKAgentError) and "connection" or
"exception"
+ if isinstance(e, MKAgentError) or isinstance(e, MKSNMPError):
+ what = "connection"
+ else:
+ what = "exception"
status = spec.get(what, 3)
sys.stdout.write("%s - %s\n" % (nagios_state_names[status], e))
sys.exit(status)