Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
watches a URL and logs it
[Unit]
Description=Network Status Monitor
After=multi-user.target
[Service]
Type=idle
WorkingDirectory=/home/pi/
ExecStart=/bin/bash -c '/usr/local/bin/net-check.sh 15 https://gist.github.com/RichardBronosky/dd13161b72284c209c5b80d88f9cdb4a/raw/acdb3e2a6e3a55055aef16580e4e613e819d1f37/random-1m.txt >> /home/pi/net-check.log'
[Install]
WantedBy=multi-user.target
#!/bin/bash
# Usage: net-check.sh <frequency_in_seconds> <url_to_check>
# ARG1: frequency defaults to 1 second
frequency=${1:-1}
# ARG2: url defaults to a 1MB file in this repo
url="${2:-https://gist.github.com/RichardBronosky/dd13161b72284c209c5b80d88f9cdb4a/raw/acdb3e2a6e3a55055aef16580e4e613e819d1f37/random-1m.txt}"
# for 1k every 5 seconds: net-check.sh 5 https://gist.github.com/RichardBronosky/dd13161b72284c209c5b80d88f9cdb4a/raw/acdb3e2a6e3a55055aef16580e4e613e819d1f37/random-1k.txt
# Assemble curl options once
write_out=""
for stat in http_code time_total time_namelookup time_connect size_download speed_download; do
write_out="${write_out}\"$stat\": \"%{$stat}\", "
done
check(){
date="\"date\": \"$(date +"%Y-%m-%dT%H:%M:%S%z")\", "
http_stats="$(curl -fsL -m 10 -o /dev/null -w "$write_out" $url | sed -E 's/\.0+"/"/' | tr -d '\n'; echo -n "\"exit_code\": \"${PIPESTATUS[0]}\"")"
echo "{${date}${http_stats}}"
}
main(){
while true; do
check &
sleep $frequency
done
}
main
1AgkRQfPAsf+V72Pa7lxOPfIakltYfnlIC94p9gZRfWoQnnZGCgQS+MGfhBiZmnvVgZvWvZUOpKq4b1Qwhp1uOACxeOQO3KEZoEK/mI9cWHrKq//dpVz78hdPTrRvN6+5HpBfAq+nrY49/Kw8jKyEUQbhWUrQMmRlLFanBcdzWeLKltjDpHgFGltoA/qZOsXgZ4iIiEqjspe/FvHVV0L3T5F2r8lpDlPPrPKOsRT61/ONujCzUum3i053Uh4ZOFPiUyTRWBG4OYDQdqXQd9j4A01ge4fVCbNgW1zEpPqdUr4u9BjoGzcyQElWw3cV+bd8WP+Rr6ggEiGCwUEcNZtYLk33UK/yht3TQ6bOrKAGbLqVv4bjBnXypLDKvlC3Ew2TbVVhqP0NH7oXRcutSewzKmtvvIYpOrf4AJ2qmivSIlu1853xSVYzctXAexYpuNNLs4JsQjIbbxd5tGueK8CtLUqqTczhEJYbxvRcgAQinszGXnWcBToJApfHeFrpO+6Ss/JrZHnlRdeYY8ApyjqnTSuto3WXGiWpgCkC9+NrEB42onOOtIJnO+gj8JsbeDL6c2WwQ8/A+HKCcKATLTKw/OJJhFBKiKYjWb2oHboTcU3XZ0Wx9K/UBsjRbA1/PECPmJ6pj/JLul7fWqDdlYDe1BrTW3HTuS77PsmXE8E6mv4RLutfOjgNGXRDZEVsYqHimq+t9M4OILxX/8hjmYFhq7zVEHydMnyVt53n0K/51yLFXx+ThVUlA9CHnr2i7DnOp5gtUmOqtessVfsdYEAew3bYfWBIzoIl7olATrJlg8CWrZWUOup1PKMP6PfDIrLvPgBfSKtzJvuGe7Pi5oG94iZlzSyWq1QbrRA9xwmwJmL3fcjf8Bius3LyInHZbxF8UYW42Y8PI5MaWUp+YsPM7pPZVmBG1I/cqqm2Ea2qdI7mZMsCt7Q5MsZgdHbp/lV1DG+L+vg7E6RVwk8pKPeb6bDXuZJWvrpYb+Y7dpp26xpz2qxq7qJVqslNjF7k9vY
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

@RichardBronosky

This comment has been minimized.

Copy link
Owner Author

RichardBronosky commented Jan 27, 2019

Random files created via:

cat /dev/urandom | base64 | dd bs=1k count=1    > random-1k.txt
cat /dev/urandom | base64 | dd bs=1k count=1024 > random-1m.txt
@RichardBronosky

This comment has been minimized.

Copy link
Owner Author

RichardBronosky commented Jan 27, 2019

How to test this on macOS

To block access to gist.github.com and cause a fake network outage

for ip in $(dig gist.github.com +short); do sudo route -n add $ip/32 127.0.0.1; done

To restore access to gist.github.com

for ip in $(dig gist.github.com +short); do sudo route -n delete $ip/32 127.0.0.1; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.