Bash HTTP Monitoring Dashboard
raymii.org
raymii.org
Related; here's an alias that I frequently use:
alias hstat="curl -o /dev/null --silent --head --write-out '%{http_code}\n'" $1
Example:$ hstat www.google.com
200
curl -s -w 'Testing Website Response Time for :%{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nAppCon Time:\t\t%{time_appconnect}\nRedirect Time:\t\t%{time_redirect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n' -o /dev/null https://google.com
Here's the explanation of each timestamp: https://blog.cloudflare.com/a-question-of-timing/ hstat(){ exec 3<>/dev/tcp/$1/80;
echo -e "GET / HTTP/1.1\r\nhost: http://$1\r\nconnection: close\r\n\r\n" >&3;
sed -n '1s/^HTTP\/1\.[01] //;s/ .*//;p' <&3;}
no bash, only busybox hstat(){ echo -e "GET / HTTP/1.1\r\nhost: http://$1\r\nconnection: close\r\n\r\n" \
|busybox nc $1 80|busybox sed "1s/^HTTP\/1\.[10] //;s/ .*//;q";}I get not using curl is a cool and fun challenge, but outside that, there's very little reason to not just rely on curl.
Is TLSv1.3 ok?
hstat(){
X="GET / HTTP/1.1\r\n"
Y="Host: $1\r\n"
Z="Connection: close\r\n\r\n"
cat << eof|stunnel -fd 0
pid = /tmp/1.pid
[hstat]
client = yes
accept = 127.0.0.1:1337
connect = $1:443
sni = $1
sslVersionMin = TLSv1.3
eof
# no curl, only bash
exec 3<>/dev/tcp/127.0.0.1/1337
printf "$X$Y$Z" >&3;sed '1s/^HTTP\/1.[01] //;1s/ .*//;q' <&3;
# no bash, only busybox
printf "$X$Y$Z"|busybox nc 127.0.0.1 1337|busybox sed '1s/^HTTP\/1.[01] //;1s/ .*//;q';
read x < /tmp/1.pid;kill -9 $x;rm /tmp/1.pid;
}
A careful observer would note that the original parent comment did not use HTTPS. "curl www.example.com" does not automatically transform the method to https nor does Google automatically redirect to HTTPS.cURL is a very popular program. However it is not the program I use on a daily basis. It has a gazillion features (HTTP 1.1 pipelining is still not among them), but it's overkill for the tasks I have. I use other programs like netcat, tcpclient, socat, openssl, stunnel, haproxy, etc. along with custom utilities for generating HTTP I wrote for myself. When I use an http client, I use tnftp or fetch, even wget or lftp, before I will use curl. I only use curl in examples on HN because it is what everyone is familiar with.
(Aside: most of the teams in this competition were from IT/sysadmin programs. Our team was entirely computer science students, with no formal sysadmin training. We managed to win the national CCDC in 2011 and 2012.[3])
[0]: https://github.com/cemeyer/ghettonagios
[1]: https://github.com/cemeyer/ghettonagios/blob/master/SCREENSH...
[2]: https://www.nationalccdc.org/
[3]: https://www.nationalccdc.org/index.php/competition/about-ccd...
Another one that I really liked and even have this crazy idea of reviving as a side project mayhaps is Argus (tcp4me)... it was written in perl and was my main intro to the beautiful hell that is perl. These days though between sensu, prometheus, zabbix, and nagios, we really have plenty of good monitoring options.
It's also very useful and powerful for any other monitoring. I even have it checking for ZFS problems, failed fans and high temperatures in my DAS shelves, and all the standard service monitoring.
Do the checks run in parallel?
This isn’t really true, from an strace it also execs cat, wc, date, echo, mkdir, mktemp, and rmdir.
I ran a Docker container and copied a statically-linked curl and bash onto a scratch image and ran it, it does not work:
$ docker run -it --rm $(docker build -q .)
/srvmon.sh: line 162: date: command not found
$ cat Dockerfile
FROM scratch
COPY bash /usr/bin/bash
COPY curl /usr/bin/curl
COPY srvmon.sh /srvmon.sh
CMD ["/usr/bin/bash", "/srvmon.sh"]
I'm not pointing this out to be pedantic. I'm pointing it out because there is a common misconception that all these things like "date" and "cat" and "mkdir" are part of Bash, but they're not, they're part of coreutils and there are dramatically different versions of coreutils on different installations of Linux, macOS, BSD, etc., and some environments (like barebones Docker containers) don't have coreutils at all.I care about this because I deal with it all the time. Coreutils programs have pretty different behavior across distributions and OSs, we shouldn’t sweep implicit dependencies under the rug.