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/ # Sources: https://www.activexperts.com/support/network-monitor/online/linux/
# Changelog : # Changelog :
# 27-06-2022 - Initial script from source, tweaked for my environment # 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 # Default values for WARNING and ERROR limits
nWarnCpuUsage=$1 WARNING_LIMIT=85
nMaxCpuUsage=$2 ERROR_LIMIT=95
#endregion
# Validate number of arguments # Check for command line arguments
if [ $# -ne 2 ] ; then if [ $# -eq 2 ]; then
echo "UNCERTAIN: Invalid number of arguments - Usage: cpu nWarnCpuUsage nMaxCpuUsage" WARNING_LIMIT=$1
exit 1 ERROR_LIMIT=$2
fi fi
# Validate numeric parameter nWarnCpuUsage # Function to check CPU usage
regExpNumber='^[0-9]+$' check_cpu_usage() {
if ! [[ $1 =~ $regExpNumber ]] ; then CPU_USAGE=$(top -bn1 | grep 'Cpu(s)' | awk '{print int($2)}')
echo "UNCERTAIN: Invalid argument: nWarnCpuUsage (number expected)" if [ "$CPU_USAGE" -ge "$ERROR_LIMIT" ]; then
exit 1 echo "ERROR: CPU usage is $CPU_USAGE% (>= $ERROR_LIMIT%)"
fi 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
}
# Validate numeric parameter nMaxCpuUsage check_cpu_usage
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

View File

@@ -13,43 +13,34 @@
# Sources: https://www.activexperts.com/support/network-monitor/online/linux/ # Sources: https://www.activexperts.com/support/network-monitor/online/linux/
# Changelog : # Changelog :
# 27-06-2022 - Initial script from source, tweaked for my environment # 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 # Default values for WARNING and ERROR limits (in percentage)
nWarnMemoryUsage=$1 WARNING_LIMIT=85
nMaxMemoryUsage=$2 ERROR_LIMIT=95
#endregion
# Validate number of arguments # Check for command line arguments
if [ $# -ne 2 ] ; then if [ $# -eq 2 ]; then
echo "UNCERTAIN: Invalid number of arguments - Usage: memory nMaxMemoryUsage" WARNING_LIMIT=$1
exit 1 ERROR_LIMIT=$2
fi fi
# Validate numeric parameter nMaxMemoryUsage # Function to check RAM usage
regExpNumber='^[0-9]+$' check_ram_usage() {
if ! [[ $1 =~ $regExpNumber ]] ; then RAM_TOTAL=$(free -m | awk '/^Mem:/ {print $2}')
echo "UNCERTAIN: Invalid argument: nMaxMemoryUsage (number expected)" RAM_USED=$(free -m | awk '/^Mem:/ {print $3}')
exit 1 RAM_USAGE_PERCENTAGE=$(echo "scale=2; $RAM_USED / $RAM_TOTAL * 100" | bc)
fi
# Check the memory usage if [ "$(echo "$RAM_USAGE_PERCENTAGE >= $ERROR_LIMIT" | bc -l)" -eq 1 ]; then
nMemoryPercentage=`ps -A -o pmem | tail -n+2 | paste -sd+ | bc` echo "ERROR: RAM usage is ${RAM_USAGE_PERCENTAGE}% (>= ${ERROR_LIMIT}%)"
nMemoryPercentage=$( echo "$nMemoryPercentage / 1" | bc ) exit 1
elif [ "$(echo "$RAM_USAGE_PERCENTAGE >= $WARNING_LIMIT" | bc -l)" -eq 1 ]; then
echo "WARNING: RAM usage is ${RAM_USAGE_PERCENTAGE}% (>= ${WARNING_LIMIT}%)"
exit 2
else
echo "OK: RAM usage is ${RAM_USAGE_PERCENTAGE}%"
exit 0
fi
}
if [ $nMemoryPercentage -le $nWarnMemoryUsage ] ; then check_ram_usage
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%]"
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