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

factory_settings["docsis_channels_downstream"] = {
    "power"     : ( 5.0, 1.0 ),
}

def inventory_docsis_channels_downstream(info):
    for line in info:
        if line[1] != '0':
            yield line[0], {}

def check_docsis_channels_downstream(item, params, info):
    for channel_id, frequency, power in info:
        if channel_id == item:
            # Power
            warn, crit = params["power"]
            power_dbmv = float(int(power)) / 10
            infotext = "Power is %.1f dBmV" % power_dbmv
            levels = " (Levels Warn/Crit at %d dBmV/ %d dBmV)" % ( warn, crit )
            state = 0
            if power_dbmv <= crit:
                state = 2
                infotext += levels
            elif power_dbmv <= warn:
                state = 1
                infotext += levels
            yield state, infotext, [ ('power', power_dbmv, warn, crit ) ]

            # Check Frequency
            frequency_mhz = float(frequency) / 1000000
            infotext = "Frequency is %.1f MHz" % frequency_mhz
            perfdata = [("frequency" , frequency_mhz, warn, crit )]
            state = 0
            if "frequency" in params:
                warn, crit = params["frequency"]
                levels = " (warn/crit at %d MHz/ %d MHz)" % ( warn, crit )
                if frequency_mhz >= crit:
                    state = 2
                    infotext += levels
                elif frequency_mhz >= warn:
                    state  = 1
                    infotext += levels
            # Change this to yield in case of future extension of the check
            yield state, infotext, perfdata
            return

    yield 3, "Channel information not found in SNMP data"


# This Check is a subcheck because there is also a upstream version possible
check_info["docsis_channels_downstream"] = {
    "check_function"          : check_docsis_channels_downstream,
    "inventory_function"      : inventory_docsis_channels_downstream,
    "service_description"     : "Downstream Channel %s",
    "has_perfdata"            : True,
    "snmp_scan_function"      : docsis_scan_function,
    "snmp_info"               : ( ".1.3.6.1.2.1.10.127.1.1.1.1", [  1, # docsIfDownChannelId
                                                                    2, # docsIfDownChannelFrequency
                                                                    6, # docsIfDownChannelPower (1/10 dBmV)
                                                                    ]),
    "group"                   : "docsis_channels_downstream",
    "default_levels_variable" : "docsis_channels_downstream",
    "includes"                : [ "docsis.include" ],
}

# Information for future extensions of the check:
# docsIfDownChannelId             1.3.6.1.2.1.10.127.1.1.1.1.1
# docsIfDownChannelFrequency      1.3.6.1.2.1.10.127.1.1.1.1.2
# docsIfDownChannelWidth          1.3.6.1.2.1.10.127.1.1.1.1.3
# docsIfDownChannelModulation     1.3.6.1.2.1.10.127.1.1.1.1.4
# docsIfDownChannelInterleave     1.3.6.1.2.1.10.127.1.1.1.1.5
# docsIfDownChannelPower          1.3.6.1.2.1.10.127.1.1.1.1.6
# docsIfDownChannelAnnex          1.3.6.1.2.1.10.127.1.1.1.1.7
