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

# <<<citrix_state>>>
# Catalog XenApp - Standard - RZ5
# Controller rx2345.extern.foobar
# DesktopGroupName XenApp - Standard
# FaultState None
# HostingServer rz1tgn03.extern.foobar
# MaintenanceMode False
# PowerState On
# RegistrationState Registered
# VMToolsState Running
# AgentVersion 7.6.0.5026

def parse_citrix_state(info):
    parsed = {
        "instance"    : {},
    }
    for line in info:
        if line[0] == "Controller":
            parsed["controller"] = " ".join(line[1:])
        elif line[0] == "HostingServer":
            parsed["hosting_server"] = " ".join(line[1:])
        elif line[0] in [
                "FaultState", "MaintenanceMode", "PowerState",
                "RegistrationState", "VMToolsState" ]:
            parsed["instance"][line[0]] = line[1]

    return parsed


#   .--Controller----------------------------------------------------------.
#   |             ____            _             _ _                        |
#   |            / ___|___  _ __ | |_ _ __ ___ | | | ___ _ __              |
#   |           | |   / _ \| '_ \| __| '__/ _ \| | |/ _ \ '__|             |
#   |           | |__| (_) | | | | |_| | | (_) | | |  __/ |                |
#   |            \____\___/|_| |_|\__|_|  \___/|_|_|\___|_|                |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_citrix_state_controller(parsed):
    if "controller" in parsed:
        return [ (None, None) ]


def check_citrix_state_controller(_no_item, _no_params, parsed):
    return 0, parsed["controller"]


check_info["citrix_state.controller"] = {
    "inventory_function"    : inventory_citrix_state_controller,
    "check_function"        : check_citrix_state_controller,
    "service_description"   : "Citrix Controller",
}

#.
#   .--Hosting Server------------------------------------------------------.
#   | _   _           _   _               ____                             |
#   || | | | ___  ___| |_(_)_ __   __ _  / ___|  ___ _ ____   _____ _ __   |
#   || |_| |/ _ \/ __| __| | '_ \ / _` | \___ \ / _ \ '__\ \ / / _ \ '__|  |
#   ||  _  | (_) \__ \ |_| | | | | (_| |  ___) |  __/ |   \ V /  __/ |     |
#   ||_| |_|\___/|___/\__|_|_| |_|\__, | |____/ \___|_|    \_/ \___|_|     |
#   |                             |___/                                    |
#   '----------------------------------------------------------------------'


def inventory_citrix_state_hosting_server(parsed):
    if "hosting_server" in parsed:
        return [ (None, None) ]


def check_citrix_state_hosting_server(_no_item, _no_params, parsed):
    return 0, parsed["hosting_server"]


check_info["citrix_state.hosting_server"] = {
    "inventory_function"    : inventory_citrix_state_hosting_server,
    "check_function"        : check_citrix_state_hosting_server,
    "service_description"   : "Citrix Hosting Server",
}

#.
#   .--State---------------------------------------------------------------.
#   |                       ____  _        _                               |
#   |                      / ___|| |_ __ _| |_ ___                         |
#   |                      \___ \| __/ _` | __/ _ \                        |
#   |                       ___) | || (_| | ||  __/                        |
#   |                      |____/ \__\__,_|\__\___|                        |
#   |                                                                      |
#   '----------------------------------------------------------------------'


def inventory_citrix_state(parsed):
    if parsed["instance"]:
        return [ (None, {}) ]


def check_citrix_state(_no_item, params, parsed):
    map_states = {
        "maintenancemode" : {
            "False" : 0,
            "True"  : 1,
        },
        "powerstate" : {
            "Unmanaged"     : 1,
            "Unknown"       : 1,
            "Unavailable"   : 2,
            "Off"           : 2,
            "On"            : 0,
            "Suspended"     : 2,
            "TurningOn"     : 1,
            "TurningOff"    : 1,
        },
        "vmtoolsstate" : {
            "NotPresent"    : 2,
            "Unknown"       : 3,
            "NotStarted"    : 1,
            "Running"       : 0,
        },
        "registrationstate" : {
            "Unregistered"  : 2,
            "Initializing"  : 1,
            "Registered"    : 0,
            "AgentError"    : 2,
        },
        "faultstate" : {
            "None"          : 0,
            "FailedToStart" : 2,
            "StuckOnBoot"   : 2,
            "Unregistered"  : 2,
            "MaxCapacity"   : 1,
        },
    }
    map_states.update(params)
    for state_type, state in parsed["instance"].items():
        state_key = state_type.lower()
        if state_key in map_states:
            yield map_states[state_key][state], "%s: %s" % (state_type, state)


check_info["citrix_state"] = {
    "parse_function"            : parse_citrix_state,
    "inventory_function"        : inventory_citrix_state,
    "check_function"            : check_citrix_state,
    "service_description"       : "Citrix Instance State",
    "group"                     : "citrix_state",
}
