Instantly share code, notes, and snippets.

@plugnburn plugnburn/mon.sh
Created Oct 6, 2016

Embed
What would you like to do?
Simplest possible monitoring solution using public etcd service and Instapush API
#!/bin/bash
# Usage
# Monitoring server: mon.sh [etcd_chan_id] [instapush_app_id] [instapush_app_secret]
# Monitoring client: echo '[event_name]#[vars_json]' | mon.sh [etcd_chan_id]
ETCD_CHANID="$1" # etcd channel id on discovery.etcd.io
INSTAPUSH_APP="$2" # instapush app id
INSTAPUSH_SECRET="$3" # instapush secret key
MSGKEY="notifikey"
MARKER="@@@"
if [[ -z "$INSTAPUSH_APP" && -z "$INSTAPUSH_SECRET" ]]; then # send mode
inp="$(< /dev/stdin)"
curl https://discovery.etcd.io/${ETCD_CHANID}/${MSGKEY} -XPUT -d value="${MARKER}${inp}${MARKER}"
else # watch mode
while true; do
rawData="$(curl -s https://discovery.etcd.io/${ETCD_CHANID}/${MSGKEY}?wait=true)"
data="$(echo $rawData|grep -Po \"${MARKER}.*?${MARKER}\"|head -n 1)"
rawInstructions="${data:4:-4}"
eventName=$(echo $rawInstructions | cut -d '#' -f 1)
if [[ -n "$eventName" ]]; then
varSection=$(echo $rawInstructions | cut -d '#' -f 2- | { read vs;echo $vs; })
reqBody="{\"event\":\"$eventName\",\"trackers\":$varSection}"
curl -s -X POST -H "x-instapush-appid: $INSTAPUSH_APP" -H "x-instapush-appsecret: $INSTAPUSH_SECRET" -H "Content-Type: application/json" -d $reqBody https://api.instapush.im/v1/post &
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment