#!/usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author:  Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
#          https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.

# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md

"""See the check's README for more details."""

import argparse
import sys
import urllib.parse

import lib.args
import lib.base
import lib.txt
import lib.url
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026040801'

DESCRIPTION = """Checks hardware sensor readings (temperature, voltage, fan speed) on FortiGate
appliances running FortiOS via the REST API. Alerts when any sensor reports an
alarm condition."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 3


def parse_args():
    """Parse command line arguments using argparse."""
    parser = argparse.ArgumentParser(description=DESCRIPTION)

    parser.add_argument(
        '-V',
        '--version',
        action='version',
        version=f'%(prog)s: v{__version__} by {__author__}',
    )

    parser.add_argument(
        '--always-ok',
        help=lib.args.help('--always-ok'),
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '-H',
        '--hostname',
        help='FortiOS-based appliance address, optionally including port. '
        'Example: `--hostname 192.168.1.1:443`.',
        dest='HOSTNAME',
        required=True,
    )

    parser.add_argument(
        '--insecure',
        help=lib.args.help('--insecure'),
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help=lib.args.help('--no-proxy'),
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--password',
        help='FortiOS REST API single-use access token.',
        dest='PASSWORD',
        required=True,
    )

    parser.add_argument(
        '--timeout',
        help=lib.args.help('--timeout') + ' Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    args, _ = parser.parse_known_args()
    return args


def shorten_uom(uom):
    if uom == 'temperature':
        return 'C'
    if uom == 'power':
        return 'W'
    if uom == 'voltage':
        return 'V'
    if uom == 'fan':
        return 'RPM'
    return uom


def main():
    """The main function. This is where the magic happens."""

    # parse the command line
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # fetch data
    # Resource to get sensor data from
    url = f'https://{args.HOSTNAME}/api/v2/monitor/system/sensor-info/select?access_token={urllib.parse.quote(args.PASSWORD)}'
    result = lib.base.coe(
        lib.url.fetch_json(
            url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT
        )
    )

    msg_header, msg_body = '', ''
    perfdata = ''
    state = STATE_OK
    sensor_counter = 0

    for sensor in result['results']:
        # {
        #   "id":"voltage.mac_avs_1v",
        #   "name":"MAC_AVS 1V",
        #   "type":"voltage", temperature, fan, power
        #   "value":0.990000,
        #   "alarm":false,
        #   "thresholds":{
        #     "lower_non_recoverable":0.872400,
        #     "lower_critical":0.892000,
        #     "lower_non_critical":0.921400,
        #     "upper_non_critical":1.078200,
        #     "upper_critical":1.107600,
        #     "upper_non_recoverable":1.127200
        #   }
        if sensor['value'] == 0.0:
            # 0.0 means "no value"
            continue

        sensor_state = STATE_OK
        if 'lower_non_critical' in sensor['thresholds']:
            sensor_state = lib.base.get_state(
                sensor['value'],
                sensor['thresholds']['lower_non_critical'],
                sensor['thresholds']['lower_critical'],
                _operator='le',
            )
            state = lib.base.get_worst(sensor_state, state)
            if sensor_state in (STATE_WARN, STATE_CRIT):
                msg_body += f'\n* {sensor["name"]} ({sensor["value"]} {shorten_uom(sensor["type"])}) is less or equal to a certain threshold ({sensor["thresholds"]["lower_non_critical"]}/{sensor["thresholds"]["lower_critical"]})'
        if 'upper_non_critical' in sensor['thresholds']:
            sensor_state = lib.base.get_state(
                sensor['value'],
                sensor['thresholds']['upper_non_critical'],
                sensor['thresholds']['upper_critical'],
                _operator='ge',
            )
            state = lib.base.get_worst(sensor_state, state)
            if sensor_state in (STATE_WARN, STATE_CRIT):
                msg_body += f'\n* {sensor["name"]} ({sensor["value"]} {shorten_uom(sensor["type"])}) is greater or equal to a certain threshold ({sensor["thresholds"]["upper_non_critical"]}/{sensor["thresholds"]["upper_critical"]})'

        perfdata += lib.base.get_perfdata(
            sensor['id'],
            sensor['value'],
            warn=sensor['thresholds']['upper_non_critical'],
            crit=sensor['thresholds']['upper_critical'],
            _min=sensor['thresholds'].get('lower_non_recoverable', 0),
            _max=sensor['thresholds']['upper_non_recoverable'],
        )
        sensor_counter += 1

    if state == STATE_CRIT:
        msg_header = f'Checked {sensor_counter} {lib.txt.pluralize("sensor", sensor_counter)}. There are critical errors.'
    elif state == STATE_WARN:
        msg_header = f'Checked {sensor_counter} {lib.txt.pluralize("sensor", sensor_counter)}. There are warnings.'
    else:
        msg_header = f'Checked {sensor_counter} {lib.txt.pluralize("sensor", sensor_counter)}, all are ok.'

    # over and out
    lib.base.oao(msg_header + msg_body, state, perfdata)


if __name__ == '__main__':
    try:
        main()
    except Exception:
        lib.base.cu()
