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

veeam_tapejobs_default_levels = (1*3600*24, 2*3600*24)

def parse_veeam_tapejobs(info):
    parsed = {}
    columns = map(lambda s: s.lower(), info[0])

    for line in info[1:]:
        name = " ".join( line[:-(len(columns)-1)] )
        job_id, last_result, last_state = line[-(len(columns)-1):]
        parsed[name] = {
            "job_id"        : job_id,
            "last_result"   : last_result,
            "last_state"    : last_state,
        }

    return parsed


def inventory_veeam_tapejobs(parsed):
    for job in parsed:
        yield job, "veeam_tapejobs_default_levels"


def check_veeam_tapejobs(item, params, parsed):
    last_result = parsed[item]["last_result"]
    last_state = parsed[item]["last_state"]
    statetext = ", last state: " + last_state

    warn, crit = params

    now = time.time()

    running_since = get_item_state("running_since")

    if last_result == "None" and last_state == "Working":
        if not running_since:
            running_since = now
            set_item_state("running_since", now)
        running_time = now - running_since
        status = 0
        levelstext = " (warn/crit at %s/%s)" % tuple(map(get_age_human_readable, params))
        if running_time >= crit:
            status = 2
        elif running_time >= warn:
            status = 1
        else:
            levelstext = ""
        infotext = "Backup in progress since " + get_timestamp_human_readable(running_since)\
                    + " (" + get_age_human_readable(running_time) + ")" + levelstext
        return status, infotext
    else:
        clear_item_state("running_since")
        if last_result == "Success":
            return 0, "Last backup successful" + statetext
        elif last_result == "Warning":
            return 1, "Last backup result: " + last_result + statetext
        else:
            return 2, "Last backup result: " + last_result + statetext



check_info["veeam_tapejobs"] = {
    'parse_function'        :   parse_veeam_tapejobs,
    'inventory_function'    :   inventory_veeam_tapejobs,
    'check_function'        :   check_veeam_tapejobs,
    'service_description'   :   'VEEAM Tape Job %s',
    'group'                 :   'veeam_tapejobs',
}
