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

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.stormshield import *  # pylint: disable=wildcard-import,unused-wildcard-import

route_type_mapping = {
    "DefaultRoute": "default route",
    "PBR": "policy based routing",
    "": "not defined",
}

route_state_mapping = {
    "UP": (0, "Route is up"),
    "DOWN": (2, "Route is down"),
    "UNDEF": (3, "Route is undefined"),
}


def inventory_stormshield_route(info):
    for line in info:
        if line[5] == "UP":
            yield (line[0], None)


def check_stormshield_route(item, params, info):
    for line in info:
        if line[0] == item:
            _index, typ, name, gateway_name, gateway_type, state = line
            yield route_state_mapping[state]
            infotext = "Type: %s, Router name: %s, Gateway name: %s, Gateway type: %s" % (
                route_type_mapping[typ],
                name,
                gateway_name,
                gateway_type,
            )
            yield 0, infotext


check_info["stormshield_route"] = {
    "inventory_function": inventory_stormshield_route,
    "check_function": check_stormshield_route,
    "service_description": "Gateway %s",
    "snmp_info": (
        ".1.3.6.1.4.1.11256.1.14.1.1",
        [
            "1",  # snsRouteIndex
            "2",  # snsRouteType
            "4",  # snsRouteRouterName
            "5",  # snsRouteGatewayName
            "7",  # snsRouteGatewayType
            "9",  # snsRouteState
        ],
    ),
    "snmp_scan_function": stormshield_scan_function,
}
