#!/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.lftest
import lib.shell
from lib.globals import (STATE_OK, STATE_UNKNOWN, STATE_WARN)

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

DESCRIPTION = '''This plugin compares the chronyd clock offset in milliseconds to that of
                 the NTP servers.'''

DEFAULT_CRIT = 86400000 # 24h in ms offset
DEFAULT_STRATUM = 6
DEFAULT_WARN = 800  # ms offset


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(
        '-c', '--critical',
        help='Set the critical threshold for the ntp time offset, in ms. '
             'Default: %(default)sms',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '--stratum',
        help=lib.args.help('--stratum') + ' '
             + 'Default: %(default)s',
        dest='STRATUM',
        type=int,
        default=DEFAULT_STRATUM,
    )

    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(
        '-w', '--warning',
        help='Set the warning threshold for the ntp time offset, in ms. '
             'Default: %(default)sms',
        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)

    if args.TEST is None:
        cmd = 'chronyc tracking'
        stdout, stderr, _ = lib.base.coe(lib.shell.shell_exec(cmd))
    else:
        # do not call the command, put in test data
        stdout, stderr, _ = lib.lftest.test(args.TEST)
    if stderr:
        lib.base.cu(stderr)

    # init some vars
    msg = ''
    state = STATE_OK
    perfdata = ''

    frequency = 0
    last_offset = 0
    leap_status = ''
    peer_used = False
    residual_freq = 0
    rms_offset = 0
    root_delay = 0
    root_dispersion = 0
    skew = 0

    # analyze data
    for line in stdout.splitlines():
        line = line.strip()
        if line.startswith('Stratum'):
            stratum = int(line.split()[-1])
            peer_used = stratum != 0
            continue
        if line.startswith('Last offset'):
            last_offset = float(line.split()[-2]) * 1000  # convert to ms
            continue
        if line.startswith('RMS offset'):
            rms_offset = float(line.split()[-2]) * 1000  # convert to ms
            continue
        if line.startswith('Frequency'):
            frequency = float(line.split()[-3])
            continue
        if line.startswith('Residual freq'):
            residual_freq = float(line.split()[-2])
            continue
        if line.startswith('Skew'):
            skew = float(line.split()[-2])
            continue
        if line.startswith('Root delay'):
            root_delay = float(line.split()[-2]) * 1000  # convert to ms
            continue
        if line.startswith('Root dispersion'):
            root_dispersion = float(line.split()[-2]) * 1000  # convert to ms
            continue
        if line.startswith('Leap status'):
            leap_status = ' '.join(line.split()[3:])

    if not peer_used:
        # let's find out why and display the configured NTP servers
        msg = 'NTP server not reachable. No NTP server is used.'
        success, result = lib.shell.shell_exec('chronyc sources')
        if success:
            msg += '\n\n' + result[0]
        lib.base.oao(msg, STATE_WARN)

    # build the message
    offset_state = lib.base.get_state(abs(last_offset), args.WARN, args.CRIT)
    state = lib.base.get_worst(state, offset_state)
    msg += f'NTP offset is {round(last_offset, 3)}ms' \
           f'{lib.base.state2str(offset_state, prefix=' ')}, ' \
           f'Stratum is {stratum}'
    if stratum >= args.STRATUM:
        stratum_state = STATE_WARN
        state = lib.base.get_worst(state, stratum_state)
        msg += f' (is >= {args.STRATUM} {lib.base.state2str(stratum_state)})'
    msg += f', Leap status is {leap_status}'

    perfdata += lib.base.get_perfdata(
        'frequency',
        frequency,
        uom=None,
        warn=None,
        crit=None,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'last_offset',
        last_offset,
        uom='ms',
        warn=args.WARN,
        crit=args.CRIT,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'residual_freq',
        residual_freq,
        uom=None,
        warn=None,
        crit=None,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'rms_offset',
        rms_offset,
        uom='ms',
        warn=None,
        crit=None,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'root_delay',
        root_delay,
        uom='ms',
        warn=None,
        crit=None,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'root_dispersion',
        root_dispersion,
        uom='ms',
        warn=None,
        crit=None,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'skew',
        skew,
        uom=None,
        warn=None,
        crit=None,
        _min=None,
        _max=None,
    )
    perfdata += lib.base.get_perfdata(
        'stratum',
        stratum,
        uom=None,
        warn=args.STRATUM,
        crit=None,
        _min=0,
        _max=16,
    )

    # over and out
    lib.base.oao(msg + '\n\n' + stdout, state, perfdata)


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