#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.genua import *  # pylint: disable=wildcard-import,unused-wildcard-import
# .1.3.6.1.4.1.3717.2.1.3.1.1.1 1
# .1.3.6.1.4.1.3717.2.1.3.1.1.2 2
# .1.3.6.1.4.1.3717.2.1.3.1.1.3 3
# .1.3.6.1.4.1.3717.2.1.3.1.1.4 4
# .1.3.6.1.4.1.3717.2.1.3.1.2.1 gc2.momatec.de
# .1.3.6.1.4.1.3717.2.1.3.1.2.2 gc1-bsge.vrznrw.de
# .1.3.6.1.4.1.3717.2.1.3.1.2.3 gc1-bochum.vrznrw.de
# .1.3.6.1.4.1.3717.2.1.3.1.2.4 gc1-hamm.vrznrw.de
# .1.3.6.1.4.1.3717.2.1.3.1.3.1
# .1.3.6.1.4.1.3717.2.1.3.1.3.2 10.99.15.250
# .1.3.6.1.4.1.3717.2.1.3.1.3.3 10.99.13.250
# .1.3.6.1.4.1.3717.2.1.3.1.3.4 10.99.14.250
# .1.3.6.1.4.1.3717.2.1.3.1.4.1 172.30.230.24/32
# .1.3.6.1.4.1.3717.2.1.3.1.4.2 172.30.230.24/32
# .1.3.6.1.4.1.3717.2.1.3.1.4.3 172.30.230.24/32
# .1.3.6.1.4.1.3717.2.1.3.1.4.4 172.30.230.24/32
# .1.3.6.1.4.1.3717.2.1.3.1.5.1 192.168.100.0/24
# .1.3.6.1.4.1.3717.2.1.3.1.5.2 10.100.15.0/24
# .1.3.6.1.4.1.3717.2.1.3.1.5.3 10.100.13.0/24
# .1.3.6.1.4.1.3717.2.1.3.1.5.4 10.100.14.0/24
# .1.3.6.1.4.1.3717.2.1.3.1.6.1 2
# .1.3.6.1.4.1.3717.2.1.3.1.6.2 2
# .1.3.6.1.4.1.3717.2.1.3.1.6.3 2
# .1.3.6.1.4.1.3717.2.1.3.1.6.4 2


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


def check_genua_vpn(item, params, info):
    for vpn_id, hostname_opposite, ip_opposite, vpn_private, vpn_remote, vpn_state in info:
        if vpn_id == item:
            ip_info = ""
            if ip_opposite:
                ip_info += " (%s)" % ip_opposite

            infotext = "Hostname: %s%s, VPN private: %s, VPN remote: %s" % \
                       (hostname_opposite, ip_info, vpn_private, vpn_remote)

            if vpn_state == '2':
                return 0, "Connected, %s" % infotext
            return 2, "Disconnected, %s" % infotext


check_info['genua_vpn'] = {
    'inventory_function': inventory_genua_vpn,
    'check_function': check_genua_vpn,
    'service_description': 'VPN %s',
    'snmp_info': (
        ".1.3.6.1.4.1.3717.2.1.3.1",
        [
            "1",  # vpn id
            "2",  # hostname opposite
            "3",  # ip opposite
            "4",  # vpn private
            "5",  # vpn remote
            "6",  # vpn status (2:OK, 1:FAULT)
        ]),
    "snmp_scan_function": scan_genua,
}
