Collecting systemd-journald logs in Wazuh: journald format and localfile filters
Wazuh localfile block for journald: PCRE2 filters, AND and OR logic, conversion to syslog format and examples with decoders for CRON and Docker.
The Wazuh agent reads the systemd-journald journal through the Logcollector module and sends events to the server in syslog format. Here I show the <localfile> block configuration, the PCRE2 filter syntax, the rules for combining filters, and the decoders and rules that CRON and Docker need.
The description covers the current Wazuh 4.14 documentation. The addresses in the examples come from the 192.0.2.0/24 range, and the host is called agent-01.
How the agent reads journald
Journald collects data from various sources and stores it in a binary format in the /var/log/journal/ directory. As a result, a record carries structured metadata, and the documentation links this to better searching and higher reliability. The agent’s Logcollector module reads these records through journald instead of tailing text files. Collecting from files is covered separately in the post on multi-line logs in Wazuh.
Wazuh builds a syslog-format line from a record to stay compatible with the rules. It uses four fields for this: _HOSTNAME, SYSLOG_IDENTIFIER, MESSAGE, and SYSLOG_PID or _PID (if available). The fields produce this template:
$TIMESTAMP $_HOSTNAME $SYSLOG_IDENTIFIER[$PID]: $MESSAGE
The agent forwards events built this way to the server. The analysis engine decodes them, matches them against rules and writes alerts to /var/ossec/logs/alerts/alerts.log and /var/ossec/logs/alerts/alerts.json.
The localfile block for journald
I enable the journald source with two options at once: <location> and <log_format> must both have the value journald. The documentation states that both are required. A block like this without filters is present by default in /var/ossec/etc/ossec.conf and passes all journald logs to the server:
<localfile>
<location>journald</location>
<log_format>journald</log_format>
</localfile>
With a correct configuration, Wazuh collects only future events. The <only-future-events> option set to no adds the events generated since wazuh-logcollector was stopped:
<localfile>
<location>journald</location>
<log_format>journald</log_format>
<only-future-events>no</only-future-events>
</localfile>
| Option | Value | Description in the documentation |
|---|---|---|
location | journald | Log source, alongside a file path, a Windows event channel and macos. Default value: none |
log_format | journald | systemd-journal events collected in syslog format |
only-future-events | yes or no | By default Logcollector reads logs created since its own start. no also collects events since it was stopped |
filter | PCRE2 expression | Selective collection of journald logs by field. Default value: none |
The filter and the ignore_if_missing attribute
The <filter> element selects logs by systemd journal fields. Its content is a PCRE2 expression, and the field attribute names the field it is matched against. The documentation gives two typical fields: _SYSTEMD_UNIT collects the logs of a specific systemd service, and PRIORITY collects logs with a given priority level. The agent skips everything that does not pass the filter, which reduces noise from unneeded services.
<localfile>
<location>journald</location>
<log_format>journald</log_format>
<filter field="_SYSTEMD_UNIT">^ssh.service$</filter>
</localfile>
The ignore_if_missing attribute decides what happens to records without the given field:
| Value | Behavior | Default |
|---|---|---|
no | A log without the field does not match the filter and is dropped | yes |
yes | Wazuh ignores the filter and accepts the log even when the field is missing | no |
Combining filters and blocks
Filters in one <localfile> block combine with the AND operator: a log must match all of them to be collected. Several blocks combine with the OR operator. The documentation shows this with an example using ssh.service and cron.service:
<localfile>
<location>journald</location>
<log_format>journald</log_format>
<filter field="_SYSTEMD_UNIT">^ssh.service$</filter>
</localfile>
<localfile>
<location>journald</location>
<log_format>journald</log_format>
<filter field="_SYSTEMD_UNIT">^cron.service$</filter>
<filter field="PRIORITY" ignore_if_missing="yes">[0-3]</filter>
</localfile>
Wazuh collects a log if _SYSTEMD_UNIT equals ssh.service. It also collects it when _SYSTEMD_UNIT equals cron.service and PRIORITY matches 0, 1, 2 or 3, or the PRIORITY field is missing. So I get all SSH logs, high-severity CRON messages and anomalous CRON entries without priority information.
With several journald blocks, one more rule applies. If no block has filters, Wazuh collects all logs. If at least one block has filters, Wazuh applies only the blocks with filters and ignores the blocks without them. Adding a block with a filter therefore turns off the default “collect everything”.
When a log matches two or more filters in one block, the agent sends it once. The documentation presents this as protection against duplicates.
A second page of the documentation uses a PRIORITY range of [0-6] without ignore_if_missing. Logs with a priority higher than 6 are then ignored:
<localfile>
<location>journald</location>
<log_format>journald</log_format>
<filter field="_SYSTEMD_UNIT">^cron.service$</filter>
<filter field="PRIORITY">[0-6]</filter>
</localfile>
Decoding and rules on the server
Wazuh decodes SSH logs by default and generates alerts from them. The SSH rules from the 0095-sshd_rules.xml file (version 4.14.10) work on the decoded sshd program. I list three of them in the front matter:
| Rule | Level | match condition | Description |
|---|---|---|---|
| 5710 | 5 | illegal user|invalid user | sshd: Attempt to login using a non-existent user |
| 5715 | 3 | ^Accepted|authenticated.$ | sshd: authentication success. |
| 5716 | 5 | ^Failed|^error: PAM: Authentication | sshd: authentication failed. |
A sample line in the journald template format looks like this:
Nov 17 12:00:00 agent-01 sshd[12345]: Failed password for invalid user admin from 192.0.2.10 port 45678 ssh2
The message contains the invalid user fragment from the condition of rule 5710 and starts with Failed, as in the condition of rule 5716.
The documentation does not ship decoders for every service. For CRON and Docker it shows its own decoders and rules, which I describe below. The general decoder syntax is covered in the post on custom Wazuh decoders, and the directories the manager loads them from in the post on loading rules and decoders.
CRON: decoders and rules
The agent configuration collects the ssh.service and cron.service logs in two blocks, as in the example from the section on combining filters. On the server I add three decoders to /var/ossec/etc/decoders/local_decoder.xml:
<decoder name="cron-service">
<program_name>cron|CRON</program_name>
</decoder>
<decoder name="cron-service1">
<parent>cron-service</parent>
<regex type="pcre2">\((\w+)\) CMD \((.*?)\)$</regex>
<order>service_user, command</order>
</decoder>
<decoder name="cron-service1">
<parent>cron-service</parent>
<regex type="pcre2">\((\w+)\) INFO \(Skipping @(\w+) jobs .*?\)$</regex>
<order>service, message</order>
</decoder>
Three rules go into /var/ossec/etc/rules/local_rules.xml:
<group name="cron-service,">
<rule id="111800" level="0">
<decoded_as>cron-service</decoded_as>
<description>Journald logs for CRON.</description>
</rule>
<rule id="111801" level="8">
<if_sid>111800</if_sid>
<match>CMD</match>
<description>CRON: The $(service_user) user executed ($(command)) on $(hostname).</description>
</rule>
<rule id="111802" level="12">
<if_sid>111800</if_sid>
<match>not system startup</match>
<description>CRON: The cron.service on $(hostname) started outside a system startup: $(message).</description>
</rule>
</group>
Rule 111800 groups the CRON logs from journald. Rule 111801 fires when CRON runs a command, and 111802 when CRON starts outside system boot, for example after a restart, and it skips @reboot jobs. After adding the files I restart the manager so the changes take effect:
systemctl restart wazuh-manager
An alert from a CRON entry has this shape in the documentation. I keep the fields that show the path through the predecoder and the decoder, and I replace the values with my own:
{
"rule": {
"level": 8,
"description": "CRON: The root user executed (/hello.py) on agent-01.",
"id": "111801",
"groups": [
"cron-service"
]
},
"full_log": "Nov 17 12:01:01 agent-01 CRON[11354]: (root) CMD (/hello.py)",
"predecoder": {
"program_name": "CRON",
"timestamp": "Nov 17 12:01:01",
"hostname": "agent-01"
},
"decoder": {
"name": "cron-service"
},
"data": {
"service_user": "root",
"command": "/hello.py"
},
"location": "journald"
}
The location field of the alert holds the value journald.
Docker: decoders and rules
For Docker the documentation uses a block with one filter on the docker.service unit:
<localfile>
<log_format>journald</log_format>
<location>journald</location>
<filter field="_SYSTEMD_UNIT">^docker.service$</filter>
</localfile>
The container must write to journald. In the documentation example the logging driver does this:
docker run --log-driver=journald --name nginx_webserver -d -p 8080:80 nginx
The --log-driver option has the value journald, and --name gives the container the name nginx_webserver. For container logs the documentation adds its own decoders. The main decoder matches program_name to twelve characters:
<decoder name="docker-service">
<program_name type="pcre2">^\w{12}$</program_name>
<prematch type="pcre2">\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2} \[\w+]</prematch>
</decoder>
<decoder name="docker-service1">
<parent>docker-service</parent>
<regex type="pcre2">(\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}) \[(\w+)] .*: (.*)$</regex>
<order>log_timestamp, severity, message</order>
</decoder>
The rules in the example have IDs from 111700 to 111702:
<group name="docker-service,">
<rule id="111700" level="0">
<decoded_as>docker-service</decoded_as>
<description>Journald logs for Docker.</description>
</rule>
<rule id="111701" level="8">
<if_sid>111700</if_sid>
<match>start worker processes</match>
<description>Docker with container ID $(program_name) just launched.</description>
</rule>
<rule id="111702" level="12">
<if_sid>111700</if_sid>
<match>gracefully shutting down</match>
<description>Docker with container ID $(program_name) just shut down.</description>
</rule>
</group>
Rule 111701 fires when a container starts, and 111702 when it stops.
Agent restart and group configuration
I apply a change in /var/ossec/etc/ossec.conf by restarting the agent:
systemctl restart wazuh-agent
When I want the same set of blocks for many agents, I use centralized configuration. The manager sends all files from a group’s directory to its agents, for example the files from /var/ossec/etc/shared/default to the default group. The agent.conf file has the root element <agent_config>, and the name, os and profile attributes select the agents:
<agent_config os="Linux">
<localfile>
<location>journald</location>
<log_format>journald</log_format>
<filter field="_SYSTEMD_UNIT">^ssh.service$</filter>
</localfile>
</agent_config>
The documentation says to start with a temporary file whose name begins with a dot, so agents do not receive an incomplete configuration, and then to check it with the verify-agent-conf tool. Preparing groups and assigning agents is covered in the post on Wazuh agent groups.
What the documentation does not specify
- How Logcollector behaves when the
/var/log/journal/directory does not exist and the journal runs in memory only. - The size limit of the
MESSAGEfield, or how the converter behaves whenMESSAGEcontains newline characters. - Whether the
multiline_regexoption works with thejournaldformat. - Whether a
journaldblock with a filter works inside<agent_config>. The centralized configuration examples show<localfile>only with text files. - Whether the Docker decoders from the example fit images other than NGINX, which the documentation uses.
- Which of rules 5710 and 5716 is reported for a line that satisfies both
matchconditions. - In which Wazuh version
journald,filterandignore_if_missingbecame available.
Sources
- https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/localfile.html
- https://documentation.wazuh.com/current/user-manual/capabilities/log-data-collection/journald.html
- https://documentation.wazuh.com/current/user-manual/capabilities/log-data-collection/how-it-works.html
- https://documentation.wazuh.com/current/user-manual/capabilities/log-data-collection/monitoring-log-files.html
- https://documentation.wazuh.com/current/user-manual/capabilities/log-data-collection/log-data-analysis.html
- https://documentation.wazuh.com/current/user-manual/reference/centralized-configuration.html
- https://raw.githubusercontent.com/wazuh/wazuh/v4.14.10/ruleset/rules/0095-sshd_rules.xml