2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
28 # <<<solaris_services>>>
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]
42 svc_category
, svc_name
= svc_attrs
[1].rsplit("/", 1)
43 return svc_attrs
[0], svc_category
, svc_name
, svc_instance
47 if len(line
) < 3 or line
== [u
'STATE', u
'STIME', u
'FMRI']:
50 type_
, category
, name
, instance
= get_parts_of_descr(svc_descr
)
63 # .--single--------------------------------------------------------------.
65 # | ___(_)_ __ __ _| | ___ |
66 # | / __| | '_ \ / _` | |/ _ \ |
67 # | \__ \ | | | | (_| | | __/ |
68 # | |___/_|_| |_|\__, |_|\___| |
70 # '----------------------------------------------------------------------'
72 factory_settings
["solaris_services_default_levels"] = {
75 ("disabled", None, 2),
76 ("legacy_run", None, 0),
77 ("maintenance", None, 0),
80 "additional_servicenames": [],
84 def inventory_solaris_services(parsed
):
85 def regex_match(what
, name
):
89 if entry
.startswith("~") and regex(entry
[1:]).match(name
):
95 def state_match(rule_state
, state
):
96 if rule_state
is not None and rule_state
!= state
:
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
:
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']:
119 name
= get_svc_name(attrs
)
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:
130 info_stime
= "Restarted in the last 24h (client's localtime: %s)" % svc_stime
133 info_stime
= "Started on %s" % svc_stime
.replace("_", " ")
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
142 check_state
= p_state
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-------------------------------------------------------------.
161 # | ___ _ _ _ __ ___ _ __ ___ __ _ _ __ _ _ |
162 # | / __| | | | '_ ` _ \| '_ ` _ \ / _` | '__| | | | |
163 # | \__ \ |_| | | | | | | | | | | | (_| | | | |_| | |
164 # | |___/\__,_|_| |_| |_|_| |_| |_|\__,_|_| \__, | |
166 # '----------------------------------------------------------------------'
169 def inventory_solaris_services_summary(parsed
):
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():
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',