#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2013             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.


def inventory_alcatel_timetra_chassis(info):
    for name, _adminstate, operstate, _alarmstate in info:
        # Only add active devices
        if operstate in ['2', '8']:
            yield name, None


def check_alcatel_timetra_chassis(item, _no_params, info):
    admin_states = {
        1: (0, "noop"),
        2: (0, "in service"),
        3: (1, "out of service"),
        4: (2, "diagnose"),
        5: (2, "operate switch"),
    }

    oper_states = {
        1: (3, "unknown"),
        2: (0, "in service"),
        3: (2, "out of service"),
        4: (1, "diagnosing"),
        5: (2, "failed"),
        6: (1, "booting"),
        7: (3, "empty"),
        8: (0, "provisioned"),
        9: (3, "unprovisioned"),
        10: (1, "upgrade"),
        11: (1, "downgrade"),
        12: (1, "in service upgrade"),
        13: (1, "in service downgrade"),
        14: (1, "reset pending"),
    }

    alarm_states = {
        0: (0, "unknown"),
        1: (2, "alarm active"),
        2: (0, "alarm cleared"),
    }
    for line in info:
        if line[0] == item:
            adminstate, operstate, alarmstate = map(int, line[1:])
            if operstate != adminstate:
                if admin_states[adminstate][0] != 0:
                    yield admin_states[adminstate][
                        0], "Admin state: %s" % admin_states[adminstate][1]

            yield oper_states[operstate][0], "Operational state: %s" % oper_states[operstate][1]

            if alarm_states[alarmstate][0] != 0:
                yield alarm_states[alarmstate][0], "Alarm state: %s" % alarm_states[alarmstate][1]
            return


check_info["alcatel_timetra_chassis"] = {
    "check_function": check_alcatel_timetra_chassis,
    "inventory_function": inventory_alcatel_timetra_chassis,
    "service_description": "Device %s",
    "snmp_scan_function": lambda oid: "TiMOS" in oid(".1.3.6.1.2.1.1.1.0"),
    "snmp_info": (
        ".1.3.6.1.4.1.6527.3.1.2.2.1.8.1",
        [
            8,  # tmnxHwName
            15,  # tmnxHwAdminState
            16,  # tmnxHwOperState
            24,  # tmnxHwAlarmState
        ]),
}
