Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Theschwartz job count plugin for Mackerel
#!/bin/bash
usage() {
echo "Usage: $0 [-m METRIC_NAME] [-u USER] [-p PASSWORD] [-d DATABASE] [-h]"
}
METRIC_NAME=theschwartz.count
DBUSER=root
DBPASS=
DATABASE=theschwartz
while getopts m:u:p:d:h OPT
do
case $OPT in
h) usage && exit 0 ;;
m) METRIC_NAME=$OPTARG ;;
u) DBUSER=$OPTARG ;;
p) PASSWORD=$OPTARG ;;
d) DATABASE=$OPTARG ;;
esac
done
shift $(($OPTIND - 1))
MYSQL="mysql --skip-column-names -u${DBUSER} ${DATABASE}"
[ -z $PASSWORD ] || MYSQL="${MYSQL} -p${PASSWORD}"
SQL="select funcname, count(jobid) from job right join funcmap using(funcid) group by funcmap.funcid;"
now=$(date +%s)
$MYSQL -e "$SQL" | while read job count; do
echo -e "${METRIC_NAME}.${job//::/_}\t${count}\t${now}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment