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


def parse_storeonce4x_d2d_services(info):
    return json.loads(info[0][0])["services"]


def check_storeonce4x_d2d_services(_item, _params, parsed):

    health_map = {"OK": 0, "WARNING": 1, "CRITICAL": 2}

    for service_name, service_data in parsed.items():

        healthLevelString = service_data["healthLevelString"]
        healthString = service_data["healthString"]
        subsystemState = service_data["subsystemState"]
        yield health_map.get(healthLevelString, 3), "%s: %s (%s)" % (
            service_name,
            healthString,
            subsystemState,
        )


check_info["storeonce4x_d2d_services"] = {
    "parse_function": parse_storeonce4x_d2d_services,
    "inventory_function": discover_single,
    "check_function": check_storeonce4x_d2d_services,
    "service_description": "D2D Services",
}
