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

# 2019-01-07, comNET GmbH, Fabian Binder
MAP_RPO_STATES = {
    "0": (1, "VPG is initializing"),
    "1": (0, "Meeting SLA specification"),
    "2": (2, "Not meeting SLA specification for RPO SLA and journal history"),
    "3": (2, "Not meeting SLA specification for RPO SLA"),
    "4": (2, "Not meeting SLA specification for journal history"),
    "5": (1, "VPG is in a failover operation"),
    "6": (1, "VPG is in a move operation"),
    "7": (1, "VPG is being deleted"),
    "8": (1, "VPG has been recovered"),
}


def parse_zerto_vpg(info):
    parsed = {}
    for line in info:
        if len(line) < 3:
            continue
        vpgname = line[0]
        vpg = parsed.setdefault(vpgname, {})
        vpg["state"] = line[1]
        vpg["actual_rpo"] = line[2]
    return parsed


@get_parsed_item_data
def check_zerto_vpg_rpo(_item, _params, data):
    state, vpg_info = MAP_RPO_STATES.get(data.get("state"), (3, "Unknown"))
    yield state, "VPG Status: %s" % vpg_info


check_info["zerto_vpg_rpo"] = {
    "parse_function": parse_zerto_vpg,
    "inventory_function": discover(),
    "check_function": check_zerto_vpg_rpo,
    "service_description": "Zerto VPG RPO %s",
    "has_perfdata": False,
    "group": "zerto_vpg_rpo",
}
