43 lines
1.2 KiB
Bash
43 lines
1.2 KiB
Bash
#!/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
|
|
# 16-09-2023 - Changed the way the CPU performance is monitored to TOP, because of false positives in alerting.
|
|
|
|
# Default values for WARNING and ERROR limits
|
|
WARNING_LIMIT=85
|
|
ERROR_LIMIT=95
|
|
|
|
# Check for command line arguments
|
|
if [ $# -eq 2 ]; then
|
|
WARNING_LIMIT=$1
|
|
ERROR_LIMIT=$2
|
|
fi
|
|
|
|
# Function to check CPU usage
|
|
check_cpu_usage() {
|
|
CPU_USAGE=$(top -bn1 | grep 'Cpu(s)' | awk '{print int($2)}')
|
|
if [ "$CPU_USAGE" -ge "$ERROR_LIMIT" ]; then
|
|
echo "ERROR: CPU usage is $CPU_USAGE% (>= $ERROR_LIMIT%)"
|
|
exit 1
|
|
elif [ "$CPU_USAGE" -ge "$WARNING_LIMIT" ]; then
|
|
echo "WARNING: CPU usage is $CPU_USAGE% (>= $WARNING_LIMIT%)"
|
|
exit 2
|
|
else
|
|
echo "OK: CPU usage is $CPU_USAGE%"
|
|
exit 0
|
|
fi
|
|
}
|
|
|
|
check_cpu_usage |