-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_stats_nb_sessions.sh
executable file
·103 lines (96 loc) · 2.55 KB
/
get_stats_nb_sessions.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
#!/bin/bash
# $1 [needed] path for logs (ex: "/mnt/path/to/fortigate/log/folder" )
# $2 $3 ... [needed] IP of destination
#find folder where is this script
SOURCE=${BASH_SOURCE[0]}
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done
script_dir=$DIR
#verify ip validity
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
if [ $# -lt 2 ] ; then
echo "Error: no enough arguments: \$1 -> need path for log ; \$2 or more -> for IP of destination" >&2
exit 1
else
if [ -z "$1" ] ; then
echo 'Error: incorrect path (null)' >&2
exit 1
else
if [[ -d "$1" && ! -L "$1" ]] ; then
log_path="$1"
else
echo "Error: incorrect path (it's not a folder)" >&2
exit 1
fi
fi
nb_IP=0
declare -a IP
args=("$@")
#delete path
unset 'args[0]'
for i in "${args[@]}"; do
if [ -z "$i" ] ; then
echo "Warning: incorrect IP (null)" >&2
else
if valid_ip "$i" ; then
((nb_IP++))
IP[${#IP[@]}]=$i
else
echo "Warning: $i is not a correct IP" >&2
fi
fi
done
if [ "$nb_IP" -lt 1 ] ; then
echo "Error: no valid IP set" >&2
exit 1
fi
echo "IP: " "${IP[@]}";
fi
#lists log files
cd "$log_path" || exit 1
nb_logs=$(find . -maxdepth 1 -name '*.log' |wc -l)
if [ "$nb_logs" -lt 1 ] ; then
echo "no *.log detected in path $1" >&2
exit 1
else
echo "$nb_logs logs files detected:"
find . -maxdepth 1 -name '*.log'
declare -a Logs
for i in $(seq 1 "$nb_logs"); do
Logs[${#Logs[@]}]=$(find . -maxdepth 1 -name '*.log'|cut -d$'\n' -f"$i")
done
fi
#echo "#Logs: ${#Logs[@]} Logs: ${Logs[@]}";
json="{\n"
#make stats
for ip in "${IP[@]}"; do
json+="\t\"$ip\": {\n"
for log in "${Logs[@]}"; do
N=$(grep "$ip" "$log" |grep 'traffic' |awk -F ' ' '{ print $24}' |grep sessionid |awk -F '=' '{print $2}' |uniq |wc -l)
echo "$ip / $log: $N"
json+="\t\t\"$log\": $N,\n"
done
json="${json::-3}"
json+="\n\t},\n"
done
json="${json::-3}"
json+="\n}"
cd "$script_dir" || exit 1
echo -e "$json" > results.json