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

# Example output from agent:
# <<<citrix_hostsystem>>>
# VMName rz1css01
# CitrixPoolName RZ1XenPool01 - Cisco UCS

# Note(1): The pool name is the same for all VMs on one host system
# Note(2): for the same host and vm this section can appear
# several times (with the same content).
def parse_citrix_hostsystem(info):
    parsed = { "vms"  : [], "pool" : "" }
    for line in info:
        if line[0] == "VMName":
            vm = " ".join(line[1:])
            if vm not in parsed["vms"]:
                parsed["vms"].append(vm)
        elif line[0] == "CitrixPoolName":
            pool = " ".join(line[1:])
            if not parsed["pool"]:
                parsed["pool"] = pool
    return parsed


#   .--Host VMs------------------------------------------------------------.
#   |              _   _           _    __     ____  __                    |
#   |             | | | | ___  ___| |_  \ \   / /  \/  |___                |
#   |             | |_| |/ _ \/ __| __|  \ \ / /| |\/| / __|               |
#   |             |  _  | (_) \__ \ |_    \ V / | |  | \__ \               |
#   |             |_| |_|\___/|___/\__|    \_/  |_|  |_|___/               |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_citrix_hostsystem_vms(parsed):
    if parsed["vms"]:
        return [ (None, None) ]


def check_citrix_hostsystem_vms(_no_item, _no_params, parsed):
    vmlist = parsed["vms"]
    return 0, "%d VMs running: %s" % (len(vmlist), ", ".join(vmlist))


check_info["citrix_hostsystem.vms"] = {
    "inventory_function"        : inventory_citrix_hostsystem_vms,
    "check_function"            : check_citrix_hostsystem_vms,
    "service_description"       : "Citrix VMs",
}

#.
#   .--Host Info-----------------------------------------------------------.
#   |              _   _           _     ___        __                     |
#   |             | | | | ___  ___| |_  |_ _|_ __  / _| ___                |
#   |             | |_| |/ _ \/ __| __|  | || '_ \| |_ / _ \               |
#   |             |  _  | (_) \__ \ |_   | || | | |  _| (_) |              |
#   |             |_| |_|\___/|___/\__| |___|_| |_|_|  \___/               |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_citrix_hostsystem(parsed):
    if parsed["pool"]:
        return [ (None, None) ]


def check_citrix_hostsystem(_no_item, no_params, parsed):
    return 0, "Citrix Pool Name: %s" % parsed["pool"]


check_info["citrix_hostsystem"] = {
    "parse_function"            : parse_citrix_hostsystem,
    "inventory_function"        : inventory_citrix_hostsystem,
    "check_function"            : check_citrix_hostsystem,
    "service_description"       : "Citrix Host Info",
}
