admin-scripts/monitoring.sh

51 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
logfile=/root/logs/monitoring.log
log_identifier="[MON]"
log() {
echo -e $@ | ts "[%Y-%m-%d %H:%M:%S] $log_identifier" >> $logfile
}
log_echo() {
echo -e $@ | ts "[%Y-%m-%d %H:%M:%S] $log_identifier" | tee -a $logfile
}
set -o allexport
source /root/scripts/.monitoring_env
set +o allexport
problems=0
log "Starting monitoring run"
for ssh_host in $SSH_MONITORING
do
if [[ $(nc -w 2 ${ssh_host//:/ } <<< "\0" ) =~ "OpenSSH" ]] ; then
log "[SSH] [OK] ${ssh_host} is reachable"
else
# TODO: Rate limit fail messages, also add is back up message
log_echo "[SSH] [FAIL] ${ssh_host} not reachable"
problems=1
fi
done
# TODO: HTTP Status Code 200 Monitoring
for http_host in $HTTP_MONITORING
do
status_code=$(curl --write-out %{http_code} --silent --output /dev/null $http_host)
if [[ "$status_code" -eq 200 ]] ; then
log "[WEB] [OK] ${http_host}"
else
# TODO: Rate limit fail messages, also add is back up message
log_echo "[WEB] [FAIL] ${http_host} status code is ${status_code}"
problems=1
fi
done
if [[ "$problems" -eq "0" ]]; then
log "Monitoring Run Successful"
else
log_echo "Monitoring Run Failed"
fi