Search code examples
rsyslogclsidfromprogid

Rsyslog omprog pass message to scripts


Accurately, I want to filter logs and send some warning email.

Firstly, I tried ommail, but unfortunately, this module only support mail server which do not need authentication, but my mail server needs.

So I tried to use omprog, I wrote a python script to logon to my mail server, it will recieve one parameter which is the log and send it as mail body. Then I got the problem, I cannot pass the log to my script, if I try like this, $msg will be recognized as a string .

if $fromhost-ip == "x.x.x.x" then {
    action(type="omprog"
            binary="/usr/bin/python3 /home/elancao/Python/sendmail.py $msg")
}

I tried to search the official doc.

module(load="omprog")
action(type="omprog"
   binary="/path/to/log.sh p1 p2 --param3=\"value 3\""
   template="RSYSLOG_TraditionalFileFormat")

but in the sample, what they are using is a string "p1", not a dynamic parameter.

Can you please help? Thanks a lot!


Solution

  • The expected use of omprog is for your program to read stdin and there it will find the full default RSYSLOG_FileFormat template data (with date, host, tag, msg). This is useful, as it means you can write your program so that it is started only once, and then it can loop and handle all messages as they arrive.

    This cuts down on the overhead of restarting your program for each message, and makes it react faster. However, if you prefer, your program can exit after reading one line, and then rsyslog will restart it for the next message. (You may want to implement confirmMessages=on).

    If you just want the msg part as data, you can use template=... in the action to specify your own minimal template.

    If you really must have the msg as an argument, you can use the legacy filter syntax:

    ^program;template
    

    This will run program once for each message, passing it as argument the output of the template. This is not recommended.