You could create a parser using syslog-ng-patterndb to extract parts of the message.
Create the XML file to define your parser (/etc/syslog-ng/template_sshd.xml
) :
<patterndb version='4' pub_date='2010-10-17'>
<ruleset name='ssh' id='123456678'>
<pattern>ssh</pattern>
<rules>
<rule provider='me' id='182437592347598' class='system'>
<patterns>
<pattern>Starting session: shell on @ESTRING:SSH_TERMINAL: @for @ESTRING:SSH_USERNAME: @from @ESTRING:SSH_CLIENT_ADDRESS: @port @NUMBER:SSH_PORT_NUMBER:@ id @NUMBER:SSH_ID@</pattern>
</patterns>
<examples>
<example>
<test_message program="ssh">Starting session: shell on pts/0 for rbackup from 10.120.192.25 port 36894 id 0</test_message>
<test_values>
<test_value name="SSH_TERMINAL">pts/0</test_value>
<test_value name="SSH_USERNAME">sampleuser</test_value>
<test_value name="SSH_CLIENT_ADDRESS">192.168.10.12</test_value>
<test_value name="SSH_PORT_NUMBER">42156</test_value>
<test_value name="SSH_ID">1</test_value>
</test_values>
</example>
</examples>
</rule>
</rules>
</ruleset>
</patterndb>
Then in your syslog-ng.conf
:
Define the parser :
parser sshd_pattern { db_parser(file("/etc/syslog-ng/template_sshd.xml")); };
Call the parser in your log directive :
log
{
# s_stunnel is defined in syslog-ng/conf.d/stunnel.conf
source(s_stunnel);
parser(sshd_pattern); <---- call parser
filter(f_sshd);
destination(d_sshd);
};
Use variable SSH_CLIENT_ADDRESS
from the parser within your destination :
destination d_sshd
{
file("/var/log/sshd.log"
template("${SSH_USERNAME}; ${SSH_CLIENT_ADDRESS}; ${HOST}; ${FACILITY}; ${PRIORITY}; ${LEVEL}; ${PID}; ${TAG}; ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MIN}:${SEC}; ${PROGRAM}; \n")
template_escape(no)
);
};
Unit test running :
pdbtool match -P "ssh" -M "Starting session: shell on pts/0 for rbackup from 10.120.192.25 port 36894 id 0" -p template_sshd.xml -c -D -v
Should return :
SSH_TERMINAL=pts/0
SSH_USERNAME=rbackup
SSH_CLIENT_ADDRESS=10.120.192.25
SSH_PORT_NUMBER=36894
SSH_ID=0
Adapted from this link : https://gist.github.com/linickx/8002981
Edit regarding your comment below :
Ideally, the SSH_TERMINAL key could swallow everything from the space
after session: to the from bareword
Change the pattern in the XML like this :
<pattern>Starting session: @ESTRING:SSH_TERMINAL:from @@ESTRING:SSH_CLIENT_ADDRESS: @port @NUMBER:SSH_PORT_NUMBER:@ id @NUMBER:SSH_ID@</pattern>
Which returns :
# pdbtool match -P "ssh" -M "Starting session: shell on pts/0 for rbackup from 10.120.192.25 port 36894 id 0" -p template_sshd.xml -c -D -v
SSH_TERMINAL=shell on pts/0 for rbackup <-- you got all between "session:" to "from"
SSH_CLIENT_ADDRESS=10.120.192.25
SSH_PORT_NUMBER=36894
SSH_ID=0
Further reading about pattern parsers : https://www.syslog-ng.com/technical-documents/doc/syslog-ng-open-source-edition/3.16/administration-guide/72