Update changelog and switch to dynamic paths

This commit is contained in:
Robin Meier 2024-03-20 00:29:22 +01:00
parent 699a2e16e8
commit 7584422e8e
11 changed files with 101 additions and 78 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
.*_env
.last_changelog_read .last_changelog_read
storage/ storage/

View File

@ -4,6 +4,19 @@
## Changelog ## Changelog
### 2024-03-20 - Improved Structure, New State Machine, Dynamic Paths
The structure of the files was overhauled.
Config files are now in `config/`.
Please remove your old setup with `rm /root/scripts /root/logs` and follow the [README](README.md) from scratch.
If you are unwilling to follow the destroy and recreate approach, you may also move the `scripts/` folder to `admin-scripts/` aswell as the config files to their apropriate location manually (don't forget about the shutdown service).
A state machine was introduced, which saves the last message and compares it to the new one.
If the message did not change, the state machine does not forward the message for two hours before renotifying again.
This repository can now be located anywhere on your disk.
The recommended location is `~/admin-scripts`, i.e. `/root/admin-scripts` or `/home/[your-username]/admin_scripts`.
### 2024-03-16 - Initial Version ### 2024-03-16 - Initial Version
Initial commit of Admin Scripts. Initial commit of Admin Scripts.

View File

@ -8,23 +8,21 @@ The covered tasks range from file change tracking via http/ssh monitoring to zfs
## Installation ## Installation
The scripts in this repo ***must*** be checked out into `/root/scripts`. This repository can be checked out wherever you like.
This is subject to change. Typically this is `~/admin-scripts`.
The scripts should be installable wherever. The rest of this README assumes you checked out this repo into `/root/admin-scripts`.
```bash ```bash
cd /root cd
git clone ssh://git@git.mneun.ch:2222/radioelephant/admin-scripts.git scripts git clone ssh://git@git.mneun.ch:2222/radioelephant/admin-scripts.git
mkdir -p /root/logs
``` ```
If you are unable to clone via SSH, try via https. If you are unable to clone via SSH, try via https.
SSH is preferrable. SSH is preferrable.
```bash ```bash
cd /root cd
git clone https://git.mneun.ch/radioelephant/admin-scripts.git scripts git clone https://git.mneun.ch/radioelephant/admin-scripts.git
mkdir -p /root/logs
``` ```
Please also quickly read the [CHANGELOG](CHANGELOG.md) and confirm this by running `date > /root/scripts/.last_changelog_read`. Please also quickly read the [CHANGELOG](CHANGELOG.md) and confirm this by running `date > /root/scripts/.last_changelog_read`.
@ -38,18 +36,17 @@ For each script you want to use, you must copy the example and fill in your own
For example (`monitoring.sh`): For example (`monitoring.sh`):
```bash ```bash
cd /root/scripts cd ~/admin-scripts
cp config/monitoring.EXAMPLE config/monitoring cp config/monitoring.EXAMPLE config/monitoring
vim config/monitoring vim config/monitoring
``` ```
If you want to use the provided example configuration, you could symbolic link the files instead of just copying them. If you want to use the provided example configuration, you could symbolic link the files instead of just copying them.
This really only makes sense for `system_health_check`. This really only makes sense for `system_health_check` and `state_machine`.
The command for this is: The command for this is:
```bash ```bash
cd /root/scripts cd ~/admin-scripts
ln -s config/system_health_check.EXAMPLE config/system_health_check ln -s config/system_health_check.EXAMPLE config/system_health_check
``` ```
@ -59,9 +56,10 @@ To be notified immediately before sever shutdown the contained `run-before-shutd
This service file will send a telegram notification informing about the pending shutdown. This service file will send a telegram notification informing about the pending shutdown.
```bash ```bash
ln -s /root/scripts/run-before-shutdown.service /etc/systemd/system/shutdown.target.wants/run-before-shutdown.service ln -s /root/admin-scripts/run-before-shutdown.service /etc/systemd/system/shutdown.target.wants/run-before-shutdown.service
systemctl daemon-reload systemctl daemon-reload
``` ```
*Adapt the above command, if your scripts are located somewhere else!*
### Startup Helper ### Startup Helper
@ -69,8 +67,8 @@ To install the [startup helper script](#using-startup-helper) into the regular u
```bash ```bash
USRNAME=radioelephant USRNAME=radioelephant
cp /root/scripts/post_startup.sh /home/$USRNAME/post_startup.sh cp /root/admin-scripts/post_startup.sh /home/$USRNAME/post_startup.sh
cp /root/scripts/config/post_startup.EXAMPLE /home/$USRNAME/.post_startup_env cp /root/admin-scripts/config/post_startup.EXAMPLE /home/$USRNAME/.post_startup_env
chown $USRNAME:$USRNAME /home/$USRNAME/post_startup.sh chown $USRNAME:$USRNAME /home/$USRNAME/post_startup.sh
chown $USRNAME:$USRNAME /home/$USRNAME/.post_startup_env chown $USRNAME:$USRNAME /home/$USRNAME/.post_startup_env
vim /home/$USRNAME/.post_startup_env vim /home/$USRNAME/.post_startup_env
@ -85,14 +83,14 @@ See also [Updating Startup Helper](#updating-startup-helper).
You can update the admin scripts by pulling from the git remote. You can update the admin scripts by pulling from the git remote.
```bash ```bash
cd /root/scripts cd ~/admin-scripts
git pull git pull
``` ```
For most of the scripts you only need to check if the example configuration file (i.e. `config/monitoring.EXAMPLE`) has changed and contains different keys than your copy (i.e. `config/monitoring`). For most of the scripts you only need to check if the example configuration file (i.e. `config/monitoring.EXAMPLE`) has changed and contains different keys than your copy (i.e. `config/monitoring`).
For your convenience, changes to configuration files will be documented in the [CHANGELOG](CHANGELOG.md). For your convenience, changes to configuration files will be documented in the [CHANGELOG](CHANGELOG.md).
If you followed the instructions in this README, then you will find the last time you pulled this repository and read the [CHANGELOG](CHANGELOG.md) in the `.last_changelog_read` file. If you followed the instructions in this README, then you will find the last time you pulled this repository and read the [CHANGELOG](CHANGELOG.md) in the `.last_changelog_read` file.
Get the value with `cat /root/scripts/.last_changelog_read`. Get the value with `cat /root/admin-scripts/.last_changelog_read`.
**Make sure to update the last reading time file after reading the CHANGELOG with `date > /root/scripts/.last_changelog_read`** **Make sure to update the last reading time file after reading the CHANGELOG with `date > /root/scripts/.last_changelog_read`**
@ -103,8 +101,8 @@ To compare use the following commands:
```bash ```bash
USRNAME=radioelephant USRNAME=radioelephant
diff /root/scripts/post_startup.sh /home/$USRNAME/post_startup.sh diff /root/admin-scripts/post_startup.sh /home/$USRNAME/post_startup.sh
diff /root/scripts/.post_startup_env.EXAMPLE /home/$USRNAME/.post_startup_env diff /root/admin-scripts/.post_startup_env.EXAMPLE /home/$USRNAME/.post_startup_env
``` ```
To copy the [startup helper script](#using-startup-helper) again, use the following commands (first line not needed, if you just ran the last set of commands). To copy the [startup helper script](#using-startup-helper) again, use the following commands (first line not needed, if you just ran the last set of commands).
@ -112,7 +110,7 @@ Please adapt the environment file `/home/[USRNAME]/.post_startup_env` manually.
```bash ```bash
USRNAME=radioelephant USRNAME=radioelephant
cp /root/scripts/post_startup.sh /home/$USRNAME/post_startup.sh cp /root/admin-scripts/post_startup.sh /home/$USRNAME/post_startup.sh
chown $USRNAME:$USRNAME /home/$USRNAME/post_startup.sh chown $USRNAME:$USRNAME /home/$USRNAME/post_startup.sh
``` ```
@ -145,15 +143,15 @@ If you are unsure about the cron schedule, use [Crontab Guru](https://crontab.gu
My current crontab looks like this: My current crontab looks like this:
```crontab ```crontab
* * * * * bash -c 'cd /root/scripts && ./file_monitor.sh | ./helpers/tg_notify.sh' * * * * * bash -c 'cd /root/admin-scripts && ./file_monitor.sh | ./helpers/tg_notify.sh'
*/2 * * * * bash -c 'cd /root/scripts && ./monitoring.sh | ./helpers/state_machine.sh "monitoring" | ./helpers/tg_notify.sh' */2 * * * * bash -c 'cd /root/admin-scripts && ./monitoring.sh | ./helpers/state_machine.sh "monitoring" | ./helpers/tg_notify.sh'
*/4 * * * * bash -c 'cd /root/scripts && ./dyndns.sh | ./helpers/tg_notify.sh' */4 * * * * bash -c 'cd /root/admin-scripts && ./dyndns.sh | ./helpers/tg_notify.sh'
*/3 * * * * bash -c 'cd /root/scripts && ./system_health_check.sh | ./helpers/state_machine.sh "system" | ./helpers/tg_notify.sh' */3 * * * * bash -c 'cd /root/admin-scripts && ./system_health_check.sh | ./helpers/state_machine.sh "system" | ./helpers/tg_notify.sh'
15 * * * * bash -c 'cd /root/scripts && ./docker_health_check.sh | ./helpers/state_machine.sh "docker" | ./helpers/tg_notify.sh' 15 * * * * bash -c 'cd /root/admin-scripts && ./docker_health_check.sh | ./helpers/state_machine.sh "docker" | ./helpers/tg_notify.sh'
*/15 * * * * bash -c 'cd /root/scripts && ./zfs_health_check.sh | ./helpers/state_machine.sh "zfs" | ./helpers/tg_notify.sh' */15 * * * * bash -c 'cd /root/admin-scripts && ./zfs_health_check.sh | ./helpers/state_machine.sh "zfs" | ./helpers/tg_notify.sh'
@reboot sleep 10 && /root/scripts/helpers/tg_notify.sh '[STARTUP] System just booted' @reboot sleep 10 && /root/admin-scripts/helpers/tg_notify.sh '[STARTUP] System just booted'
@reboot sleep 30 && bash -c 'cd /root/scripts && ./zfs_health_check.sh | ./helpers/state_machine.sh "zfs" | ./helpers/tg_notify.sh' @reboot sleep 30 && bash -c 'cd /root/admin-scripts && ./zfs_health_check.sh | ./helpers/state_machine.sh "zfs" | ./helpers/tg_notify.sh'
``` ```
Adapt this to your needs, you might also implement other checks and only use the `helpers/tg_notify.sh` script from this repo. Adapt this to your needs, you might also implement other checks and only use the `helpers/tg_notify.sh` script from this repo.

View File

@ -1,2 +1 @@
STORAGE_PATH=/root/scripts/storage/state_machine # NO trailing slash RENOTIFY_AGE_SEC=7185 # In seconds (2h-15 sec)
RENOTIFY_AGE_SEC=7200 # In seconds (2h)

View File

@ -1,14 +1,16 @@
#!/bin/bash #!/bin/bash
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Load configuration # Load configuration
set -o allexport set -o allexport
source /root/scripts/config/dyndns source ${script_dir}/config/dyndns
set +o allexport set +o allexport
# Import logging functionality # Import logging functionality
logfile=/root/scripts/log/dyndns.log logfile=${script_dir}/log/dyndns.log
log_identifier="DNS" log_identifier="DNS"
source /root/scripts/functions/logging.sh source ${script_dir}/functions/logging.sh
url="https://${USERNAME}:${PASSWORD}@infomaniak.com/nic/update?hostname=" url="https://${USERNAME}:${PASSWORD}@infomaniak.com/nic/update?hostname="

View File

@ -8,28 +8,30 @@
# Author: Robin Meier - robin@meier.si # Author: Robin Meier - robin@meier.si
################################################################################ ################################################################################
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Load configuration # Load configuration
set -o allexport set -o allexport
source /root/scripts/config/file_monitor source ${script_dir}/config/file_monitor
set +o allexport set +o allexport
# Import logging functionality # Import logging functionality
logfile=/root/scripts/log/file_monitor.log logfile=${script_dir}/log/file_monitor.log
log_identifier="FILE" log_identifier="FILE"
source /root/scripts/functions/logging.sh source ${script_dir}/functions/logging.sh
# Make sure directory exists # Make sure directory exists
mkdir -p /root/scripts/storage/file_monitor mkdir -p ${script_dir}/storage/file_monitor
for file in $FILES for file in $FILES
do do
# Touch storage file if not existing # Touch storage file if not existing
if [ ! -f /root/scripts/storage/file_monitor/${file//\//_} ]; then if [ ! -f ${script_dir}/storage/file_monitor/${file//\//_} ]; then
touch /root/scripts/storage/file_monitor/${file//\//_} touch ${script_dir}/storage/file_monitor/${file//\//_}
fi fi
if [ "$file" -nt "/root/scripts/storage/file_monitor/${file//\//_}" ]; then if [ "$file" -nt "${script_dir}/storage/file_monitor/${file//\//_}" ]; then
log_echo "[CHANGE] $file" log_echo "[CHANGE] $file"
touch /root/scripts/storage/file_monitor/${file//\//_} touch ${script_dir}/storage/file_monitor/${file//\//_}
fi fi
done done

View File

@ -9,70 +9,72 @@
# Author: Robin Meier - robin@meier.si # Author: Robin Meier - robin@meier.si
################################################################################ ################################################################################
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
set -o allexport set -o allexport
source /root/scripts/config/state_machine source ${script_dir}/../config/state_machine
set +o allexport set +o allexport
mkdir -p $STORAGE_PATH # Make sure STORAGE_PATH exists mkdir -p ${script_dir}/storage/state_machine
# Get input from standard input or via first parameter # Get input from standard input or via first parameter
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
echo "[ERROR] Not enough arguments!" echo "[ERROR] Not enough arguments!"
exit 1 exit 1
elif [[ $# -eq 1 ]]; then elif [[ $# -eq 1 ]]; then
MESSAGE=$(timeout 32 cat) message=$(timeout 32 cat)
KEY=$1 key=$1
elif [[ $# -eq 2 ]]; then elif [[ $# -eq 2 ]]; then
KEY=$1 key=$1
MESSAGE=$2 message=$2
else else
echo "[ERROR] Too many arguments!" echo "[ERROR] Too many arguments!"
exit 1 exit 1
fi fi
# Check if KEY is empty # Check if key is empty
if [[ -z "${KEY}" ]]; then if [[ -z "${key}" ]]; then
echo "[ERROR] KEY argument is missing!" echo "[ERROR] key argument is missing!"
exit 1 exit 1
fi fi
KEY_FILE="${STORAGE_PATH}/${KEY}.txt" key_file="${script_dir}/storage/state_machine/${key}.txt"
if [[ -f $KEY_FILE && -z "${MESSAGE}" ]]; then if [[ -f $key_file && -z "${message}" ]]; then
# Previous message present and empty message now # Previous message present and empty message now
OLD_MESSAGE=$(cat $KEY_FILE) OLD_message=$(cat $key_file)
echo "✅ Resolved" echo "✅ Resolved"
echo "$OLD_MESSAGE" echo "$OLD_message"
rm $KEY_FILE rm $key_file
exit 0 exit 0
elif [[ -f $KEY_FILE ]]; then elif [[ -f $key_file ]]; then
# Message and previous message present # Message and previous message present
OLD_MESSAGE=$(cat $KEY_FILE) OLD_message=$(cat $key_file)
# Compare contents # Compare contents
if [[ "$OLD_MESSAGE" == "$MESSAGE" ]]; then if [[ "$OLD_message" == "$message" ]]; then
# Check last notification # Check last notification
if [ "$(( $(date +"%s") - $(stat -c "%Y" "$KEY_FILE") ))" -gt "$RENOTIFY_AGE_SEC" ]; then if [ "$(( $(date +"%s") - $(stat -c "%Y" "$key_file") ))" -gt "$RENOTIFY_AGE_SEC" ]; then
touch $KEY_FILE touch $key_file
echo "‼Renotify" echo "‼Renotify"
else else
exit 0 exit 0
fi fi
else else
echo "$MESSAGE" > $KEY_FILE echo "$message" > $key_file
echo "⁉Changed" echo "⁉Changed"
fi fi
else else
if [[ -z "${MESSAGE}" ]]; then if [[ -z "${message}" ]]; then
# No message present # No message present
exit 0 exit 0
fi fi
# New message present, create KEY_FILE, continue to relaying # New message present, create key_file, continue to relaying
echo "$MESSAGE" > $KEY_FILE echo "$message" > $key_file
echo "❗New" echo "❗New"
fi fi
# Relay message if made it until here (Quotes are important here, so lines dont get .join(' ')-ed) # Relay message if made it until here (Quotes are important here, so lines dont get .join(' ')-ed)
echo "$MESSAGE" echo "$message"
exit 0 exit 0

View File

@ -9,8 +9,10 @@
# Author: Robin Meier - robin@meier.si # Author: Robin Meier - robin@meier.si
################################################################################ ################################################################################
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
set -o allexport set -o allexport
source /root/scripts/config/tg_notify source ${script_dir}/../config/tg_notify
set +o allexport set +o allexport
BOT_API_URL=https://api.telegram.org/bot${BOT_TOKEN} BOT_API_URL=https://api.telegram.org/bot${BOT_TOKEN}
@ -44,6 +46,7 @@ if [[ $? -ne 0 ]]; then
else else
if [ "${resp:1:9}" == "\"ok\":true" ]; then if [ "${resp:1:9}" == "\"ok\":true" ]; then
# echo "Sent Telegram: \n${MESSAGE//$'\n'/\n}" # echo "Sent Telegram: \n${MESSAGE//$'\n'/\n}"
sleep 0
else else
echo "[ERROR] Telegram sending did not succeed: $resp" echo "[ERROR] Telegram sending did not succeed: $resp"
echo "MESSAGE: \n${MESSAGE//$'\n'/\n}" echo "MESSAGE: \n${MESSAGE//$'\n'/\n}"

View File

@ -1,14 +1,16 @@
#!/bin/bash #!/bin/bash
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Load configuration # Load configuration
set -o allexport set -o allexport
source /root/scripts/config/monitoring source ${script_dir}/config/monitoring
set +o allexport set +o allexport
# Import logging functionality # Import logging functionality
logfile=/root/scripts/log/monitoring.log logfile=${script_dir}/log/monitoring.log
log_identifier="MON" log_identifier="MON"
source /root/scripts/functions/logging.sh source ${script_dir}/functions/logging.sh
problems=0 problems=0

View File

@ -8,15 +8,17 @@
# Author: Robin Meier - robin@meier.si # Author: Robin Meier - robin@meier.si
################################################################################ ################################################################################
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Load configuration # Load configuration
set -o allexport set -o allexport
source /root/scripts/config/system_health_check source ${script_dir}/config/system_health_check
set +o allexport set +o allexport
# Import logging functionality # Import logging functionality
logfile=/root/scripts/log/system_health_check.log logfile=${script_dir}/log/system_health_check.log
log_identifier="SYS" log_identifier="SYS"
source /root/scripts/functions/logging.sh source ${script_dir}/functions/logging.sh
problems=0 problems=0

View File

@ -10,15 +10,17 @@
# Author: Robin Meier - robin@meier.si # Author: Robin Meier - robin@meier.si
################################################################################ ################################################################################
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Load configuration # Load configuration
set -o allexport set -o allexport
source /root/scripts/config/zfs_health_check source ${script_dir}/config/zfs_health_check
set +o allexport set +o allexport
# Import logging functionality # Import logging functionality
logfile=/root/scripts/log/zfs_health_check.log logfile=${script_dir}/log/zfs_health_check.log
log_identifier="ZFS" log_identifier="ZFS"
source /root/scripts/functions/logging.sh source ${script_dir}/functions/logging.sh
problems=0 problems=0