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

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

DESCRIPTION = """Monitors the high-availability cluster status on FortiGate appliances running FortiOS
via the REST API. Alerts if the number of HA members differs from the expected count
(default: 2). Reports serial number, role, priority, hostname, and synchronization
status per member."""

DEFAULT_COUNT = 2
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(
        '--count',
        help='Expected number of HA cluster members. '
        'Alerts if the actual count differs. '
        'Default: %(default)s',
        dest='COUNT',
        type=int,
        default=DEFAULT_COUNT,
    )

    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 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
    url = f'https://{args.HOSTNAME}/api/v2/monitor/system/ha-statistics/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
        )
    )
    member_count = len(result['results'])

    # we expect for example 2 members in our ha cluster
    state = STATE_WARN if member_count != args.COUNT else STATE_OK

    total_sessions = sum(item['sessions'] for item in result['results'])
    total_traffic = lib.human.bytes2human(
        sum(item['tbyte'] for item in result['results'])
    )
    msg = (
        f'Found {member_count} HA cluster'
        f' {lib.txt.pluralize("member", member_count)}'
        f'{lib.base.state2str(state, prefix=" (", suffix=")")}'
        f', which handled {total_sessions} sessions'
        f' and {total_traffic} traffic so far.'
    )

    perfdata = ''
    for sensor in result['results']:
        name = sensor['hostname']
        perfdata += lib.base.get_perfdata(
            f'{name}_sessions',
            sensor['sessions'],
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            f'{name}_net_usage',
            sensor['net_usage'],
            uom='%',
            _min=0,
            _max=100,
        )
        perfdata += lib.base.get_perfdata(
            f'{name}_tbyte',
            sensor['tbyte'],
            _min=0,
        )
        perfdata += lib.base.get_perfdata(
            f'{name}_cpu_usage',
            sensor['cpu_usage'],
            uom='%',
            _min=0,
            _max=100,
        )
        perfdata += lib.base.get_perfdata(
            f'{name}_mem_usage',
            sensor['mem_usage'],
            uom='%',
            _min=0,
            _max=100,
        )

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


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