#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@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-
# tails. 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_locks>>>
# [databases_start]
# postgres
# zweitedb
# testdb
# datenbank
# [databases_end]
# datname;granted;mode
# postgres;t;AccessShareLock
# zweitedb;;
# template1;;
# datenbank;;

def inventory_postgres_locks(parsed):
    for entry in parsed.keys():
        yield entry, {}

def check_postgres_locks(item, params, parsed):
    locks = {}

    database = parsed.get(item)
    if database == None:
        return

    for element in database:
        if element["granted"]:
            locks.setdefault(element["mode"], 0)
            locks[element["mode"]] += 1

    shared_locks = locks.get("AccessShareLock", 0)
    yield 0, "Access Share Locks %d" % shared_locks, [("shared_locks", shared_locks)]

    if "levels_shared" in params:
        warn, crit = params["levels_shared"]
        if shared_locks >= crit:
            yield 2, "too high (Levels at %d/%d)" % (warn, crit)
        elif shared_locks >= warn:
            yield 1, "too high (Levels at %d/%d)" % (warn, crit)

    exclusive_locks = locks.get("ExclusiveLock", 0)
    yield 0, "Exclusive Locks %d" % exclusive_locks, [("exclusive_locks", exclusive_locks)]
    if "levels_exclusive" in params:
        warn, crit = params["levels_exclusive"]
        if exclusive_locks >= crit:
            yield 2, "too high (Levels at %d/%d)" % (warn, crit)
        elif exclusive_locks >= warn:
            yield 1, "too high (Levels at %d/%d)" % (warn, crit)

check_info['postgres_locks'] = {
    "parse_function"          : parse_postgres_dbs,
    "check_function"          : check_postgres_locks,
    "inventory_function"      : inventory_postgres_locks,
    "service_description"     : "PostgreSQL Locks %s",
    "has_perfdata"            : True,
    "group"                   : "postgres_locks",
    "includes"                : [ "postgres.include" ],
}

