Module: check_mk
Branch: master
Commit: f6f33997875531bbf925c3c7a368db7f4d2e959b
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f6f33997875531…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Sun Mar 31 15:57:34 2019 +0200
k8s_service_port: new check to monitor ports of Kubernetes services
Change-Id: I8d61af850f3deec8134e2c6f854a34e1281583ab
---
checkman/k8s_service_port | 14 ++++
checks/k8s_service_port | 71 +++++++++++++++++
cmk/gui/plugins/wato/check_parameters/k8s_port.py | 97 +++++++++++++++++++++++
cmk/special_agents/agent_kubernetes.py | 5 +-
4 files changed, 185 insertions(+), 2 deletions(-)
diff --git a/checkman/k8s_service_port b/checkman/k8s_service_port
new file mode 100644
index 0000000..28f007b
--- /dev/null
+++ b/checkman/k8s_service_port
@@ -0,0 +1,14 @@
+title: Kubernetes service ports
+agents: kubernetes
+catalog: containerization/kubernetes
+license: GPL
+distribution: check_mk
+description:
+ This check monitors the ports of a Kubernetes service.
+ Levels for expected values can be set via WATO.
+
+inventory:
+ One service per port is created.
+
+item:
+ The name of the port if given otherwise the port number.
\ No newline at end of file
diff --git a/checks/k8s_service_port b/checks/k8s_service_port
new file mode 100644
index 0000000..e6b875e
--- /dev/null
+++ b/checks/k8s_service_port
@@ -0,0 +1,71 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2018 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 k8s_check_value(value, expected, infotext_template):
+ state = 0
+ infotext = infotext_template % value
+ if expected is not None and value != expected:
+ state = 2
+ infotext += ' (expected %s)' % expected
+ return state, infotext
+
+
+@get_parsed_item_data
+def check_k8s_service_port(item, params, parsed):
+ yield k8s_check_value(
+ parsed['port'],
+ params.get('port'),
+ 'Port: %s',
+ )
+ yield k8s_check_value(
+ parsed['target_port'],
+ params.get('target_port'),
+ 'Target port: %s',
+ )
+
+ if parsed['node_port']:
+ yield k8s_check_value(
+ parsed['node_port'],
+ params.get('node_port'),
+ 'Node port: %s',
+ )
+
+ yield k8s_check_value(
+ parsed['protocol'],
+ params.get('protocol'),
+ 'Protocol: %s',
+ )
+
+
+check_info['k8s_service_port'] = {
+ 'parse_function': parse_k8s,
+ 'inventory_function': discover(),
+ 'check_function': check_k8s_service_port,
+ 'service_description': 'Port %s',
+ 'group': 'k8s_port',
+ 'includes': ['k8s.include'],
+}
diff --git a/cmk/gui/plugins/wato/check_parameters/k8s_port.py
b/cmk/gui/plugins/wato/check_parameters/k8s_port.py
new file mode 100644
index 0000000..145b210
--- /dev/null
+++ b/cmk/gui/plugins/wato/check_parameters/k8s_port.py
@@ -0,0 +1,97 @@
+#!/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.
+
+from cmk.gui.i18n import _
+from cmk.gui.valuespec import (
+ Dictionary,
+ DropdownChoice,
+ Integer,
+ TextAscii,
+)
+
+from cmk.gui.plugins.wato import (
+ CheckParameterRulespecWithItem,
+ rulespec_registry,
+ RulespecGroupCheckParametersApplications,
+)
+
+
+(a)rulespec_registry.register
+class RulespecCheckgroupParametersK8SResources(CheckParameterRulespecWithItem):
+ @property
+ def group(self):
+ return RulespecGroupCheckParametersApplications
+
+ @property
+ def check_group_name(self):
+ return "k8s_port"
+
+ @property
+ def title(self):
+ return _("Kubernetes Port")
+
+ @property
+ def match_type(self):
+ return "dict"
+
+ @property
+ def parameter_valuespec(self):
+ return Dictionary(
+ elements=[
+ ('port', Integer(
+ title=_('Port'),
+ minvalue=0,
+ maxvalue=65535,
+ )),
+ ('target_port', Integer(
+ title=_('Target port'),
+ minvalue=0,
+ maxvalue=65535,
+ )),
+ ('node_port', Integer(
+ title=_('Node port'),
+ minvalue=0,
+ maxvalue=65535,
+ )),
+ ('protocol',
+ DropdownChoice(
+ title=_('Protocol'),
+ choices=[
+ ('TCP', _('TCP')),
+ ('UDP', _('UDP')),
+ ('HTTP', _('HTTP')),
+ ('PROXY', _('PROXY')),
+ ('SCTP', _('SCTP')),
+ ],
+ )),
+ ],)
+
+ @property
+ def item_spec(self):
+ return TextAscii(
+ title=_("Port"),
+ help=_("Name or number of the port"),
+ )
diff --git a/cmk/special_agents/agent_kubernetes.py
b/cmk/special_agents/agent_kubernetes.py
index 036c08d..351e152 100644
--- a/cmk/special_agents/agent_kubernetes.py
+++ b/cmk/special_agents/agent_kubernetes.py
@@ -311,7 +311,8 @@ class Service(Metadata):
def ports(self):
# port is the only field that is not optional
return {
- port.port: {
+ port.name if port.name else port.port: {
+ 'port': port.port,
'name': port.name,
'protocol': port.protocol,
'target_port': port.target_port,
@@ -1032,7 +1033,7 @@ class ApiData(object):
g.join('labels', self.services.labels())
g.join('k8s_selector', self.services.selector())
g.join('k8s_service_info', self.services.infos())
- g.join('k8s_service_ports', self.services.ports())
+ g.join('k8s_service_port', self.services.ports())
pod_names = {
service_name: {
'names': [pod.name for pod in pods]