Module: check_mk
Branch: master
Commit: bae17bfb14edc7207000b7bff5b72bd3f2413922
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=bae17bfb14edc7…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Wed Apr 10 12:18:06 2019 +0200
mysql: consolidate
Change-Id: Ibd74dd26dd156086205d6127fd3b1aa71207367f
---
checks/mysql.include | 45 +++++++++++++++++++++++++++++++++
checks/mysql_capacity | 69 ++++++++++++++++++---------------------------------
checks/mysql_ping | 29 +++++++---------------
checks/mysql_slave | 57 ++++++++++++++----------------------------
4 files changed, 96 insertions(+), 104 deletions(-)
diff --git a/checks/mysql.include b/checks/mysql.include
new file mode 100644
index 0000000..64e103d
--- /dev/null
+++ b/checks/mysql.include
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2019 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.
+
+
+def _mysql_iter_instance_lines(lines):
+ def _detect_instance(line):
+ if line[0].startswith("[["):
+ instance = line[0].strip('[]')
+ return instance or "mysql"
+ return None
+
+ instance = None
+ for line in lines:
+ detected_instance = _detect_instance(line)
+ if detected_instance:
+ instance = detected_instance
+ elif instance:
+ yield instance, line
+
+ if instance is None:
+ for line in lines:
+ yield "mysql", line
diff --git a/checks/mysql_capacity b/checks/mysql_capacity
index dd1d47c..05db2e7 100644
--- a/checks/mysql_capacity
+++ b/checks/mysql_capacity
@@ -41,36 +41,21 @@
# test 409255936 54525952
-# FIXME: Crapy copy n paste! Consolidate with other mysql_* parse functions
def parse_mysql_capacity(info):
- def cut_spaces(line):
+ def _name_size_avail(line):
name = " ".join(line[:-2])
- size = line[-2]
- avail = line[-1]
- return name, size, avail
+ return name, line[-2], line[-1]
parsed = {}
- instance = False
- for line in info:
- if line[0].startswith("[["):
- instance = line[0][2:-2]
- if instance == "":
- instance = "mysql"
- parsed[instance] = []
- elif instance:
- parsed[instance].append(cut_spaces(line))
-
- if not instance:
- parsed['mysql'] = []
- for line in info:
- parsed['mysql'].append(cut_spaces(line))
-
+ for instance, line in _mysql_iter_instance_lines(info): # pylint:
disable=undefined-variable
+ name, size, avail = _name_size_avail(line)
+ parsed.setdefault(instance, {})[name] = (size, avail)
return parsed
@discover
def inventory_mysql_size(instance, values):
- for dbname, used, avail in values:
+ for dbname, (used, avail) in values.iteritems():
if dbname not in ["information_schema", "mysql",
"performance_schema"] \
and used != 'NULL' and avail != 'NULL':
yield "%s:%s" % (instance, dbname)
@@ -84,32 +69,25 @@ def check_mysql_size(item, params, parsed):
else:
instance, dbname = item.split(':')
- dbs = parsed[instance]
+ instance_data = parsed.get(instance)
+ if not instance_data or dbname not in instance_data:
+ return
+
+ size, _avail = instance_data[dbname]
# size and avail are given as bytes
- for db, size, _avail in dbs:
- if db == dbname:
- if size == 'NULL':
- return 3, "Missing information - Size is reported as
'NULL'"
- size = int(size)
- infotext = "Size: %s" % get_bytes_human_readable(size)
- if params:
- state = 0
- warn, crit = params
- warn_b = warn * 1024
- crit_b = crit * 1024
- perfdata = [("database_size", size, warn_b, crit_b)]
- if size > crit_b:
- state = 2
- elif size > warn_b:
- state = 1
- if state:
- infotext += " (warn/crit at %s/%s)" %
(get_bytes_human_readable(warn_b),
-
get_bytes_human_readable(crit_b))
- else:
- state = 0
- perfdata = [("database_size", size)]
- return state, infotext, perfdata
+ if size == 'NULL':
+ yield 3, "Missing information - Size is reported as 'NULL'"
+ return
+
+ size = int(size)
+ levels = (params[0] * 1024, params[1] * 1024) if params else None
+ yield check_levels(
+ size,
+ 'database_size',
+ levels,
+ human_readable_func=get_bytes_human_readable,
+ infoname="Size")
check_info['mysql_capacity'] = {
@@ -119,4 +97,5 @@ check_info['mysql_capacity'] = {
"service_description": "MySQL DB Size %s",
"has_perfdata": True,
"group": "mysql_db_size",
+ "includes": ["mysql.include"],
}
diff --git a/checks/mysql_ping b/checks/mysql_ping
index 3cb6a57..78d5fc9 100644
--- a/checks/mysql_ping
+++ b/checks/mysql_ping
@@ -34,29 +34,17 @@
# FIXME: Crapy copy n paste! Consolidate with other mysql_* parse functions
def parse_mysql_ping(info):
parsed = {}
- instance = False
- for line in info:
- if line[0].startswith("[["):
- instance = line[0][2:-2]
- if instance == "":
- instance = "mysql"
- parsed[instance] = []
- elif instance:
- parsed[instance].append(" ".join(line))
-
- # Support for the old plugin version without instances
- if not instance:
- parsed['mysql'] = [" ".join(x) for x in info]
-
+ for instance, line in _mysql_iter_instance_lines(info):
+ parsed.setdefault(instance, []).append(line)
return parsed
-def check_mysql_ping(item, _no_params, parsed):
- if item in parsed:
- message = parsed[item][0]
- if message == "mysqld is alive":
- return 0, "MySQL Deamon is alive"
- return 2, message
+@get_parsed_item_data
+def check_mysql_ping(_no_item, _no_params, data):
+ message = " ".join(data[0])
+ if message == "mysqld is alive":
+ return 0, "MySQL Deamon is alive"
+ return 2, message
check_info['mysql_ping'] = {
@@ -64,4 +52,5 @@ check_info['mysql_ping'] = {
"inventory_function": discover(),
"check_function": check_mysql_ping,
"service_description": "MySQL Instance %s",
+ "includes": ["mysql.include"],
}
diff --git a/checks/mysql_slave b/checks/mysql_slave
index 1dfdf90..4dc5831 100644
--- a/checks/mysql_slave
+++ b/checks/mysql_slave
@@ -25,56 +25,34 @@
# Boston, MA 02110-1301 USA.
-# FIXME: Crapy copy n paste! Consolidate with other mysql_* parse functions
def parse_mysql_slave(info):
- def parse_line(l):
- if ':' in l[0]:
- val = ' '.join(l[1:])
-
- # Parse some values
- try:
- val = int(val)
- except ValueError:
- if val == 'Yes':
- val = True
- elif val == 'No':
- val = False
- elif val == 'None':
- val = None
- return line[0][:-1], val
- return None, None
+ def parse_line(line):
+ if not line[0].endswith(':'):
+ return None, None
+ key = line[0][:-1]
+
+ val = ' '.join(line[1:])
+ # Parse some values
+ try:
+ val = int(val)
+ except ValueError:
+ val = {'Yes': True, 'No': False, 'None':
None}.get(val, val)
+ return key, val
parsed = {}
- instance = False
- for line in info:
- if line[0].startswith("[["):
- instance = line[0][2:-2]
- if instance == "":
- instance = "mysql"
- parsed[instance] = {}
- elif instance:
- key, value = parse_line(line)
- if key:
- parsed[instance][key] = value
-
- # Support for old Plugin version
- if not instance:
- parsed['mysql'] = {}
- for line in info:
- key, value = parse_line(line)
- if key:
- parsed['mysql'][key] = value
-
+ for instance, line in _mysql_iter_instance_lines(info): # pylint:
disable=undefined-variable
+ key, value = parse_line(line)
+ parsed.setdefault(instance, {})[key] = value
return parsed
@discover
-def inventory_mysql_slave(key, values):
+def inventory_mysql_slave(_key, values):
return len(values) > 0
@get_parsed_item_data
-def check_mysql_slave(item, params, data):
+def check_mysql_slave(_no_item, params, data):
state = 0
perfdata = []
output = []
@@ -122,4 +100,5 @@ check_info['mysql_slave'] = {
"service_description": "MySQL DB Slave %s",
"has_perfdata": True,
"group": "mysql_slave",
+ "includes": ["mysql.include"],
}