2
0

Changed the performance monitor to TOP

This commit is contained in:
2023-09-16 15:22:34 +02:00
parent fc2a377587
commit 46a7dcdebe
2 changed files with 50 additions and 79 deletions

View File

@@ -13,51 +13,31 @@
# 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.
#region Parameters, functions and global variables
nWarnCpuUsage=$1
nMaxCpuUsage=$2
#endregion
# Default values for WARNING and ERROR limits
WARNING_LIMIT=85
ERROR_LIMIT=95
# Validate number of arguments
if [ $# -ne 2 ] ; then
echo "UNCERTAIN: Invalid number of arguments - Usage: cpu nWarnCpuUsage nMaxCpuUsage"
# 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
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%]"
elif [ "$CPU_USAGE" -ge "$WARNING_LIMIT" ]; then
echo "WARNING: CPU usage is $CPU_USAGE% (>= $WARNING_LIMIT%)"
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
else
echo "OK: CPU usage is $CPU_USAGE%"
exit 0
fi
}
check_cpu_usage

View File

@@ -13,43 +13,34 @@
# 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 RAM performance is monitored to TOP, because of false positives in alerting.
#region Parameters, functions and global variables
nWarnMemoryUsage=$1
nMaxMemoryUsage=$2
#endregion
# Validate number of arguments
if [ $# -ne 2 ] ; then
echo "UNCERTAIN: Invalid number of arguments - Usage: memory nMaxMemoryUsage"
# Default values for WARNING and ERROR limits (in percentage)
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 RAM usage
check_ram_usage() {
RAM_TOTAL=$(free -m | awk '/^Mem:/ {print $2}')
RAM_USED=$(free -m | awk '/^Mem:/ {print $3}')
RAM_USAGE_PERCENTAGE=$(echo "scale=2; $RAM_USED / $RAM_TOTAL * 100" | bc)
if [ "$(echo "$RAM_USAGE_PERCENTAGE >= $ERROR_LIMIT" | bc -l)" -eq 1 ]; then
echo "ERROR: RAM usage is ${RAM_USAGE_PERCENTAGE}% (>= ${ERROR_LIMIT}%)"
exit 1
fi
# Validate numeric parameter nMaxMemoryUsage
regExpNumber='^[0-9]+$'
if ! [[ $1 =~ $regExpNumber ]] ; then
echo "UNCERTAIN: Invalid argument: nMaxMemoryUsage (number expected)"
exit 1
fi
# Check the memory usage
nMemoryPercentage=`ps -A -o pmem | tail -n+2 | paste -sd+ | bc`
nMemoryPercentage=$( echo "$nMemoryPercentage / 1" | bc )
if [ $nMemoryPercentage -le $nWarnMemoryUsage ] ; then
echo "SUCCESS: Memory usage is [$nMemoryPercentage%]"
echo ""
echo "Warning limit=[$1%], Error limit=[$2%]"
exit 0
fi
if [ $nMemoryPercentage -gt $nWarnMemoryUsage -a $nMemoryPercentage -le $nMaxMemoryUsage ] ; then
echo "WARNING: Memory usage is [$nMemoryPercentage%]"
echo ""
echo "Warning limit=[$1%], Error limit=[$2%]"
elif [ "$(echo "$RAM_USAGE_PERCENTAGE >= $WARNING_LIMIT" | bc -l)" -eq 1 ]; then
echo "WARNING: RAM usage is ${RAM_USAGE_PERCENTAGE}% (>= ${WARNING_LIMIT}%)"
exit 2
fi
if [ $nMemoryPercentage -gt $nMaxMemoryUsage ] ; then
echo "ERROR: Memory usage is [$nMemoryPercentage%]"
echo ""
echo "Warning limit=[$1%], Error limit=[$2%]"
exit 1
fi
else
echo "OK: RAM usage is ${RAM_USAGE_PERCENTAGE}%"
exit 0
fi
}
check_ram_usage