Compare commits
4 commits
704f7ecb0c
...
89ef16ca80
| Author | SHA1 | Date | |
|---|---|---|---|
| 89ef16ca80 | |||
| 72f0365b4b | |||
| db5720dfe3 | |||
| 56e8ec388b |
8 changed files with 140 additions and 88 deletions
|
|
@ -6,7 +6,7 @@
|
|||
# --- $2 = Database location on VPS, e.g. "thomas@systemsobscure.net:/home/thomas/backups/forgejo/*.sql.gz"
|
||||
# --- $3 = Backup location path on VPS, e.g. "/vps_backups/mysql/forgejo/"
|
||||
|
||||
source "$HOME/.env"
|
||||
source /home/thomas/.env
|
||||
|
||||
LOCAL_MOUNTPOINT="/media/my-passport"
|
||||
RCHAT_NOTIFIER="${HOME}/repos/utilities/rocketchat_notifier.sh"
|
||||
61
deploy_eolas.sh
Executable file
61
deploy_eolas.sh
Executable file
|
|
@ -0,0 +1,61 @@
|
|||
#!/bin/bash
|
||||
|
||||
source /home/thomas/.env
|
||||
|
||||
SOURCE="$(whoami)@$(hostname) ($(hostname -i | awk '{print $1}'))"
|
||||
EOLAS_DIR="/home/thomas/repos/eolas"
|
||||
EOLAS_DB_EXECUTABLE="/usr/local/bin/eolas-db/app"
|
||||
|
||||
function notify() {
|
||||
message=""
|
||||
if [ "$1" == 'error' ]; then
|
||||
message="🟥 $2"
|
||||
else
|
||||
message="🟩 $2"
|
||||
fi
|
||||
|
||||
curl -s -u thomas:${NTFY_PASSWORD} \
|
||||
-H "Tags: $SOURCE" \
|
||||
-d "${message} " \
|
||||
https://ntfy.systemsobscure.net/eolas
|
||||
}
|
||||
|
||||
echo "TB-INFO Running script deploy_eolas.sh"
|
||||
|
||||
cd "$EOLAS_DIR" || exit
|
||||
|
||||
${EOLAS_DIR}/scripts/upload_images_to_s3.sh
|
||||
|
||||
echo "TB-INFO Uploaded images to S3"
|
||||
|
||||
echo "TB-INFO Generating database"
|
||||
sudo ${EOLAS_DB_EXECUTABLE} --source="${EOLAS_DIR}/zk" --target="/tmp"
|
||||
|
||||
db_size=$(stat --printf="%s" /tmp/eolas.db | awk '{printf "%.2f", $1/1000000}')
|
||||
|
||||
echo "TB-INFO Stopping eolas-api on VPS"
|
||||
|
||||
ssh -T -i /home/thomas/.ssh/deploy_self_host_server deploy@systemsobscure.net <<'EOF'
|
||||
sudo systemctl stop eolas-api.service
|
||||
EOF
|
||||
|
||||
sudo scp -i /home/thomas/.ssh/deploy_self_host_server /tmp/eolas.db deploy@systemsobscure.net:/data/sqlite/eolas >/dev/null
|
||||
|
||||
echo "TB-INFO scp new database file to VPS"
|
||||
|
||||
ssh -T -i /home/thomas/.ssh/deploy_self_host_server deploy@systemsobscure.net <<'EOF'
|
||||
sudo systemctl start eolas-api.service
|
||||
EOF
|
||||
|
||||
echo "TB-INFO Restarting eolas-api on VPS"
|
||||
|
||||
bucket_info=$(aws --profile garage --endpoint-url https://s3.systemsobscure.net s3 ls --summarize --human-readable s3://eolas/ | tail --lines=2 | tr -ds '\n' " ")
|
||||
|
||||
last_commit=$(cd "$EOLAS_DIR" && git rev-parse HEAD)
|
||||
last_commit_trimmed=$(echo "$last_commit" | cut -b -6)
|
||||
|
||||
notify 'success' "eolas deployed at commit ${last_commit_trimmed}.
|
||||
Database size: ${db_size}MB
|
||||
Image bucket info: $bucket_info
|
||||
https://forgejo.systemsobscure.net/thomasabishop/eolas/commit/${last_commit}
|
||||
" >/dev/null
|
||||
78
ping-pihole.py
Normal file
78
ping-pihole.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import base64
|
||||
import getpass
|
||||
import os
|
||||
import socket
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import requests
|
||||
|
||||
PIHOLE_IP = "192.168.68.53"
|
||||
PIHOLE_PASSWORD = os.environ.get("PIHOLE_PASSWORD")
|
||||
HOSTNAME = socket.gethostname()
|
||||
USER = getpass.getuser()
|
||||
NTFY_URL = "https://ntfy.systemsobscure.net/homelab"
|
||||
NTFY_PASSWORD = os.environ.get("NTFY_PASSWORD")
|
||||
NTFY_ENCODED_AUTH = f"Basic {base64.b64encode(f'thomas:{NTFY_PASSWORD}'.encode('utf-8')).decode('utf-8')}"
|
||||
HOST_IP = socket.gethostbyname(HOSTNAME)
|
||||
|
||||
|
||||
def get_authorisation_token():
|
||||
r = requests.post(
|
||||
f"http://{PIHOLE_IP}/api/auth",
|
||||
json={"password": PIHOLE_PASSWORD},
|
||||
)
|
||||
return r.json()["session"]["sid"]
|
||||
|
||||
|
||||
def get_stats(headers):
|
||||
r = requests.get(f"http://{PIHOLE_IP}/api/stats/summary", headers=headers)
|
||||
return r.json()["queries"]
|
||||
|
||||
|
||||
def convertUptime(seconds):
|
||||
duration = timedelta(seconds=seconds)
|
||||
days = duration.days
|
||||
hours = duration.seconds // 3600
|
||||
minutes = (duration.seconds % 3600) // 60
|
||||
secs = duration.seconds % 60
|
||||
since = datetime.now() - duration
|
||||
|
||||
return {
|
||||
"days": days,
|
||||
"hours": hours,
|
||||
"minutes": minutes,
|
||||
"seconds": secs,
|
||||
"since": since.strftime("%a %d %b %Y %H:%M:%S"),
|
||||
}
|
||||
|
||||
|
||||
def get_uptime(headers):
|
||||
r = requests.get(f"http://{PIHOLE_IP}/api/info/system", headers=headers)
|
||||
uptime_in_secs = r.json()["system"]["uptime"]
|
||||
return convertUptime(uptime_in_secs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
ntfy_headers = {
|
||||
"Authorization": NTFY_ENCODED_AUTH,
|
||||
"Tags": f"{USER}@{HOSTNAME}({HOST_IP})",
|
||||
}
|
||||
|
||||
try:
|
||||
auth_token = get_authorisation_token()
|
||||
headers = {"X-FTL-SID": auth_token}
|
||||
stats = get_stats(headers)
|
||||
uptime = get_uptime(headers)
|
||||
message = f"""🟩 pihole@{PIHOLE_IP} is up.\nTotal queries blocked: {stats["blocked"]} of {stats["total"]} ({round(stats["percent_blocked"])}%)\nUptime: {uptime["days"]} days, {uptime["hours"]} hours, {uptime["minutes"]} minutes (since {uptime["since"]})
|
||||
"""
|
||||
requests.post(
|
||||
NTFY_URL,
|
||||
data=message,
|
||||
headers=ntfy_headers,
|
||||
)
|
||||
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
|
||||
requests.post(
|
||||
NTFY_URL, data=f"🟥 pihole@{PIHOLE_IP} is down.", headers=ntfy_headers
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"TB-ERROR: Error fetching Pihole stats and status: {e}")
|
||||
|
|
@ -1,87 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Send error and success notifications to Slack channels
|
||||
|
||||
# Env vars:
|
||||
# --- Webhook URLs for given channel, eg $SLACK_WEBHOOK_TEST, $SLACK_WEBHOOK_EOLAS
|
||||
# --- sourced from `.env` file in Zsh path
|
||||
|
||||
# Parameters:
|
||||
# --- $1 = Slack channel,
|
||||
# --- $2 = type 'error' | 'success'
|
||||
# --- $3 = Message
|
||||
# --- $4 = (Opt) Error details
|
||||
# --- $5 = (Opt) Error source
|
||||
|
||||
# Usage:
|
||||
# --- ./slack_notifier.sh test 'SUCCESS: ...'
|
||||
# --- ./slack_notifier.sh test 'ERROR: ... ' 'Error details' 'source'
|
||||
|
||||
declare -A CHANNEL_TO_WEBHOOK
|
||||
CHANNEL_TO_WEBHOOK["test"]=$SLACK_WEBHOOK_TEST
|
||||
CHANNEL_TO_WEBHOOK["backups"]=$SLACK_WEBHOOK_BACKUPS
|
||||
CHANNEL_TO_WEBHOOK["eolas"]=$SLACK_WEBHOOK_EOLAS
|
||||
CHANNEL_TO_WEBHOOK["website"]=$SLACK_WEBHOOK_SYSTEMS_OBSCURE
|
||||
CHANNEL_TO_WEBHOOK["time-tracking"]=$SLACK_WEBHOOK_TIME_TRACKING
|
||||
|
||||
WEBHOOK=${CHANNEL_TO_WEBHOOK[$1]}
|
||||
|
||||
ERROR_BLOCKS=$(
|
||||
jq -n \
|
||||
--arg channel "$1" \
|
||||
--arg message "$3" \
|
||||
--arg details "$4" \
|
||||
--arg source "$5" \
|
||||
'{
|
||||
channel: $channel,
|
||||
blocks: ([
|
||||
{
|
||||
type: "section",
|
||||
text: {
|
||||
type: "plain_text",
|
||||
text: "🔴 \($message)"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "section",
|
||||
text: {
|
||||
type: "mrkdwn",
|
||||
text: "```\n\($details)\n```"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "context",
|
||||
"elements": [
|
||||
{
|
||||
"type": "plain_text",
|
||||
text: $source
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
}'
|
||||
)
|
||||
|
||||
# Initialise sound playback
|
||||
|
||||
# mpv --volume=0 --start=0 --length=0.1 "${HOME}/dotfiles/sounds/star-trek-computer-success.mp3" \
|
||||
# >/dev/null 2>&1
|
||||
# sleep 1
|
||||
|
||||
# Process notification
|
||||
if [ "$2" != "error" ]; then
|
||||
curl -X POST \
|
||||
-H 'Content-type: application/json' \
|
||||
--data '{"text":"🟢 '"$3"'"}' \
|
||||
"$WEBHOOK"
|
||||
# mpv --volume=100 "${HOME}/dotfiles/sounds/star-trek-computer-success.mp3" \
|
||||
>/dev/null 2>&1
|
||||
|
||||
else
|
||||
curl -X POST \
|
||||
-H 'Content-type: application/json' \
|
||||
--json "$ERROR_BLOCKS" \
|
||||
"$WEBHOOK"
|
||||
# mpv --volume=100 "${HOME}/dotfiles/sounds/star-trek-computer-error.mp3" \
|
||||
>/dev/null 2>&1
|
||||
fi
|
||||
Loading…
Add table
Reference in a new issue