Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / checks / mssql_instance
blobd0afa858b29feded49170b6ba7f67b2cd7074b3a
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 # <<<mssql_instance:sep(124)>>>
28 # MSSQL_MSSQLSERVER|config|10.50.1600.1|Enterprise Edition|BLABLA
29 # <<<mssql_instance:sep(124)>>>
30 # MSSQL_SQLEXPRESS|config|10.50.1600.1|Express Edition|
31 # <<<mssql_instance:sep(124)>>>
32 # MSSQL_MICROSOFT##SSEE|config|9.00.5000.00|Windows Internal Database|
33 # <<<mssql_instance:sep(124)>>>
34 # MSSQL_MSSQLSERVER|state|0|[DBNETLIB][ConnectionOpen (Connect()).]SQL Server existiert nicht oder Zugriff verweigert.
35 # <<<mssql_instance:sep(124)>>>
36 # MSSQL_SQLEXPRESS|state|1|[DBNETLIB][ConnectionOpen (Connect()).]SQL Server existiert nicht oder Zugriff verweigert.
37 # <<<mssql_instance:sep(124)>>>
38 # MSSQL_MICROSOFT##SSEE|state|0|[DBNETLIB][ConnectionOpen (Connect()).]SQL Server existiert nicht oder Zugriff verweigert.
40 # <<<mssql_instance:sep(124)>>>
41 # ERROR: Failed to gather SQL server instances
44 def _parse_prod_version(entry):
45 if entry.startswith("8."):
46 version = "2000"
47 elif entry.startswith("9."):
48 version = "2005"
49 elif entry.startswith("10.0"):
50 version = "2008"
51 elif entry.startswith("10.50"):
52 version = "2008R2"
53 elif entry.startswith("11."):
54 version = "2012"
55 elif entry.startswith("12."):
56 version = "2014"
57 elif entry.startswith("13."):
58 version = "2016"
59 elif entry.startswith("14."):
60 version = "2017"
61 else:
62 return "unknown[%s]" % entry
63 return "Microsoft SQL Server %s" % version
66 def parse_mssql_instance(info):
67 parsed = {}
68 for line in info:
69 if line[0].startswith("ERROR:"):
70 continue
71 elif line[0][:6] == "MSSQL_":
72 # Remove the MSSQL_ prefix from the ID for this check
73 instance_id = line[0][6:]
74 else:
75 instance_id = line[0]
77 instance = parsed.setdefault(
78 instance_id,
80 # it may happen that the state line is missing, add some fallback as default here
81 "state": "0",
82 "error_msg": "Unable to connect to database (Agent reported no state)",
85 if line[1] == "config":
86 instance.update({
87 "version_info": "%s - %s" % (line[2], line[3]),
88 "cluster_name": line[4],
90 elif line[1] == "state":
91 instance.update({
92 "state": line[2],
93 "error_msg": "|".join(line[3:]),
96 elif line[1] == "details":
97 _parse_prod_version(line[2])
98 instance.update({
99 "prod_version_info": "%s (%s) (%s) - %s" % (_parse_prod_version(line[2]), line[3],
100 line[2], line[4])
103 return parsed
106 def inventory_mssql_instance(parsed):
107 for instance_id in parsed.iterkeys():
108 yield instance_id, {}
111 def check_mssql_instance(item, params, parsed):
112 instance = parsed.get(item)
113 if not instance:
114 return
116 state = 2
117 if params is not None and \
118 params.get("map_connection_state") is not None:
119 state = params["map_connection_state"]
121 if instance["state"] == "0":
122 yield state, "Failed to connect to database (%s)" % instance["error_msg"]
124 yield 0, "Version: %s" % instance.get("prod_version_info", instance["version_info"])
125 if instance["cluster_name"] != "":
126 yield 0, "Clustered as %s" % instance["cluster_name"]
129 check_info["mssql_instance"] = {
130 'parse_function': parse_mssql_instance,
131 'check_function': check_mssql_instance,
132 'inventory_function': inventory_mssql_instance,
133 'service_description': 'MSSQL %s Instance',
134 'group': 'mssql_instance',