From d447944eb579da5c71994a60dd4522e61a1ca9e4 Mon Sep 17 00:00:00 2001 From: David Schroeder Date: Thu, 29 Nov 2018 08:56:35 -0600 Subject: [PATCH] filewatcher --- filewatcher-install.sh | 48 ++++++++++++++++++++++++++++++++++++++++++ filewatcher.sh | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100755 filewatcher-install.sh create mode 100755 filewatcher.sh diff --git a/filewatcher-install.sh b/filewatcher-install.sh new file mode 100755 index 0000000..8a8f3df --- /dev/null +++ b/filewatcher-install.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# +# Monitor file $1 for changes +# Send an alert emai to $2 if file $1 changes +# usage: ffilewatcher /var/log/messages your.name@domain.com +# + +source /dev/stdin <<< "$(curl -sL http://scity.xyz/colorsinc)" +source /dev/stdin <<< "$(curl -sL http://scity.xyz/defaultinc)" + +if type apt &>/dev/null; then + ATYPE="apt" +elif type apt-get &>/dev/null; then + ATYPE="apt-get" +elif type yum &>/dev/null; then + ATYPE="yum" +else + ATYPE="unknown" + exit 1 +fi + +if [ "$EUID" -ne 0 ]; then APTFUNC="sudo ${ATYPE}" +else APTFUNC="${ATYPE}" +fi + +echo "" +echo -e "${CS[color,LightGreen]}Filewatcher Installation Script${CS[color,Default]}" + +do_with_root $APTFUNC -y install inotify-tools + +do_with_root set -eu + +do_with_root mkdir /opt/filewatcher + +do_with_root wget /opt/filewatcher/filewatcher.sh + +do_with_root ln -s /opt/filewatcher/filewatcher.sh /usr/local/bin/filewatcher +do_with_root chmod +x /opt/filewatcher/filewatcher.sh + + +echo "" +echo -e "${CS[color,LightYellow]}Filewatcher has been Installed${CS[color,Default]}" +echo "" +echo -e "To run updates, use the command: ${CS[color,Green]}runup${CS[color,Default]}" +echo "" +echo "" + +exit 0 \ No newline at end of file diff --git a/filewatcher.sh b/filewatcher.sh new file mode 100755 index 0000000..c57a07e --- /dev/null +++ b/filewatcher.sh @@ -0,0 +1,38 @@ +#!/bin/sh +# +# Monitor file $1 for changes +# Send an alert emai to $2 if file $1 changes +# usage: filewatcher /var/log/messages your.name@domain.com +# + +if [ -z "$2" ]; then + echo "Usage: filewatcher /var/log/messages your.name@domain.com" + exit 1 +fi + +#if a inotifywait for this file is already running +if [ $(ps aux | grep inotifywait | grep -c "$1" ) -gt '0' ]; then + echo "A process monitoring the file $1 is already running: $(ps aux | grep inotifywait | grep "$1" )"; + exit 1; +fi + +#if inotifywait exists +type -P inotifywait &>/dev/null || { echo "Error: This script requires inotifywait(http://wiki.github.com/rvoicilas/inotify-tools/) .... apt-get install inotify-tools ... " >&2; exit 1; } + +#if the file exists +# if [ -f $1 ]; then + +echo "Monitoring file $1 for changes - sending alerts to $2" + +while inotifywait -e modify -e attrib -e move -e delete $1 -o /opt/filewatcher/audit.log; do + sleep 1 + + # changes="$(tail -n5 $1)" + + changes="$(cat /opt/filewatcher/audit.log)" + echo "The following change occurred in the file $1 : $changes" | mail -s "Change in $1" $2 + rm /opt/filewatcher/audit.log + touch /opt/filewatcher/audit.log + done +else + echo "Error: File $1 not found"