#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

#.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)
            else:
                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",
    'has_perfdata'       : False,
    '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
                                 ]),
                            ]
}

