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

route_type_mapping = {
    'DefaultRoute'  : 'default route',
    'PBR'           : 'policy based routing',
    ''              : 'not defined',
    }

route_state_mapping = {
    'UP'    : ( 0, 'Route is up' ),
    'DOWN'  : ( 2, 'Route is down' ),
    'UNDEF' : ( 3, 'Route is undefined' ),
    }

def inventory_stormshield_route(info):
    for line in info:
        if line[5] == 'UP':
            yield ( line[0], None )


def check_stormshield_route(item, params, info):
    for line in info:
        if line[0] == item:
            index, typ, name, gateway_name, gateway_type, state = line
            yield route_state_mapping[state]
            infotext = 'Type: %s, Router name: %s, Gateway name: %s, Gateway type: %s' \
                    % ( route_type_mapping[typ], name, gateway_name, gateway_type )
            yield 0, infotext


check_info['stormshield_route'] = {
    'inventory_function'        : inventory_stormshield_route,
    'check_function'            : check_stormshield_route,
    'service_description'       : 'Gateway %s',
    'snmp_info'                 : ('.1.3.6.1.4.1.11256.1.14.1.1', [
                                      '1',  # snsRouteIndex
                                      '2',  # snsRouteType
                                      '4',  # snsRouteRouterName
                                      '5',  # snsRouteGatewayName
                                      '7',  # snsRouteGatewayType
                                      '9',  # snsRouteState
                                  ]),
    'snmp_scan_function'        : stormshield_scan_function,
    'includes'                  : [ 'stormshield.include' ],
}
