This time, I created a bash script to analyze web server access log.
Step 1: Create a new folder using mkdir
mkdir log_analysis_folder
Step 2: Move into the new folder
cd log_analysis_folder
Step 3: Create a new bash script using vi
vi log_analysis_script.sh
I created few variables to find unique ip address, total number of records and request method etc.
- unique_ip=$(cat “$log_file” | awk ‘{print $3}’ | sort -u)
→ This command utilizes cat
to read the log file, awk
to extract the IP address field, and sort
to obtain a list of unique IP addresses.
- record=$(cat “$log_file” | wc -l)
- method=$(cat “$log_file” | awk ‘{print $6}’ | sort -u | tr ‘\n’ ‘,’)
→ This will only display sixth field and sort unique method request.
- access_count=$(cat “$log_file” | awk ‘{print $3}’ | sort | uniq -c | sort -nr | head -1 | awk ‘{print $2})
→ This command extracts the sixth field from the specified log file using awk
, sorts the unique method requests using sort -u
, and then uses tr
to replace newlines with commas for a formatted display.
- timestamp=$(cat “$log_file” | awk ‘{print strftime(“%Y-%m-%d %H:%M:%S”, $1)}’ | sort -u | head -1 | awk ‘{print $1})
→ This command converts the timestamps in the specified log file to a human-readable format using awk
and strftime
. It then sorts the unique timestamps, selects the earliest one with head -1
, and extracts the first field of the result using another awk
command.
- total_response_time=$( cat “$log_file” |awk ‘{sum += $2} END {print sum}’)
→ This command calculates the total response time by summing the values in the second field (response time) of each log entry in the specified log file using awk
. It then displays the total sum of response times.
- avg_response=$((total_response_time / record))
Let’s implement those variables in the echo
statements.
Once you are done, make bash script executable with chmod command.
Result