#!/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 SNMP output:
#
# .1.3.6.1.4.1.5951.4.1.1.56.1.1.1.20.67.79.77.79.68.79.95.82.83.65.95.67.101.114.116.95.65.117.116.104 COMODO_RSA_Cert_Auth
# .1.3.6.1.4.1.5951.4.1.1.56.1.1.1.21.110.115.45.115.101.114.118.101.114.45.99.101.114.116.105.102.105.99.97.116.101 ns-server-certificate
# .1.3.6.1.4.1.5951.4.1.1.56.1.1.1.25.67.79.77.79.68.79.95.82.83.65.95.67.101.114.116.95.65.117.116.104.95.82.111.111.116 COMODO_RSA_Cert_Auth_Root
# .1.3.6.1.4.1.5951.4.1.1.56.1.1.5.20.67.79.77.79.68.79.95.82.83.65.95.67.101.114.116.95.65.117.116.104 4286
# .1.3.6.1.4.1.5951.4.1.1.56.1.1.5.21.110.115.45.115.101.114.118.101.114.45.99.101.114.116.105.102.105.99.97.116.101 3655
# .1.3.6.1.4.1.5951.4.1.1.56.1.1.5.25.67.79.77.79.68.79.95.82.83.65.95.67.101.114.116.95.65.117.116.104.95.82.111.111.116 1106


factory_settings["netscaler_sslcerts_default_levels"] = {
    "age_levels" : (30, 10),
}


def inventory_netscaler_sslcerts(info):
    for node, certname, daysleft in info:
        if certname:
            yield certname, {}


def check_netscaler_sslcerts(item, params, info):
    for node, certname, daysleft in info:
        if certname == item:
            state, daysleft = 0, int(daysleft)
            warn, crit   = params["age_levels"]

            if daysleft <= crit:
                state = 2
            elif daysleft <= warn:
                state = 1

            infotext = "Certificate valid for %d days" % daysleft

            if node:
                infotext = ": ".join([node, infotext])

            if state > 0:
                infotext += " (warn/crit below %s/%s)" % (warn, crit)

            yield state, infotext, [("daysleft", daysleft, warn, crit)]


check_info["netscaler_sslcertificates"] = {
    "check_function"          : check_netscaler_sslcerts,
    "inventory_function"      : inventory_netscaler_sslcerts,
    "service_description"     : "SSL Certificate %s",
    "node_info"               : True,
    "snmp_info"               : (".1.3.6.1.4.1.5951.4.1.1.56.1.1", [  
                                            1, # sslCertKeyName
                                            5, # sslDaysToExpire
                                        ]),
    "has_perfdata"            : True,
    "snmp_scan_function"      : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.5951.1"),
    "group"                   : "netscaler_sslcerts",
    "default_levels_variable" : "netscaler_sslcerts_default_levels",
}
