fix check_iowait for bsd

This commit is contained in:
Cyberes 2023-06-09 17:35:47 -06:00
parent e78294d7d9
commit 385ee78b5e
2 changed files with 50 additions and 110 deletions

View File

@ -1,63 +1,65 @@
#!/usr/bin/env bash
# Nagios plugin to check iowait on Linux
if ! command -v iostat &>/dev/null; then
echo "iostat not found! Please install sysstat:"
echo "sudo apt install sysstat"
exit 1
fi
#!/usr/bin/env bash
# Nagios plugin to check iowait on Linux
# Define usage function
function usage {
echo "Usage: $0 [-w <warning threshold>] [-c <critical threshold>] [-a <average seconds>]"
echo " -w: Warning threshold for iowait percentage (default: 50)"
echo " -c: Critical threshold for iowait percentage (default: 75)"
echo " -a: Number of seconds to average iowait (default: 5)"
exit 3
}
WARNING_THRESHOLD=15
CRITICAL_THRESHOLD=25
AVERAGE_SECONDS=5
SHOW_TOP_PROCESSES=false
# Parse command line arguments
while getopts "w:c:a:" opt; do
while getopts "w:c:n:t" opt; do
case $opt in
w) WARNING_THRESHOLD=$OPTARG ;;
c) CRITICAL_THRESHOLD=$OPTARG ;;
a) AVERAGE_SECONDS=$OPTARG ;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
w)
WARNING_THRESHOLD="$OPTARG"
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
c)
CRITICAL_THRESHOLD="$OPTARG"
;;
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
;;
esac
done
# Set default warning and critical thresholds if not provided
WARNING_THRESHOLD=${WARNING_THRESHOLD:-50}
CRITICAL_THRESHOLD=${CRITICAL_THRESHOLD:-75}
AVERAGE_SECONDS=${AVERAGE_SECONDS:-5}
if ! command -v iostat &>/dev/null; then
echo "UNKNOWN - iostat not found! Please install sysstat"
exit -1
fi
# Get current iowait percentage and number of processes
IOWAIT=$(iostat -c $AVERAGE_SECONDS 2 | awk '/^ /{print $4}' | tail -n 1)
PROCESSES=$(ps -A --no-headers | wc -l)
# 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
# Check if iowait percentage is above critical threshold
if (($(echo "$IOWAIT > $CRITICAL_THRESHOLD" | bc -l))); then
echo "CRITICAL - iowait percentage is $IOWAIT% | iowait=$IOWAIT%;$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100"
if $SHOW_TOP_PROCESSES; then
iowait_str="- iowait: $iowait%"$'\n'"Top 3 processes causing iowait: $top_processes"
else
iowait_str="- iowait: $iowait%"
fi
perfdata="iowait=$iowait%;$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100"
# Check if iowait is above thresholds
if (($(echo "$iowait > $CRITICAL_THRESHOLD" | bc -l))); then
echo "CRITICAL $iowait_str | $perfdata"
exit 2
fi
# Check if iowait percentage is above warning threshold
if (($(echo "$IOWAIT > $WARNING_THRESHOLD" | bc -l))); then
echo "WARNING - iowait percentage is $IOWAIT% | iowait=$IOWAIT%;$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100"
elif (($(echo "$iowait > $WARNING_THRESHOLD" | bc -l))); then
echo "WARNING $iowait_str | $perfdata"
exit 1
else
echo -e "OK $iowait_str | $perfdata"
exit 0
fi
# If iowait percentage is below warning threshold, exit with OK status
echo "OK - iowait percentage is $IOWAIT% | iowait=$IOWAIT%;$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100"
exit 0

View File

@ -1,62 +0,0 @@
#!/usr/bin/env bash
WARNING_THRESHOLD=10
CRITICAL_THRESHOLD=20
# Parse command line arguments
while getopts "w:c:" opt; do
case $opt in
w)
WARNING_THRESHOLD="$OPTARG"
;;
c)
CRITICAL_THRESHOLD="$OPTARG"
;;
\?)
echo "Usage: check_iowait.sh [-w warning_threshold] [-c critical_threshold]"
exit 1
;;
esac
done
if ! command -v iostat &>/dev/null; then
echo "UNKNOWN - iostat not found! Please install sysstat"
exit -1
fi
if ! command -v iotop &>/dev/null; then
echo "UNKNWON - iotop not found! Please install iotop"
exit -1
fi
# Get iowait value
iowait=$(iostat -c | awk 'NR==4 {print $4}')
# Get process list header and top 3 processes causing iowait
header=$(iotop -b -n 1 -P -k -o | head -n 1)
top_processes=$(iotop -b -n 1 -P -k -o | tail -n +4 | head -n 3)
# Check if there are no processes causing iowait
if [ -z "$top_processes" ]; then
top_processes="No processes causing significant iowait."
else
top_processes="$header"$'\n'"$top_processes"
fi
iowait_str="- iowait: $iowait%"$'\n'"Top 3 processes causing iowait:"
perfdata="iowait=$iowait%;$WARNING_THRESHOLD;$CRITICAL_THRESHOLD;0;100"
# Check if iowait is above thresholds
if (( $(echo "$iowait > $CRITICAL_THRESHOLD" | bc -l) )); then
echo "CRITICAL $iowait_str"
echo "$top_processes | $perfdata"
exit 2
elif (( $(echo "$iowait > $WARNING_THRESHOLD" | bc -l) )); then
echo "WARNING $iowait_str"
echo "$top_processes | $perfdata"
exit 1
else
echo -e "OK $iowait_str"
echo "$top_processes | $perfdata"
exit 0
fi