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

# .1.3.6.1.4.1.2620.1.5.2.0 1
# .1.3.6.1.4.1.2620.1.5.3.0 6
# .1.3.6.1.4.1.2620.1.5.4.0 0
# .1.3.6.1.4.1.2620.1.5.5.0 yes
# .1.3.6.1.4.1.2620.1.5.6.0 active
# .1.3.6.1.4.1.2620.1.5.7.0 OK
# .1.3.6.1.4.1.2620.1.5.101.0 0
# .1.3.6.1.4.1.2620.1.5.103.0


def inventory_checkpoint_ha_status(info):
    installed, _major, _minor, _started, _state, _block_state, _stat_code, _stat_long = info[0]
    if installed != "0":
        return [(None, None)]


def check_checkpoint_ha_status(_no_item, _no_params, info):
    installed, major, minor, started, state, block_state, stat_code, stat_long = info[0]

    # Some devices have a trailing "\n" in the state field. Drop it.
    state = state.rstrip()

    if installed == "0":
        yield 2, "Not installed"
    else:
        yield 0, "Installed: v%s.%s" % (major, minor)

        for val, infotext, ok_vals, warn_vals in [(started, "Started", ["yes"], None),
                                                  (state, "Status", ["active", "standby"], None),
                                                  (block_state, "Blocking", ["ok"],
                                                   ["initializing"])]:
            if ok_vals is None or val.lower() in ok_vals:
                status = 0
            elif warn_vals is not None and val.lower() in warn_vals:
                status = 1
            else:
                status = 2

            yield status, "%s: %s" % (infotext, val)

        if stat_code != "0":
            yield 2, "Problem: %s" % stat_long


check_info['checkpoint_ha_status'] = {
    'check_function': check_checkpoint_ha_status,
    'inventory_function': inventory_checkpoint_ha_status,
    'service_description': "HA Status",
    'snmp_scan_function': scan_checkpoint,
    'snmp_info': (
        '.1.3.6.1.4.1.2620.1.5',
        [
            2,  # haInstalled
            3,  # haVerMajor
            4,  # haVerMinor
            5,  # haStarted
            6,  # haState
            7,  # haBlockState
            101,  # haStatCode
            103,  # haStatLong
        ]),
    'includes': ['checkpoint.include'],
}
