-
Notifications
You must be signed in to change notification settings - Fork 11
/
spike_check
executable file
·151 lines (140 loc) · 5.81 KB
/
spike_check
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
#!/bin/bash
##
# 2018 Bobby I. <[email protected]>
# Script that summarizes your access logs
# This includes:
# - POST requests
# - GET requests
# - IP logs and their geo location
##
########################
### Color Variables ###
########################
green='\e[32m'
blue='\e[34m'
clear='\e[0m'
orange='\e[33m'
red='\e[31m'
#########################
### Color Functions ###
#########################
ColorGreen(){
echo -ne $green$1$clear
}
ColorBlue(){
echo -ne $blue$1$clear
}
ColorRed(){
echo -ne $red$1$clear
}
ColorOrange(){
echo -ne $orange$1$clear
}
###########################################################################
## GeoIP domain ##
## Uses our own GEO location API script to get the country code of an IP ##
## The connection goes though HTTPS ##
## To disable the GEO location check set the value to 0 ##
###########################################################################
geoipdomain="https://bobbyiliev.com/ip.php"
#################
## Apache logs ##
#################
function access_log_summary() {
# The log is the first argument
log=${1}
if [[ $log ]] ; then
# Check if the log is empty
if [[ $(cat $log 2>/dev/null | wc -l ) -lt 1 ]]; then
echo ""
echo $(ColorOrange " Empty log or does not exist");
echo $(ColorGreen " Specify the log file that you want to summarize");
echo $(ColorOrange " Example:");
echo $(ColorOrange " ./spike_check your_log");
echo ""
exit 0
fi
echo ""
echo $(ColorOrange "Summarizing log..")
echo $(ColorOrange "This might take a while depending on the size of the log")
echo ""
echo $(ColorGreen "Top 20 GET requests: ")
sleep 1
cat $log 2>/dev/null | grep -v 'ftp.' | grep GET | cut -d\" -f2 | awk '{print $1 " " $2}' | cut -d? -f1 | sort | uniq -c | sort -n | sed 's/[ ]*//' | tail -20 | sed 's/^ *//g' | column -s '' -s ' ' -t
sleep 1
echo ""
echo $(ColorGreen "Most Recent top 20 GET requests: ")
sleep 1
tail -n 1000 $log 2>/dev/null | grep -v 'ftp.' | grep GET | cut -d\" -f2 | awk '{print $1 " " $2}' | cut -d? -f1 | sort | uniq -c | sort -n | sed 's/[ ]*//' | tail -20 | sed 's/^ *//g' | column -s '' -s ' ' -t
sleep 1
echo ""
echo $(ColorGreen "Top 20 POST requests for: ")
sleep 1
cat $log 2>/dev/null | grep -v 'ftp.' | grep POST | cut -d\" -f2 | awk '{print $1 " " $2}' | cut -d? -f1 | sort | uniq -c | sort -n | sed 's/[ ]*//' | tail -20 | sed 's/^ *//g' | column -s '' -s ' ' -t
sleep 1
echo ""
echo $(ColorGreen "Most Recent top 20 POST requests: ")
sleep 1
tail -n 1000 $log 2>/dev/null | grep -v 'ftp.' | grep POST | cut -d\" -f2 | awk '{print $1 " " $2}' | cut -d? -f1 | sort | uniq -c | sort -n | sed 's/[ ]*//' | tail -20 | sed 's/^ *//g' | column -s '' -s ' ' -t
sleep 1
echo ""
echo $(ColorGreen "Top 20 IP addresses that have been accessing your site: ")
sleep 1
echo "Do you want geo location check for the IPs? [yes/no]"
read geo
if [[ $geo == 'yes' ]] ; then
oIFS="$IFS"
IFS=$'\n'
for ips in $(cat $log 2>/dev/null | awk '{print $1}' |sort | uniq -c | sort -rn | head -20 | sed 's/^ *//g' | column -s '' -s ' ' -t); do
IFS=' '
array=($ips)
hits="${array[0]}"
ip="${array[1]}"
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
location=$(curl ${geoipdomain}?ip=$ip 2>/dev/null)
echo $hits - $ip - $location | sed 's/^ *//g' | column -s '-' -t
fi
unset location
done
IFS="$oIFS"
else
cat $log 2>/dev/null | awk '{print $1}' |sort | uniq -c | sort -rn | head -20 | sed 's/^ *//g' | column -s ' ' -s ' ' -t
fi
echo ""
echo $(ColorGreen "Most Recent top 20 IP addresses: ")
if [[ $geo == "yes" ]] ; then
oIFS="$IFS"
IFS=$'\n'
for ips in $(tail -n 1000 $log 2>/dev/null | awk '{print $1}' |sort | uniq -c | sort -rn | head -20 | sed 's/^ *//g' | column -s '' -s ' ' -t); do
IFS=' '
array=($ips)
hits="${array[0]}"
ip="${array[1]}"
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
location=$(curl ${geoipdomain}?ip=$ip 2>/dev/null)
echo $hits - $ip - $location | sed 's/^ *//g' | column -s '-' -t
fi
unset location
done
IFS="$oIFS"
else
tail -n 1000 $log 2>/dev/null | awk '{print $1}' |sort | uniq -c | sort -rn | head -20 | sed 's/^ *//g' | column -s '' -s ' ' -t
fi
else
echo ""
echo $(ColorGreen "No log found..");
sleep 1
fi
echo $(ColorRed "########## END log ###########");
}
if [[ -z $1 ]] ; then
echo ""
echo $(ColorGreen " This script summarizes your access logs, optimized for Apache");
echo ""
echo $(ColorGreen " Specify the log that you want to summarize..");
echo $(ColorOrange " Example:");
echo $(ColorOrange " ./spike_check your_log");
echo ""
else
access_log_summary $1
fi