Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created August 24, 2024 06:53
Show Gist options
  • Save copyleftdev/12d7e66a4b60c783074271c935415339 to your computer and use it in GitHub Desktop.
Save copyleftdev/12d7e66a4b60c783074271c935415339 to your computer and use it in GitHub Desktop.
Log Analysis Mastery: The Ultimate Cheat Sheet

πŸ“Š Log Analysis Mastery: The Ultimate Cheat Sheet

πŸ“œ Basic Log Viewing

Command Description Example
cat πŸ“„ Display entire log file cat /var/log/syslog
less πŸ“ƒ View log file with pagination less /var/log/auth.log
tail πŸ”š View end of log file tail /var/log/apache2/access.log
head πŸ” View beginning of log file head /var/log/mysql/error.log
tail -f πŸ‘€ Follow log file in real-time tail -f /var/log/syslog

πŸ” Searching and Filtering

Command Description Example
grep πŸ”Ž Search for patterns in logs grep "error" /var/log/syslog
egrep πŸ”¬ Extended grep (supports regex) egrep "error|warning" /var/log/syslog
fgrep πŸš€ Fast grep (literal strings only) fgrep "literal string" /var/log/syslog
zgrep πŸ—œοΈ Search compressed log files zgrep "error" /var/log/syslog.1.gz
sed πŸ”€ Stream editor for filtering/transforming sed -n '/error/p' /var/log/syslog
awk πŸ”§ Pattern scanning and processing awk '/error/ {print $0}' /var/log/syslog

πŸ“Š Log Analysis Tools

Tool Description Example
logwatch πŸ“ˆ Summarize log files logwatch --detail high
goaccess πŸ“Š Real-time web log analyzer goaccess /var/log/apache2/access.log
lnav πŸ” Log file navigator lnav /var/log/syslog
multitail πŸ‘€ Monitor multiple log files multitail /var/log/syslog /var/log/auth.log
journalctl πŸ“š Query systemd journal journalctl -u nginx.service

πŸ•°οΈ Time-based Log Analysis

Command Description Example
date -d πŸ“… Convert log timestamps date -d "2023-05-01 14:30:00" +%s
awk with time ⏱️ Filter logs by time awk '$1$2 >= "2023-05-01 14:30:00" && $1$2 < "2023-05-01 15:00:00"' /var/log/syslog
sed with time πŸ•°οΈ Extract time range sed -n '/2023-05-01 14:30:00/,/2023-05-01 15:00:00/p' /var/log/syslog

πŸ“ˆ Log Statistics

Command Description Example
wc -l πŸ”’ Count log entries wc -l /var/log/auth.log
sort | uniq -c πŸ“Š Count unique entries cat /var/log/auth.log | sort | uniq -c | sort -nr
cut -d' ' -f4 | sort | uniq -c πŸ“Š Count events by field cat /var/log/auth.log | cut -d' ' -f4 | sort | uniq -c | sort -nr

πŸ”„ Log Rotation

Command Description Example
logrotate πŸ”„ Rotate, compress, and mail logs logrotate /etc/logrotate.conf
ls -lth /var/log πŸ“ List log files by modification time ls -lth /var/log | head

πŸ”’ Secure Log Viewing

Command Description Example
sudo πŸ” Run commands with superuser privileges sudo tail /var/log/auth.log
zcat πŸ—œοΈ View compressed log files zcat /var/log/syslog.2.gz
zless πŸ“ƒ View compressed logs with pagination zless /var/log/syslog.2.gz

πŸ”§ Advanced Techniques

Technique Description Example
Combining tools πŸ”— Use pipes to combine commands grep "error" /var/log/syslog | awk '{print $1, $2, $3}'
Regular expressions πŸ”¬ Use regex for complex pattern matching grep -E "error|warning" /var/log/syslog
Log parsing scripts πŸ“œ Write custom scripts for log analysis #!/bin/bash\ngrep "error" $1 | awk '{print $1, $2}'

πŸ† Pro Tips

  1. πŸ“š Always check log locations in /etc/rsyslog.conf or /etc/syslog-conf
  2. πŸ” Use grep -v to exclude patterns from your search
  3. πŸ”’ Use sed -n '10,20p' to view specific line ranges in a log file
  4. πŸ“Š Combine sort, uniq, and cut for powerful log analysis
  5. πŸ” Remember to use sudo when accessing system logs
  6. πŸ—œοΈ Use zgrep, zcat, and zless for compressed logs
  7. πŸ”„ Set up log rotation to manage log file sizes
  8. πŸ“ˆ Use watch command to repeatedly run log analysis commands
  9. πŸ•°οΈ Convert timestamps to epoch time for easier comparison
  10. πŸ”§ Learn awk for powerful text processing in logs

πŸ“œ Example: Complex Log Analysis

#!/bin/bash

# Count HTTP status codes in Apache access log
echo "HTTP Status Code Distribution:"
awk '{print $9}' /var/log/apache2/access.log | sort | uniq -c | sort -rn

# Find top 10 IP addresses
echo -e "\nTop 10 IP Addresses:"
awk '{print $1}' /var/log/apache2/access.log | sort | uniq -c | sort -rn | head -n 10

# Count errors in syslog for the last hour
echo -e "\nErrors in syslog (last hour):"
sudo awk -v date=$(date -d '1 hour ago' '+%Y-%m-%d %H:%M:%S') '$0 > date && /error/' /var/log/syslog | wc -l

# Find failed SSH login attempts
echo -e "\nFailed SSH Login Attempts:"
sudo grep "Failed password" /var/log/auth.log | awk '{print $11}' | sort | uniq -c | sort -nr

# Monitor real-time error rate
echo -e "\nReal-time Error Rate (Ctrl+C to stop):"
tail -f /var/log/syslog | grep --line-buffered "error" | awk '{print $1, $2, $3}' | 
    while read date ; do 
        echo "$(date): Error occurred" ; 
    done | uniq -c

Remember, log analysis is crucial for system administration and security. Always handle logs securely and respect privacy concerns! πŸ”’πŸ“Š

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment