#!/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 sys  # pylint: disable=C0413
import urllib.parse  # pylint: disable=C0413

import lib.base  # pylint: disable=C0413
import lib.db_sqlite  # 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__ = '2023112901'

DESCRIPTION = """Returns the current system-wide CPU utilization as a percentage from Forti
                 Appliances like FortiGate running FortiOS via FortiOS REST API. Warns only if the
                 overall CPU usage is above a certain threshold within the last n checks
                 (default: 5). The authentication is done via a single API token (Token-based
                 authentication), not via Session-based authentication, which is stated as "legacy".
                 Hint: This plugin tries to check against the global configured `cpu-use-threshold`
                 first; only if there is no value, the check\'s command line values (or their
                 defaults) are used."""

DEFAULT_WARN = 80        # %
DEFAULT_CRIT = 90        # %
DEFAULT_COUNT = 5        # measurements; if check runs once per minute, this is a 5 minute interval
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='{0}: v{1} by {2}'.format('%(prog)s', __version__, __author__)
    )

    parser.add_argument(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '--count',
        help='Number of times the value must exceed specified thresholds before alerting. '
             'Default: %(default)s',
        dest='COUNT',
        type=int,
        default=DEFAULT_COUNT,
    )

    parser.add_argument(
        '-c', '--critical',
        help="Set the critical threshold CPU Usage Percentage. Hint: This plugin tries to check against the global configured `cpu-use-threshold` first; only if there is no value, the check's command line values (or their defaults) are used. Default: %(default)s",
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '-H', '--hostname',
        help='FortiOS-based Appliance address, optional including port ("192.168.1.1:443").',
        dest='HOSTNAME',
        required = True,
    )

    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(
        '--password',
        help='FortiOS REST API Single Access Token.',
        dest='PASSWORD',
        required = True,
    )

    parser.add_argument(
        '--timeout',
        help='Network timeout in seconds. Default: %(default)s (seconds)',
        dest='TIMEOUT',
        type=int,
        default=DEFAULT_TIMEOUT,
    )

    parser.add_argument(
        '-w', '--warning',
        help="Set the warning threshold CPU Usage Percentage. Hint: This plugin tries to check against the global configured `cpu-use-threshold` first; only if there is no value, the check's command line values (or their defaults) are used. Default: %(default)s",
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

    return parser.parse_args()


def get_from_db(conn, threshold):
    result = lib.base.coe(lib.db_sqlite.select(conn,
        '''
        SELECT count(*) as cnt
        FROM perfdata
        WHERE cpu_usage > :cpu_usage
        ''',
        {'cpu_usage': threshold},
        fetchone=True
    ))
    return int(result['cnt'])


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
    # Resource to get usage data for [cpu|memory|disk|sessions|lograte].
    url = 'https://{}/api/v2/monitor/system/resource/usage?resource=cpu&interval=1-min&access_token={}'.format(args.HOSTNAME, 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))

    stats = {}
    stats['cpu_usage'] = result['results']['cpu'][0]['current']

    # create the db table
    definition = '''
                cpu_usage REAL NOT NULL
        '''
    conn = lib.base.coe(lib.db_sqlite.connect(filename='linuxfabrik-monitoring-plugins-fortios-cpu-usage.db'))
    lib.base.coe(lib.db_sqlite.create_table(conn, definition))

    # save trend data to local sqlite database, limited to "count" rows max.
    lib.base.coe(lib.db_sqlite.insert(conn, stats))
    lib.base.coe(lib.db_sqlite.cut(conn, _max=args.COUNT))
    lib.base.coe(lib.db_sqlite.commit(conn))

    # try to get the global configured cpu warning threshold from Forti OS
    # if success, use it, otherwise our from args.WARN etc.
    url = 'https://{}/api/v2/cmdb/system/global?access_token={}'.format(args.HOSTNAME, urllib.parse.quote(args.PASSWORD))
    thresholds = lib.base.coe(lib.url.fetch_json(url, insecure=args.INSECURE, no_proxy=args.NO_PROXY, timeout=args.TIMEOUT))
    warn = thresholds['results'].get('cpu-use-threshold', args.WARN)

    # now, calculate the WARN or CRIT.
    # overall state is not ok, if ...
    #   in a row in any column there is a value above the threshold
    #   and this is true for every row
    state = STATE_OK
    if get_from_db(conn, args.CRIT) == args.COUNT:
        state = STATE_CRIT
    elif get_from_db(conn, warn) == args.COUNT:
        state = STATE_WARN
    lib.db_sqlite.close(conn)

    # build the message
    msg = '{}%'.format(stats['cpu_usage'])
    perfdata = lib.base.get_perfdata('cpu-usage', stats['cpu_usage'], '%', warn, args.CRIT, 0, 100)

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


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