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

# Example output from agent:
# <<<ibm_svc_license:sep(58)>>>
# used_flash:0.00
# used_remote:0.00
# used_virtualization:192.94
# license_flash:0
# license_remote:0
# license_virtualization:412
# license_physical_disks:0
# license_physical_flash:off
# license_physical_remote:off
# used_compression_capacity:0.00
# license_compression_capacity:0
# license_compression_enclosures:0


def parse_ibm_svc_license(info):
    licenses = {}
    for line in info:
        if line[0].startswith("license_"):
            license_ = line[0].replace("license_", "")
            if not license_ in licenses.keys():
                licenses[license_] = [0.0, 0.0]
            if line[1] == "off":
                licenses[license_][0] = 0.0
            else:
                licenses[license_][0] = float(line[1])
        if line[0].startswith("used_"):
            license_ = line[0].replace("used_", "")
            if not license_ in licenses.keys():
                licenses[license_] = [0.0, 0.0]
            licenses[license_][1] = float(line[1])
    return licenses


def inventory_ibm_svc_license(parsed):
    for item, data in parsed.items():
        if data != [0.0, 0.0]:
            # Omit unused svc features
            yield item, None


def check_ibm_svc_license(item, params, parsed):
    licensed, used = parsed[item]
    return license_check_levels(licensed, used, params)


check_info["ibm_svc_license"] = {
    "check_function": check_ibm_svc_license,
    "inventory_function": inventory_ibm_svc_license,
    "parse_function": parse_ibm_svc_license,
    "service_description": "License %s",
    "group": "ibmsvc_licenses",
    "has_perfdata": True,
    "includes": ["license.include"]
}
