#!/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 json  # 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.lftest  # pylint: disable=C0413
import lib.time  # pylint: disable=C0413
import lib.txt  # pylint: disable=C0413
import lib.url  # 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 = 'Checks OnlyOffice statistics and license usage via HTTP.'

DEFAULT_INSECURE = False
DEFAULT_NO_PROXY = False
DEFAULT_TIMEOUT = 3
DEFAULT_URL = 'http://localhost'


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(
        '--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(
        '--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(
        '--url',
        help='OnlyOffice API URL. '
             'Default: %(default)s',
        dest='URL',
        default=DEFAULT_URL,
    )

    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.base.coe(lib.url.fetch_json(
            args.URL + '/info/info.json',
            insecure=args.INSECURE,
            no_proxy=args.NO_PROXY,
            timeout=args.TIMEOUT,
        ))
    else:
        # do not call the command, put in test data
        stdout, stderr, retc = lib.lftest.test(args.TEST)
        try:
            result = json.loads(stdout)
        except:
            lib.base.cu('ValueError: No JSON object could be decoded')

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

    # get thresholds automatically
    max_conn = result['licenseInfo']['connections']
    max_conn_warn = max_conn * 0.9
    max_conn_crit = max_conn * 0.95

    # build the message and perfdata
    msg += 'Max {} {}, '.format(max_conn, lib.txt.pluralize('connection', max_conn))

    # license; expired?
    now = lib.time.now(as_type='iso')
    msg += 'licensed' if result['licenseInfo']['hasLicense'] else 'no license'
    if result['licenseInfo']['endDate']:
        if result['licenseInfo']['endDate'][0:10] <= now[0:10]:
            msg += ' (expired) [WARNING]'
            state = lib.base.get_worst(STATE_WARN, state)
        expires_in = lib.time.timestrdiff(result['licenseInfo']['endDate'][0:10], now[0:10], pattern1='%Y-%m-%d', pattern2='%Y-%m-%d')
        if expires_in <= 10 * 24 * 60 * 60:
            msg += ' (expires in {}) [WARNING]'.format(lib.human.seconds2human(expires_in))
            state = lib.base.get_worst(STATE_WARN, state)
    msg += ', '

    view_state = lib.base.get_state(result['connectionsStat']['hour']['view']['max'], max_conn_warn, max_conn_crit, _operator='ge')
    state = lib.base.get_worst(view_state, state)
    msg += 'last hour: {}/{}/{} views{} '.format(
        result['connectionsStat']['hour']['view']['min'],
        result['connectionsStat']['hour']['view']['avr'],
        result['connectionsStat']['hour']['view']['max'],
        lib.base.state2str(view_state, prefix=' ')
    )
    perfdata += lib.base.get_perfdata('conn_hour_view_min', result['connectionsStat']['hour']['view']['min'], None, max_conn_warn, max_conn_crit, 0, result['licenseInfo']['connections'])
    perfdata += lib.base.get_perfdata('conn_hour_view_avr', result['connectionsStat']['hour']['view']['avr'], None, max_conn_warn, max_conn_crit, 0, result['licenseInfo']['connections'])
    perfdata += lib.base.get_perfdata('conn_hour_view_max', result['connectionsStat']['hour']['view']['max'], None, max_conn_warn, max_conn_crit, 0, result['licenseInfo']['connections'])

    edit_state = lib.base.get_state(result['connectionsStat']['hour']['edit']['max'], max_conn_warn, max_conn_crit, _operator='ge')
    state = lib.base.get_worst(edit_state, state)
    msg += 'and {}/{}/{} edits{} (min/avr/max), '.format(
        result['connectionsStat']['hour']['edit']['min'],
        result['connectionsStat']['hour']['edit']['avr'],
        result['connectionsStat']['hour']['edit']['max'],
        lib.base.state2str(edit_state, prefix=' ')
    )
    perfdata += lib.base.get_perfdata('conn_hour_edit_min', result['connectionsStat']['hour']['edit']['min'], None, max_conn_warn, max_conn_crit, 0, result['licenseInfo']['connections'])
    perfdata += lib.base.get_perfdata('conn_hour_edit_avr', result['connectionsStat']['hour']['edit']['avr'], None, max_conn_warn, max_conn_crit, 0, result['licenseInfo']['connections'])
    perfdata += lib.base.get_perfdata('conn_hour_edit_max', result['connectionsStat']['hour']['edit']['max'], None, max_conn_warn, max_conn_crit, 0, result['licenseInfo']['connections'])

    if 'usersInfo' in result:
        msg += '{} unique {}, '.format(result['usersInfo']['uniqueUserCount'], lib.txt.pluralize('user', result['usersInfo']['uniqueUserCount']))
        perfdata += lib.base.get_perfdata('unique_users', result['usersInfo']['uniqueUserCount'], None, None, None, 0, None)

    msg += 'v{}, '.format(result['serverInfo']['buildVersion'])


    # over and out
    lib.base.oao(msg[:-2], state, perfdata)


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