#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# Example output from agent on success:
# <<<timemachine>>>
# /Volumes/Backup/Backups.backupdb/macvm/2013-11-28-202610
#
# Example output from agent on failure:
# <<<timemachine>>>
# Unable to locate machine directory for host.

factory_settings["timemachine_default_levels"] = {"age": (86400, 172800)}  # 1d/2d


def inventory_timemachine(info):
    if " ".join(info[0]) != "Unable to locate machine directory for host.":
        return [(None, {})]


def check_timemachine(item, params, info):
    line = " ".join(info[0])  # We expect at least one line
    if not line.startswith("/Volumes/"):
        return 2, "Backup seems to have failed, message was: " + line

    tokens = line.split("/")
    timestamp = tokens[-1]
    backup_timeb = time.strptime(timestamp, "%Y-%m-%d-%H%M%S")
    backup_time = time.mktime(backup_timeb)
    backup_age = time.time() - backup_time

    if backup_age < 0:
        return 3, "Timestamp of last backup is in the future: " + timestamp

    infotext = "Last backup was at %s (%s ago)" % (
        time.strftime("%c", backup_timeb),
        get_age_human_readable(backup_age),
    )

    warn, crit = params["age"]
    if backup_age >= crit:
        state = 2
        infotext += ", more than %s ago" % get_age_human_readable(crit)
    elif backup_age >= warn:
        state = 1
        infotext += ", more than %s ago" % get_age_human_readable(warn)
    else:
        state = 0

    return state, infotext


check_info["timemachine"] = {
    "check_function": check_timemachine,
    "inventory_function": inventory_timemachine,
    "default_levels_variable": "timemachine_default_levels",
    "service_description": "Backup Timemachine",
    "group": "backup_timemachine",
}
