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


# .1.3.6.1.4.1.12356.101.4.2.1.0 27.00768(2015-09-01 15:10)
# .1.3.6.1.4.1.12356.101.4.2.2.0 6.00689(2015-09-01 00:15)


# signature ages (defaults are 1/2 days)
factory_settings['fortigate_signature_default_levels'] = {
    'av_age': (86400, 172800),
    'ips_age': (86400, 172800)
}


def inventory_fortigate_signatures(info):
    if info:
        return [(None, {})]
    else:
        return []

def check_fortigate_signatures(_no_item, params, info):
    def parse_version(ver):
        # sample: 27.00768(2015-09-01 15:10)
        ver_regex = regex("([0-9.]*)\(([0-9-: ]*)\)")
        match = ver_regex.match(ver)
        if match is None:
            return None, None
        # what timezone is this in?
        t = time.strptime(match.group(2), "%Y-%m-%d %H:%S")
        ts = time.mktime(t)
        return match.group(1), time.time() - ts

    def age_status(age, levels):
        if age >= levels[1]:
            return 2
        elif age >= levels[0]:
            return 1
        else:
            return 0

    def output_status(typ, signature_info, levels):
        version, age = parse_version(signature_info)
        status = age_status(age, levels)

        if status != 0:
            return status, "%s Signature %s is %s old (warn/crit at %s/%s)" %\
                (typ, version,
                 get_age_human_readable(age),
                 get_age_human_readable(levels[0]),
                 get_age_human_readable(levels[1]))
        else:
            return 0, "%s Signature %s is current" % (typ, version)

    if info:
        yield output_status("AV", info[0][0], params['av_age'])
        yield output_status("IPS", info[0][1], params['ips_age'])


check_info['fortigate_signatures'] = {
    'inventory_function'      : inventory_fortigate_signatures,
    'check_function'          : check_fortigate_signatures,
    'service_description'     : "Signatures",
    'snmp_scan_function'      : lambda oid: ".1.3.6.1.4.1.12356.101.1" in oid(".1.3.6.1.2.1.1.2.0"),
    'snmp_info'               : (".1.3.6.1.4.1.12356.101.4.2", [1, 2]),
    'default_levels_variable' : "fortigate_signature_default_levels",
    'group'                   : 'fortinet_signatures'
}

