-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo.sh
More file actions
executable file
·44 lines (40 loc) · 1.37 KB
/
Copy pathgeo.sh
File metadata and controls
executable file
·44 lines (40 loc) · 1.37 KB
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
#!/bin/bash
file="/var/log/haproxy-access.log"
get_Xends () {
frontends="$(cat $file | awk '{ print $3 }' | grep -v "stopped" | sort -r | uniq)"
backends="$(cat $file | awk '{ print $4 }' | grep "/" | grep -v "$frontends" | cut -f1 -d"/" | sort | uniq)"
}
if [ ! -z "${!#}" ] && [ -f "${!#}" ];
then
if [ "$(file ${!#})" == "ASCII text, with very long lines" ];
then
file="${!#}"
fi
fi
if [ -z $1 ] || [ "$1" == "help" ];
then
echo -e "Usage:\ngeo <backend> [<logfile>] - geo stat by backend\ngeo report <\"backends\" (space separated) | all> - generate daily report by backends [<logfile>]\ngeo list [<logfile>] - list frontends and backends\n"
elif [ "$1" == "list" ]
then
get_Xends
echo -e "Frontends:\n$frontends\n"
echo -e "Backends:\n$backends\n"
elif [ "$1" == "report" ]
then
echo -e "Daily report by backends\n"
if [ "$2" == "all" ];
then
get_Xends
else
backends="$2"
fi
for backend in $backends
do
echo "Backend: $backend"
grep "$(LANG=en_EN date +"%d\/%b\/%Y")" $file | grep $backend | awk '{ print $1 }' | cut -f1 -d":" | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | xargs -r -n 1 geoiplookup { } | cut -f2 -d":" |sort | uniq -c | sort -k1 -n -r | head -n 10
echo
done
else
echo "Requests from file $file"
grep $1 $file | awk '{ print $1 }' | cut -f1 -d":" | xargs -n 1 geoiplookup { } | cut -f2 -d":" | sort | uniq -c | sort -k1 -n -r
fi