Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / checks / postgres_query_duration
blob6f9d45c415c3e2962a39e89ab33f9c0a3c0a3edd
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_query_duration>>>
28 # [databases_start]
29 # postgres
30 # testdb
31 # datenbank
32 # [databases_end]
33 # datname;datid;usename;client_addr;state;seconds;pid;current_query
34 # postgres;12068;postgres;;active;0;12631;SELECT datname, datid, usename, ....
36 # instance
37 # <<<postgres_query_duration>>>
38 # [[[foobar]]]
39 # [databases_start]
40 # postgres
41 # testdb
42 # [databases_end]
43 # ...
46 def inventory_postgres_query_duration(parsed):
47 for entry in parsed.keys():
48 yield entry, {}
51 def check_postgres_query_duration(item, _no_params, parsed):
52 if item not in parsed:
53 # In case of missing information we assume that the login into
54 # the database has failed and we simply skip this check. It won't
55 # switch to UNKNOWN, but will get stale.
56 raise MKCounterWrapped("Login into database failed")
58 longest_time = -1
59 longest_info = None
60 for element in parsed[item]:
61 if int(element["seconds"]) > longest_time:
62 longest_info = element
63 longest_time = int(element["seconds"])
65 if longest_info:
66 yield 0, "Longest query is %s seconds" % longest_info["seconds"]
67 if longest_info["usename"]:
68 yield 0, "Username: %s" % longest_info["usename"]
69 if longest_info["client_addr"]:
70 yield 0, "Client: %s" % longest_info["client_addr"]
71 if longest_info["state"].lower() != "active":
72 yield 0, "Query state: %s" % longest_info["state"]
73 yield 0, "PID: %s, Query: %s" % (longest_info["pid"], longest_info["current_query"])
75 else:
76 yield 0, "No query is running"
79 check_info['postgres_query_duration'] = {
80 "parse_function": parse_postgres_dbs,
81 "check_function": check_postgres_query_duration,
82 "inventory_function": inventory_postgres_query_duration,
83 "service_description": "PostgreSQL Query Duration %s",
84 "includes": ["postgres.include"]