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

# Example output from agent:
# Put here the example output from your TCP-Based agent. If the
# check is SNMP-Based, then remove this section

tsm_scratch_default_levels = (5, 7)


def parse_tsm_scratch(info):
    parsed = {}
    for line in info:
        if len(line) != 3:
            continue

        inst, tapes, library = line
        try:
            num_tapes = int(tapes)
        except ValueError:
            continue

        if inst != "default":
            item = "%s / %s" % (inst, library)
        else:
            item = library

        parsed[item] = num_tapes
    return parsed


def inventory_tsm_scratch(parsed):
    return [(lib, "tsm_scratch_default_levels") for lib in parsed]


def check_tsm_scratch(item, params, parsed):
    # Conventionally upper or lower levels are of the form
    #   warn, crit = levels
    # Be aware of the following levels construction:
    crit, warn = params

    num_tapes = parsed.get(item)
    if num_tapes is None:
        return
    return check_levels(
        num_tapes,
        "tapes_free",
        (None, None, warn, crit),
        human_readable_func=lambda x: "%d" % x,
        infoname="Found tapes",
    )


check_info["tsm_scratch"] = {
    "parse_function": parse_tsm_scratch,
    "inventory_function": inventory_tsm_scratch,
    "check_function": check_tsm_scratch,
    "service_description": "Scratch Pool %s",
    "has_perfdata": True,
}
