#!/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
from collections import Counter  # pylint: disable=C0413

import lib.base  # pylint: disable=C0413
import lib.human  # pylint: disable=C0413
import lib.version  # pylint: disable=C0413
from lib.globals import (STATE_CRIT, STATE_OK,  # pylint: disable=C0413
                          STATE_UNKNOWN, STATE_WARN)

try:
    import psutil  # pylint: disable=C0413
except ImportError:
    print('Python module "psutil" is not installed.')
    sys.exit(STATE_UNKNOWN)


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

DESCRIPTION = 'Checks the number of allocated file handles in percent.'

DEFAULT_CRIT  = 95      # %
DEFAULT_TOP = 5
DEFAULT_WARN  = 90      # %


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(
        '--always-ok',
        help='Always returns OK.',
        dest='ALWAYS_OK',
        action='store_true',
        default=False,
    )

    parser.add_argument(
        '-c', '--critical',
        help='Set the critical threshold for file descriptor usage (in percent). Default: %(default)s',
        dest='CRIT',
        type=int,
        default=DEFAULT_CRIT,
    )

    parser.add_argument(
        '--top',
        help='List x "Top processes opening file descriptors". '
             'Default: %(default)s',
        dest='TOP',
        type=int,
        default=DEFAULT_TOP,
    )

    parser.add_argument(
        '-w', '--warning',
        help='Set the warning threshold for file descriptor usage (in percent). Default: %(default)s',
        dest='WARN',
        type=int,
        default=DEFAULT_WARN,
    )

    return parser.parse_args()


def top(count):
    """Get top X processes opening file descriptors.
    """
    cnt = Counter()
    msg = '\nTop {} processes opening file descriptors:\n'.format(count)
    if lib.version.version(psutil.__version__) >= lib.version.version('5.3.0'):
        try:
            for p in psutil.process_iter(attrs=['name', 'num_fds']):
                if p.info['num_fds']:
                    cnt[p.info['name']] += p.info['num_fds']
        except psutil.NoSuchProcess:
            pass
    else:
        try:
            for p in [x.as_dict(attrs=['name', 'num_fds']) for x in psutil.process_iter()]:
                if p['num_fds']:
                    cnt[p['name']] += p['num_fds']
        except psutil.NoSuchProcess:
            pass

    for i, p in enumerate(cnt.most_common(count)):
        msg += '{}. {}: {} FD\n'.format(i + 1, p[0], p[1])
    return msg


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)

    msg = ''
    perfdata = ''
    state = STATE_OK

    with open('/proc/sys/fs/file-nr') as file:
        fs = [float(item) for item in file.readline().split('\t')]

    files_allocated = fs[0]     # The number of allocated file handles
    files_max = fs[2]           # The number of system-wide maximum number of file handles
    # fs[1] is the number of unused-but-allocated file handles
    files_percent = round(files_allocated / files_max * 100, 1)

    msg = '{}% file descriptors used ({}/{})\n'.format(
        files_percent,
        lib.human.number2human(files_allocated),
        lib.human.number2human(files_max)
    )
    perfdata += lib.base.get_perfdata('fd', files_percent, '%', args.WARN, args.CRIT, 0, 100)
    state = lib.base.get_state(files_percent, args.WARN, args.CRIT, 'ge')

    # Top X processes opening file descriptors
    msg += top(args.TOP)

    # 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()
