Browse Source

Adding HTTP LOG per minute counter

dyurk 1 year ago
parent
commit
2cd5b77fa4
1 changed files with 45 additions and 0 deletions
  1. 45 0
      linux/HTTP_LOG_counter.sh

+ 45 - 0
linux/HTTP_LOG_counter.sh

@@ -0,0 +1,45 @@
1
+#!/bin/bash
2
+
3
+usage(){
4
+	echo "Usage: $0 logfile_name"
5
+	echo  "Returns total count for each HTTP code per minute"
6
+	exit 1
7
+}
8
+[[ $# -eq 0 ]] && usage
9
+
10
+
11
+for LOG in "$@"
12
+do
13
+#get list of HTTP codes to set header
14
+GET_HTTP_CODES+=($(cat $LOG | awk '{print $9}' | sort | uniq))
15
+done
16
+
17
+#set array with uniq list of HTTP codes
18
+HTTP_CODES=($(printf "%q\n" "${GET_HTTP_CODES[@]}" | sort -u))
19
+
20
+#changing bash internal field separator to ','
21
+pp_codes=$(IFS=,; echo "${HTTP_CODES[*]}")
22
+
23
+#print header
24
+echo "#time,$pp_codes"
25
+
26
+#Count and print for each time
27
+for LOG in "$@"
28
+do
29
+	#Create array for each uniq time/date
30
+	DATES=($(cat $LOG | awk '{sub(/\[/,""); print $4}' | sort | rev | cut -c4- | rev| uniq))
31
+	# get length of array
32
+	NUM_dates=${#DATES[@]}
33
+	NUM_codes=${#HTTP_CODES[@]}
34
+	#for each unique time/date get count of HTTP codes
35
+	for (( i=0; i<${NUM_dates}; i++ ));
36
+	do
37
+		for x in ${HTTP_CODES[@]};
38
+		do
39
+			count=0
40
+			ROW_COUNT+=($(cat $LOG | grep ${DATES[$i]} | awk -v var="$x" -v count="$count" '$9 ~ var {count++} END{printf "%s ",count}'))
41
+		done
42
+	        echo ${DATES[$i]},$(IFS=,; echo "${ROW_COUNT[*]}")
43
+	        unset ROW_COUNT
44
+	done
45
+done