#!/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_drives>>>
# tsmfarm3   LIBRARY3           DRIVE01        LOADED             YES            000782XXXX
# tsmfarm3   LIBRARY3           DRIVE02        LOADED             YES            002348XXXX
# tsmfarm3   LIBRARY3           DRIVE03        EMPTY              YES            000783XXXX
# tsmfarm3   LIBRARY3           DRIVE04        EMPTY              NO            000784XXXX
# tsmfarm3   LIBRARY3           DRIVE05        LOADED             YES            000785XXXX

# <<<tsm_drives>>>
# default        GPFSFILE        GPFSFILE1       UNKNOWN YES
# default        GPFSFILE        GPFSFILE10      UNKNOWN YES
# default        GPFSFILE        GPFSFILE11      UNKNOWN YES
# default        GPFSFILE        GPFSFILE12      UNKNOWN YES
# default        GPFSFILE        GPFSFILE13      UNKNOWN YES

# Possible values for state:
# LOADED
# EMPTY
# UNAVAILABLE  -> crit
# UNLOADED
# RESERVED
# UNKNOWN      -> crit

# Possible values for loaded:
# YES          -> OK
# NO
# UNAVAILABLE_SINCE?
# POLLING?


def inventory_tsm_drives(info):
    inventory = []
    for line in info:
        if len(line) == 6:
            inst, library, drive, _state, _online = line[:5]
            item = "%s / %s" % (library, drive)
            if inst != "default":
                item = inst + " / " + item
            inventory.append((item, None))

    return inventory


def check_tsm_drives(item, params, info):
    for line in info:
        if len(line) >= 5:
            inst, library, drive, state, online = line[:5]
            libdev = "%s / %s" % (library, drive)
            if item == libdev or item == inst + " / " + libdev:
                if len(line) >= 6:
                    serial = line[5]
                    infotext = "[%s] " % serial
                else:
                    serial = None
                    infotext = ""

                monstate = 0
                infotext += "state: %s" % state
                if state in ["UNAVAILABLE", "UNKNOWN"]:
                    monstate = 2
                    infotext += "(!!)"

                infotext += ", online: %s" % online
                if online != "YES":
                    monstate = 2
                    infotext += "(!!)"

                return (monstate, infotext)
    return (3, "drive not found")


check_info["tsm_drives"] = {
    "check_function": check_tsm_drives,
    "inventory_function": inventory_tsm_drives,
    "service_description": "TSM Drive %s",
}
