Cleanup config.nodes_of
[check_mk.git] / checks / lnx_quota
blob1fa17b821068e381cfa854cbe4c50d82a9f4a06b
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 # Example output from old agent until version 1.2.6
28 #<<<lnx_quota>>>
29 #[[[/home]]]
30 #root -- 62743228 0 0 0 137561 0 0 0
31 #proxy -- 288648 0 0 0 14370 0 0 0
32 #http -- 208 0 0 0 53 0 0 0
33 #mysql -- 7915856 0 0 0 173 0 0 0
35 # Example output from new agent since version 1.2.8
36 #[[[usr:/home]]]
37 #root -- 62743228 0 0 0 137561 0 0 0
38 #proxy -- 288648 0 0 0 14370 0 0 0
39 #http -- 208 0 0 0 53 0 0 0
40 #mysql -- 7915856 0 0 0 173 0 0 0
41 #[[[grp:/home]]]
42 #root -- 62743228 0 0 0 137561 0 0 0
43 #proxy -- 288648 0 0 0 14370 0 0 0
44 #http -- 208 0 0 0 53 0 0 0
45 #mysql -- 7915856 0 0 0 173 0 0 0
48 def parse_lnx_quota(info):
49 parsed = {}
50 mode = None
51 fs = None
53 for line in info:
54 if line[0].startswith('[[['):
55 # new filesystem detected
56 parts = line[0][3:-3].split(':')
58 # compatible check for old format
59 # was changed in version 1.2.8
60 if len(parts) == 1:
61 mode = 'usr'
62 fs = line[0][3:-3]
63 elif len(parts) == 2:
64 # new format for check data
65 mode, fs = parts
67 # new filesystem for quota
68 parsed.setdefault(fs, {})
69 parsed[fs].setdefault(mode, {})
71 elif fs and mode and len(line) == 10:
72 # new table entry for quota
73 parsed[fs][mode][line[0]] = [int(x) * 1024 for x in line[2:5]
74 ] + [int(x) for x in line[5:]]
76 return parsed
79 def lnx_quota_limit_check(mode, what, user, used, soft, hard, grace):
80 fmt = lambda value, what: what == 'files' and '%d' % value \
81 or get_bytes_human_readable(value)
83 if mode == 'usr':
84 txt = 'User %s' % user
85 elif mode == 'grp':
86 txt = 'Group %s' % user
88 if used > hard:
89 # check, if hard limit is exceeded
90 state = 2
91 if what == 'blocks':
92 txt += ' exceeded space hard limit %s/%s' % (fmt(used, what), fmt(hard, what))
93 elif what == 'files':
94 txt += ' exceeded file hard limit %s/%s' % (fmt(used, what), fmt(hard, what))
95 elif used > soft:
96 # check, if soft limit is exceeded
97 state = 1
98 if what == 'blocks':
99 txt += ' exceeded space soft limit %s/%s' % (fmt(used, what), fmt(soft, what))
100 elif what == 'files':
101 txt += ' exceeded file soft limit %s/%s' % (fmt(used, what), fmt(soft, what))
103 if grace != 0:
104 # check, if grace time is specified
105 if grace <= time.time():
106 # check, if it was in grace time
107 state = 2
108 txt += ', grace time exceeded'
109 else:
110 # check, if it is in grace time
111 state = 1
112 txt += ', within grace time'
113 else:
114 state = 0
115 txt = ''
116 return state, txt
119 def inventory_lnx_quota(parsed):
120 inv = []
122 for item in parsed.keys():
123 inv.append((item, {'user': True}))
124 return inv
127 def check_lnx_quota(item, params, parsed):
128 #perfdata = []
130 if item not in parsed:
131 yield 3, 'Quota info not found for this filesystem'
133 if not params:
134 # old discovery until version 1.2.6
135 # params were empty dictionary and enabled by standard user checking
136 params = {'user': True}
138 for param_key, thing, name in [('user', 'usr', 'users'), ('group', 'grp', 'groups')]:
139 if params.get(param_key) is True:
140 at_least_one_problematic = False
141 for user, values in parsed[item][thing].items():
142 for what, (used, soft, hard, grace) in [
143 ('blocks', values[:4]),
144 ('files', values[4:]),
147 if soft == 0 and hard == 0:
148 continue # skip entries without limits
150 state, txt = lnx_quota_limit_check(thing, what, user, used, soft, hard, grace)
152 # Performance data was removed, because the graphing system is not
153 # able to handle such kind of dynamic performance data. Performance
154 # variables were named like group_USERNAME_blocks, group_USERNAME_files,
155 # USERNAME_blocks or USERNAME_files and can appear and disappear in the
156 # configuration.
157 # Additional the graphs show the performance data like filesystem usage
158 # in the standard logarithmic scale which is not a useful view for the user.
159 #perfdata.append(('%s%s_%s' % (perf_prefix, user, what),
160 # used, soft, hard, 0, hard))
162 if txt:
163 at_least_one_problematic = True
164 yield state, txt
165 #yield state, (txt + ', '), perfdata
166 else:
167 yield state, ''
168 #yield state, None, perfdata
170 if not at_least_one_problematic:
171 yield 0, 'All %s within quota limits' % name
173 if params.get('user') is False and params.get('group') is False:
174 yield 0, 'Disabled quota checking'
177 check_info['lnx_quota'] = {
178 'parse_function': parse_lnx_quota,
179 'check_function': check_lnx_quota,
180 'inventory_function': inventory_lnx_quota,
181 'service_description': 'Quota %s',
182 'has_perfdata': True,
183 'group': 'lnx_quota',