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

import lib.args
import lib.base
import lib.lftest
import lib.powershell
import lib.winrm
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN

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

DESCRIPTION = """Monitors IPv4 DHCP scope usage on a Windows DHCP server. Connects via WinRM and
queries scope statistics using PowerShell. On servers with thousands of scopes, --brief
hides rows within the thresholds so the output only lists scopes in WARN/CRIT state.
Alerts when the address pool usage of any scope exceeds the configured thresholds
(default: WARN at 80%, CRIT at 90%)."""

DEFAULT_CRIT = 90
DEFAULT_HOSTNAME = 'localhost'
DEFAULT_WARN = 80
DEFAULT_WINRM_DOMAIN = None
DEFAULT_WINRM_TRANSPORT = 'ntlm'
DEFAULT_WINRM_USERNAME = 'Administrator'


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(
        '--brief',
        help='Hide scopes within the thresholds and show only those in WARN/CRIT state. '
        'Inverse of `--lengthy` (which adds columns); `--brief` filters rows. The two '
        'are orthogonal and can be combined. Perfdata and alerting are unaffected: all '
        'scopes still emit perfdata and still drive the overall check state. '
        'Default: %(default)s',
        dest='BRIEF',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '-c',
        '--critical',
        help=lib.args.help('--critical') + ' Default: >= %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '-H',
        '--hostname',
        help='DNS name, IPv4, or IPv6 address of the DHCP server. Default: %(default)s',
        dest='HOSTNAME',
        default=DEFAULT_HOSTNAME,
    )

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

    parser.add_argument(
        '-w',
        '--warning',
        help=lib.args.help('--warning') + ' Default: >= %(default)s',
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

    parser.add_argument(
        '--winrm-domain',
        help='WinRM Domain Name. Default: %(default)s',
        dest='WINRM_DOMAIN',
        default=DEFAULT_WINRM_DOMAIN,
    )

    parser.add_argument(
        '--winrm-hostname',
        help='Target Windows computer on which the command will be executed.',
        dest='WINRM_HOSTNAME',
        required=True,
    )

    parser.add_argument(
        '--winrm-password',
        help='WinRM account password.',
        dest='WINRM_PASSWORD',
        required=True,
    )

    parser.add_argument(
        '--winrm-transport',
        help='WinRM transport type. Default: %(default)s',
        dest='WINRM_TRANSPORT',
        choices=['basic', 'ntlm', 'kerberos', 'credssp', 'plaintext'],
        default=DEFAULT_WINRM_TRANSPORT,
    )

    parser.add_argument(
        '--winrm-username',
        help='WinRM account name. Default: %(default)s',
        dest='WINRM_USERNAME',
        default=DEFAULT_WINRM_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)

    # fetch data
    if args.TEST is None:
        # https://docs.microsoft.com/en-us/powershell/module/dhcpserver/get-dhcpserverv4scopestatistics
        cmd = f'Get-DhcpServerv4ScopeStatistics -ComputerName "{args.HOSTNAME}"'
        if args.WINRM_HOSTNAME:
            result = lib.winrm.run_ps(args, cmd)
        else:
            if lib.base.LINUX:
                lib.base.cu(
                    'Running this check plugin directly on Linux is not supported. Use the --winrm parameters.'
                )
            result = lib.powershell.run_ps(cmd)
        stdout, stderr, retc = result['stdout'], result['stderr'], result['retc']
    else:
        # do not call the command, put in test data
        stdout, stderr, retc = lib.lftest.test(args.TEST)

    if retc != 0:
        lib.base.oao(stderr, STATE_WARN)

    # init some vars
    state = STATE_OK
    perfdata = ''
    rows = []

    # ScopeId          Free            InUse           PercentageInUse  Reserved        Pending         SuperscopeName
    # -------          ----            -----           ---------------  --------        -------         --------------
    # 192.0.2.120      3               0               0                0               0

    # get state and collect per-scope rows
    data_section = False
    for line in stdout.splitlines():
        # ignore all header lines including "----"
        if not line:
            continue
        if not data_section:
            data_section = line.startswith('----')
            continue

        scope = line.split()
        scope_id = scope[0]

        # "PercentageInUse" is a floating point number and uses the user's preferred locale
        # number format. we don't care about the fraction part, separated depending on
        # the user's locale by `.`, `,` or whatever - we simply cut it off.
        scope_used = re.split(r'\D+', scope[3])[0]

        scope_state = lib.base.get_state(scope_used, args.WARN, args.CRIT)
        state = lib.base.get_worst(scope_state, state)

        # Perfdata is emitted for every scope regardless of --brief so
        # Grafana sees the full picture and trend lines stay continuous.
        perfdata += lib.base.get_perfdata(
            f'scope_{scope_id}',
            scope_used,
            uom='%',
            warn=args.WARN,
            crit=args.CRIT,
            _min=0,
            _max=100,
        )
        rows.append((scope_id, scope_used, scope_state))

    # Filter rows for --brief display. --brief is the inverse of
    # --lengthy: --lengthy adds columns, --brief filters rows. They
    # are orthogonal and both can be set at the same time. Perfdata
    # and alerting stay untouched above this point; --brief only
    # reshapes the human-readable output.
    display_rows = (
        [row for row in rows if row[2] != STATE_OK] if args.BRIEF else rows
    )

    # build the message
    headers = {
        STATE_CRIT: 'There are one or more criticals.',
        STATE_WARN: 'There are one or more warnings.',
        STATE_OK: 'Everything is ok.',
    }
    header = headers.get(state, headers[STATE_OK])
    body = ''.join(
        f'* {scope_id}: {scope_used}% used'
        f'{lib.base.state2str(scope_state, prefix=" ")}\n'
        for scope_id, scope_used, scope_state in display_rows
    )
    msg = f'{header}\n\n{body}' if body else header

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


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