#!/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  # pylint: disable=C0413
import sys  # pylint: disable=C0413

import lib.args  # pylint: disable=C0413
import lib.base  # pylint: disable=C0413
import lib.human  # pylint: disable=C0413
import lib.nodebb  # pylint: disable=C0413
import lib.lftest  # pylint: disable=C0413
from lib.globals import (STATE_CRIT, STATE_OK,  # pylint: disable=C0413
                          STATE_UNKNOWN, STATE_WARN)

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

DESCRIPTION = """Get NodeBB system cache info."""

DEFAULT_CRIT = 90
DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_SERVERITY = 'warn'
DEFAULT_TIMEOUT = 3
DEFAULT_URL = 'http://localhost:4567/forum'
DEFAULT_WARN = 80


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

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

    parser.add_argument(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '-c', '--critical',
        help='Set the CRIT threshold as a percentage. '
             'Default: >= %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '--insecure',
        help='This option explicitly allows to perform "insecure" SSL connections. '
             'Default: %(default)s',
        dest='INSECURE',
        action='store_true',
        default=DEFAULT_INSECURE,
    )

    parser.add_argument(
        '--no-proxy',
        help='Do not use a proxy. '
             'Default: %(default)s',
        dest='NO_PROXY',
        action='store_true',
        default=DEFAULT_NO_PROXY,
    )

    parser.add_argument(
        '--severity',
        help='Severity for alerts that do not depend on thresholds. One of "warn" or "crit". '
             'Default: %(default)s',
        dest='SEVERITY',
        default=DEFAULT_SERVERITY,
        choices=['warn', 'crit'],
    )

    parser.add_argument(
        '--test',
        help='For unit tests. Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".',
        dest='TEST',
        type=lib.args.csv,
    )

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

    parser.add_argument(
        '-p', '--token',
        help='NodeBB API Bearer token.',
        dest='TOKEN',
        required=True,
    )

    parser.add_argument(
        '--url',
        help='NodeBB API URL. '
             'Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    parser.add_argument(
        '-w', '--warning',
        help='Set the WARN threshold as a percentage. '
             'Default: >= %(default)s',
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

    return parser.parse_args()


def main():
    """The main function. Hier spielt die Musik.
    """

    # parse the command line, exit with UNKNOWN if it fails
    try:
        args = parse_args()
    except SystemExit:
        sys.exit(STATE_UNKNOWN)

    # fetch data
    if args.TEST is None:
        result = lib.nodebb.get_data(args, '/api/admin/advanced/cache')
    else:
        # do not call the command, put in test data
        import json
        stdout, stderr, retc = lib.lftest.test(args.TEST)
        result = json.loads(stdout)

    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = ''
    table_data = []

    # analyze data
    for cache in ['postCache', 'groupCache', 'localCache', 'objectCache']:
        result[cache]['hits'] = result[cache]['hits'].replace(',', '')
        result[cache]['misses'] = result[cache]['misses'].replace(',', '')
        result[cache]['hitRatio'] = round(float(result[cache]['hitRatio']) * 100, 1)

        perfdata += lib.base.get_perfdata('cache_' + cache + '_hitRatio', result[cache]['hitRatio'], '%', None, None, 0, 100)
        perfdata += lib.base.get_perfdata('cache_' + cache + '_hits', result[cache]['hits'], 'c', None, None, 0, None)
        perfdata += lib.base.get_perfdata('cache_' + cache + '_itemCount', result[cache]['itemCount'], None, None, None, 0, None)
        perfdata += lib.base.get_perfdata('cache_' + cache + '_length', result[cache]['length'], None, None, None, 0, result[cache]['max'])
        perfdata += lib.base.get_perfdata('cache_' + cache + '_misses', result[cache]['misses'], 'c', None, None, 0, None)
        perfdata += lib.base.get_perfdata('cache_' + cache + '_percentFull', result[cache]['percentFull'], '%', args.WARN, args.CRIT, 0, 100)

        result[cache]['name'] = cache
        if not result[cache]['enabled']:
            cache_enabled_state = lib.base.str2state(args.SEVERITY)
            state = lib.base.get_worst(state, cache_enabled_state)
            result[cache]['enabled'] = '{}{}'.format(
                str(result[cache]['enabled']),
                lib.base.state2str(cache_enabled_state, prefix=' '),
            )
        cache_usage_state = lib.base.get_state(result[cache]['percentFull'], args.WARN, args.CRIT)
        state = lib.base.get_worst(state, cache_usage_state)
        result[cache]['percentFull'] = '{}%{}'.format(
            result[cache]['percentFull'],
            lib.base.state2str(cache_usage_state, prefix=' '),
        )
        result[cache]['size'] = '{} / {}'.format(
            lib.human.number2human(result[cache]['length']),
            lib.human.number2human(result[cache]['max']),
        )
        result[cache]['hits'] = lib.human.number2human(result[cache]['hits'])
        result[cache]['misses'] = lib.human.number2human(result[cache]['misses'])
        result[cache]['hitRatio'] = '{}%'.format(
            lib.human.number2human(result[cache]['hitRatio']),
        )

        table_data.append(result[cache])

    # build the message
    if table_data:
        keys = [
            'name',
            'enabled',
            'percentFull',
            'size',
            'hits',
            'misses',
            'hitRatio',
        ]
        headers = [
            'Cache',
            'Enabled',
            'Usage',
            'Size',
            'Hits',
            'Misses',
            'HitRatio',
        ]
        msg += lib.base.get_table(table_data, keys, header=headers)

    if state == STATE_CRIT:
        msg = 'There are critical errors.\n\n' + msg
    elif state == STATE_WARN:
        msg = 'There are warnings.\n\n' + msg
    else:
        msg = 'Everything is ok.\n\n' + msg

    # over and out
    lib.base.oao(msg, state, perfdata, always_ok=args.ALWAYS_OK)



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