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

# <<<hyperv_checkpoints>>>
# Has_Checkpoints
# f5689086-243b-4dfe-9775-571ef6be8a1b 2063
# c85ae17b-1a6c-4a34-949a-a1b9385ef67a 2040


def inventory_hyperv_checkpoints(info):
    return [(None, {})]


def check_hyperv_checkpoints(item, params, info):
    snapshots = []
    for line in info:
        if len(line) != 2:
            continue
        snapshots.append((line[0], int(line[1])))

    if snapshots:
        yield 0, "%s checkpoints" % len(snapshots)

        # We assume that the last snapshot is the last line
        # of the agent output
        for title, key, snapshot in [
            ("Oldest", "age_oldest", max(snapshots, key=lambda x:x[1])),
            ("Last",   "age",        snapshots[-1]),
        ]:
            name, age = snapshot
            infotext = "%s: %s (%s)" % (title, get_age_human_readable(age), name)
            warn, crit = params.get(key, (None, None))
            if crit is not None and age >= crit:
                state = 2
            elif warn is not None and age >= warn:
                state = 1
            else:
                state = 0
            if state:
                infotext += " (warn/crit at %s/%s)" % (
                            get_age_human_readable(warn),
                            get_age_human_readable(crit))
            yield state, infotext, [(key, age, warn, crit)]
    else:
        yield 0, "No Checkpoints found"


check_info["hyperv_checkpoints"] = {
    "check_function"        : check_hyperv_checkpoints,
    "inventory_function"    : inventory_hyperv_checkpoints,
    "service_description"   : "HyperV Checkpoints",
    "group"                 : "vm_snapshots",
    "has_perfdata"          : True,
}
