spacestatus/spacestatus.sh

78 lines
1.6 KiB
Bash
Raw Normal View History

2018-11-29 19:50:26 +01:00
#!/usr/bin/env bash
[ ! -z "$DEBUG" ] && set -x
2018-11-29 22:55:34 +01:00
#set -eu
STATUS_HTML=/var/tmp/spacestatus.html
STATUS_JSON=/var/tmp/spacestatus.json
2018-11-29 19:50:26 +01:00
function write_html {
2018-11-29 23:04:36 +01:00
status=$1
TEMPLATE_HTML='<!DOCTYPE html><html><head><title>Status /usr/space: %s</title></head><body><p><code style="color: %s">%s</code></p></html>'
CLOSED_HTML='Closed darkred Closed'
OPEN_HTML='Open brightgreen Open'
if [ "$status" -gt 0 ]
then
printf "$TEMPLATE_HTML" $OPEN_HTML > $STATUS_HTML
else
printf "$TEMPLATE_HTML" $CLOSED_HTML > $STATUS_HTML
fi
2018-11-29 19:50:26 +01:00
}
function write_json {
2018-11-29 23:04:36 +01:00
status=$1
TEMPLATE_JSON=$( cat /usr/local/etc/spacestatus.json )
CLOSED_JSON='false'
OPEN_JSON='true'
2018-11-29 19:50:26 +01:00
2018-11-29 22:55:34 +01:00
# Previous status Open & Open now -> skip
grep -q '"open":true' $STATUS_JSON
2018-11-29 23:04:36 +01:00
if [[ $? -eq 0 && "$status" -gt 0 ]]
2018-11-29 22:55:34 +01:00
then
return
fi
# Previous status Closed & Closed now -> skip
grep -q '"open":false' $STATUS_JSON
2018-11-29 23:04:36 +01:00
if [[ $? -eq 0 && "$status" -eq 0 ]]
2018-11-29 22:55:34 +01:00
then
2018-11-29 23:04:36 +01:00
return
2018-11-29 22:55:34 +01:00
fi
2018-11-29 23:04:36 +01:00
if [ "$status" -gt 0 ]
then
printf "$TEMPLATE_JSON" $OPEN_JSON $( date +%s ) > $STATUS_JSON
else
printf "$TEMPLATE_JSON" $CLOSED_JSON $( date +%s ) > $STATUS_JSON
fi
2018-11-29 19:50:26 +01:00
}
function publish {
CURL=/usr/bin/curl
WEBDAV=https://example.com
CREDS='user:pass'
${CURL} --user "${CREDS}" -T "${STATUS_HTML}" "${WEBDAV}"
${CURL} --user "${CREDS}" -T "${STATUS_JSON}" "${WEBDAV}"
}
2018-11-29 19:50:26 +01:00
tmpcount=$(mktemp)
exec 3>$tmpcount
/usr/sbin/dhcp-lease-list --parsable | awk '{print $4}' | while read ip
do
2018-11-29 23:04:36 +01:00
/usr/bin/arping -w 1 -f -q $ip
[ $? -eq 0 ] && echo 1 >&3
2018-11-29 19:50:26 +01:00
done
exec 3>&-
leases=$( cat $tmpcount | wc -l )
write_html $leases
2018-11-29 22:55:34 +01:00
write_json $leases
2018-11-29 19:50:26 +01:00
publish
2018-11-29 19:50:26 +01:00
rm $tmpcount