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

import datetime, time

veeam_client_default_levels = { "age": ( 108000, 172800 ) } # 30h/2d

state = { "Success": 0, "Failed": 2, "Warning":1, "InProgress":0, "Pending":0 }

def inventory_veeam_client(info):
    if info[0][0] == "Status":
        return [ (None, veeam_client_default_levels) ]

def check_veeam_client(item, params, info):
    if params.get("age"):
        warn, crit = params['age']
    else:
        warn, crit = veeam_default_levels.get("age")

    if not info[0][0] == "Status":
        return 3, "No status data found in first line of agent data"
    result = ""
    starttime = ""
    stoptime = ""
    duration_string = ""
    infotxt = ""
    for line in info:
        if line[0] == "Status":
            result = line[1]
            perfdata = []
            infotxt = line[0] + ":" + line[1] + " "
        elif line[0] == "DurationDDHHMMSS" and result != "InProgress" and result != "Pending":
            duration_string = line[1]
            spur = map(saveint, duration_string.split(":") )
            if len(spur) == 4:
                duration = str(spur[0]*86400 + spur[1] * 3600 + spur[2] * 60 + spur[3])
            else:
                duration = "0"
            perfdata.append(("duration", duration+"s", 0, 0, 0))
        elif line[0] == "AvgSpeedBps":
            avgspeed = saveint(line[1])
            perfdata.append(("avgspeed", avgspeed, 0, 0, 0))
        elif line[0] == "TotalSizeByte":
            totalsize = saveint(line[1])
            perfdata.append(("totalsize", totalsize, 0, 0, 0))
        elif line[0] == "StopTime":
            if (result == "InProgress" or result == "Pending"):
                infotxt += line[0] + ": - "
            else:
                now = datetime.datetime.now()
                date, time = line[1].split(" ")
                day, month, year = map(int, date.split("."))
                hour, minute, second = map(int, time.split(":"))
                stoptime = datetime.datetime( year, month, day, hour, minute, second)
                warndiff = datetime.timedelta(seconds=warn)
                critdiff = datetime.timedelta(seconds=crit)

                if critdiff < now - stoptime:
                    stopstate = 2
                    stopsym = "( >%s ago)(!!)" % get_age_human_readable(crit)
                elif warndiff < now - stoptime:
                    stopstate = 1
                    stopsym = "( >%s ago)(!)" % get_age_human_readable(warn)
                else:
                    stopstate = 0
                    stopsym = ""
                infotxt += line[0] + ":" + line[1] + stopsym + " "
        elif len(line) == 2:
            infotxt += line[0] + ":" + line[1] + " "

    status = max(state[result], stopstate)
    return (status, infotxt, perfdata)

check_info["veeam_client"] = {
    'check_function':          check_veeam_client,
    'inventory_function':      inventory_veeam_client,
    'service_description':     'VEEAM Client',
    'group':                   'veeam_backup',
    'has_perfdata':            True,
}

