#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

factory_settings["informix_locks_default_levels"] = {
    "levels": (70, 80),
}


def parse_informix_locks(info):
    parsed = {}
    instance = None
    for line in info:
        if line[0].startswith("[[[") and line[0].endswith("]]]"):
            instance = line[0][3:-3]

        elif instance is not None and line[0] == "LOCKS":
            parsed.setdefault(instance, {"locks": line[1], "type": line[2]})

    return parsed


def inventory_informix_locks(parsed):
    return [(instance, {}) for instance in parsed]


def check_informix_locks(item, params, parsed):
    if item in parsed:
        data = parsed[item]
        locks = int(data["locks"])
        warn, crit = params["levels"]
        state = 0
        infotext = "Type: %s, Locks: %s" % (data["type"], locks)
        if locks >= crit:
            state = 2
        elif locks >= crit:
            state = 1
        if state:
            infotext += " (warn/crit at %s/%s)" % (warn, crit)
        return state, infotext, [("locks", locks)]


check_info["informix_locks"] = {
    "parse_function": parse_informix_locks,
    "inventory_function": inventory_informix_locks,
    "check_function": check_informix_locks,
    "has_perfdata": True,
    "service_description": "Informix Locks %s",
    "group": "informix_locks",
    "default_levels_variable": "informix_locks_default_levels",
}
