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

def inventory_cmciii_lcp_waterflow(info):
    if info:
        return [(None, None)]

def check_cmciii_lcp_waterflow(item, params, info):
    if info[0]:
        # We have a list of values where no item has a fixed index. We
        # try to detect the starting index for the needed values now.
        try:
            index = info[0].index('Waterflow')
            name, flow, maxflow, minflow, status = info[0][index:index+5]
        except ValueError:
            return 3, 'Waterflow information not found'

        unit    = flow.split(" ", 1)[1]
        flow    = float(flow.split(" ", 1)[0])
        minflow = float(minflow.split(" ", 1)[0])
        maxflow = float(maxflow.split(" ", 1)[0])

        sym = ""
        state = 0
        if status != "OK":
            state = 2
            sym = "(!!)"
        elif flow < minflow or flow > maxflow:
            state = 1
            sym = "(!)"

        info_text = "%s Status: %s Flow: %.1f%s, MinFlow: %.1f, MaxFLow: %.1f" \
                % (name, status, flow, sym, minflow, maxflow)

        perfdata = [ ("flow", str(flow)+unit , str(minflow)+":"+str(maxflow), 0, 0 ) ]

        return state, info_text, perfdata

    return (3, "no SNMP data found")

check_info['cmciii_lcp_waterflow'] = {
    "check_function"      : check_cmciii_lcp_waterflow,
    "inventory_function"  : inventory_cmciii_lcp_waterflow,
    "has_perfdata"        : True,
    "service_description" : "LCP Fanunit WATER FLOW",
    "snmp_scan_function"  : lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("Rittal LCP"),
    "snmp_info"           : ( '.1.3.6.1.4.1.2606.7.4.2.2.1.10.2', range(74, 87)),
}
