-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_warmup.sh
156 lines (133 loc) · 3.87 KB
/
cache_warmup.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# vars
verbose=0
grep_date=`date --date="yesterday" +%d/%b/%Y`
url_file="urls_`date +%Y%m%d`.txt"
hitrate=10
concurrency=3
invert_match="(no_cache=1|\.gif|\.jpeg|.jpg)"
# define help function
function help(){
echo "Website Cache WarmUp";
echo "parsing apache log, finding and open most hit urls"
echo "=================================================="
echo "Usage example:";
echo "cache-warmup (-l|--logfile) string (-u|--url) string [(-h|--help)] [(-v|--verbose)] [(-c|--concurrency) integer] [--hitrate integer]";
echo
echo "Options:";
echo "-h or --help: displays this information";
echo "-v or --verbose: verbose mode on";
echo "-l or --logfile (string): apache logfile. required";
echo "-c or --concurrency (integer): concurrency (threads)";
echo "--hitrate (integer): hitrate in apache log";
echo "-u or --url (string): base url with protocol. required";
exit 1;
}
# execute getopt
ARGS=$(getopt -o "hvl:c:u:" -l "help,verbose,logfile:,concurrency:,hitrate:,url:" -n "Cache WarmUp" -- "$@");
# bad arguments :)
if [ $? -ne 0 ];
then
help;
fi
eval set -- "$ARGS";
while true; do
case "$1" in
-h|--help)
shift;
help;
;;
-v|--verbose)
shift;
verbose="1";
;;
-l|--logfile)
shift;
if [ -n "$1" ];
then
logfile="$1";
shift;
fi
;;
-c|--concurrency)
shift;
if [ -n "$1" ];
then
concurrency="$1";
shift;
fi
;;
--hitrate)
shift;
if [ -n "$1" ];
then
hitrate="$1";
shift;
fi
;;
-u|--url)
shift;
if [ -n "$1" ];
then
url="$1";
shift;
fi
;;
--)
shift;
break;
;;
esac
done
# check required arguments
if [ -z "$logfile" ]
then
echo "logfile is required. use help for more information";
exit 1
fi
if [ -z "$url" ]
then
echo "base url is required. use help for more information";
exit 1
fi
# check if given logfile exists
if [ ! -f $logfile ]
then
echo "logfile does not exist: $logfile"
exit 1
fi
# save URLs to temp file
cat $logfile | grep $grep_date | egrep "\.html.* HTTP" | grep -v -E $invert_match | awk '{ print $7 }' | sort | uniq -c | sort -n -r | awk '$1 >= '"$hitrate" | awk -v url="$url" '{ print url$2 }' > $url_file
# check if temp url file has been created
if [ ! -f $url_file ]
then
echo "missing generated url file ($url_file)"
exit 1
fi
# look for siege, alternative: wget (no multiple threads)
if which siege >/dev/null; then
echo "siege found"
reps=`wc -l $url_file | awk '{ print $1 }'`
arguments="--delay=1 -i --file=$url_file --concurrent=$concurrency --reps=$reps"
if [ $verbose = 1 ]
then
arguments="$arguments -v"
else
arguments="$arguments -q"
fi
echo "executing => siege $arguments"
siege $arguments
else
echo "siege not found, using wget"
arguments="--spider -i $url_file"
if [ $verbose = 1 ]
then
arguments="$arguments -v"
else
arguments="$arguments -q"
fi
echo "executing => wget $arguments"
wget $arguments
fi
# remove temp url file
rm $url_file