Skip to content

Instantly share code, notes, and snippets.

@tott
Last active July 10, 2018 03:32
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tott/abaa61adfe81d99b3878fda836d1cd10 to your computer and use it in GitHub Desktop.
WordPress options table size Nagios monitor
#!/bin/bash
OPTIND=1
verbose=0
dbuser=""
dbpasswd=""
while getopts "vh?U:P:H:" opt; do
case "$opt" in
h|\?)
echo "Usage: $0 -U <dbuser> -P <dbpasswd> -H <dbhost>"
exit 0
;;
v)
verbose=1
;;
U)
dbuser=$OPTARG
;;
P)
dbpasswd=$OPTARG
;;
H)
dbhost=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ -z "$dbuser" ] || [ -z "$dbpasswd" ]; then
echo "Usage: $0 -U <dbuser> -P <dbpasswd> -H <dbhost>"
exit 1;
fi
select="select concat('select sum(char_length(option_value)/(1024*1024)) from \`', \`tables\`.\`table_schema\`, '\`.\`', \`tables\`.\`table_name\`, '\` where autoload=\"yes\"') as query from \`information_schema\`.\`tables\` where \`tables\`.\`table_name\` like '%\_options';"
temp_file=$(mktemp)
mysql_command="mysql -N -u $dbuser -p$dbpasswd -h $dbhost"
warn_threshold=0.90
alarm_threshold=0.98
echo $select | ${mysql_command} > ${temp_file}
if [ -f "$temp_file" ]; then
alarm_level=0
messages=""
while IFS= read -r query; do
result=`echo $query | ${mysql_command}`
table=`echo $query | grep -oE '\.\`(.+)\`\s' | cut -d '\`' -f 2`
if (( $(echo "$result > $alarm_threshold" | bc -l) )); then
if [ $alarm_level -le 2 ]; then
alarm_level=2
fi
messages="$messages $table=$result"
elif (( $(echo "$result >= $warn_threshold" | bc -l) )); then
if [ "$alarm_level" -le 1 ]; then
alarm_level=1
fi
messages="$messages $table=$result"
fi
done < ${temp_file}
if [ $alarm_level -lt 1 ]; then
echo "OK"
elif [ $alarm_level -ge 1 -a $alarm_level -lt 2 ]; then
echo "WARNING -$messages"
elif [ $alarm_level -ge 2 ]; then
echo "CRITICAL -$messages"
else
echo "UNKNOWN -$messages"
fi
rm ${temp_file}
exit $alarm_level
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment