#!/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.7779.3.1.1.2.1.2.1.1.X.X.X.X.X X.X.X.X --> IB-PLATFORMONE-MIB::ibNodeIPAddress."11.112.133.14"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.1.X.X.X.X.X X.X.X.X --> IB-PLATFORMONE-MIB::ibNodeIPAddress."11.112.133.17"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.2.X.X.X.X.X Online --> IB-PLATFORMONE-MIB::ibNodeReplicationStatus."11.112.133.14"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.2.X.X.X.X.X Online --> IB-PLATFORMONE-MIB::ibNodeReplicationStatus."11.112.133.17"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.3.X.X.X.X.X 0 --> IB-PLATFORMONE-MIB::ibNodeQueueFromMaster."11.112.133.14"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.3.X.X.X.X.X 0 --> IB-PLATFORMONE-MIB::ibNodeQueueFromMaster."11.112.133.17"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.4.X.X.X.X.X 2016/04/13 14:15:51 --> IB-PLATFORMONE-MIB::ibNodeLastRepTimeFromMaster."11.112.133.14"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.4.X.X.X.X.X 2016/04/13 14:15:51 --> IB-PLATFORMONE-MIB::ibNodeLastRepTimeFromMaster."11.112.133.17"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.5.X.X.X.X.X 0 --> IB-PLATFORMONE-MIB::ibNodeQueueToMaster."11.112.133.14"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.5.X.X.X.X.X 0 --> IB-PLATFORMONE-MIB::ibNodeQueueToMaster."11.112.133.17"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.6.X.X.X.X.X 2016/04/13 14:12:59 --> IB-PLATFORMONE-MIB::ibNodeLastRepTimeToMaster."11.112.133.14"
# .1.3.6.1.4.1.7779.3.1.1.2.1.2.1.6.X.X.X.X.X 2016/04/13 14:15:51 --> IB-PLATFORMONE-MIB::ibNodeLastRepTimeToMaster."11.112.133.17"


def inventory_infoblox_replication_status(info):
    return [(line[0], None) for line in info]


def check_infoblox_replication_status(item, _no_params, info):
    for ip_addr, status, queue_from_master, time_from_master, \
        queue_to_master, time_to_master in info:
        if ip_addr == item:
            status_readable = status.lower()
            if status_readable == "online":
                state = 0
            else:
                state = 2

            return state, "Status: %s, Queue from master: %s (%s), Queue to master: %s (%s)" % \
                          (status_readable, queue_from_master, time_from_master,
                           queue_to_master, time_to_master)


check_info['infoblox_replication_status'] = {
    'inventory_function': inventory_infoblox_replication_status,
    'check_function': check_infoblox_replication_status,
    'service_description': 'Replication %s',
    'snmp_info': (
        ".1.3.6.1.4.1.7779.3.1.1.2.1.2.1",
        [
            "1",  # IB-PLATFORMONE-MIB::ibNodeIPAddress
            "2",  # IB-PLATFORMONE-MIB::ibNodeReplicationStatus
            "3",  # IB-PLATFORMONE-MIB::ibNodeQueueFromMaster
            "4",  # IB-PLATFORMONE-MIB::ibNodeLastRepTimeFromMaster
            "5",  # IB-PLATFORMONE-MIB::ibNodeQueueToMaster
            "6",  # IB-PLATFORMONE-MIB::ibNodeLastRepTimeToMaster
        ]),
    'snmp_scan_function': scan_infoblox,
    'includes': ["infoblox.include"],
}
