Created
November 4, 2022 21:28
-
-
Save moqmar/c15fef5f5dc9b90ccd20a214c6c701aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# matrix-monitor.sh: a simple shell script to monitor service availability and report to a Matrix channel. | |
# Requirements: | |
# - matrix-commander (set up with --login to the room that should be used) | |
# - curl | |
# Usage: check my-service "$(curl my-service.example.org)" "<h1>Hello World</h1>" "My service unexpectedly doesn't say hello to the world." | |
TRIES=3 | |
CHECK_ONLINE=1 | |
I18N_CHECK_FAILED="Check fehlgeschlagen:" | |
I18N_CHECK_SUCCEEDED="Fehler behoben:" | |
I18N_EXPECTED="Erwartete Ausgabe:" | |
check() { | |
id="$1"; actual="$2"; expected="$3"; message="$4" | |
countfile="matrix-monitor-$id.count"; restorefile="matrix-monitor-$id.restored" | |
if [ "$actual" != "$expected" ]; then | |
# Check failed $n times | |
n=$(($(cat "$countfile" 2>/dev/null) + 1)) | |
echo "$id: $message ($n/$TRIES)" | |
if [ $n -eq $TRIES ]; then | |
# Count is exactly $TRIES, so we need to send a message that it failed! | |
msg="<details>" | |
msg="$msg<summary>🔴 <strong>$I18N_CHECK_FAILED</strong> <em>$message</em></summary>" | |
msg="$msg<div><pre><code>$actual</code></pre></div>" | |
msg="$msg<div>$I18N_EXPECTED</div><div><pre><code>$expected</code></pre></div>" | |
msg="$msg</details>" | |
matrix-commander --html -m "$msg" | |
fi | |
# Update countfile | |
echo "$n" > "$countfile" | |
rm -f "$restorefile" | |
# Fail for additional tests | |
return 1 | |
else | |
# Check succeeded | |
if [ -f "$countfile" ]; then | |
# Countfile still exists, wait until another successful check. | |
echo "$id: ok (just restored, waiting for a second successful check)" | |
touch "$restorefile" | |
rm -f "$countfile" | |
elif [ -f "$restorefile" ]; then | |
echo "$id: ok (just restored)" | |
# Restorefile exists, send a message that the service is back up. | |
msg="✅ <strong>$I18N_CHECK_SUCCEEDED</strong> <em>$message</em>" | |
matrix-commander --html -m "$msg" | |
rm -f "$restorefile" | |
else | |
echo "$id: ok" | |
fi | |
fi | |
} | |
if [ -n "$CHECK_ONLINE" ]; then | |
# Check if internet connection is available at all, otherwise just quit. | |
[ "$(curl -so /dev/null -w "%{http_code}" http://detectportal.firefox.com/canonical.html)" = "200" ] || \ | |
[ "$(curl -so /dev/null -w "%{http_code}" http://www.msftconnecttest.com/connecttest.txt)" = "200" ] || \ | |
[ "$(curl -so /dev/null -w "%{http_code}" http://clients3.google.com/generate_204)" = "204" ] || \ | |
exit 1 # We are really offline! | |
fi | |
######################################################################################################################## | |
# Test if Uptime Kuma is working | |
actual=$(curl -s https://mailbox.org/.well-known/security.txt 2>&1 | grep '^Contact:') | |
expected=$(printf 'Contact: mailto:[email protected]\n') | |
message="Statusseite nicht erreichbar oder unerwartete Antwort!" | |
check "statuspage" "$actual" "$expected" "$message" | |
# Test if port 25 is working | |
actual=$(printf 'QUIT\r\n' | timeout 10 nc smtp.mailbox.org 25 2>&1 | sed 's/smtp[0-9]*\.mailbox\.org/smtp.mailbox.org/') | |
expected=$(printf '220 smtp.mailbox.org ESMTP Postfix\r\n221 2.0.0 Bye\r\n') | |
message="SMTP-Server nicht erreichbar oder unerwartete Antwort!" | |
check "smtp-handshake" "$actual" "$expected" "$message" | |
if [ $? -eq 0 ]; then | |
# Only test SMTP certificate if port 25 is working already | |
actual=$(timeout 10 openssl s_client -showcerts -servername smtp.mailbox.org -connect smtp.mailbox.org:465 </dev/null 2>&1 | grep '^Verification') | |
expected=$(printf 'Verification: OK\n') | |
message="SMTP-Zertifikat ungültig oder keine Verbindung möglich!" | |
check "smtp-certificate" "$actual" "$expected" "$message" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment