-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraverse_directory.sh
89 lines (83 loc) · 3.24 KB
/
traverse_directory.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
#write the shell script for the following.
#In a directory contains six sub directories called a,b,c,d,e,f.
#Go to each directory and read the files contained in a directory one by one and
#check the below three conditions.
#a. If we received a file today, then check file is corrupted or not.
#b. Find the average file size of the last 30 days and compare with today's file size.If there is a deviation in file size +/- by 20% then send a mail.
#c. If today's File Time difference is more or less than 2 hours compared with the average time of the last 30 days then send a mail.
#!/bin/bash
#script to recursively travel a dir of n levels
DIRECTORY_LIST=("a" "b" "c" "d" "e" "f")
TODAY=`date '+%b %e'`
REPORT_NAME='/tmp/report.txt'
function traverse() {
for directory in ${DIRECTORY_LIST[@]}
do
echo "directory ${1}/${directory}"
echo "$(pwd)"
echo $TODAY
if [[ -d ${1}/${directory} ]]; then
#logic
if [ "$(ls -A ${1}/${directory})" ]; then
cd ${1}/${directory}
echo $(pwd)
echo $(ls -ltr)
echo $TODAY
if [ "$(ls -ltr | grep -i "$TODAY")" ]; then
echo "entered"
name=`ls -ltr | grep -i "$TODAY" | awk '{print $9}'`
untar_file=`tar -xvzf $name -O`
if [ $? -eq 0 ]; then
echo "file is not corrupted"
#find average file size
average_size=$(find $(pwd) -type f -mtime -30 -ls | awk '{sum += $7; n++;} END {print sum/n;}')
average_time=$(find $(pwd) -type f -mtime -30 -ls | awk '{print $10}' | awk '{split($0, a, ":"); sum += a[1]; n++;} END {print sum/n;}')
today_file_size=$(ls -l | grep -v 'total'| awk '{print $5}')
today_file_time=$(ls -l | grep -v 'total'| awk '{print $8}' |awk '{split($0, a, ":"); print a[1]}')
size_deviation=$(echo $average_size*0.2| bc)
time_deviation=2
int_size_deviation=$( printf "%.0f" $size_deviation )
max_size=$(($average_size + $int_size_deviation))
min_size=$(($average_size - $int_size_deviation))
max_time=$(($average_time + $time_deviation))
min_time=$(($average_time - $time_deviation))
if [[ $today_file_size -ge $max_size ]] || [[ $today_file_size -le $min_size ]]; then
echo "Size of file not met defined criteria , $name in directory $directory" >> $REPORT_NAME
else
echo "do nothing"
fi
if [[ 10#$today_file_time -ge $max_time ]] || [[ 10#$today_file_time -le $min_time ]]; then
echo "time of file not met defined criteria , $name in directory $directory" >> $REPORT_NAME
else
echo "do nothing"
fi
else
echo "file corrupted : $name, in directory: $directory" >> $REPORT_NAME
fi
else
echo "no file arrived in $directory" > file.txt
fi
cd ../..
else
echo "${1}/${directory} is Empty take action" >> $REPORT_NAME
fi
else
echo "found a file with same name , skipping file ${1}/${directory}"
fi
done
}
function send_mail() {
subject="System logs Status Alert"
##sending mail as
from="[email protected]"
## sending mail to
to="[email protected]"
## send carbon copy to
also_to="[email protected]"
## send email
echo "sending email..." | mailx -a "$REPORT_NAME" -s "$subject" -r "$from" -c "$to" "$also_to"
}
function main() {
traverse "$1"
}
main "$1"