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

# <<<tsm_storagepool>>>
# tsmfarm2      Bkup      LTOBACK               1399378.64
# tsmfarm2      Arch      LTOARCHCOPY            157288.14

# <<<tsm_storagepool>>>
# default        Bkup      VP4200.GOLD                                                254776345.58^M
# default        Bkup      VP4200.TDP                                                 204386407.76^M
# default        Bkup      VP860.CM                                                   122661787.24^M
# default                  DPC.EXC.2013                                                           ^M
# default                  DPC.EXC.CM                                                             ^M
# default                  DPC.EXCDAG                                                             ^M
# default                  DPC.GOLD.ALL                                                           ^M
# default                  DPC.GOLD.UNIX                                                          ^M
# default                  DPC.GOLD.VE


def parse_tsm_storagepools(info):
    parsed = {}
    for line in info:
        if len(line) < 4:
            continue

        inst, stype, name, size = line[:4]
        if inst == "default":
            item = name
        else:
            item = inst + " / " + name
        parsed.setdefault(item, {"type": stype, "size": size.replace(",", ".")})

    return parsed


def inventory_tsm_storagepools(parsed):
    for inst in parsed:
        yield inst, None


def check_tsm_storagepools(item, _no_params, parsed):
    if item not in parsed:
        return 3, "no such storage pool"

    data = parsed[item]
    stype = data["type"]
    size = int(float(data["size"]) * 1024**2)
    return (
        0,
        "Used size: %s, Type: %s" % (get_bytes_human_readable(size), stype),
        [("used_space", size)],
    )


check_info["tsm_storagepools"] = {
    "parse_function": parse_tsm_storagepools,
    "inventory_function": inventory_tsm_storagepools,
    "check_function": check_tsm_storagepools,
    "service_description": "TSM Storagepool %s",
    "has_perfdata": True,
}
