Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / postgres_stat_database
blob3dba8e36824a9ff6b0e55ba91dc6f78a51326c64
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_stat_database>>>
28 # datid datname numbackends xact_commit xact_rollback blks_read blks_hit tup_returned tup_fetched tup_inserted tup_updated tup_deleted
29 # 1 template1 0 0 0 0 0 0 0 0 0 0
30 # 11563 template0 0 0 0 0 0 0 0 0 0 0
31 # 11564 postgres 2 568360 17 811 8855508 157949341 879922 0 0 0
32 # 16385 foobardb 7 43619118 262 3589 838098632 854602076 441785363 8298 602481 2806
35 def parse_postgres_stat_database(info):
36 if len(info) == 0:
37 return {}
39 def s(x):
40 try:
41 return int(x)
42 except:
43 return x
45 parsed = {}
46 instance_name = ""
47 for line in info:
48 if line[0].startswith("[[[") and line[0].endswith("]]]"):
49 instance_name = line[0][3:-3].upper()
50 continue
51 elif line[0] == "datid" and line[1] == "datname":
52 headers = line
53 else:
54 row = dict(zip(headers, map(s, line)))
55 if instance_name:
56 db_name = "%s/%s" % (instance_name, row["datname"])
57 else:
58 db_name = row["datname"]
59 parsed[db_name] = row
61 return parsed
64 # Create a check for all databases that have seen at least
65 # one commit in their live.
66 def inventory_postgres_stat_database(parsed):
67 return [(k, {}) for k in parsed.keys() if parsed[k]["xact_commit"] > 0]
70 def check_postgres_stat_database(item, params, parsed):
71 if item not in parsed:
72 return (3, "Database not found")
74 stats = parsed[item]
75 status = 0
76 infos = []
77 perfdata = []
78 this_time = time.time()
79 for what, title in [
80 ("blks_read", "Blocks Read"),
81 ("tup_fetched", "Fetches"),
82 ("xact_commit", "Commits"),
83 ("tup_deleted", "Deletes"),
84 ("tup_updated", "Updates"),
85 ("tup_inserted", "Inserts"),
87 rate = get_rate("postgres_stat_database.%s.%s" % (item, what), this_time, stats[what])
88 infos.append("%s: %.2f/s" % (title, rate))
89 if what in params:
90 warn, crit = params[what]
91 if rate >= crit:
92 status = 2
93 infos[-1] += '(!!)'
94 elif rate >= warn:
95 status = max(status, 1)
96 infos[-1] += '(!)'
97 else:
98 warn, crit = None, None
99 perfdata.append((what, rate, warn, crit))
100 return (status, ", ".join(infos), perfdata)
103 check_info['postgres_stat_database'] = {
104 "parse_function": parse_postgres_stat_database,
105 "inventory_function": inventory_postgres_stat_database,
106 "check_function": check_postgres_stat_database,
107 "service_description": "PostgreSQL DB %s Statistics",
108 "has_perfdata": True,
109 "group": "postgres_stat_database",
113 def check_postgres_stat_database_size(item, _no_params, parsed):
114 if item not in parsed:
115 # In case of missing information we assume that the login into
116 # the database has failed and we simply skip this check. It won't
117 # switch to UNKNOWN, but will get stale.
118 raise MKCounterWrapped("Login into database failed")
120 stats = parsed[item]
121 size = stats["datsize"]
122 return (0, "Size is %s" % get_bytes_human_readable(size), [("size", size)])
125 check_info['postgres_stat_database.size'] = {
126 "check_function": check_postgres_stat_database_size,
127 "inventory_function": inventory_postgres_stat_database,
128 "service_description": "PostgreSQL DB %s Size",
129 "has_perfdata": True,