first scripts uploaded

This commit is contained in:
andrea.noni 2023-09-19 14:45:14 +02:00
parent cc21cfad3c
commit 10d44f9234
4 changed files with 164 additions and 0 deletions

76
borg_HOSTNAME.sh Executable file
View File

@ -0,0 +1,76 @@
#!/bin/bash
### I lost the link where I copied this script from, sorry, i'll update as soon as I find it back!
## This scripts checks for the last succesful BORG backups of a HOST, and alerts ntfy in case no succesful backup has been created for the last X days
backup_host="HOST_NAME"
n_days=3 # if backup was less recent than n_days, alert administrator
topicurl=https://NTFY_SERVER_IP/borg_alerts
make_it_fail=0 # set to 1 to test your alerts by simulating an old backup
export BORG_REPO=/mnt/BORG/$backup_host # point to your borg repo
export BORG_REMOTE_PATH=/usr/bin/borg # point to your borg executable
#export BORG_PASSPHRASE='redacted'
die()
{
echo "Error fetching or calculating date"
curl -k --retry 3 \
-d "Error fetching or calculating date" \
-H "Title: Borg on $backup_host" \
-H "Priority: high" \
-H "Tags: warning,boar" \
$topicurl
exit 1
}
is_num() # returns (exits) 0 if passed a valid integer
{
if (( $1 == $1 )) 2>/dev/null; then
return 0
else
return 1
fi
}
last=`borg list --last 10 --format "{archive} {end} {NEWLINE}" | grep --invert-match checkpoint | tail -1` # get the last 10 backups from the repo, filter out checkpoints, get last one
if (( $make_it_fail )); then
last="qwho-1989-05-08T11:11:11 Thu, 1989-05-08 11:11:11" # simulate an old backup
fi
echo Last backup: $last
last_date=$( echo $last | cut -d " " -f 3 )
if [[ -z $last_date ]]; then
die # didn't get a string
fi
last_timestamp=$( date --date=$last_date +%s 2>/dev/null )
now_timestamp=$( date +%s )
if ! ( ( is_num $last_timestamp ) && ( is_num $now_timestamp ) ); then
die # invalid timestamp
fi
days_ago=$( echo "($now_timestamp - $last_timestamp) / (3600 * 24)" | bc )
if ! ( ( is_num $days_ago ) && (( $days_ago >= 0 )) ); then
die # invalid or negative result
fi
# Sums successful - check backup time and send appropriate ntfy alert
if (( $days_ago < $n_days )); then
echo Good - last backup was $days_ago days ago - less than the $n_days day limit
echo Not sending alert to ntfy server...
else
echo Bad - last backup was $days_ago days ago - greater than the $n_days day limit
echo Sending alert...
curl -k --retry 3 \
-d "Bad - last backup was $days_ago days ago - greater than the $n_days day limit" \
-H "Title: Borg on $backup_host" \
-H "Priority: high" \
-H "Tags: warning,boar" \
$topicurl
fi

21
clouds_check.sh Executable file
View File

@ -0,0 +1,21 @@
#!/bin/bash
### this scripts checks if the login page of a nextcloud instance is reacheable, and alerts ntfy in case it cannot receive a "200 OK" response from the webserver
# written by tech_at_hangar.org
HOSTS="https://cloud.EXAMPLE.ORG/index.php,https://cloud.CHANGE.ME/index.php,https://nuvol.EIXAMPLE.CAT/index.php"
topicurl=https://172.26.0.15/cloud_alerts
for i in ${HOSTS//,/ }
do
RESPONSE=$(wget --no-check-certificate "$i" 2>&1 | grep -wic "200 OK")
if [ ${RESPONSE} != 1 ]; then
curl -k --retry 3 \
-d "Cloud $i not online" \
-H "Title: Cloud $i has some problems" \
-H "Priority: high" \
-H "Tags: warning,boar" \
$topicurl
fi
done

47
disks_checks.sh Executable file
View File

@ -0,0 +1,47 @@
#!/bin/bash
### I lost the link where I copied this script from, sorry, i'll update as soon as I find it back! The btrfs raid1 check part is written by tech_at_hangar.org
## The first part of this script checks for given minimum space on a mounted volume, and contacts ntfy in case the limit is surpassed.
## The second part of this script checks for missing disks on btrfs raid1 devices and contacs ntfy in case of missing devices
# root volume
mingigs=10
avail=$(df | awk '$6 == "/" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }')
topicurl=https://NTFY_SERVER_IP/disk_alerts
if [ -n "$avail" ]; then
curl -k --retry 3 \
-d "Only $avail GB available on the root disk. Better clean that up." \
-H "Title: Low disk space alert on $(hostname)" \
-H "Priority: default" \
-H "Tags: warning,cd" \
$topicurl
fi
# OTHER VOLUME NAME
mingigs=500
avail=$(df | awk '$6 == "/mnt/OTHER_VOLUME" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }')
topicurl=https://NTFY_SERVER_IP/disk_alerts
if [ -n "$avail" ]; then
curl -k --retry 3 \
-d "Only $avail GB available on the /mnt/OTHER_VOLUME disk. Better clean that up." \
-H "Title: Low disk space alert on $(hostname)" \
-H "Priority: default" \
-H "Tags: warning,cd" \
$topicurl
fi
# check btrfs raid 1 status on /mnt/OTHER_VOLUME NAME
if (btrfs fi show | grep -wic "Some devices missing"); then
echo Bad: - Btrfs is missing devices, some disk is broken
echo Sending alert...
curl -k --retry 3 \
-d "Bad - Some devices are missing in btrfs: maybe a broken disk?" \
-H "Title: Missing disks in btrfs on $(hostname)" \
-H "Priority: high" \
-H "Tags: warning,boar" \
$topicurl
fi

20
ping_check.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/bash
### This script checks for available ping response from HOST_IP and alerts ntfy in case they're not responding
HOSTS="IP_HOST_1,IP_HOST_2,IP_HOST_3"
topicurl=https://NTFY_SERVER_IP/net_alerts
for i in ${HOSTS//,/ }
do
LAST_PINGS=$(ping -c 5 "$i" | grep -wic "Unreachable")
if [ ${LAST_PINGS} != 0 ]; then
curl -k --retry 3 \
-d "Ping lost with $i" \
-H "Title: Ping lost with $i" \
-H "Priority: high" \
-H "Tags: warning,boar" \
$topicurl
fi
done