How to use the command goaccess (with examples)
- Linux
- December 25, 2023
GoAccess is an open source real-time web log analyzer, which allows users to analyze log files and generate detailed reports in real-time. This command is useful for website administrators and developers who need to monitor the performance of their websites, track user activity, and identify potential issues or bottlenecks.
Use case 1: Analyze one or more log files in interactive mode
Code:
goaccess path/to/logfile1 path/to/file2 ...
Motivation:
Analyzing log files in interactive mode allows users to get real-time insights and visualizations from their log data. This is especially useful for monitoring website traffic, identifying popular pages, and tracking potential security threats or unusual activity.
Explanation:
goaccess
: The command name.path/to/logfile1
,path/to/logfile2
, … : Paths to the log files that need to be analyzed. Multiple log files can be specified.
Example output:
...
Summary:
---------
Total Requests: 301
Unique Visitors: 193
Requested Files: 37
...
Use case 2: Use a specific log-format (or pre-defined formats like “combined”)
Code:
goaccess path/to/logfile --log-format=format
Motivation:
Using a specific log format or pre-defined formats like “combined” allows GoAccess to understand the structure of the log files and parse them correctly. This ensures accurate analysis and reporting of website data.
Explanation:
goaccess
: The command name.path/to/logfile
: Path to the log file that needs to be analyzed.--log-format=format
: Specifies the format of the log file. “format” should be replaced with the actual format used in the log file, or one of the pre-defined formats like “combined”.
Example output:
...
Summary:
---------
Total Requests: 301
Unique Visitors: 193
Requested Files: 37
...
Use case 3: Analyze a log from stdin
Code:
tail -f path/to/logfile | goaccess -
Motivation:
Analyzing a log file from stdin
allows users to analyze logs that are being continuously written to, such as real-time access logs. This is useful for monitoring web server activity as it happens and generating real-time reports.
Explanation:
tail -f path/to/logfile
: Thetail
command with the-f
option is used to output the contents of the log file and follow it as new lines are written.| goaccess -
: The|
symbol is the pipe operator, which redirects the output of the previous command (tail -f
) as input to thegoaccess
command. The-
argument specifies that the log data is coming fromstdin
.
Example output:
...
Summary:
---------
Total Requests: 301
Unique Visitors: 193
Requested Files: 37
...
Use case 4: Analyze a log and write it to an HTML file in real-time
Code:
goaccess path/to/logfile --output path/to/file.html --real-time-html
Motivation:
Writing the log analysis results to an HTML file in real-time allows users to visualize the data and share it with others easily. This is useful for generating reports, monitoring website performance, and sharing statistics with stakeholders.
Explanation:
goaccess
: The command name.path/to/logfile
: Path to the log file that needs to be analyzed.--output path/to/file.html
: Specifies the output file where the HTML report will be saved. “path/to/file.html” should be replaced with the desired location and filename.--real-time-html
: Enables real-time updates in the HTML report, ensuring that the data is updated as new log entries are analyzed.
Example output:
An HTML report is generated in real-time, which can be opened in a web browser to display the log analysis results.
Conclusion:
The goaccess
command is a powerful tool for analyzing web server logs and generating real-time reports. With its various use cases, it provides insights into website traffic, user behavior, and potential issues. Whether analyzing log files in interactive mode, using specific log formats, analyzing logs from stdin
, or generating real-time HTML reports, GoAccess is a valuable tool for web administrators and developers.