Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / postgres_stats
blob7238b3aec828de7fe104424aabe35f5e9ddccec2
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_stats>>>
28 # [databases_start]
29 # postgres
30 # testdb
31 # datenbank
32 # [databases_end]
33 # datname;sname;tname;vtime;atime
34 # postgres;pg_catalog;pg_statistic;-1;-1
35 # postgres;pg_catalog;pg_type;-1;-1
36 # postgres;pg_catalog;pg_authid;-1;-1
37 # postgres;pg_catalog;pg_attribute;-1;-1
40 def inventory_postgres_stats(parsed):
41 for db in parsed:
42 yield "VACUUM %s" % db, {}
43 yield "ANALYZE %s" % db, {}
46 def check_postgres_stats(item, params, parsed):
47 item_type, database = item.split(" ", 1)
49 if database not in parsed:
50 # In case of missing information we assume that the login into
51 # the database has failed and we simply skip this check. It won't
52 # switch to UNKNOWN, but will get stale.
53 raise MKCounterWrapped("Login into database failed")
55 if item_type.startswith("VACUUM"):
56 stats_field, paramskey, text = "vtime", "vacuum", "vacuumed"
57 else:
58 stats_field, paramskey, text = "atime", "analyse", "analyzed"
60 # namespace, tablename, last_vacuum, last_analyze
61 # ['public', 'my_table', '1424352356', '1424352356'],
62 oldest_element = None
63 never_checked = []
64 for line in parsed[database]:
65 # Tables with metadata are ignored
66 if line["sname"] == "pg_catalog":
67 continue
69 value = line[stats_field]
70 if value == "-1" or value == "":
71 never_checked.append(line["tname"])
72 continue
74 last_time = int(value)
75 if not oldest_element or last_time < oldest_element[stats_field]:
76 oldest_element = line
78 now = time.time()
79 if oldest_element:
80 oldest_time = int(oldest_element[stats_field])
81 warn, crit = params.get("last_%s" % paramskey, (None, None))
82 yield 0, "Table: %s" % oldest_element["tname"]
84 state = 0
85 if crit and now - crit > oldest_time:
86 state = 2
87 elif warn and now - warn > oldest_time:
88 state = 1
90 extra_info = ""
91 if state:
92 extra_info = " (warn/crit at %s/%s)" % (get_age_human_readable(warn),
93 get_age_human_readable(crit))
94 yield state, "Time since last vacuum %s%s" % (get_age_human_readable(now - oldest_time),
95 extra_info)
97 key = "postgres_stats.%s" % item
98 if not never_checked:
99 set_item_state(key, now)
100 yield 0, "No never checked tables"
102 elif never_checked:
103 infotext = "%d tables were never %s: %s%s" % \
104 (len(never_checked), text, "/".join(never_checked[:5]),
105 len(never_checked) > 5 and " (first %d shown)" % min(5, len(never_checked)) or "")
107 if "never_analyze_vacuum" in params:
108 last_ts = get_item_state(key)
109 if last_ts is None:
110 set_item_state(key, now)
111 yield 0, infotext
112 return
114 age = now - last_ts
115 warn, crit = params["never_analyze_vacuum"]
116 state = 0
117 if age >= crit:
118 state = 2
119 elif age >= warn:
120 state = 1
121 if state:
122 infotext += " (warn/crit at %s/%s)" % \
123 (get_age_human_readable(warn),
124 get_age_human_readable(crit),)
125 yield state, infotext
127 else:
128 yield 1, infotext
131 check_info['postgres_stats'] = {
132 "parse_function": parse_postgres_dbs,
133 "check_function": check_postgres_stats,
134 "inventory_function": inventory_postgres_stats,
135 "service_description": "PostgreSQL %s",
136 "group": "postgres_maintenance",
137 "includes": ["postgres.include"],