#!/bin/bash # Usage: "Linux - CPU usage monitor.sh 75 95" # # Description: # This script is used from inside of Tactical RMM instances to check the CPU usage. # This script should be deployed as a check script. # # Options: # Arg1 - Warning limit # Arg2 - Error limit # # Author: D. de Kooker (info@dcomputers.nl) # Sources: https://www.activexperts.com/support/network-monitor/online/linux/ # Changelog : # 27-06-2022 - Initial script from source, tweaked for my environment #region Parameters, functions and global variables nWarnCpuUsage=$1 nMaxCpuUsage=$2 #endregion # Validate number of arguments if [ $# -ne 2 ] ; then echo "UNCERTAIN: Invalid number of arguments - Usage: cpu nWarnCpuUsage nMaxCpuUsage" exit 1 fi # Validate numeric parameter nWarnCpuUsage regExpNumber='^[0-9]+$' if ! [[ $1 =~ $regExpNumber ]] ; then echo "UNCERTAIN: Invalid argument: nWarnCpuUsage (number expected)" exit 1 fi # Validate numeric parameter nMaxCpuUsage regExpNumber='^[0-9]+$' if ! [[ $1 =~ $regExpNumber ]] ; then echo "UNCERTAIN: Invalid argument: nMaxCpuUsage (number expected)" exit 1 fi # Check the CPU usage nCpuLoadPercentage=`ps -A -o pcpu | tail -n+2 | paste -sd+ | bc` nCpuLoadPercentage=$( echo "$nCpuLoadPercentage / 1" | bc ) if [ $nCpuLoadPercentage -le $nWarnCpuUsage ] ; then echo "SUCCESS: CPU usage is [$nCpuLoadPercentage%]" echo "" echo "Warning limit=[$1%], Error limit=[$2%]" exit 0 fi if [ $nCpuLoadPercentage -gt $nWarnCpuUsage -a $nCpuLoadPercentage -le $nMaxCpuUsage ] ; then echo "WARNING: CPU usage is [$nCpuLoadPercentage%]" echo "" echo "Warning limit=[$1%], Error limit=[$2%]" exit 2 fi if [ $nCpuLoadPercentage -gt $nMaxCpuUsage ] ; then echo "ERROR: CPU usage is [$nCpuLoadPercentage%]" echo "" echo "Warning limit=[$1%], Error limit=[$2%]" exit 1 fi