Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / solaris_services
blob91e5f498f7a39d49bcc8b786c9eb98b73d2f03a1
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2016 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 # OLD
28 # <<<solaris_services>>>
29 # STATE STIME FMRI
30 # $STATE $STIME $TYPE:${/CATEGORY/PATH/}$NAME:$INSTANCE
32 inventory_solaris_services_rules = []
35 def parse_solaris_services(info):
36 def get_parts_of_descr(descr):
37 svc_attrs = descr.split(":")
38 if len(svc_attrs) == 3:
39 svc_instance = svc_attrs[-1]
40 else:
41 svc_instance = None
42 svc_category, svc_name = svc_attrs[1].rsplit("/", 1)
43 return svc_attrs[0], svc_category, svc_name, svc_instance
45 parsed = {}
46 for line in info:
47 if len(line) < 3 or line == [u'STATE', u'STIME', u'FMRI']:
48 continue
49 svc_descr = line[2]
50 type_, category, name, instance = get_parts_of_descr(svc_descr)
51 parsed.setdefault(
52 svc_descr, {
53 "state": line[0],
54 "stime": line[1],
55 "type": type_,
56 "category": category,
57 "name": name,
58 "instance": instance,
60 return parsed
63 # .--single--------------------------------------------------------------.
64 # | _ _ |
65 # | ___(_)_ __ __ _| | ___ |
66 # | / __| | '_ \ / _` | |/ _ \ |
67 # | \__ \ | | | | (_| | | __/ |
68 # | |___/_|_| |_|\__, |_|\___| |
69 # | |___/ |
70 # '----------------------------------------------------------------------'
72 factory_settings["solaris_services_default_levels"] = {
73 "states": [
74 ("online", None, 0),
75 ("disabled", None, 2),
76 ("legacy_run", None, 0),
77 ("maintenance", None, 0),
79 "else": 2,
80 "additional_servicenames": [],
84 def inventory_solaris_services(parsed):
85 def regex_match(what, name):
86 if not what:
87 return True
88 for entry in what:
89 if entry.startswith("~") and regex(entry[1:]).match(name):
90 return True
91 elif entry == name:
92 return True
93 return False
95 def state_match(rule_state, state):
96 if rule_state is not None and rule_state != state:
97 return False
98 return True
100 def get_svc_name(svc_attrs):
101 return "%s/%s:%s" % (svc_attrs["category"], svc_attrs["name"], svc_attrs["instance"])
103 for rule in inventory_solaris_services_rules:
104 settings = rule[0]
105 descriptions = settings.get("descriptions", [])
106 categories = settings.get("categories", [])
107 names = settings.get("names", [])
108 instances = settings.get("instances", [])
109 state = settings.get("state")
110 for svc_descr, attrs in parsed.iteritems():
111 if regex_match(descriptions, svc_descr) \
112 and regex_match(categories, attrs["category"]) \
113 and regex_match(names, attrs["name"]) \
114 and regex_match(instances, attrs["instance"]) \
115 and state_match(state, attrs["state"]):
116 if settings.get('outcome') in [None, 'full_descr']:
117 name = svc_descr
118 else:
119 name = get_svc_name(attrs)
120 yield name, {}
123 def check_solaris_services(item, params, parsed):
124 for svc_name, attrs in parsed.iteritems():
125 if item in svc_name or svc_name in params['additional_servicenames']:
126 svc_state = attrs["state"]
127 svc_stime = attrs["stime"]
128 if svc_stime.count(":") == 2:
129 has_changed = True
130 info_stime = "Restarted in the last 24h (client's localtime: %s)" % svc_stime
131 else:
132 has_changed = False
133 info_stime = "Started on %s" % svc_stime.replace("_", " ")
135 check_state = 0
136 for _, p_stime, p_state in [x for x in params["states"] if x[0] == svc_state]:
137 if p_stime is not None:
138 if has_changed == p_stime:
139 check_state = p_state
140 break
141 else:
142 check_state = p_state
143 break
144 return check_state, "Status: %s, %s" % (svc_state, info_stime)
146 return params["else"], "Service not found"
149 check_info['solaris_services'] = {
150 'parse_function': parse_solaris_services,
151 'inventory_function': inventory_solaris_services,
152 'check_function': check_solaris_services,
153 'service_description': 'SMF Service %s', # Service Management Facility
154 'group': 'solaris_services',
155 'default_levels_variable': 'solaris_services_default_levels',
159 # .--summary-------------------------------------------------------------.
160 # | |
161 # | ___ _ _ _ __ ___ _ __ ___ __ _ _ __ _ _ |
162 # | / __| | | | '_ ` _ \| '_ ` _ \ / _` | '__| | | | |
163 # | \__ \ |_| | | | | | | | | | | | (_| | | | |_| | |
164 # | |___/\__,_|_| |_| |_|_| |_| |_|\__,_|_| \__, | |
165 # | |___/ |
166 # '----------------------------------------------------------------------'
169 def inventory_solaris_services_summary(parsed):
170 return [(None, {})]
173 def check_solaris_services_summary(_no_item, params, parsed):
174 yield 0, "%d services" % len(parsed)
176 services_by_state = {}
177 for svc_name, attrs in parsed.iteritems():
178 services = services_by_state.setdefault(attrs["state"], [])
179 services.append(svc_name)
181 for svc_state, svc_names in services_by_state.iteritems():
182 state = 0
183 extra_info = ""
184 if svc_state == "maintenance" and params.get("maintenance_state", 0):
185 extra_info += " (%s)" % ", ".join(svc_names)
186 state = params["maintenance_state"]
188 yield state, "%d %s%s" % (len(svc_names), svc_state.replace("_", " "), extra_info)
191 check_info['solaris_services.summary'] = {
192 'parse_function': parse_solaris_services,
193 'inventory_function': inventory_solaris_services_summary,
194 'check_function': check_solaris_services_summary,
195 'service_description': 'SMF Services Summary', # Service Management Facility
196 'group': 'solaris_services_summary',