Introduction to Graylog for Log Aggregation
Managing multiple distributed environments can get complicated quickly. Many logs, many sources a perfect job for a centralized log aggregator like Graylog.
This is a game-changer.
We’ll explore how to use Graylog to aggregate logs from multiple sources, including Nginx, and customize the log format for better insights.
Graylog can aggregate from a variety of inputs, but we’ll be focused on the GELF UDP (Graylog Extended Log Format).
More information can be found here
Setting up Graylog (destination)
For easy sake, we’ll be using the Graylog Docker
pierre@goldfeesh_graylog ~ $: mkdir -p ~/dev/docker/graylog/
pierre@goldfeesh_graylog ~/dev/docker/graylog $: git clone [email protected]:Graylog2/docker-compose.git
pierre@goldfeesh_graylog ~/dev/docker/graylog $: cd docker-compose/open-core
pierre@goldfeesh_graylog ~/dev/docker/graylog/docker-compose/open-core $: cp .env.example .env
Generate the secret and the password
pierre@goldfeesh_graylog ~/dev/docker/graylog/docker-compose/open-core $: pwgen -N 1 -s 96
pierre@goldfeesh_graylog ~/dev/docker/graylog/docker-compose/open-core $: echo -n superSECRETpasswordCHANGEmePlS | shasum -a 256
Copy the secret and the hashed password to the env.
GRAYLOG_PASSWORD_SECRET and GRAYLOG_ROOT_PASSWORD_SHA2.
Once updated, start the image.
pierre@goldfeesh_graylog ~/dev/docker/graylog/docker-compose/open-core $: docker compose up -d
pierre@goldfeesh_graylog ~/dev/docker/graylog/docker-compose/open-core $: docker compose logs -f
NOTE: The last command will tail the container’s output.
For demo purposes the IP address is: 10.13.3.187
Verify it’s up by accessing the server http://10.13.3.187:9000
Setting up Inputs
In Graylog, we need setup an input to receive data.
System/InputsInputsRaw/Plaintext UDPLaunch new input
Now the input should be streaming (accepting connections).
Now with graylog loaded we can concentrate on Nginx.
Configuring Nginx (source)
In Nginx /etc/nginx/nginx.conf configure the module to send Graylog.
##
# Logging Settings
##
log_format graylog_json escape=json '{ "nginx_timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"connection": "$connection", '
'"connection_requests": $connection_requests, '
'"pipe": "$pipe", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_length": $request_length, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host", '
'"upstream_cache_status": "$upstream_cache_status", '
'"upstream_addr": "$upstream_addr", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent", '
'"http_version": "$server_protocol", '
'"remote_user": "$remote_user", '
'"http_x_forwarded_proto": "$http_x_forwarded_proto", '
'"upstream_response_time": "$upstream_response_time", '
'"nginx_access": true }';
access_log syslog:server=10.13.3.187:5555,tag=default; graylog_json;
error_log syslog:server=10.13.3.187:5555,tag=default;
By using the graylog_json log format, we can send our logs to Graylog in a standardized format that’s easy to parse and analyze.
But what about vhosts? How do we handle separate tags for each virtual host? The answer lies in customizing the access_log and error_log directives. We can add a new tag for each vhost by modifying the log format as follows:
access_log syslog:server=10.13.3.187:5555,tag=${SERVER_NAME}_access; graylog_json;
error_log syslog:server=10.13.3.187:5555,tag=${SERVER_NAME}_error;
This will send logs for each vhost to Graylog with a separate tag, making it easier to analyze and filter logs.
Verify Logs
Head over to Graylog, click the Search heading in the navbar. If successful, we should see our Nginx logs.
Fixing the logs
At this point, we’re sending the logs, but they’re not in the format we need. We need to build two extractors.
Extractor 1 - Extract JSON
Navigate to System Pipelines. Click on Create Pipeline Rule
Description name it something like: Extract Nginx JSON
You want to use the source editor. Paste the following:
rule "Extract Nginx JSON"
when
has_field("message")
then
let json_string = regex(
".*? (\\{.*\\})$",
to_string($message.message),
["json"]
);
set_field("nginx_json", json_string["json"]);
end
A little explanation. It parses the www-data nginx { ... } and just extracts the json into field called nginx_json
Extractor 2. Parse JSON into Fields
Like the previous step we’ll use the source editor and paste the following
rule "Parse Nginx JSON"
when
has_field("nginx_json")
then
let json = parse_json(to_string($message.nginx_json));
let fields = to_map(json);
set_fields(fields);
end
Attach the extractors to our pipeline
Now with our extractors set, we need to attach them to our pipeline.
SystemPipelinesand select Nginx- Stage 0 rule click “Edit”
- Select “Extract Nginx JSON”
- Add a new Stage (Stage 1)
- Select “Parse Nginx JSON”
Verify
Head over to your Search and verify that your logs are now being extracted properly.
Whats next?
Adding more logging!
I currently have my fail2ban, pfsense, and nginx logs.
Create some dashboards.