#!/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.human
import lib.txt
import lib.wildfly
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN

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

DESCRIPTION = """Checks Java memory pool usage (Eden, Survivor, Old Gen, Metaspace, etc.) on a
WildFly/JBoss AS server via its HTTP management API. Alerts when any pool exceeds the
configured usage threshold."""

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 3
DEFAULT_URL = 'http://localhost:9990'
DEFAULT_USERNAME = 'wildfly-monitoring'


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(
        '--instance',
        help='WildFly instance (server-config) to check when running in domain mode.',
        dest='INSTANCE',
    )

    parser.add_argument(
        '--mode',
        help='WildFly server mode. Default: %(default)s',
        dest='MODE',
        choices=['standalone', 'domain'],
        default='standalone',
    )

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

    parser.add_argument(
        '--node',
        help='WildFly node (host) when running in domain mode.',
        dest='NODE',
    )

    parser.add_argument(
        '-p',
        '--password',
        help='WildFly management 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='WildFly management API URL. Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

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

    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)

    # fetch data
    # https://docs.wildfly.org/23/Admin_Guide.html
    data = {
        'operation': 'read-resource',
        'include-runtime': 'true',
        'recursive': 'true',
        # /core-service/platform-mbean/type/memory-pool
        'address': [{'core-service': 'platform-mbean'}, {'type': 'memory-pool'}],
        'json': 1,
    }
    result = lib.wildfly.get_data(args, data)

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

    # analyze data
    # "Peak" values are not of interest in a monitoring scenario where values are
    # checked every minute. It is safe to ignore them.
    for name, value in result['name'].items():
        if not value['valid']:
            state = STATE_WARN
            value['name'] += f' (invalid {lib.base.state2str(STATE_WARN)})'

        value['usage-calc'] = (
            f'{lib.human.bytes2human(value["usage"]["used"])}'
            f' / '
            f'{lib.human.bytes2human(value["usage"]["committed"])}'
            f' / '
            f'{lib.human.bytes2human(value["usage"]["max"])}'
        )
        if value['usage-threshold-exceeded']:
            state = STATE_WARN
            value['usage-calc'] += lib.base.state2str(STATE_WARN, prefix=' ')
            value['usage-calc'] += f' ({value["usage-threshold-count"]}x)'

        perfdata += lib.base.get_perfdata(
            f'memory-pool-{name}-usage-init',
            value['usage']['init'],
            uom='B',
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            f'memory-pool-{name}-usage-used',
            value['usage']['used'],
            uom='B',
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            f'memory-pool-{name}-usage-committed',
            value['usage']['committed'],
            uom='B',
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            f'memory-pool-{name}-usage-max',
            value['usage']['max'],
            uom='B',
            _min=0,
        )

        if value['collection-usage']:
            value['collection-usage-calc'] = (
                f'{lib.human.bytes2human(value["collection-usage"]["used"])}'
                f' / '
                f'{lib.human.bytes2human(value["collection-usage"]["committed"])}'
                f' / '
                f'{lib.human.bytes2human(value["collection-usage"]["max"])}'
            )
            if value['collection-usage-threshold-exceeded']:
                state = STATE_WARN
                value['collection-usage-calc'] += lib.base.state2str(
                    STATE_WARN, prefix=' '
                )
                value['collection-usage-calc'] += (
                    f' ({value["collection-usage-threshold-count"]}x)'
                )
            perfdata += lib.base.get_perfdata(
                f'memory-pool-{name}-collection-usage-init',
                value['collection-usage']['init'],
                uom='B',
                _min=0,
            )
            perfdata += lib.base.get_perfdata(
                f'memory-pool-{name}-collection-usage-used',
                value['collection-usage']['used'],
                uom='B',
                _min=0,
            )
            perfdata += lib.base.get_perfdata(
                f'memory-pool-{name}-collection-usage-committed',
                value['collection-usage']['committed'],
                uom='B',
                _min=0,
            )
            perfdata += lib.base.get_perfdata(
                f'memory-pool-{name}-collection-usage-max',
                value['collection-usage']['max'],
                uom='B',
                _min=0,
            )
        else:
            value['collection-usage-calc'] = 'N/A'

        table_data.append(value)

    if table_data:
        keys = [
            'name',
            'type',
            'usage-calc',
            'collection-usage-calc',
        ]
        headers = [
            'name',
            'Type',
            'Usage used / committed / max',
            'Collection used / committed/ max',
        ]

        # build the message
        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:
        lib.base.cu()
