Skip to content

Instantly share code, notes, and snippets.

@shyamvyas5
Last active November 21, 2024 10:43
Show Gist options
  • Save shyamvyas5/1e2474fd3ef1b86672292f10cbcfb97b to your computer and use it in GitHub Desktop.
Save shyamvyas5/1e2474fd3ef1b86672292f10cbcfb97b to your computer and use it in GitHub Desktop.
getting count of 4xx and 5xx logs from the logs file

logs-generate

For generating apache logs used https://github.com/mingrammer/flog

flog > apache.log

For generating nginx logs used https://github.com:kscarlett/nginx-log-generator

nginx-log-generator > nginx.log

apache logs

4xx

cat apache.log | grep -e "[: ]4..[: ]" | awk 'BEGIN {print "IP,REQUEST,PATH,STATUSCODE"} {print $1,$6,$7,$9}' | tr " " "," | sed 's/"//g' > apache-4xx.csv

5xx

cat apache.log | grep -e "[: ]5..[: ]" | awk 'BEGIN {print "IP,REQUEST,PATH,STATUSCODE"} {print $1,$6,$7,$9}' | tr " " "," | sed 's/"//g' > apache-5xx.csv

4xx-5xx

cat apache.log | grep -e "[: ]4..[: ]" -e "[: ]5..[: ]" | awk 'BEGIN {print "IP,REQUEST,PATH,STATUSCODE"} {print $1,$6,$7,$9}' | tr " " "," | sed 's/"//g' > apache-4xx-5xx.csv

nginx logs

4xx

cat nginx.log | grep -e "[: ]4..[: ]" | awk 'BEGIN {print "IP,REQUEST,PATH,STATUSCODE"} {print $1,$6,$7,$9}' | tr " " "," | sed 's/"//g' > nginx-4xx.csv

5xx

cat nginx.log | grep -e "[: ]5..[: ]" | awk 'BEGIN {print "IP,REQUEST,PATH,STATUSCODE"} {print $1,$6,$7,$9}' | tr " " "," | sed 's/"//g' > nginx-5xx.csv

4xx-5xx

cat nginx.log | grep -e "[: ]4..[: ]" -e "[: ]5..[: ]" | awk 'BEGIN {print "IP,REQUEST,PATH,STATUSCODE"} {print $1,$6,$7,$9}' | tr " " "," | sed 's/"//g' > nginx-4xx-5xx.csv

get status counts from those csv by running(it'll ask for the file name of the csv file)

./get-count.sh
#!/bin/bash
# Input CSV file
read -p "csv file name here... => " input_file
# Initialize count to 0
count_1xx=0
count_2xx=0
count_3xx=0
count_4xx=0
count_5xx=0
# Extract count of status codes using awk
count_1xx=$(awk -F, '$4 ~ /^1/ { count++ } END { print count }' "$input_file")
count_2xx=$(awk -F, '$4 ~ /^2/ { count++ } END { print count }' "$input_file")
count_3xx=$(awk -F, '$4 ~ /^3/ { count++ } END { print count }' "$input_file")
count_4xx=$(awk -F, '$4 ~ /^4/ { count++ } END { print count }' "$input_file")
count_5xx=$(awk -F, '$4 ~ /^5/ { count++ } END { print count }' "$input_file")
# Check and set count to 0 if they are empty or not found
[ -z "$count_1xx" ] && count_1xx=0
[ -z "$count_2xx" ] && count_2xx=0
[ -z "$count_3xx" ] && count_3xx=0
[ -z "$count_4xx" ] && count_4xx=0
[ -z "$count_5xx" ] && count_5xx=0
echo "Counts:"
echo "+-------------------+-------+"
echo "| Status Code | Count |"
echo "+-------------------+-------+"
echo "| 1xx | $count_1xx |"
echo "| 2xx | $count_2xx |"
echo "| 3xx | $count_3xx |"
echo "| 4xx | $count_4xx |"
echo "| 5xx | $count_5xx |"
echo "+-------------------+-------+"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment