Fix state change

This commit is contained in:
Peter 2018-11-29 23:04:36 +01:00
parent 029397c2d4
commit d84ba6a6a4
Signed by: pludi
GPG Key ID: CFBA360E696EDC99
1 changed files with 28 additions and 28 deletions

View File

@ -5,55 +5,55 @@
#set -eu #set -eu
function write_html { function write_html {
status=$1 status=$1
STATUS_HTML=/srv/www/spacestatus.html STATUS_HTML=/srv/www/spacestatus.html
TEMPLATE_HTML='<!DOCTYPE html><html><head><title>Status /usr/space: %s</title></head><body><p><code style="color: %s">%s</code></p></html>' 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' CLOSED_HTML='Closed darkred Closed'
OPEN_HTML='Open brightgreen Open' OPEN_HTML='Open brightgreen Open'
if [ "$status" -gt 0 ] if [ "$status" -gt 0 ]
then then
printf "$TEMPLATE_HTML" $OPEN_HTML > $STATUS_HTML printf "$TEMPLATE_HTML" $OPEN_HTML > $STATUS_HTML
else else
printf "$TEMPLATE_HTML" $CLOSED_HTML > $STATUS_HTML printf "$TEMPLATE_HTML" $CLOSED_HTML > $STATUS_HTML
fi fi
} }
function write_json { function write_json {
status=$1 status=$1
STATUS_JSON=/srv/www/spacestatus.json STATUS_JSON=/srv/www/spacestatus.json
TEMPLATE_JSON=$( cat /usr/local/etc/spacestatus.json ) TEMPLATE_JSON=$( cat /usr/local/etc/spacestatus.json )
CLOSED_JSON='false' CLOSED_JSON='false'
OPEN_JSON='true' OPEN_JSON='true'
# Previous status Open & Open now -> skip # Previous status Open & Open now -> skip
grep -q '"open":true' $STATUS_JSON grep -q '"open":true' $STATUS_JSON
if [[ $? -eq 0 && "$status" -gt 0 ]] if [[ $? -eq 0 && "$status" -gt 0 ]]
then then
return return
fi fi
# Previous status Closed & Closed now -> skip # Previous status Closed & Closed now -> skip
grep -q '"open":false' $STATUS_JSON grep -q '"open":false' $STATUS_JSON
if [[ $? -eq 0 && "$status" -eq 0 ]] if [[ $? -eq 0 && "$status" -eq 0 ]]
then then
return return
fi fi
if [ "$status" -eq 0 ] if [ "$status" -gt 0 ]
then then
printf "$TEMPLATE_JSON" $OPEN_JSON $( date +%s ) > $STATUS_JSON printf "$TEMPLATE_JSON" $OPEN_JSON $( date +%s ) > $STATUS_JSON
else else
printf "$TEMPLATE_JSON" $CLOSED_JSON $( date +%s ) > $STATUS_JSON printf "$TEMPLATE_JSON" $CLOSED_JSON $( date +%s ) > $STATUS_JSON
fi fi
} }
tmpcount=$(mktemp) tmpcount=$(mktemp)
exec 3>$tmpcount exec 3>$tmpcount
/usr/sbin/dhcp-lease-list --parsable | awk '{print $4}' | while read ip /usr/sbin/dhcp-lease-list --parsable | awk '{print $4}' | while read ip
do do
/usr/bin/arping -w 1 -f -q $ip /usr/bin/arping -w 1 -f -q $ip
[ $? -eq 0 ] && echo 1 >&3 [ $? -eq 0 ] && echo 1 >&3
done done
exec 3>&- exec 3>&-