#!/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 lib.args
import lib.base
import lib.qts
import lib.txt
import lib.url
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN

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


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

DESCRIPTION = """Checks disk SMART values on QNAP appliances running QTS via the API. Reports drive
health, temperature, and SMART attribute status.
Alerts when any disk reports a non-normal SMART status."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 6
DEFAULT_USERNAME = 'admin'


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(
        '--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='QTS API password.',
        dest='PASSWORD',
        required=True,
    )

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

    parser.add_argument(
        '--url',
        help='QTS-based appliance URL. Example: `--url=https://192.168.1.1:8080`.',
        dest='URL',
        required=True,
    )

    parser.add_argument(
        '--username',
        help='QTS API username. Default: %(default)s',
        dest='USERNAME',
        default=DEFAULT_USERNAME,
    )

    args, _ = parser.parse_known_args()
    return args


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)

    auth_sid = lib.base.coe(lib.qts.get_auth_sid(args))

    # get all HDD/SSD data
    url = f'{args.URL}/cgi-bin/disk/qsmart.cgi?func=all_hd_data&sid={auth_sid}'
    result = lib.base.coe(
        lib.url.fetch(
            url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT
        )
    )
    data = xmltodict.parse(result)['QDocRoot']
    if data['authPassed'] == '0':
        lib.base.cu('Insufficient permissions.')

    # get system information
    url = (
        f'{args.URL}/cgi-bin/management/'
        f'manaRequest.cgi?subfunc=sysinfo'
        f'&hd=no&multicpu=1&sid={auth_sid}'
    )
    result = lib.base.coe(
        lib.url.fetch(
            url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT
        )
    )
    sysinfo = xmltodict.parse(result)['QDocRoot']
    if sysinfo['authPassed'] == '0':
        lib.base.cu('Insufficient permissions.')

    # init some vars
    disk_count = 0
    state = STATE_OK
    msg_body = ''
    perfdata = ''

    # analyze data
    for disk in data['Disk_Info']['entry']:
        if disk['Serial']:  # skip empty entries
            disk_count += 1
            disk_state = STATE_OK

            disk_temperature = disk['Temperature'].get('oC', 0)
            if disk_temperature is None:
                disk_temperature = 0
            if disk['hd_is_ssd'] == '1':
                warn = int(sysinfo['func']['ownContent']['root']['SSDTempWarnT'])
                crit = int(sysinfo['func']['ownContent']['root']['SSDTempErrT'])
            else:
                warn = int(sysinfo['func']['ownContent']['root']['HDTempWarnT'])
                crit = int(sysinfo['func']['ownContent']['root']['HDTempErrT'])

            alias = disk.get('Disk_Alias', 'Unknown Alias').replace(' ', '_')
            model = disk['Model'].replace(' ', '_')
            serial = disk['Serial'].replace(' ', '_')
            perfdata += lib.base.get_perfdata(
                f'{alias}_{model}_{serial}_temperature',
                disk_temperature,
                warn=warn,
                crit=crit,
                _min=0,
            )

            disk_state = lib.base.get_state(
                disk_temperature, warn, crit, _operator='ge'
            )
            if disk['Health'] != 'OK':
                disk_state = STATE_WARN

            state = lib.base.get_worst(disk_state, state)
            msg_body += (
                f'* {disk.get("Disk_Alias", "Unknown Alias")}'
                f' ({disk["Model"]}'
                f', SerNo {disk["Serial"]}'
                f', Temp {disk_temperature}°C'
                f' (Thresholds: {warn}/{crit}°C)'
                f'{lib.base.state2str(disk_state, prefix="", suffix=" ")}\n'
            )

    # build the message
    if disk_count:
        msg_header = f'Checked {disk_count} {lib.txt.pluralize("disk", disk_count)}.'
        if state == STATE_CRIT:
            msg_header += ' There are critical errors.'
        elif state == STATE_WARN:
            msg_header += ' There are warnings.'
        else:
            msg_header += ' All are healthy.'
        # over and out
        lib.base.oao(
            f'{msg_header}\n{msg_body}',
            state,
            perfdata,
            always_ok=args.ALWAYS_OK,
        )
    else:
        # over and out
        lib.base.cu('Did not find any disk.')


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