Add stuff

This commit is contained in:
Jeffrey Serio 2023-12-30 21:04:27 -06:00
parent 8077719a96
commit 909594bbd0
4 changed files with 88 additions and 1 deletions

View File

@ -7,7 +7,7 @@ send_matrix_webhook() {
/usr/local/bin/send-matrix-webhook "btrfs-backup failed on $(hostname)" /usr/local/bin/send-matrix-webhook "btrfs-backup failed on $(hostname)"
} }
trap send_matrix_webhook SIGINT SIGTERM EXIT trap send_matrix_webhook SIGINT SIGTERM
# Check if device is mounted # Check if device is mounted
if ! grep "/srv/backup" /etc/mtab >/dev/null; then if ! grep "/srv/backup" /etc/mtab >/dev/null; then

22
btrfs-snapshot Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
LOGFILE="/var/log/btrfs-snapshot.log"
SNAP_DATE=$(date '+%Y-%m-%d_%H%M%S')
send_matrix_webhook() {
/usr/local/bin/send-matrix-webhook "btrfs-snapshot failed on $(hostname)"
}
trap send_matrix_webhook SIGINT SIGTERM EXIT
create_snapshot() {
if ! btrfs subvolume snapshot -r "$1" "${1}/.snapshots/$2-$SNAP_DATE" >/dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error creating snapshot of $1" | tee -a "$LOGFILE"
notify-send -i computer-fail "Error creating snapshot of $1"
exit 1
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Create snapshot of $1: OK" | tee -a "$LOGFILE"
fi
}
create_snapshot "/home" "home"

16
http-status-note Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
source /usr/local/etc/matrix-webhook-env
for url in "${site_urls[@]}"; do
status_code=$(curl -o /dev/null -s -w "%{http_code}" "$url")
if [ "$status_code" = 200 ] && ! [ "$status_code" == "302" ]; then
if ! /usr/local/bin/send-matrix-webhook "$room_id" "$url: ✅ UP"; then
echo "Error sending webhook to Matrix"
exit 1
fi
else if ! /usr/local/bin/send-matrix-webhook "$room_id" "$url: ❌ DOWN"; then
echo "Error sending webhook to Matrix"
exit 1
fi
done

49
send-matrix-webhook Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env python
import json
import sys
import requests
def send_matrix_webhook(access_token, room_id, message, user_id_to_ping, matrix_server):
"""
Send a message to a Matrix room using a webhook.
:param access_token: The access token for Matrix API authentication.
:param room_id: The ID of the room where the message will be sent.
:param message: The message to be sent.
:param user_id_to_ping: The user to ping in the message.
:param matrix_server: The URL of the Matrix homeserver.
"""
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
full_message = f"{user_id_to_ping}: {message}"
data = {
"msgtype": "m.text",
"body": full_message,
"format": "org.matrix.custom.html",
"formatted_body": f'<a href="https://matrix.to/#/{user_id_to_ping}">{user_id_to_ping}</a>: {message}',
}
url = f"{matrix_server}/_matrix/client/r0/rooms/{room_id}/send/m.room.message"
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print("Message sent successfully.")
else:
print(f"Failed to send message. Status code: {response.status_code}")
if __name__ == "__main__":
access_token = ""
room_id = sys.argv[1]
message = sys.argv[2]
user_id_to_ping = ""
matrix_server = ""
send_matrix_webhook(access_token, room_id, message, user_id_to_ping, matrix_server)