Skip to content

Instantly share code, notes, and snippets.

@ojan
Last active August 29, 2015 14:17
Show Gist options
  • Save ojan/ca4103a3302b08c8fa0c to your computer and use it in GitHub Desktop.
Save ojan/ca4103a3302b08c8fa0c to your computer and use it in GitHub Desktop.
#!/bin/bash
# Parse the Checkmy Website API JSON output and feed it into Graphite
# Expect a Check my Website ID as argument
# echo "foo 1 timestamp" | nc -u -w 1 127.0.0.1 2003
# url https://api.checkmy.ws/api/status/ea726685-9b45...
# Dependencies
# Jq version 1.4 mininum : http://stedolan.github.io/jq/
# Netcat
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# VARIABLES
GRAPHITE_URL=127.0.0.1
GRAPHITE_PORT=2003
CURL=$(which curl)
NC=$(which netcat)
JQ=$(which jq)
NC_OPTIONS="${NC} -u -w 1"
NC_CMD="${NC_OPTIONS} ${GRAPHITE_URL} ${GRAPHITE_PORT}"
JQ_CMD="${JQ} -c"
API_URL="https://api.checkmy.ws/api/status/$1"
GRAPHITE_PREFIX="cmws"
TS=$(date +%s)
# Get new data from Check my Website API
JSON_INPUT=$(${CURL} ${API_URL})
echo ${JSON_INPUT}
WEBSITE_URL=$(echo ${JSON_INPUT} | ${JQ_CMD} .url | cut -d/ -f2- | cut -d/ -f2- | cut -d\" -f1)
## FUNCTIONS
poller_name() {
echo $1 | tr '[:upper:]' '[:lower:]' | sed s/\:/./g
}
## httptime by pollers
for KEY in $(echo ${JSON_INPUT} | ${JQ_CMD} '.lastvalues.httptime | keys' | sed 's/","/ /g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/"//g')
do
LOCATION=$(poller_name $KEY)
VALUE=$(echo ${JSON_INPUT} | ${JQ_CMD} .lastvalues.httptime.\"${KEY}\")
echo "${GRAPHITE_PREFIX}.${WEBSITE_URL}.httptime.${LOCATION} ${VALUE} ${TS}" | ${NC_CMD}
#echo ${VALUE}
echo "${GRAPHITE_PREFIX}.${WEBSITE_URL}.httptime.${LOCATION} ${VALUE} sent to Graphite"
done
## states by pollers
for KEY in $(echo ${JSON_INPUT} | ${JQ_CMD} '.states | keys' | sed 's/","/ /g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/"//g')
do
LOCATION=$(poller_name $KEY)
VALUE=$(echo ${JSON_INPUT} | ${JQ_CMD} .states.\"${KEY}\")
echo "${GRAPHITE_PREFIX}.${WEBSITE_URL}.states.${LOCATION} ${VALUE} ${TS}" | ${NC_CMD}
#echo ${VALUE}
echo "${GRAPHITE_PREFIX}.${WEBSITE_URL}.states.${LOCATION} ${VALUE} sent to Graphite"
done
# metas
for KEY in $(echo ${JSON_INPUT} | ${JQ_CMD} '.metas | keys' | sed 's/","/ /g' | sed 's/\[//g' | sed 's/\]//g' | sed 's/"//g' | sed 's/title //g')
do
VALUE=$(echo ${JSON_INPUT} | ${JQ_CMD} .metas.${KEY})
echo "${GRAPHITE_PREFIX}.${WEBSITE_URL}.metas.${KEY} ${VALUE} ${TS}" | ${NC_CMD}
#echo ${VALUE}
echo "${GRAPHITE_PREFIX}.${WEBSITE_URL}.metas.${KEY} ${VALUE} sent to Graphite"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment