#!/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-
# ails.  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:
# <<<esx_vsphere_objects:sep(9)>>>
# hostsystem  esx.wacker.corp
# virtualmachine  LinuxI
# virtualmachine  OpenSUSE_II
# virtualmachine  OpenSUSE_III
# virtualmachine  OpenSUSE_IV
# virtualmachine  OpenSUSE_V
# virtualmachine  WindowsXP I
# virtualmachine  LinuxII
# virtualmachine  LinuxIII
# virtualmachine  LinuxIV
# virtualmachine  LinuxV
# virtualmachine  OpenSUSE_I

vsphere_object_names = {
    "hostsystem"     : "HostSystem",
    "virtualmachine" : "VM",
}


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

def inventory_esx_vsphere_objects(info):
    return [ (vsphere_object_names[line[0]] + " " + line[1], {}) for line in info ]

# params is a dict with the allowed target states for the systems
# Example:
# params = {
#   "states" : {
#        "poweredOn" : 0,
#        "poweredOff" : 0,
#        "suspended" : 3,
#   }
# }


def check_esx_vsphere_objects(item, params, info):
    if params == None:
        params = {}

    for line in info:
        if vsphere_object_names[line[0]] + " " + line[1] == item and len(line) > 3:
            running_on = line[2]
            power_state = line[3]
            state = params.get("states",{}).get(power_state)
            if state == None:
                if power_state == "poweredOn":
                    state = 0
                elif power_state == "poweredOff":
                    state = 1
                elif power_state == "suspended":
                    state = 1
                else:
                    state = 3
            infotext = "power state: %s" % power_state
            if running_on:
                if state == 0:
                    infotext += ', running on [%s]' % running_on
                else:
                    infotext += ', defined on [%s]' % running_on
            return state, infotext

    what = item.split()[0]
    name = item.split()[1]

    if what == "VM":
        return 3, "Virtual machine %s is missing" % name
    else:
        return 3, "No data about host system %s" % name


check_info['esx_vsphere_objects'] = {
  "inventory_function"  : inventory_esx_vsphere_objects,
  "check_function"      : check_esx_vsphere_objects,
  "service_description" : "%s",
  "group"               : "esx_vsphere_objects",
}

def inventory_esx_vsphere_objects_count(info):
    return [(None, None)]

def check_esx_vsphere_objects_count(_no_item, _no_params, info):
    virtualmachines = 0
    hostsystems = 0
    for line in info:
        if line[0] == 'hostsystem':
            hostsystems += 1
        elif line[0] == 'virtualmachine':
            virtualmachines += 1
    messages = "Virtualmachines: %d" % virtualmachines
    perfdata = [('vms', virtualmachines)]
    if hostsystems > 1:
        messages += ", Hostsystems: %d" % hostsystems
        perfdata.append(('hosts', hostsystems))
    return 0, messages, perfdata


check_info['esx_vsphere_objects.count'] = {
  "inventory_function"  : inventory_esx_vsphere_objects_count,
  "check_function"      : check_esx_vsphere_objects_count,
  "service_description" : "Object count",
  "has_perfdata"        : True,
}
#   .--Cluster-------------------------------------------------------------.
#   |                    ____ _           _                                |
#   |                   / ___| |_   _ ___| |_ ___ _ __                     |
#   |                  | |   | | | | / __| __/ _ \ '__|                    |
#   |                  | |___| | |_| \__ \ ||  __/ |                       |
#   |                   \____|_|\__,_|___/\__\___|_|                       |
#   |                                                                      |
#   '----------------------------------------------------------------------'

# def check_esx_vsphere_objects_cluster(_no_item, params, info):
#     print info
#     return 3, "MIST"
#
# check_info['esx_vsphere_objects.cluster'] = {
#   "check_function"      : check_esx_vsphere_objects_cluster,
#   "service_description" : "HostSystem SUMMARY",
# }
