Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / postgres_bloat
blob2c89ae36d571af0ab6a1df1a6f4e2ea9c14d8f23
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 # <<<postgres_bloat>>>
28 # [databases_start]
29 # postgres
30 # testdb
31 # datenbank
32 # [databases_end]
33 # db;schemaname;tablename;tups;pages;otta;tbloat;wastedpages;wastedbytes;wastedsize;iname;itups;ipages;iotta;ibloat;wastedipages;wastedibytes;wastedisize;totalwastedbytes
34 # postgres;pg_catalog;pg_amop;403;4;3;1.3;1;8192;8192;pg_amop_oid_index;403;4;2;2.0;2;16384;16384;24576
35 # postgres;pg_catalog;pg_amproc;291;3;2;1.5;1;8192;8192;pg_amproc_fam_proc_index;291;4;2;2.0;2;16384;16384;24576
36 # postgres;pg_catalog;pg_amop;403;4;3;1.3;1;8192;8192;pg_amop_opr_fam_index;403;4;2;2.0;2;16384;16384;24576
38 # instances
39 # <<<postgres_bloat>>>
40 # [[[foobar]]]
41 # [databases_start]
42 # postgres
43 # testdb
44 # [databases_end]
45 # ...
47 factory_settings["postgres_bloat_default_levels"] = {
48 'table_bloat_perc': (180.0, 200.0), # WARN at 180%, CRIT at 200%
49 'index_bloat_perc': (180.0, 200.0)
53 def inventory_postgres_bloat(parsed):
54 return [(entry, {}) for entry, values in parsed.items() if values]
57 def check_postgres_bloat(item, params, parsed):
58 database = parsed.get(item)
59 if not database:
60 # In case of missing information we assume that the login into
61 # the database has failed and we simply skip this check. It won't
62 # switch to UNKNOWN, but will get stale.
63 raise MKCounterWrapped("Login into database failed")
65 table_perc_max = None
66 table_abs_max = None
67 index_perc_max = None
68 index_abs_max = None
70 table_abs_total = 0
71 index_abs_total = 0
73 show_levels = False
74 for line in database:
75 tbloat = float(line["tbloat"])
76 twasted = int(line["wastedbytes"])
77 ibloat = float(line["ibloat"])
78 iwasted = int(line["wastedibytes"])
80 table_abs_total += twasted
81 index_abs_total += iwasted
83 # Calculate highest loss
84 if not table_perc_max or tbloat > float(table_perc_max["tbloat"]):
85 table_perc_max = line
86 if not table_abs_max or twasted > int(table_abs_max["wastedbytes"]):
87 table_abs_max = line
88 if not index_perc_max or ibloat > float(index_perc_max["ibloat"]):
89 index_perc_max = line
90 if not index_abs_max or iwasted > int(index_abs_max["wastedibytes"]):
91 index_abs_max = line
93 for what, bloat, wasted in [("table", tbloat, twasted), ("index", ibloat, iwasted)]:
94 if "%s_bloat_perc" % what in params:
95 warn, crit = params["%s_bloat_perc" % what]
96 if bloat >= crit:
97 yield 2, "%s %s bloat: %s%% (too high)" % (line["tablename"], what, bloat)
98 show_levels = True
99 elif bloat >= warn:
100 yield 1, "%s %s bloat: %s%% (too high)" % (line["tablename"], what, bloat)
101 show_levels = True
103 if "%s_bloat_abs" % what in params:
104 warn, crit = params["%s_bloat_abs" % what]
105 if wasted >= crit:
106 yield 2, "%s wasted %s bytes: %s (too high)" % (
107 line["tablename"], what, get_bytes_human_readable(wasted))
108 show_levels = True
109 elif wasted >= warn:
110 yield 1, "%s wasted %s bytes: %s (too high)" % (
111 line["tablename"], what, get_bytes_human_readable(wasted))
112 show_levels = True
114 if show_levels:
115 levels_info = ["Levels:"]
116 for what in ["table", "index"]:
117 if "%s_bloat_perc" % what in params:
118 levels_info.append(
119 "%s Perc (%.0f%%/%.0f%%)" % ((what.title(),) + params["%s_bloat_perc" % what]))
120 if "%s_bloat_abs" % what in params:
121 levels_info.append("%s Abs (%s/%s)" % ((what.title(),) + \
122 tuple(get_bytes_human_readable(int(x)) for x in params["%s_bloat_abs" % what])))
123 yield 0, " ".join(levels_info)
124 else:
125 # No errors. Show some general information
126 for what, perc_max, abs_max in [("table", table_perc_max, table_abs_max),
127 ("index", index_perc_max, index_abs_max)]:
128 yield 0, "Maximum %s bloat at %s: %d%%" % (what, perc_max["tablename"],
129 float(perc_max["%sbloat" % what[0]]) * 100)
130 yield 0, "Maximum wasted %sspace at %s: %s" % (
131 what, abs_max["tablename"],
132 get_bytes_human_readable(
133 int(abs_max["wasted%sbytes" % (what == "index" and "i" or "")])))
135 # Summary information
136 for what, total_value in [("table", table_abs_total), ("index", index_abs_total)]:
137 yield (
139 "Summary of top %d wasted %sspace: %s" % (len(database), what,
140 get_bytes_human_readable(total_value)),
141 [("%sspace_wasted" % what, total_value)],
145 check_info['postgres_bloat'] = {
146 "parse_function": parse_postgres_dbs,
147 "check_function": check_postgres_bloat,
148 "inventory_function": inventory_postgres_bloat,
149 "service_description": "PostgreSQL Bloat %s",
150 "group": "db_bloat",
151 "default_levels_variable": "postgres_bloat_default_levels",
152 "has_perfdata": True,
153 "includes": ["postgres.include"]