diff --git a/Check_scripts/Linux - CPU usage monitor.sh b/Check_scripts/Linux - CPU usage monitor.sh index 86b830a..8a92d8d 100644 --- a/Check_scripts/Linux - CPU usage monitor.sh +++ b/Check_scripts/Linux - CPU usage monitor.sh @@ -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" - exit 1 +# Check for command line arguments +if [ $# -eq 2 ]; then + WARNING_LIMIT=$1 + ERROR_LIMIT=$2 fi -# Validate numeric parameter nWarnCpuUsage -regExpNumber='^[0-9]+$' -if ! [[ $1 =~ $regExpNumber ]] ; then - echo "UNCERTAIN: Invalid argument: nWarnCpuUsage (number expected)" - exit 1 -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 +} -# 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 \ No newline at end of file +check_cpu_usage \ No newline at end of file diff --git a/Check_scripts/Linux - RAM usage monitor.sh b/Check_scripts/Linux - RAM usage monitor.sh index 50cef4c..0f9de7d 100644 --- a/Check_scripts/Linux - RAM usage monitor.sh +++ b/Check_scripts/Linux - RAM usage monitor.sh @@ -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" - exit 1 +# 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 -# Validate numeric parameter nMaxMemoryUsage -regExpNumber='^[0-9]+$' -if ! [[ $1 =~ $regExpNumber ]] ; then - echo "UNCERTAIN: Invalid argument: nMaxMemoryUsage (number expected)" - exit 1 -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 + 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 +} -# 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%]" - 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 \ No newline at end of file +check_ram_usage \ No newline at end of file