First Draft and should close this MR

This commit is contained in:
Lutchy Horace 2021-10-26 08:10:32 -04:00
parent 3095c10873
commit a0ab07359e
2 changed files with 53 additions and 0 deletions

6
monitor_ip.conf.example Normal file
View File

@ -0,0 +1,6 @@
#
# Configuration file for monitor_ip.sh
#
#
SERVICES="nginx"

47
monitor_ip.sh Normal file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Copyright (C) 2021 by LHProjects <copyright@lhpmail.us>
#
# Permission is granted to use, copy, modify, and/or distribute this work for any purpose with or without fee. This work is offered as-is, with absolutely no warranty whatsoever. The author is not responsible for any damages that result from using this work.
#
#
# Monitors host public IP, when it differs, restart services.
#
#
# I wrote this script for services like Nginx, which caches hostnames and fall out of sync when hostname IP changes.
# This is problematic when you have edge servers connecting to backends with hostnames that change
# ips frequently.
# Define variables
CONF_NAME="monitor_ip.conf"
POSSIBLE_PATHS="./ /etc"
CACHE_IP_FILE=/tmp/monitor_ip.cache
PUBLIC_IP=$(curl --silent https://api.ipify.org/)
FOUND=false
IFS=" "
for P in $POSSIBLE_PATHS; do
if test -f "$P"/$CONF_NAME; then
FOUND=true
source "$P"/$CONF_NAME
fi
done
if [ $FOUND = false ]; then
echo "Configuration file not found in '$POSSIBLE_PATHS'"
exit 1
fi
# Check if we have cache IP
if test -f $CACHE_IP_FILE; then
CACHE_IP=$(cat $CACHE_IP_FILE)
if [ "$PUBLIC_IP" != "$CACHE_IP" ]; then
for S in $SERVICES; do
systemctl restart "$S"
done
fi
else
echo "$PUBLIC_IP" > $CACHE_IP_FILE
fi
exit 0