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

# .1.3.6.1.4.1.231.7.2.9.3.8.1.3.1  "OpenStage 30"
# .1.3.6.1.4.1.231.7.2.9.3.8.1.3.2  "OpenStage 30"
# .1.3.6.1.4.1.231.7.2.9.3.8.1.3.7  "P. O. T."
# .1.3.6.1.4.1.231.7.2.9.3.8.1.3.9  "S0 extension"
# .1.3.6.1.4.1.231.7.2.9.3.8.1.3.11  "S0 trunk: extern"
# .1.3.6.1.4.1.231.7.2.9.3.8.1.3.13  "<not configured>: extern"
# [...]
# .1.3.6.1.4.1.231.7.2.9.3.8.1.4.11  2
# .1.3.6.1.4.1.231.7.2.9.3.8.1.4.13  1


def inventory_octopus_trunks(info):
    trunkports = ["S0 trunk: extern"]
    inventory = []
    for line in info[0]:
        if len(line) == 4:
            portindex, cardindex, porttype, portstate = line
            portdesc = "%s/%s" % (cardindex, portindex)
            if porttype in trunkports and portstate == "2":
                inventory.append((portdesc, None))
    return inventory


def check_octopus_trunks(item, _no_params, info):
    for line in info[0]:
        portindex, cardindex, porttype, portstate = line
        portdesc = "%s/%s" % (cardindex, portindex)
        if item == portdesc:
            # There are two relevant card states, we use the one from
            # octoPortTable
            if portstate == "1":
                return (2, "Port [%s] is inactive" % porttype)
            return (0, "Port [%s] is active" % porttype)

    return (3, "UNKW - unknown data received from agent")


check_info["sni_octopuse_trunks"] = {
    "check_function": check_octopus_trunks,
    "inventory_function": inventory_octopus_trunks,
    "service_description": "Trunk Port %s",
    "snmp_scan_function": lambda oid: "agent for hipath" in oid(".1.3.6.1.2.1.1.1.0").lower(),
    "snmp_info": [
        (
            ".1.3.6.1.4.1.231.7.2.9.3.8.1",
            [
                "1",  # portIndex
                "2",  # port card index (slot)
                "3",  # PortType
                "4",  # Port State
            ],
        ),
    ],
}
