#!/bin/sh

#Uptime=5541
#Threads=5
#Questions=2985
#Slow_queries=0
#Opens=32
#Flush_tables=1
#Open_tables=26

export RET=`/usr/lib/nagios/plugins/check_mysql "$@"`
STATUS=$?
echo "$RET" \
  | sed -e 's/  /\n/g' -e 's/: /=/g' -e 's/ /_/g' \
  | tr 'A-Z' 'a-z' \
  | grep '^[a-z_]\+=[0-9.]\+$' \
  | awk -F= '
BEGIN {
  fmt_float = "%s=%.3fc;%d;%d;0; "
  fmt_long = "%s=%dc;%d;%d;0; "
  fmt["questions"]=fmt_long
  warn["questions"]=10000
  crit["questions"]=100000
  fmt["slow_queries"]=fmt_long
  warn["slow_queries"]=10
  crit["slow_queries"]=100
  fmt["opens"]=fmt_long
  warn["opens"]=100
  crit["opens"]=1000
  fmt["flush_tables"]=fmt_long
  warn["flush_tables"]=100
  crit["flush_tables"]=1000
  fmt["open_tables"]=fmt_long
  warn["open_tables"]=100
  crit["open_tables"]=1000
  fmt["threads"]=fmt_long
  warn["threads"]=300
  crit["threads"]=1000
}
{
  a[$1]=$2
}
END {
  printf "%s|", ENVIRON["RET"]
  for (key in fmt) {
    printf fmt[key], key, a[key], warn[key], crit[key]
  }
  printf "\n"
}
'

exit $STATUS
