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

fast_lta_headunit_info = [(".1.3.6.1.4.1.27417.2", [1,  # headUnitStatus
                                                    2,  # replicationMode
                                                    5]  # replicationRunning
                         )]

def fast_lta_headunit_scan(oid):
        return oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.8072.3.2.10")

#   .--status--------------------------------------------------------------.
#   |                         _        _                                   |
#   |                     ___| |_ __ _| |_ _   _ ___                       |
#   |                    / __| __/ _` | __| | | / __|                      |
#   |                    \__ \ || (_| | |_| |_| \__ \                      |
#   |                    |___/\__\__,_|\__|\__,_|___/                      |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_fast_lta_headunit_status(info):
    if len(info[0]) > 0:
        return [ (None, None) ]
    else:
        return []

def check_fast_lta_headunit_status(item, _no_params, info):
    head_unit_status_map = {
        "-1": "workerDefect",
        "-2": "workerNotStarted",
        "2" : "workerBooting",
        "3" : "workerRfRRunning",
        "10": "appBooting",
        "20": "appNoCubes",
        "30": "appVirginCubes",
        "40": "appRfrPossible",
        "45": "appRfrMixedCubes",
        "50": "appRfrActive",
        "60": "appReady",
        "65": "appMixedCubes",
        "70": "appReadOnly",
        "75": "appEnterpriseCubes",
        "80": "appEnterpriseMixedCubes",
    }

    if info[0][0][0] == "60":
        status = 0
    elif info[0][0][0] == "70" and info[0][0][1] == "0":
        # on Slave node appReadOnly is also an ok state
        status = 0
    else:
        status = 2

    if info[0][0][0] in head_unit_status_map.keys():
        message = "Head Unit status is %s." % head_unit_status_map[info[0][0][0]]
    else:
        message = "Head Unit status is %s." % info[0][0][0]

    return status, message

check_info["fast_lta_headunit.status"] = {
    "check_function"        : check_fast_lta_headunit_status,
    "inventory_function"    : inventory_fast_lta_headunit_status,
    "service_description"   : "Fast LTA Headunit Status",
    "has_perfdata"          : False,
    "snmp_info"             : fast_lta_headunit_info,
    "snmp_scan_function"    : fast_lta_headunit_scan
}

#.
#   .--replication---------------------------------------------------------.
#   |                          _ _           _   _                         |
#   |           _ __ ___ _ __ | (_) ___ __ _| |_(_) ___  _ __              |
#   |          | '__/ _ \ '_ \| | |/ __/ _` | __| |/ _ \| '_ \             |
#   |          | | |  __/ |_) | | | (_| (_| | |_| | (_) | | | |            |
#   |          |_|  \___| .__/|_|_|\___\__,_|\__|_|\___/|_| |_|            |
#   |                   |_|                                                |
#   '----------------------------------------------------------------------'

def inventory_fast_lta_headunit_replication(info):
    if len(info[0]) > 0:
        return [ (None, None) ]
    else:
        return []

def check_fast_lta_headunit_replication(item, _no_params, info):
    head_unit_replication_map = {
        "0"   : "Slave",
        "1"   : "Master",
        "255" : "standalone",
    }

    if info[0][0][2] == "1":
        message = "Replication is running."
        status = 0
    else:
        message = "Replication is not running (!!)."
        status = 2

    if info[0][0][1] in head_unit_replication_map.keys():
        message += " This node is %s." \
                   % head_unit_replication_map[info[0][0][1]]
    else:
        message += " Replication mode of this node is %s." % info[0][0][1]

    return status, message

check_info["fast_lta_headunit.replication"] = {
    "check_function"        : check_fast_lta_headunit_replication,
    "inventory_function"    : inventory_fast_lta_headunit_replication,
    "service_description"   : "Fast LTA Replication",
    "has_perfdata"          : False,
    "snmp_info"             : fast_lta_headunit_info,
    "snmp_scan_function"    : fast_lta_headunit_scan
}

#.
