#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2016             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.


# <<<rds_licenses:sep(44)>>>
# KeyPackId,Description,KeyPackType,ProductType,ProductVersion,ProductVersionID,TotalLicenses,IssuedLi...
# 13,A02-6.00-S,4,0,Windows Server 2008 oder Windows Server 2008 R2,2,-1,100,-1,20380119031407.000000-000
# 2,A02-5.00-EX,6,3,Windows 2000 Server,0,-1,0,-1,20351231230000.000000-000
# 3,A02-6.00-S,2,0,Windows Server 2008 oder Windows Server 2008 R2,2,250,250,0,20230209121549.000000-000
# 14,A02-6.00-S,2,0,Windows Server 2008 oder Windows Server 2008 R2,2,1050,731,319,20380101081108.000000-000
# 16,A02-6.00-S,2,0,Windows Server 2008 oder Windows Server 2008 R2,2,50,23,27,20380101150128.000000-000
# 17,A02-6.00-S,2,0,Windows Server 2008 oder Windows Server 2008 R2,2,50,18,32,20380101110453.000000-000
# 18,A02-6.00-S,2,0,Windows Server 2008 oder Windows Server 2008 R2,2,65,22,43,20380101083530.000000-000
# 19,A02-6.00-S,2,0,Windows Server 2008 oder Windows Server 2008 R2,2,600,0,600,20380101083248.000000-000


# Insert any new keys here
# https://msdn.microsoft.com/en-us/library/aa383803%28v=vs.85%29.aspx#properties
rds_licenses_product_versionid_map = {
    "4": "Windows Server 2012",
    "3": "Windows Server 2008 R2",
    "2": "Windows Server 2008"
#1    Not supported.
#0    Not supported.
}

def parse_rds_licenses(info):
    parsed = {}
    headers = info[0]
    for line in info[1:]:
        data = dict(zip(headers, line))
        version_id = data.get("ProductVersionID")
        if version_id not in rds_licenses_product_versionid_map:
            continue
        version = rds_licenses_product_versionid_map[version_id]
        parsed.setdefault(version, [])
        parsed[version].append(data)
    return parsed

def inventory_rds_licenses(parsed):
    for key in parsed:
        yield key, None

def check_rds_licenses(item, params, parsed):
    license_pack = parsed.get(item)
    if not license_pack:
        return

    total = 0
    used  = 0
    for pack in license_pack:
        pack_total = int(pack.get("TotalLicenses"))
        pack_avail = int(pack.get("AvailableLicenses"))
        total += pack_total
        used  += pack_total - pack_avail

    return license_check_levels(total, used, params)

check_info['rds_licenses'] = {
    'parse_function'            : parse_rds_licenses,
    'inventory_function'        : inventory_rds_licenses,
    'check_function'            : check_rds_licenses,
    'service_description'       : "RDS Licenses %s",
    'group'                     : "rds_licenses",
    'has_perfdata'              : True,
}
