#!/bin/bash # # you need root access to restart a service! Be carefully! # # local base path, you can reuse in config #SCRIPT_DIR=$(dirname $0); SCRIPT_DIR=/opt/scripts/ipsec; # # libraries # #source $SCRIPT_DIR/lib/lib.simulation.sh ; #source $SCRIPT_DIR/lib/lib.logging.sh ; # # configuration, in extra config file or local here # #source $SCRIPT_DIR/conf/check.dns.conf.sh; # directory where the stats file are living STAT_DIR=${SCRIPT_DIR}/stats ; # dynamic host names separated by space #WATCHHOSTS="foo.dyn.ip bar.dynamic.net"; WATCHHOSTS=""; # services to restart separated by space, maybe useful: ipsec, ntp, named, my-smooth-backup #SERVICES="ipsec ntp"; SERVICES=""; # email addresses separated by space, for debugging or noisy information, leave blank if not needed # Note: a local MTA is needed #RECEIVER="admin@foobar.net user@foo.com"; RECEIVER=""; # internal parameters, do not change RESTART=0; MAILTXT=""; # check stats dir if [ ! -d $STAT_DIR ]; then echo $(date "+%F-%H:%M:%S")": stats directory $STAT_DIR created."; mkdir -p $STAT_DIR; fi; # # working on hosts # if [ ${#WATCHHOSTS} -gt 0 ]; then for SRV in $WATCHHOSTS; do # checking stats file STAT_FILE=${STAT_DIR}/$SRV.dns ; # resolving host RESOLVE=$(host $SRV | grep "has address"); if [ ${#RESOLVE} -gt 0 ]; then IP=$(echo $RESOLVE | awk '{print $4}') ; else IP=""; fi; if [ ! -f $STAT_FILE ]; then # no stats file -> create echo $(date "+%F-%H:%M:%S")": stat file $STAT_FILE created."; touch $STAT_DIR; if [ ${#IP} -gt 0 ]; then # setting current IP as OLD, because we don't have a stats file IP_OLD=$IP ; echo $(date "+%F-%H:%M:%S")": setting up old IP for HOST: $SRV to ${IP_OLD}, because we have not monitored it before"; else # could not resolve host IP_OLD="NEW_STATS_FILE" ; fi; echo $IP_OLD > $STAT_FILE ; else # found stats file and reading it IP_OLD=$(cat $STAT_FILE); fi; if [ ${#IP} -gt 0 ]; then # we were able to resolve the host if [ $IP_OLD != $IP ]; then # remember restart of services RESTART=1; # IP has changed echo $(date "+%F-%H:%M:%S")": Host $SRV has changed their IP $IP, restarting services ..."; # enable for logging to syslog # logger "Host $SRV has changed their IP $IP, restarting services ..."; # append to maillog MAILTXT=${MAILTXT}"Host: ${SRV}, IP change: ${IP_OLD} to ${IP}"$'\n'; # write new IP to stats echo $IP > $STAT_FILE; else # IP has *NOT* changed echo $(date "+%F-%H:%M:%S")": Host $SRV has not changed the IP $IP since we saw them before"; # enable if you want unchanged IPs mailed too #MAILTXT=${MAILTXT}"Host: ${SRV}, IP *NOT* changed: ${IP_OLD}"$'\n'; fi; else # could not resolve IP echo $(date "+%F-%H:%M:%S")": Could not resolve HOST $SRV. Maybe DNS or Internet down?"; fi; done; # # restarting services # if [ $RESTART -eq 1 ]; then # we have an IP change detected if [ ${#SERVICES} -gt 0 ]; then # services are defined for SVC in $SERVICES; do echo $(date "+%F-%H:%M:%S")": service $SVC restart"; # be carefully with new upstart scripts, check the cron enviroment for proper configuration # UPSTART: service $SVC restart; /etc/init.d/$SVC restart; done; else # no restart services defined? You want mailing right? echo $(date "+%F-%H:%M:%S")": There are no services for restart defined. But you want mailing right?"; fi; # Mailing to admins? if [ ${#RECEIVER} -gt 0 ]; then for MAILRECEIVER in $RECEIVER; do echo $(date "+%F-%H:%M:%S")": mailing updates to: "$MAILRECEIVER; echo "current results for dynamic host monitoring: "$'\n'"-----------------------------------------------------------------------"$'\n'${MAILTXT}$'\n\n'"restarted services: $SERVICES" | mail -s "[$(hostname)] IP change detected" $MAILRECEIVER; done; else if [ ${#SERVICES} -eq 0 ]; then echo $(date "+%F-%H:%M:%S")": Look Ma, I have not setup mailing AND service restarts ;) You want logging right?"; fi; fi; unset MAILRECEIVER; fi; else # # no WATCHHOSTS defined # echo $(date "+%F-%H:%M:%S")": What should I look for? Please check the config to setup WATCHHOSTS."; fi;