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


def parse_seh_ports(info):
    parsed = {}
    for oid_end, tag, status, port_number in info[0]:
        oid_index = oid_end.split('.')[0]
        if tag != u"":
            parsed.setdefault(oid_index, {}).update(tag=tag)
        if port_number != "0":
            parsed.setdefault(port_number, {}).update(status=status)
    return parsed


def inventory_seh_ports(parsed):
    for key, port in parsed.items():
        yield key, {"status_at_discovery": port.get("status")}


@get_parsed_item_data
def check_seh_ports(item, params, data):
    for key in ("tag", "status"):
        if key in data:
            yield 0, "%s: %s" % (key.title(), data[key])

    if params.get("status_at_discovery") != data.get("status"):
        yield 1, "Status during discovery: %s" % (params.get("status_at_discovery") or "unknown")


check_info['seh_ports'] = {
    "parse_function": parse_seh_ports,
    "inventory_function": inventory_seh_ports,
    "check_function": check_seh_ports,
    "service_description": "Port %s",
    "snmp_info": [(
        ".1.3.6.1.4.1.1229.2.50.2.1",
        [
            OID_END,
            "10",  #PortTag
            "26",  #Connection status
            "27",  #UTN slot number
        ])],
    "snmp_scan_function": lambda oid: '.1.3.6.1.4.1.1229.1.1' in oid(".1.3.6.1.2.1.1.2.0"),
}
