-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsra.locations.sh
executable file
·100 lines (87 loc) · 2.87 KB
/
sra.locations.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# Script which will scan the directories
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
if [ ! -f $DIR/.env ]; then
echo "";
echo "Configuration .env file is missing";
echo "";
exit
fi
set -a
source $DIR/.env
set +a
#no locations to scan, so exit
total=`find $DIR/locations/ -type f -name "*.loc" | wc -l`
if [ $total == 0 ]; then
exit
fi
#start of the report
report=$(date '+%Y-%m-%d %H:%M:%S')$'\n\n'
total_changed_files=0
for file in $DIR/locations/*.loc
do
if [ ! -f $file ]; then
continue
fi
#scanning location (a directory)
location=$(head -n 1 $file)
#allowed extensions
extensions=`head -2 $file | tail -1`
#exceptions which would be ignored
exceptions=`head -3 $file | tail -1`
#the command
find_command="find $location -type f "
#extensions which would be searched
if [ ! -z $extensions ]; then
IFS=',' read -r -a ext_array <<< "$extensions"
find_command+=" \( "
for ext in "${ext_array[@]}"; do
find_command+=" -name '*.$(echo "$ext" | xargs)' -o"
done
#remove the last -o
find_command="${find_command% -o}"
find_command+=" \) "
fi
#exceptions part of the command
if [ ! -z $extensions ]; then
IFS=',' read -r -a exp_array <<< "$exceptions"
for exp in "${exp_array[@]}"; do
find_command+=" ! -path '$exp'"
done
fi
#Set how long back it would look for changes
find_command+=" -mmin $LOCATION_INTERVAL"
#use full file list
find_command+=" -exec ls -lt {} +"
#execute the command
changed_files=`eval $find_command`
#get the lines
line_count=$(echo "$changed_files" | wc -l)
#add location results to the report if there are lines and characters in the result
if [ $line_count -gt 0 ] && [ ${#changed_files} -gt 0 ]; then
total_changed_files=$((total_changed_files + line_count))
report+="$location"
report+=$'\n=======================================================\n'
report+='changed files: '$line_count$'\n'
report+=$'=======================================================\n'
report+="$changed_files"
report+=$'\n'
fi
done
#notify if there are some results
if [ $total_changed_files -gt 0 ] && [ $NOTIFY_LOCATION_SCAN == 1 ];then
SUBJECT="Alert the server $NAME have changed files in the last $LOCATION_INTERVAL minutes"
message="Please check the /var/log/sra/sra_$(date '+%Y%m%d') file for the list of changed files"
#create directory if it doesn't exists
if [ ! -d "/var/log/sra" ]; then
mkdir "/var/log/sra"
fi
#print the contents into the file
echo "$report" >> /var/log/sra/sra_$(date '+%Y%m%d')
if [[ ! -z $message ]]; then
for file in $DIR/channels/*
do
result=$($file "$SUBJECT" "$message")
done
fi
fi