#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2017             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-
# tails. 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.2606.4.2.3.1.0 2
# .1.3.6.1.4.1.2606.4.2.3.2.0 CMC-TC-IOU
# .1.3.6.1.4.1.2606.4.2.3.3.0 60263
# .1.3.6.1.4.1.2606.4.2.3.4.0 1


def parse_cmctc_ports(info):
    def parse_single_port(port_info):
        type_map = {
            '1': 'not available',
            '2': 'IO',
            '3': 'Access',
            '4': 'Climate',
            '5': 'FCS',
            '6': 'RTT',
            '7': 'RTC',
            '8': 'PSM',
            '9': 'PSM8',
            '10': 'PSM metered',
            '11': 'IO wireless',
            '12': 'PSM6 Schuko',
            '13': 'PSM6C19',
            '14': 'Fuel Cell',
            '15': 'DRC',
            '16': 'TE cooler',
            '17': 'PSM32 metered',
            '18': 'PSM8x8',
            '19': 'PSM6x6 Schuko',
            '20': 'PSM6x6C19',
        }

        status_map = {
            '1': 'ok',
            '2': 'error',
            '3': 'configuration changed',
            '4': 'quit from sensor unit',
            '5': 'timeout',
            '6': 'unit detected',
            '7': 'not available',
            '8': 'supply voltage low',
        }

        _, device_type, description, serial_number, device_status = port_info

        parsed = {
            'type': type_map.get(device_type),
            'status': status_map.get(device_status),
            'serial': serial_number,
        }

        if parsed['status'] == 'not available':
            return None

        return description, parsed

    parsed = {}
    # cmctc_lcp uses port numbers the range 3-6.
    # Therefore, we start counting at 3 here as well
    # to stay consistent.
    for number, port_info in enumerate(info, 3):
        parsed_port = parse_single_port(port_info)
        if parsed_port:
            description, entry = parsed_port
            name = '%d %s' % (number, description)
            parsed[name] = entry

    return parsed


def inventory_cmctc_ports(parsed):
    for entry in parsed:
        yield entry, {}


def check_cmctc_ports(item, _no_params, parsed):
    port = parsed.get(item)
    if not port:
        return None

    status_map = {
        'ok': 0,
        'configuration changed': 1,
        'unit detected': 1,
        'error': 2,
        'quit from sensor unit': 2,
        'timeout': 2,
        'not available': 2,
        'supply voltage low': 2,
    }

    state = status_map.get(port['status'], 3)
    infotext = ('Status: %(status)s, '
                'Device type: %(type)s, '
                'Serial number: %(serial)s') % port

    return state, infotext


check_info['cmctc_ports'] = {
    "parse_function"      : parse_cmctc_ports,
    "inventory_function"  : inventory_cmctc_ports,
    "check_function"      : check_cmctc_ports,
    "service_description" : "Port %s",
    "snmp_scan_function"  : cmctc_snmp_scan_function,
    "snmp_info"           : (".1.3.6.1.4.1.2606.4.2",  # RITTAL-CMC-TC-MIB
                                [
                                    "3",  # cmcTcStatusSensorUnit1
                                    "4",  # cmcTcStatusSensorUnit2
                                    "5",  # cmcTcStatusSensorUnit3
                                    "6",  # cmcTcStatusSensorUnit4
                                ],
                                [
                                    OID_END,
                                    "1",  # cmcTcUnit[1-4]TypeOfDevice
                                    "2",  # cmcTcUnit[1-4]Text
                                    "3",  # cmcTcUnit[1-4]Serial
                                    "4",  # cmcTcUnit[1-4]Status
                                ],
                            ),
    "includes"            : [ "cmctc.include" ],
}
