#!/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.

sansymphony_pool_default_values = (80, 90)


def inventory_sansymphony_pool(info):
    for line in info:
        yield line[0], "sansymphony_pool_default_values"


def check_sansymphony_pool(item, params, info):
    warn, crit = params
    for poolname, allocated, poolstatus, cachemode, pooltype in info:
        if poolname == item:
            if poolstatus == "Running" and cachemode == "ReadWrite":
                yield 0, "%s pool %s is running, its cache is in %s mode" % (
                    pooltype,
                    poolname,
                    cachemode,
                )
            elif poolstatus == "Running" and cachemode != "ReadWrite":
                yield 1, "%s Pool %s is %s, its cache is %s" % (
                    pooltype,
                    poolname,
                    poolstatus,
                    cachemode,
                )
            else:
                yield 2, "%s pool %s is %s, its cache is in %s mode" % (
                    pooltype,
                    poolname,
                    poolstatus,
                    cachemode,
                )

            allocated = int(allocated)
            infotxt = "Pool allocation: %d%%" % allocated
            levels = " (warn/crit at %d/%d%%)" % (warn, crit)
            perfdata = [("pool_allocation", allocated, warn, crit)]

            if allocated >= crit:
                yield 2, infotxt + levels, perfdata
            elif allocated >= warn:
                yield 1, infotxt + levels, perfdata
            else:
                yield 0, infotxt, perfdata


check_info["sansymphony_pool"] = {
    "check_function": check_sansymphony_pool,
    "inventory_function": inventory_sansymphony_pool,
    "service_description": "sansymphony Pool %s",
    "has_perfdata": True,
    "group": "sansymphony_pool",
}
