Module: check_mk
Branch: master
Commit: a507bea84f2b193aa719fde28277476e08453d49
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=a507bea84f2b19…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Mon Jun 4 20:07:11 2012 +0200
postgres_stat_database: New check for PostgreSQL database statistics
---
ChangeLog | 1 +
agents/plugins/mk_postgres | 2 +
checks/postgres_stat_database | 113 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 116 insertions(+), 0 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index f19d22e..65cea20 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,7 @@
* mysql.innodb_io: New check for Disk-IO of InnoDB
* mysql_capacity: New check for used/free capacity of MySQL databases
* postgres_sessions: New check for PostgreSQL number of sessions
+ * postgres_stat_database: New check for PostgreSQL database statistics
1.2.0b6:
Multisite:
diff --git a/agents/plugins/mk_postgres b/agents/plugins/mk_postgres
index 3dc2840..225690f 100755
--- a/agents/plugins/mk_postgres
+++ b/agents/plugins/mk_postgres
@@ -3,4 +3,6 @@
if id postgres >/dev/null ; then
echo '<<<postgres_sessions>>>'
echo "select current_query = '<IDLE>', count(*) from
pg_stat_activity group by (current_query = '<IDLE>');" | su - postgres
-c "psql -A -t -F' '"
+ echo '<<<postgres_stat_database>>>'
+ echo 'select * from pg_stat_database' | su - postgres -c "psql -A -F'
'" | sed '$d'
fi
diff --git a/checks/postgres_stat_database b/checks/postgres_stat_database
new file mode 100644
index 0000000..c151f81
--- /dev/null
+++ b/checks/postgres_stat_database
@@ -0,0 +1,113 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2012 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# ails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+
+# <<<postgres_stat_database>>>
+# datid datname numbackends xact_commit xact_rollback blks_read blks_hit tup_returned
tup_fetched tup_inserted tup_updated tup_deleted
+# 1 template1 0 0 0 0 0 0 0 0 0 0
+# 11563 template0 0 0 0 0 0 0 0 0 0 0
+# 11564 postgres 2 568360 17 811 8855508 157949341 879922 0 0 0
+# 16385 foobardb 7 43619118 262 3589 838098632 854602076 441785363 8298 602481 2806
+
+
+# Create a check for all databases that have seen at least
+# one commit in their live.
+def inventory_postgres_stat_database(info):
+ parsed = parse_postgres_stat(info)
+ return [ (k, {}) for k in parsed.keys()
+ if parsed[k]["xact_commit"] > 0 ]
+
+def parse_postgres_stat(info):
+ if len(info) == 0:
+ return {}
+
+ def s(x):
+ try:
+ return int(x)
+ except:
+ return x
+
+ parsed = {}
+ headers = info[0]
+ for line in info[1:]:
+ row = dict(zip(headers, map(s, line)))
+ parsed[row["datname"]] = row
+
+ return parsed
+
+
+def check_postgres_stat_database(item, params, info):
+ parsed = parse_postgres_stat(info)
+ if item not in parsed:
+ return (3, "UNKNOWN - Database not found")
+ stats = parsed[item]
+
+ status = 0
+ infos = []
+ perfdata = []
+ this_time = time.time()
+ one_wrapped = False
+ for what, title in [
+ ( "blks_read", "Blocks Read" ),
+ ( "tup_fetched", "Fetches" ),
+ ( "xact_commit", "Commits" ),
+ ( "tup_deleted", "Deletes" ),
+ ( "tup_updated", "Updates" ),
+ ( "tup_inserted", "Inserts" ), ]:
+ counter = stats[what]
+ counter_name = "postgres_stat_database.%s.%s" % (item, what)
+ try:
+ timedif, rate = get_counter(counter_name, this_time, counter)
+ except MKCounterWrapped, e:
+ one_wrapped = e
+ continue
+
+ infos.append("%s: %.2f/s" % (title, rate))
+ if what in params:
+ warn, crit = params[what]
+ if rate >= crit:
+ status = 2
+ infos[-1] += '(!!)'
+ elif rate >= warn:
+ status = max(status, 1)
+ infos[-1] += '(!)'
+ else:
+ warn, crit = None, None
+ perfdata.append((what, rate, warn, crit))
+
+ if one_wrapped:
+ raise one_wrapped
+
+ return (status, nagios_state_names[status] + " - " + ",
".join(infos), perfdata)
+
+
+check_info['postgres_stat_database'] = {
+ "check_function" : check_postgres_stat_database,
+ "inventory_function" : inventory_postgres_stat_database,
+ "service_description" : "PostgreSQL Statistics",
+ "has_perfdata" : True,
+ "group" : "postgres_stat_database",
+}