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


# example output


def parse_cisco_asa_conn(info):
    parsed = {}
    for line in info[0]:
        parsed[line[0]] = [line[1]]
    for line in info[2]:
        parsed[line[0]].append(line[1])
        parsed[line[0]].append(line[2])
    for line in info[1]:
        parsed[line[0]].append(line[1])

    # Example:
    # parsed = { u'4': [u'outside-adsl', u'1', u'1', u'8.8.8.8'] }
    return parsed

def inventory_cisco_asa_conn(parsed):
    for key, values in parsed.iteritems():
        if values[1] == "1" and len(values) == 4:
            yield ( key, None )


def check_cisco_asa_conn(item, _no_params, parsed):
    translate_status = {
        "1" : (0, "up"),
        "2" : (2, "down"),
        "3" : (3, "testing"),
        "4" : (3, "unknown"),
        "5" : (2, "dormant"),
        "6" : (2, "not present"),
        "7" : (2, "lower layer down"),
    }

    for key, values in parsed.iteritems():
        if item == key:
            yield 0, "Name: %s" % values[0]

            if not values[3] == "":
                yield 0, "IP: %s" % values[3]
            else: # CRIT if no IP is assigned
                yield 2, "IP: Not found!"

            state, state_readable = translate_status[values[2]]
            yield state, "Status: %s" % state_readable


check_info['cisco_asa_conn'] = {
    'parse_function'        : parse_cisco_asa_conn,
    'inventory_function'    : inventory_cisco_asa_conn,
    'check_function'        : check_cisco_asa_conn,
    'service_description'   : 'Connection %s',
    'snmp_info'             : [('.1.3.6.1.2.1.31.1.1.1', [
                                    OID_END, # Index
                                    "1",     # IF-MIB::ifName
                               ]),
                               ('.1.3.6.1.2.1.4.20.1', [
                                    "2",     # IP-MIB::ipAdEntIfIndex
                                    "1",     # IP-MIB::ipAdEntAddr
                               ]),
                               ('.1.3.6.1.2.1.2.2.1', [
                                    OID_END, # Index
                                    "7",     # IF-MIB::ifAdminStatus
                                    "8",     # IF-MIB::ifOperStatus
                               ])],
    'snmp_scan_function'    : lambda oid: oid(".1.3.6.1.2.1.1.1.0").lower().startswith("cisco adaptive security") \
                              or "cisco pix security" in oid(".1.3.6.1.2.1.1.1.0").lower(),
}
