#!/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 json
import sys

import lib.args
import lib.base
import lib.lftest
from lib.globals import STATE_OK, STATE_UNKNOWN

try:
    import psutil
except ImportError:
    print('Python module "psutil" is not installed.')
    sys.exit(STATE_UNKNOWN)

# psutil 5.x exposes sfan under psutil._common, psutil 6+ under
# psutil._ntuples. Fall back to a plain namedtuple if neither is
# available.
try:
    from psutil._ntuples import sfan
except ImportError:
    try:
        from psutil._common import sfan
    except ImportError:
        from collections import namedtuple
        sfan = namedtuple('sfan', ['label', 'current'])


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

DESCRIPTION = """Reports hardware fan speeds in RPM (rounds per minute). Alerts when fan speeds fall
outside the thresholds reported by the hardware sensors."""

DEFAULT_WARN = 10000  # RPM
DEFAULT_CRIT = 20000  # RPM


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(
        '-c',
        '--critical',
        help='CRIT threshold for fan speed, in RPM. Default: %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '--test',
        help=lib.args.help('--test'),
        dest='TEST',
        type=lib.args.csv,
    )

    parser.add_argument(
        '-w',
        '--warning',
        help='WARN threshold for fan speed, in RPM. Default: %(default)s',
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

    args, _ = parser.parse_known_args()
    return args


def _load_fans_fixture(raw_json):
    """Convert a test fixture into the shape that `psutil.sensors_fans()`
    returns: a dict mapping the sensor group name to a list of
    `sfan(label, current)` namedtuples.
    """
    data = json.loads(raw_json)
    return {
        name: [sfan(entry.get('label', ''), entry['current']) for entry in entries]
        for name, entries in data.items()
    }


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)

    if args.TEST is None and not hasattr(psutil, 'sensors_fans'):
        lib.base.cu('Platform not supported.')

    # fetch data
    if args.TEST is None:
        fans = psutil.sensors_fans()
    else:
        stdout, _, _ = lib.lftest.test(args.TEST)
        fans = _load_fans_fixture(stdout)
    if not fans:
        lib.base.oao('No fans detected.', STATE_OK, always_ok=args.ALWAYS_OK)


    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = ''

    for name, entries in fans.items():

        # build the message
        msg += f'{name}: '
        for entry in entries:
            perfdata += lib.base.get_perfdata(
                f'{name}_{entry.label}'.replace(' ', '_').lower(),
                entry.current,
                warn=args.WARN,
                crit=args.CRIT,
                _min=0,
            )
            sensor_state = lib.base.get_state(entry.current, args.WARN, args.CRIT, 'ge')
            msg += f'{entry.label or name} = {entry.current} RPM{lib.base.state2str(sensor_state, prefix=" (", suffix=")")}, '
            state = lib.base.get_worst(state, sensor_state)
        msg = msg[:-2] + '; '

    # over and out
    lib.base.oao(msg[:-2], state, perfdata, always_ok=args.ALWAYS_OK)


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