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


def inventory_fortigate_sensors(info):
    return [(None, None)]


def check_fortigate_sensors(item, params, info):
    amount_of_sensors = []
    sensors_in_alarm = []

    for name, value, dev_status in info:
        # We assume that sensors with value "0" are not connected.
        # The related MIB includes no other hint for that.
        if value != "0":
            amount_of_sensors.append(name)
            if dev_status == "1":
                sensors_in_alarm.append(name)

    infotexts = ["%s sensors" % len(amount_of_sensors)]
    count_ok_sensors = len(amount_of_sensors) - len(sensors_in_alarm)
    if count_ok_sensors:
        infotexts.append("%s OK" % count_ok_sensors)

    state = 0
    if sensors_in_alarm:
        infotexts.append("%s with alarm: %s" % \
                         (len(sensors_in_alarm),
                          ", ".join(sensors_in_alarm)))
        state = 2

    return state, " - ".join(infotexts)


check_info['fortigate_sensors'] = {
    'inventory_function': inventory_fortigate_sensors,
    'check_function': check_fortigate_sensors,
    'service_description': 'Sensor Summary',
    'snmp_info': (
        '.1.3.6.1.4.1.12356.101.4.3.2.1',
        [
            '2',  # FORTINET-FORTIGATE-MIB::fgHwSensorEntName
            '3',  # FORTINET-FORTIGATE-MIB::fgHwSensorEntValue
            '4',  # FORTINET-FORTIGATE-MIB::fgHwSensorEntAlarmStatus
        ]),
    'snmp_scan_function': lambda oid: oid(".1.3.6.1.2.1.1.2.0") in [
        '.1.3.6.1.4.1.12356.101.1.5004',
        '.1.3.6.1.4.1.12356.101.1.10004',
    ],
}
