#!/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-
# 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_jobs:sep(9) >>>
# BACKUP_RIS      Backup  Stopped Success 27.10.2013 22:00:17     27.10.2013 22:06:12
# BACKUP_R43-local_HXWH44 Backup  Stopped Success 26.10.2013 18:00:20     26.10.2013 18:46:03
# BACKUP_R43-Pool4_HXWH44 Backup  Stopped Failed  26.10.2013 23:13:13     27.10.2013 00:51:17
# BACKUP_R43-Pool3_HXWH44 Backup  Stopped Failed  27.10.2013 02:59:29     27.10.2013 08:59:51
# REPL_KNESXIDMZ  Replica Stopped Success 27.10.2013 44:00:01     27.10.2013 44:44:26
# BACKUP_KNESXI   Backup  Stopped Success 28.10.2013 05:00:04     28.10.2013 05:32:15
# BACKUP_KNESXit  Backup  Stopped Success 26.10.2013 22:30:02     27.10.2013 02:37:30
# BACKUP_R43-Pool5_HXWH44 Backup  Stopped Success 27.10.2013 23:00:00     27.10.2013 23:04:53
# BACKUP_R43-Pool2_HXWH44 Backup  Stopped Failed  27.10.2013 02:37:45     27.10.2013 02:45:35


def inventory_veeam_jobs(info):
    return [(x[0], None) for x in info]


def check_veeam_jobs(item, _no_params, info):
    for line in info:
        if len(line) < 6:
            continue  # Skip incomplete lines

        job_name, job_type, job_last_state, job_last_result, \
            job_creation_time, job_end_time = line[:6]

        if job_name != item:
            continue  # Skip not matching lines

        if job_last_state in ["Working", "Postprocessing"]:
            return 0, "Running since %s (current state is: %s)" % (job_creation_time,
                                                                   job_last_state)

        if job_last_result == "Success":
            state = 0
        elif job_last_state == 'Idle' and job_type == "BackupSync":
            # A sync job is always idle
            state = 0
        elif job_last_result == "Failed":
            state = 2
        elif job_last_state == "Stopped" and job_last_result == "Warning":
            state = 1
        else:
            state = 3

        return state, "State: %s, Result: %s, Creation time: %s, End time: %s, Type: %s" % \
            (job_last_state, job_last_result, job_creation_time, job_end_time, job_type)


check_info["veeam_jobs"] = {
    'check_function': check_veeam_jobs,
    'inventory_function': inventory_veeam_jobs,
    'service_description': 'VEEAM Job %s',
}
