icinga2-checks/check_iowait.sh

66 lines
1.7 KiB
Bash
Raw Normal View History

2023-06-07 14:31:09 -06:00
#!/usr/bin/env bash
2023-04-21 23:54:18 -06:00
2023-06-09 17:35:47 -06:00
WARNING_THRESHOLD=15
CRITICAL_THRESHOLD=25
AVERAGE_SECONDS=5
SHOW_TOP_PROCESSES=false
2023-04-21 23:54:18 -06:00
# Parse command line arguments
2023-06-09 17:35:47 -06:00
while getopts "w:c:n:t" opt; do
2023-04-21 23:54:18 -06:00
case $opt in
2023-06-09 17:35:47 -06:00
w)
WARNING_THRESHOLD="$OPTARG"
;;
c)
CRITICAL_THRESHOLD="$OPTARG"
2023-04-21 23:54:18 -06:00
;;
2023-06-09 17:35:47 -06:00
n)
AVERAGE_SECONDS="$OPTARG"
;;
t)
SHOW_TOP_PROCESSES=true
;;
\?)
echo "Usage: check_iowait.sh [-w warning_threshold] [-c critical_threshold] [-n average_seconds] [-t]"
exit 1
2023-04-21 23:54:18 -06:00
;;
esac
done
2023-06-09 17:35:47 -06:00
if ! command -v iostat &>/dev/null; then
echo "UNKNOWN - iostat not found! Please install sysstat"
exit -1
fi
2023-04-21 23:54:18 -06:00
2023-06-09 17:35:47 -06:00
# Get iowait value and top 3 processes causing iowait
if uname | grep -q "BSD"; then
iowait=$(iostat -c $(($AVERAGE_SECONDS + 1)) -w 1 | awk 'NR==4 {print $5}')
if $SHOW_TOP_PROCESSES; then
top_processes=$(ps -axo pid,comm,%cpu,%mem | sort -k 3 -nr | head -n 4 | awk 'NR>1 {printf "%s%s", sep, $2; sep=", "} END {print ""}')
fi
else
iowait=$(iostat -c $AVERAGE_SECONDS 2 | awk 'NR==4 {print $4}')
if $SHOW_TOP_PROCESSES; then
top_processes=$(pidstat -d -l -u -r 1 1 | awk 'NR>4 {print $1, $NF, $8}' | sort -k3 -nr | head -n 3 | awk '{printf "%s%s", sep, $2; sep=", "} END {print ""}')
fi
fi
2023-04-21 23:54:18 -06:00
2023-06-09 17:35:47 -06:00
if $SHOW_TOP_PROCESSES; then
iowait_str="- iowait: $iowait%"$'\n'"Top 3 processes causing iowait: $top_processes"
else
iowait_str="- iowait: $iowait%"
2023-04-21 23:54:18 -06:00
fi
2023-06-09 17:35:47 -06:00
perfdata="iowait=$iowait%;$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100"
2023-04-21 23:54:18 -06:00
2023-06-09 17:35:47 -06:00
# Check if iowait is above thresholds
if (($(echo "$iowait > $CRITICAL_THRESHOLD" | bc -l))); then
echo "CRITICAL $iowait_str | $perfdata"
exit 2
elif (($(echo "$iowait > $WARNING_THRESHOLD" | bc -l))); then
echo "WARNING $iowait_str | $perfdata"
2023-04-21 23:54:18 -06:00
exit 1
2023-06-09 17:35:47 -06:00
else
echo -e "OK $iowait_str | $perfdata"
exit 0
2023-04-21 23:54:18 -06:00
fi