mirror of
https://codeberg.org/hyperreal/bin
synced 2024-11-01 08:33:06 +01:00
Add stuff
This commit is contained in:
parent
8077719a96
commit
909594bbd0
@ -7,7 +7,7 @@ send_matrix_webhook() {
|
||||
/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
|
||||
if ! grep "/srv/backup" /etc/mtab >/dev/null; then
|
||||
|
22
btrfs-snapshot
Executable file
22
btrfs-snapshot
Executable 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
16
http-status-note
Executable 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
49
send-matrix-webhook
Executable 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)
|
Loading…
Reference in New Issue
Block a user