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

# docsIfCmStatusValue                         1.3.6.1.2.1.10.127.1.2.2.1.1
# docsIfCmStatusT1Timeouts                    1.3.6.1.2.1.10.127.1.2.2.1.10
# docsIfCmStatusT2Timeouts                    1.3.6.1.2.1.10.127.1.2.2.1.11
# docsIfCmStatusT3Timeouts                    1.3.6.1.2.1.10.127.1.2.2.1.12
# docsIfCmStatusT4Timeouts                    1.3.6.1.2.1.10.127.1.2.2.1.13
# docsIfCmStatusRangingAborteds               1.3.6.1.2.1.10.127.1.2.2.1.14
# docsIfCmStatusDocsisOperMode                1.3.6.1.2.1.10.127.1.2.2.1.15
# docsIfCmStatusModulationType                1.3.6.1.2.1.10.127.1.2.2.1.16
# docsIfCmStatusCode                          1.3.6.1.2.1.10.127.1.2.2.1.2
# docsIfCmStatusTxPower                       1.3.6.1.2.1.10.127.1.2.2.1.3
# docsIfCmStatusResets                        1.3.6.1.2.1.10.127.1.2.2.1.4
# docsIfCmStatusLostSyncs                     1.3.6.1.2.1.10.127.1.2.2.1.5
# docsIfCmStatusInvalidMaps                   1.3.6.1.2.1.10.127.1.2.2.1.6
# docsIfCmStatusInvalidUcds                   1.3.6.1.2.1.10.127.1.2.2.1.7
# docsIfCmStatusInvalidRangingResponses       1.3.6.1.2.1.10.127.1.2.2.1.8
# docsIfCmStatusInvalidRegistrationResponses  1.3.6.1.2.1.10.127.1.2.2.1.9

factory_settings["docsis_cm_status_default_levels"] = {
    "tx_power": (20.0, 10.0),
    "error_states": [13, 2, 1],
}


def inventory_docsis_cm_status(info):
    for line in info:
        yield line[0], {}


def check_docsis_cm_status(item, params, info):
    status_table = {
        1: "other",
        2: "not ready",
        3: "not synchronized",
        4: "PHY synchronized",
        5: "upstream parameters acquired",
        6: "ranging complete",
        7: "IP complete",
        8: "TOD established",
        9: "security established",
        10: "params transfer complete",
        11: "registration complete",
        12: "operational",
        13: "access denied",
    }

    for sid, status, tx_power in info:
        if sid == item:
            # Modem StatusD
            status = int(status)
            infotext = "Status: %s" % status_table[status]
            state = 0
            if status in params['error_states']:
                state = 2
            yield state, infotext

            # TX Power
            tx_power_dbmv = float(tx_power) / 10
            warn, crit = params['tx_power']
            levels = " (warn/crit at %.1f/%.1f dBmV)" % (warn, crit)
            state = 0
            infotext = "TX Power is %.1f dBmV" % tx_power_dbmv
            if tx_power_dbmv <= crit:
                state = 2
                infotext += levels
            elif tx_power_dbmv <= warn:
                state = 1
                infotext += levels
            yield state, infotext, [('tx_power', tx_power_dbmv, warn, crit)]
            return

        yield 3, "Status Entry not found"


check_info["docsis_cm_status"] = {
    "check_function": check_docsis_cm_status,
    "inventory_function": inventory_docsis_cm_status,
    "service_description": "Cable Modem %s Status",
    "snmp_scan_function": docsis_scan_function_cable_modem,
    "snmp_info": (
        ".1.3.6.1.2.1.10.127.1.2.2.1",
        [
            OID_END,
            1,  #docsIfCmStatusValue
            3,  #docsIfCmStatusTxPower
        ]),
    "default_levels_variable": "docsis_cm_status_default_levels",
    "group": "docsis_cm_status",
    "has_perfdata": True,
    "includes": ["docsis.include"],
}
