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

def parse_safenet_ntls(info):
    return {
        "operation_status"          : info[0][0],
        "connected_clients"         : int(info[0][1]),
        "links"                     : int(info[0][2]),
        "successful_connections"    : int(info[0][3]),
        "failed_connections"        : int(info[0][4]),
        "expiration_date"           : info[0][5],
    }


#.
#   .--Connection rate-----------------------------------------------------.
#   |          ____                            _   _                       |
#   |         / ___|___  _ __  _ __   ___  ___| |_(_) ___  _ __            |
#   |        | |   / _ \| '_ \| '_ \ / _ \/ __| __| |/ _ \| '_ \           |
#   |        | |__| (_) | | | | | | |  __/ (__| |_| | (_) | | | |          |
#   |         \____\___/|_| |_|_| |_|\___|\___|\__|_|\___/|_| |_|          |
#   |                                                                      |
#   |                                    _                                 |
#   |                          _ __ __ _| |_ ___                           |
#   |                         | '__/ _` | __/ _ \                          |
#   |                         | | | (_| | ||  __/                          |
#   |                         |_|  \__,_|\__\___|                          |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_safenet_ntls_connrate(parsed):
    if parsed:
        yield "successful", None
        yield "failed", None


def check_safenet_ntls_connrate(item, _no_params, parsed):
    now = time.time()
    connections_rate = get_rate(item, now, parsed[item + "_connections"])
    perfdata = [ ("connections_rate", connections_rate) ]
    return 0, "%.2f connections/s" % connections_rate, perfdata


check_info["safenet_ntls.connrate"] = {
    "inventory_function"    : inventory_safenet_ntls_connrate,
    "check_function"        : check_safenet_ntls_connrate,
    "service_description"   : "NTLS Connection Rate: %s",
    "has_perfdata"          : True,
}


#.
#   .--Expiration date-----------------------------------------------------.
#   |           _____            _           _   _                         |
#   |          | ____|_  ___ __ (_)_ __ __ _| |_(_) ___  _ __              |
#   |          |  _| \ \/ / '_ \| | '__/ _` | __| |/ _ \| '_ \             |
#   |          | |___ >  <| |_) | | | | (_| | |_| | (_) | | | |            |
#   |          |_____/_/\_\ .__/|_|_|  \__,_|\__|_|\___/|_| |_|            |
#   |                     |_|                                              |
#   |                             _       _                                |
#   |                          __| | __ _| |_ ___                          |
#   |                         / _` |/ _` | __/ _ \                         |
#   |                        | (_| | (_| | ||  __/                         |
#   |                         \__,_|\__,_|\__\___|                         |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_safenet_ntls_expiration(parsed):
    if parsed:
        return [ (None, None) ]


def check_safenet_ntls_expiration(_no_item, _no_params, parsed):
    return 0, "The NTLS server certificate expires on " + parsed["expiration_date"]


check_info["safenet_ntls.expiration"] = {
    "inventory_function"    : inventory_safenet_ntls_expiration,
    "check_function"        : check_safenet_ntls_expiration,
    "service_description"   : "NTLS Expiration Date",
}

#.
#   .--Links---------------------------------------------------------------.
#   |                       _     _       _                                |
#   |                      | |   (_)_ __ | | _____                         |
#   |                      | |   | | '_ \| |/ / __|                        |
#   |                      | |___| | | | |   <\__ \                        |
#   |                      |_____|_|_| |_|_|\_\___/                        |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_safenet_ntls_links(parsed):
    if parsed:
        return [ (None, None) ]


def check_safenet_ntls_links(_no_item, params, parsed):
    connections = parsed["links"]
    perfdata = [ ("connections", connections) ]
    infotext = "%d links" % connections
    status, extrainfo, extraperf = check_levels(connections, "connections", params)
    return status, infotext + extrainfo, perfdata + extraperf


check_info["safenet_ntls.links"] = {
    "inventory_function"    : inventory_safenet_ntls_links,
    "check_function"        : check_safenet_ntls_links,
    "service_description"   : "NTLS Links",
    "has_perfdata"          : True,
    "group"                 : "safenet_ntls_links",
}


#.
#   .--Connected clients---------------------------------------------------.
#   |            ____                            _           _             |
#   |           / ___|___  _ __  _ __   ___  ___| |_ ___  __| |            |
#   |          | |   / _ \| '_ \| '_ \ / _ \/ __| __/ _ \/ _` |            |
#   |          | |__| (_) | | | | | | |  __/ (__| ||  __/ (_| |            |
#   |           \____\___/|_| |_|_| |_|\___|\___|\__\___|\__,_|            |
#   |                                                                      |
#   |                          _ _            _                            |
#   |                      ___| (_) ___ _ __ | |_ ___                      |
#   |                     / __| | |/ _ \ '_ \| __/ __|                     |
#   |                    | (__| | |  __/ | | | |_\__ \                     |
#   |                     \___|_|_|\___|_| |_|\__|___/                     |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_safenet_ntls_clients(parsed):
    if parsed:
        return [ (None, None) ]


def check_safenet_ntls_clients(_no_item, params, parsed):
    connections = parsed["connected_clients"]
    perfdata = [ ("connections", connections) ]
    infotext = "%d connected clients" % connections
    status, extrainfo, extraperf = check_levels(connections, "connections", params)
    return status, infotext + extrainfo, perfdata + extraperf


check_info["safenet_ntls.clients"] = {
    "inventory_function"    : inventory_safenet_ntls_clients,
    "check_function"        : check_safenet_ntls_clients,
    "service_description"   : "NTLS Clients",
    "has_perfdata"          : True,
    "group"                 : "safenet_ntls_clients",
}

#.
#   .--Operation status----------------------------------------------------.
#   |             ___                       _   _                          |
#   |            / _ \ _ __   ___ _ __ __ _| |_(_) ___  _ __               |
#   |           | | | | '_ \ / _ \ '__/ _` | __| |/ _ \| '_ \              |
#   |           | |_| | |_) |  __/ | | (_| | |_| | (_) | | | |             |
#   |            \___/| .__/ \___|_|  \__,_|\__|_|\___/|_| |_|             |
#   |                 |_|                                                  |
#   |                         _        _                                   |
#   |                     ___| |_ __ _| |_ _   _ ___                       |
#   |                    / __| __/ _` | __| | | / __|                      |
#   |                    \__ \ || (_| | |_| |_| \__ \                      |
#   |                    |___/\__\__,_|\__|\__,_|___/                      |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def inventory_safenet_ntls(parsed):
    if parsed:
        return [ (None, None) ]


def check_safenet_ntls(_no_item, _no_params, parsed):
    operation_status = parsed["operation_status"]
    if operation_status == "1":
        return 0, "Running"
    elif operation_status == "2":
        return 2, "Down"
    elif operation_status == "3":
        return 3, "Unknown"


check_info["safenet_ntls"] = {
    "parse_function"            : parse_safenet_ntls,
    "inventory_function"        : inventory_safenet_ntls,
    "check_function"            : check_safenet_ntls,
    "service_description"       : "NTLS Operation Status",
    "snmp_scan_function"        : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.12383"),
    "snmp_info"                 :  (".1.3.6.1.4.1.12383.3.1.2", [
                                                            "1", # ntlsOperStatus
                                                            "2", # ntlsConnectedClients
                                                            "3", # ntlsLinks
                                                            "4", # ntlsSuccessfulClientConnections
                                                            "5", # ntlsFailedClientConnections
                                                            "6", # ntlsCertExpireDay
                                                        ]),
}
