-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.sh
executable file
·77 lines (65 loc) · 1.79 KB
/
analyzer.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
# * COMMAND LINE OPTION ANALYSIS
# There are 3 command line options, i, s, and o. They all require arguments.
# -i <input size, in lines>
# -s <sort method>
ivalue=1000
svalue=
while getopts "i:s:" opt;
do
case $opt in
i)
ivalue="$OPTARG"
;;
s)
svalue="$OPTARG"
;;
\?) echo "Usage: ./analyzer.sh [OPTION] LOGFILE"
echo "Valid options are:" >&2
echo " -i NUM Number of input lines" >&2
echo " -s <sort method> Sort method" >&2
exit 1;;
esac
done
# * VALIDATION CHECKS
# Check whether the option ``-i`` is an integer.
if [ ! -z $ivalue ]; then
re='^[0-9]+$'
if ! [[ $ivalue =~ $re ]] ; then
echo "Option -i expects an integer" >&2; exit 1
fi
fi
# Check whether the parameter ``-s`` contains a valid value.
sort=""
if [ ! -z $svalue ] ; then
if [ "$svalue" == "ascsize" ] ; then
sort="sort -h -k4"
elif [ "$svalue" == "descsize" ] ; then
sort="sort -h -r -k4"
elif [ "$svalue" == "asctime" ] ; then
sort="sort -h -k5"
elif [ "$svalue" == "desctime" ] ; then
sort="sort -h -r -k5"
else
printf "Option -s expects either value of 'ascsize', 'dessize', 'asctime' or 'desctime'";
exit 1
fi
fi
# Get the remaining parameters, which is the name of the logfile
shift $(($OPTIND -1))
logfile="$*"
if [[ -z $logfile ]] ; then
echo "No LOGFILE given"
echo "Usage: ./analyzer.sh [OPTION] LOGFILE"
exit 1
fi
# * COMMAND CONSTRUCTION
# Costruct the basic command
command="tail -n $ivalue $logfile | awk {'print \$4, \$6, \$7, \$10, \$11'} | sed 's/\[//g' | sed 's/\"//g'"
# Has sorting been asked? If yes, we append it
if [[ ! -z $sort ]]; then
command="$command | $sort"
fi
# echo "$command"
# Execute the command
eval $command
#tail -n 40000 log/access | awk {'print $4, $6, $7, $10'} | sed 's/\[//g' | sed 's/\"//g' | sort -h -r -k4 | more