GUI CSS: Removed snapin styles from py modules and added a _snapins.scss for the...
[check_mk.git] / checks / filestats
blob88e4f8aa6b99685515b0194469c170164a1be230
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
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.
26 import ast
28 # params = {
29 # "mincount": (tuple, integer),
30 # "maxcount": -"-,
31 # "minage_oldest": (tuple, seconds),
32 # "maxage_oldest": -"-,
33 # "minage_newest": -"-,
34 # "maxage_newest": -"-,
35 # "minsize_smallest": (tuple, bytes),
36 # "maxsize_...
37 # "minsize_largest": -"-,
38 # "maxsize_...
39 # }
42 # .--Parsing-------------------------------------------------------------.
43 # | ____ _ |
44 # | | _ \ __ _ _ __ ___(_)_ __ __ _ |
45 # | | |_) / _` | '__/ __| | '_ \ / _` | |
46 # | | __/ (_| | | \__ \ | | | | (_| | |
47 # | |_| \__,_|_| |___/_|_| |_|\__, | |
48 # | |___/ |
49 # +----------------------------------------------------------------------+
50 # | |
51 # '----------------------------------------------------------------------'
54 def parse_filestats(info):
55 sections_info = {}
56 current = [] # should never be used, but better safe than sorry
57 for line in info:
58 if not line:
59 continue
60 if line[0].startswith('[[['):
61 _output_variety, subsection_name = line[0][3:-3].split(None, 1)
62 current = sections_info.setdefault(subsection_name, [])
63 else:
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):
70 list_of_dicts = []
71 for line in info:
72 try:
73 list_of_dicts.append(ast.literal_eval(line))
74 except SyntaxError:
75 pass
76 return list_of_dicts
80 # .--Helpers-------------------------------------------------------------.
81 # | _ _ _ |
82 # | | | | | ___| |_ __ ___ _ __ ___ |
83 # | | |_| |/ _ \ | '_ \ / _ \ '__/ __| |
84 # | | _ | __/ | |_) | __/ | \__ \ |
85 # | |_| |_|\___|_| .__/ \___|_| |___/ |
86 # | |_| |
87 # +----------------------------------------------------------------------+
88 # | |
89 # '----------------------------------------------------------------------'
92 def filestats_check_int_levels(value,
93 lower_levels_pair,
94 upper_levels_pair,
95 hr_function=lambda i: "%d" % i):
96 state, msg = 0, ""
97 if lower_levels_pair not in (None, (None, None)):
98 warn, crit = lower_levels_pair
99 if value <= warn:
100 state = 1
101 msg = " (warn/crit below %s/%s)" % (hr_function(warn), hr_function(crit))
102 if value <= crit:
103 return 2, msg
105 if upper_levels_pair not in (None, (None, None)):
106 warn, crit = upper_levels_pair
107 if value >= warn:
108 state = 1
109 msg = " (warn/crit at %s/%s)" % (hr_function(warn), hr_function(crit))
110 if value >= crit:
111 return 2, msg
113 return state, msg
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(
133 efile.get(key),
134 params.get("min%s_%s" % (key, label)),
135 params.get("max%s_%s" % (key, label)),
136 hr_function,
138 if state:
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--------------------------------------------------------------.
146 # | ____ _ _ |
147 # | / ___| |__ ___ ___| | _____ |
148 # | | | | '_ \ / _ \/ __| |/ / __| |
149 # | | |___| | | | __/ (__| <\__ \ |
150 # | \____|_| |_|\___|\___|_|\_\___/ |
151 # | |
152 # +----------------------------------------------------------------------+
153 # | |
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"]
166 if files:
167 for subresult in check_filestats_extremes(files, params):
168 yield subresult
171 # pylint: disable=undefined-variable
172 check_info['filestats'] = {
173 "parse_function": parse_filestats,
174 "inventory_function": discover(),
175 "check_function": check_filestats,
176 "service_description": "File group %s",
177 "has_perfdata": True,
178 "group": "filestats",