|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +echo "Welcome to Log Analyzer" |
| 4 | + |
| 5 | +log_path="$1" |
| 6 | + |
| 7 | +# Function to extract errors count |
| 8 | +function err_count(){ |
| 9 | + error_count=$(grep -i "Error\|Failed" $log_path | wc -l) |
| 10 | + echo "Number of Errors in log file: $error_count" |
| 11 | +} |
| 12 | + |
| 13 | +# Function to print critical errors |
| 14 | +function critical(){ |
| 15 | + critical_issues=$(grep -i -n "Critical" $log_path) |
| 16 | + echo "These are the Critical logs" |
| 17 | + echo "$critical_issues" |
| 18 | +} |
| 19 | + |
| 20 | +# Function to extract top 5 common logs |
| 21 | +function common_logs(){ |
| 22 | + number_of_top_messages=5 |
| 23 | + most_frequent_log=$(tr -cs '[:alpha:]' '[\n*]' < ~/Desktop/logfile.log | sort | uniq -ci | sort -nr | head -5 | awk '{ print $2 " ------ "$1}') |
| 24 | + |
| 25 | + echo "The most common Log Messages are (showing top 5)" |
| 26 | + echo "Log Message ---- Count" |
| 27 | + echo "$most_frequent_log" |
| 28 | +} |
| 29 | + |
| 30 | +# Function to export summary to a file |
| 31 | +function export(){ |
| 32 | + |
| 33 | + echo "Date of Analysing logs : $(date)" |
| 34 | + echo -e "\nName of log file: $log_path" |
| 35 | + echo -e "\nNumber of lines processed: $(wc -l $log_path | awk '{print $1}')\n" |
| 36 | + err_count; |
| 37 | + echo |
| 38 | + critical; |
| 39 | + echo |
| 40 | + common_logs |
| 41 | +} |
| 42 | + |
| 43 | +#Calling the functions |
| 44 | +err_count;critical;common_logs |
| 45 | + |
| 46 | +# Asking user to export summary |
| 47 | +read -p "Do you want to export the summary of log analytics to a seperate file [Y/N]: " response |
| 48 | + |
| 49 | +if [[ $response == "Y" ]] || [[ $response == "y" ]]; then |
| 50 | + echo "Exporting summary of log analytics to a seperate file." |
| 51 | + export > $( dirname $log_path )/log_summary.txt |
| 52 | +else |
| 53 | + echo "Thanks for using our script" |
| 54 | +fi |
| 55 | + |
| 56 | +# Moving the log file to another location after analysis |
| 57 | +echo "Analysis complete. Moving the logfile to $HOME" |
| 58 | +mv $log_path ~ |
0 commit comments