#!/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 lib.base  # pylint: disable=C0413
import lib.shell  # pylint: disable=C0413
from lib.globals import (STATE_OK, STATE_UNKNOWN,  # pylint: disable=C0413
                          STATE_WARN)

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

DESCRIPTION = """Attempts to obtain entries from Name Service Switch (NSS) libraries and warns of
                 errors or missing matches."""

DEFAULT_DATABASE = 'group'


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(
        '--database',
        default=DEFAULT_DATABASE,
        dest='DATABASE',
        help='May be any of those supported by "getent", for example "group", "hosts" etc. Default: %(default)s',
    )

    parser.add_argument(
        '--key',
        help="""If one or more key arguments are provided, then only the entries that match the
                supplied keys will be fetched. Otherwise, if no key is provided, all entries
                will be fetched (unless the database does not support enumeration). (repeating)""",
        dest='KEY',
        action='append',
    )

    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)

    # build the getent command
    cmd = '/usr/bin/getent {}'.format(args.DATABASE)
    if args.KEY:
        for key in args.KEY:
            cmd += ' {}'.format(key)

    # execute the shell command and return its result and exit code
    stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(cmd))
    if stderr:
        lib.base.cu(stderr)

    count = len(stdout.strip().split('\n'))

    # build the message
    if retc == 0:
        msg = 'Everything is ok. Executed `{}`, got {} results.'.format(cmd, count)
        state = STATE_OK
    if retc == 1:
        msg = 'Missing arguments for `{}`, or database "{}" unknown.'.format(cmd, args.DATABASE)
        state = STATE_UNKNOWN
    if retc == 2:
        msg = 'One or more supplied keys could not be found in "{}", executing `{}`.'.format(args.DATABASE, cmd)
        state = STATE_WARN
    if retc == 3:
        msg = 'Multiple keys (enumeration) not supported on database "{}".'.format(args.DATABASE)
        state = STATE_UNKNOWN

    # over and out
    lib.base.oao(msg, state)


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