Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to read the environmental variable in rsyslog config #2735

Open
guptarishu opened this issue May 23, 2018 · 4 comments
Open

Not able to read the environmental variable in rsyslog config #2735

guptarishu opened this issue May 23, 2018 · 4 comments

Comments

@guptarishu
Copy link

Expected behavior

{
"@timestamp": "2018-05-23T21:05:46.919925+00:00",
"host": "10.11.1.1",
"severity": "INFO",
"syslog-tag": "test-Log:",
"tenant": "tenant 1",
"message": "some log"
}

Actual behavior

{
"@timestamp": "2018-05-23T21:05:46.919925+00:00",
"host": "10.11.1.1",
"severity": "INFO",
"syslog-tag": "test-Log:",
"tenant": "",
"message": "some log"
}

Steps to reproduce the behavior

#steps to set the env variable on linux

  1. TEST_VAR='tenant 1'
  2. export TEST_VAR
  3. printenv | grep TEST_VAR
  4. echo $TEST_VAR #should print tenant1

create file /opt/test.log whose contents are just "some log"

create rsyslog.conf as defined below and restart.
Reading this env variable in rsyslog.conf results in empty string

Environment

rsyslogd 8.24.0, compiled with:
PLATFORM: x86_64-redhat-linux-gnu
PLATFORM (lsb_release -d):
FEATURE_REGEXP: Yes
GSSAPI Kerberos 5 support: Yes
FEATURE_DEBUG (debug build, slow code): No
32bit Atomic operations supported: Yes
64bit Atomic operations supported: Yes
memory allocator: system default
Runtime Instrumentation (slow code): No
uuid support: Yes
Number of Bits in RainerScript integers: 64

###rsyslog.conf

# rsyslog configuration file

# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html

#### MODULES ####

# The imjournal module bellow is now used as a message source instead of imuxsock.
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imjournal # provides access to the systemd journal
#$ModLoad imklog # reads kernel messages (the same are read from journald)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514

# define a template to specify which fields we send
template(name="jsonLogFormat" type="list" option.json="on") {
  constant(value="{")
  constant(value="\"@timestamp\":\"")
  property(name="timereported" dateFormat="rfc3339")
  constant(value="\",\"host\":\"")
  property(name="hostname")
  constant(value="\",\"severity\":\"")
  property(name="syslogseverity-text" caseConversion="upper")
  constant(value="\",\"syslog-tag\":\"")
  property(name="syslogtag")
  constant(value="\",\"tenant\":\"")
  property(name="$.tenantid")
  constant(value="\",\"message\":\"")
  property(name="msg")
  constant(value="\"}")
}

ruleset(name="logforwarder") {  
  #forward to syslog server
  action(template="jsonLogFormat" type="omfwd" target="10.11.2.251" port="514" protocol="tcp")
}

ruleset(name="testruleset") {
  #set $.tenantid="tenantId";
  set $.tenantid=getenv("TEST_VAR");
  call logforwarder
}

module(load="imfile" PollingInterval="5") #needs to be done just once
# File 1
input(type="imfile"
  File="/opt/test.log"
  Tag="test-Log:"
  Severity="info"
  Facility="local7"
  ReadMode="2"
  Ruleset="testruleset"
)
#### GLOBAL DIRECTIVES ####

# Where to place auxiliary files
$WorkDirectory /var/lib/rsyslog

# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on

# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf

# Turn off message reception via local log socket;
# local messages are retrieved through imjournal now.
$OmitLocalLogging on

# File to store the position in the journal
$IMJournalStateFile imjournal.state

#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
#authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
#mail.*                                                  -/var/log/maillog


# Log cron stuff
#cron.*                                                  /var/log/cron

# Everybody gets emergency messages
#*.emerg                                                 :omusrmsg:*

# Save news errors of level crit and higher in a special file.
#uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
#local7.*                                                /var/log/boot.log


@guptarishu guptarishu changed the title Not able to read the env variable Not able to read the environmental variable in rsyslog config May 23, 2018
@Karl90
Copy link

Karl90 commented Oct 3, 2019

Same issue here, did you find a solution?

@rgerhards
Copy link
Member

You need to set the variable in the service environment. This is different from your user session environment.

@rgerhards
Copy link
Member

@Karl90
Copy link

Karl90 commented Oct 6, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants