2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | Copyright Mathias Kettner 2018 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.
29 # "mincount": (tuple, integer),
31 # "minage_oldest": (tuple, seconds),
32 # "maxage_oldest": -"-,
33 # "minage_newest": -"-,
34 # "maxage_newest": -"-,
35 # "minsize_smallest": (tuple, bytes),
37 # "minsize_largest": -"-,
42 # .--Parsing-------------------------------------------------------------.
44 # | | _ \ __ _ _ __ ___(_)_ __ __ _ |
45 # | | |_) / _` | '__/ __| | '_ \ / _` | |
46 # | | __/ (_| | | \__ \ | | | | (_| | |
47 # | |_| \__,_|_| |___/_|_| |_|\__, | |
49 # +----------------------------------------------------------------------+
51 # '----------------------------------------------------------------------'
54 def parse_filestats(info
):
56 current
= [] # should never be used, but better safe than sorry
60 if line
[0].startswith('[[['):
61 _output_variety
, subsection_name
= line
[0][3:-3].split(None, 1)
62 current
= sections_info
.setdefault(subsection_name
, [])
64 current
.append(line
[0])
66 return {k
: _parse_filestats_load_lines(v
) for k
, v
in sections_info
.items() if v
}
69 def _parse_filestats_load_lines(info
):
73 list_of_dicts
.append(ast
.literal_eval(line
))
80 # .--Helpers-------------------------------------------------------------.
82 # | | | | | ___| |_ __ ___ _ __ ___ |
83 # | | |_| |/ _ \ | '_ \ / _ \ '__/ __| |
84 # | | _ | __/ | |_) | __/ | \__ \ |
85 # | |_| |_|\___|_| .__/ \___|_| |___/ |
87 # +----------------------------------------------------------------------+
89 # '----------------------------------------------------------------------'
92 def filestats_check_int_levels(value
,
95 hr_function
=lambda i
: "%d" % i
):
97 if lower_levels_pair
not in (None, (None, None)):
98 warn
, crit
= lower_levels_pair
101 msg
= " (warn/crit below %s/%s)" % (hr_function(warn
), hr_function(crit
))
105 if upper_levels_pair
not in (None, (None, None)):
106 warn
, crit
= upper_levels_pair
109 msg
= " (warn/crit at %s/%s)" % (hr_function(warn
), hr_function(crit
))
116 def check_filestats_count(count
, params
):
117 '''common check result - used by main and count_only check'''
118 state
, text
= filestats_check_int_levels(count
, params
.get("mincount"), params
.get("maxcount"))
119 warn
, crit
= params
.get("maxcount", (None, None))
120 return state
, "Files in total: %d%s" % (count
, text
), \
121 [('file_count', count
, warn
, crit
, None, None)]
124 def check_filestats_extremes(files
, params
):
125 '''common check result - used by main and extremes_only check'''
126 for key
, hr_function
, minlabel
, maxlabel
in (
127 ('size', get_bytes_human_readable
, 'smallest', 'largest'),
128 ('age', get_age_human_readable
, 'newest', 'oldest'),
130 files
.sort(key
=lambda f
: f
.get(key
)) # pylint: disable=cell-var-from-loop
131 for efile
, label
in ((files
[0], minlabel
), (files
[-1], maxlabel
)):
132 state
, text
= filestats_check_int_levels(
134 params
.get("min%s_%s" % (key
, label
)),
135 params
.get("max%s_%s" % (key
, label
)),
139 text
= "%s: %s" % (text
, efile
.get('path', "<path info missing>"))
140 value_hr
= hr_function(efile
.get(key
))
141 yield state
, "%s: %s%s" % (label
.title(), value_hr
, text
)
145 # .--Checks--------------------------------------------------------------.
147 # | / ___| |__ ___ ___| | _____ |
148 # | | | | '_ \ / _ \/ __| |/ / __| |
149 # | | |___| | | | __/ (__| <\__ \ |
150 # | \____|_| |_|\___|\___|_|\_\___/ |
152 # +----------------------------------------------------------------------+
154 # '----------------------------------------------------------------------'
157 @get_parsed_item_data
158 def check_filestats(_item
, params
, data
):
160 sumry
= [s
for s
in data
if s
.get("type") == "summary"]
161 count
= sumry
[0].get("count", None) if sumry
else None
162 if count
is not None:
163 yield check_filestats_count(count
, params
)
165 files
= [i
for i
in data
if i
.get("type") == "file"]
167 for subresult
in check_filestats_extremes(files
, params
):
171 check_info
['filestats'] = {
172 "parse_function": parse_filestats
,
173 "inventory_function": discover(),
174 "check_function": check_filestats
,
175 "service_description": "File group %s",
176 "has_perfdata": True,
177 "group": "filestats",