diff --git a/nginx-log-analyser b/nginx-log-analyser new file mode 100644 index 0000000..e606ad0 --- /dev/null +++ b/nginx-log-analyser @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +# ===== Colors ===== +GRAY="\e[1;30m" +BLUE="\e[1;34m" +WHITE="\e[1;37m" +RED="\e[1;31m" +RESET="\e[0m" + +# ===== Check File ===== +logFile=$1 + +if [ -z "$logFile" ] || [ ! -f "$logFile" ]; then + echo -e "${RED}Log file not found or not specified.${RESET}" + echo -e "${GRAY}Usage:${RESET} ${WHITE}nginx-log-analyser${RESET} ${BLUE}${RESET}" + exit 1 +fi + +# ===== Top 5s ===== +top5() { + local data="$1" + echo "$data" | sort | uniq -c | sort -rn | head -5 +} + +top5Ips=$(top5 "$(awk '{print $1}' "$logFile")") +top5Paths=$(top5 "$(awk -F'"' '{print $2}' "$logFile" | awk '{print $2}')") +top5Codes=$(top5 "$(awk '{print $9}' "$logFile" | grep -E '^[0-9]{3}$')") +top5UserAgents=$(top5 "$(awk -F'"' '{print $6}' "$logFile" | cut -c1-37 | sed 's/^\(.\{37\}\).*/\1.../')") + +# ===== Display Top 5s ===== +printTop5() { + local title=$1 + local data=$2 + local label=$3 + + echo -e "${BLUE}$title${GRAY}:${RESET}" + while read -r requests value; do + printf "${WHITE}%-s ${GRAY}- ${WHITE}%s ${GRAY}%s\n${RESET}" "$value" "$requests" "$label" + done <<< "$data" + echo "" +} + +# ===== Output ===== +printTop5 "Top 5 IP addresses with the most requests" "$top5Ips" "requests" +printTop5 "Top 5 most requested paths" "$top5Paths" "requests" +printTop5 "Top 5 response status codes" "$top5Codes" "requests" +printTop5 "Top 5 user agents" "$top5UserAgents" "requests" + +exit 0 \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..d476131 --- /dev/null +++ b/setup.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# ===== Colors ===== +GRAY="\e[1;30m" +GREEN="\e[1;32m" +BLUE="\e[1;34m" +WHITE="\e[1;37m" +RESET="\e[0m" + +# ===== Installation ===== +echo -e "${GRAY}Installing${RESET} ${WHITE}Nginx Log Analyser${RESET}${GRAY}..." +sudo cp ./nginx-log-analyser /usr/bin/ +sudo chmod +x /usr/bin/nginx-log-analyser + +# ===== Output ===== +echo -e "${GREEN}Successfully installed!${RESET}" +echo -e "${GRAY}Usage:${RESET} ${WHITE}nginx-log-analyser${RESET} ${BLUE}${RESET}" \ No newline at end of file