2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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>>>
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
):
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"
58 stats_field
, paramskey
, text
= "atime", "analyse", "analyzed"
60 # namespace, tablename, last_vacuum, last_analyze
61 # ['public', 'my_table', '1424352356', '1424352356'],
64 for line
in parsed
[database
]:
65 # Tables with metadata are ignored
66 if line
["sname"] == "pg_catalog":
69 value
= line
[stats_field
]
70 if value
== "-1" or value
== "":
71 never_checked
.append(line
["tname"])
74 last_time
= int(value
)
75 if not oldest_element
or last_time
< oldest_element
[stats_field
]:
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"]
85 if crit
and now
- crit
> oldest_time
:
87 elif warn
and now
- warn
> oldest_time
:
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
),
97 key
= "postgres_stats.%s" % item
99 set_item_state(key
, now
)
100 yield 0, "No never checked tables"
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
)
110 set_item_state(key
, now
)
115 warn
, crit
= params
["never_analyze_vacuum"]
122 infotext
+= " (warn/crit at %s/%s)" % \
123 (get_age_human_readable(warn
),
124 get_age_human_readable(crit
),)
125 yield state
, 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"],