Skip to content

Instantly share code, notes, and snippets.

@brendanhay
Created October 26, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanhay/3959309 to your computer and use it in GitHub Desktop.
Save brendanhay/3959309 to your computer and use it in GitHub Desktop.
Random metrics generation for statsd
#!/bin/bash
BUCKETS=(ball sack nut job hax red saw new grunge)
TYPES=(g c s ms)
usage() {
echo "Usage: -h [host] -p [port]"
exit 1
}
rand() {
printf $(($1 * RANDOM / 32767))
}
sample() {
declare -a ary=("${!1}")
unset ary[0]
printf $'%s\n' "${ary[$(($(rand "${#ary[*]}")+1))]}"
}
emit() {
local samples rate metric
samples=('' "|@0.${RANDOM:0:2}" "")
rate=$(sample samples[@])
metric="$(sample BUCKETS[@]):$RANDOM|$(sample TYPES[@])$rate"
echo $metric
echo $metric | nc -u ${HOST-"127.0.0.1"} ${PORT-"5111"} -c
}
while getopts ":h:p" opt
do
case $opt in
h) HOST=$OPTARG;;
p) PORT=$OPTARG;;
*) usage;;
esac
done
while true
do
emit
sleep 0.5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment