#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# <<<tsm_stagingpools>>>
# tsmfarm2       SL8500_STGPOOL_05       99.9
# tsmfarm2       SL8500_STGPOOL_05       97.9
# tsmfarm2       SL8500_LTO4_STGPOOL_01  48.6
# tsmfarm2       SL8500_LTO4_STGPOOL_01  35.9
# tsmfarm2       SL8500_LTO4_STGPOOL_01  58.9
# tsmfarm2       SL8500_LTO4_STGPOOL_01  61.6
#
# Example for params
# params = {
#    "free_below" : 30.0, # consider as free if utilized <= this
#    "levels" : (5, 2), # warn/crit if less then that many free tapes
# }

factory_settings["tsm_stagingpools_default_levels"] = {
    "free_below" : 70,
}

def parse_tsm_stagingpools(info):
    parsed = {}
    def add_item(node, lineinfo):
        inst, pool, util = lineinfo
        if inst == "default":
            item = pool
        else:
            item = inst + " / " + pool
        parsed.setdefault( item, {} )
        parsed[item].setdefault( node, [] )
        parsed[item][node].append( util.replace(",", ".") )

    for line in info:
        node = line[0]
        add_item(node, line[1:4])
        # The agent plugin sometimes seems to mix two lines together some
        # times. Detect and fix that.
        if len(line) == 7:
            add_item(node, line[4:])

    return parsed


def inventory_tsm_stagingpools(parsed):
    for item in parsed:
        yield item, {}


def check_tsm_stagingpools(item, params, parsed):
    if item not in parsed:
        return 3, "Item not found in agent output"

    datasets, nodeinfos = [], []
    for node, data in parsed[item].items():
        if node is not None:
            nodeinfos.append(node)
        datasets.append( tuple(data) )

    num_tapes      = 0
    num_free_tapes = 0
    utilization    = 0.0 # in relation to one tape size
    for util in datasets[0]:
        util = float(util) / 100.0
        utilization += util
        num_tapes   += 1
        if util <= params["free_below"] / 100.0:
            num_free_tapes += 1

    if nodeinfos:
        infotext = "%s: " % "/".join( nodeinfos )
    else:
        infotext = ""
    infotext += "Total tapes: %d, Tapes less then %d%% full: %d, Utilization: %.1f tapes" % \
                 (num_tapes, params["free_below"], num_free_tapes, utilization)
    state = 0
    if "levels" in params:
        warn, crit = params["levels"]
        if num_free_tapes < crit:
            state = 2
            infotext += "(!!)"
        elif num_free_tapes < warn:
            state = 1
            infotext += "(!)"
    else:
        warn, crit = None, None

    if state == 0 and num_tapes == 0:
        state = 3
        infotext = "no tapes in this pool or pool not existant"

    # In cluster mode we check if data sets are equal from all nodes
    # else we have only one data set
    if len(set( datasets )) > 1:
        state = 3
        infotext += ", Cluster: data from nodes are not equal"

    return state, infotext, [ ("tapes", num_tapes), ("free", num_free_tapes, warn, crit), ("util", utilization) ]


check_info['tsm_stagingpools'] = {
    "parse_function"          : parse_tsm_stagingpools,
    "check_function"          : check_tsm_stagingpools,
    "inventory_function"      : inventory_tsm_stagingpools,
    "service_description"     : "TSM Stagingpool %s",
    "has_perfdata"            : True,
    "group"                   : "tsm_stagingpools",
    "default_levels_variable" : "tsm_stagingpools_default_levels",
    "node_info"               : True,
}
