#!/usr/bin/env python3
# ***************************************************************************
#  IIIIII NNN  NNN  Copyright (C) 2022 Innovative Networks, Inc.
#    II    NNN  N   All Rights Reserved. Any redistribution or reproduction
#    II    N NN N   of part or all of the content of this program in any form
#    II    N  NNN   without expressed written consent of the copyright holder
#  IIIIII NNN  NNN  is strictly prohibited.  Please contact admins@in-kc.com
#   Be Innovative.  for additional information.
# ***************************************************************************
#  check_process_count.py
#  Author - Ian Perry <iperry@indigex.com>
#
#  Purpose:  Checks number of processes on macOS
#
#  Version History:
#       2023.05.10 - Initial creation
# ***************************************************************************

try:
    from inmon_utils import *
except:
    import sys

    print("Unable to import inmon utils")
    sys.exit(3)

# Installs psutil if it's not there.
ensure_module(psutil)


def check_process_count(config):
    """Check process count.

    Arguments:
    warn -- Warning number of processes
    crit -- Critical number of processes
    """
    crit = config["crit"]
    warn = config["warn"]

    stat = psutil.cpu_times_percent()

    count = len(psutil.pids())

    if count >= crit:
        ret = [2, f"[CRIT] - Process count critical: {count}%"]
    elif crit > count >= warn:
        ret = [1, f"[WARN] - Process count high: {count}%"]
    elif warn > count:
        ret = [0, f"[OK] - Process count nominal: {count}%"]
    else:
        ret = [3, f"[UNKN] - Unknown error occurred."]

    ret[2] = f"processes={count}"

    return ret
