nrpe.cfg not passing through a global environment variable.

An open discussion forum for obtaining help with Nagios Core. Nagios Core users of all experience levels are welcome here. Subforum have been created for the discussion of Nagios Core and Nagios Plugin development.

NOTE: The SourceForge.net mailing lists have been deprecated in favor of this forum in order to expedite support and provide additional features not available on the old mailing list.

nrpe.cfg not passing through a global environment variable.

Postby CrazyHorse019 » Wed Mar 27, 2019 6:38 am

Hi,

We have an issue were the nrpe_user is configured to use nagios. However when the nagios user is called, it ignores the environment variables configured in bash.bashrc and profile.d/appenv.sh.

When the environment variable is hardcoded within our scripts this then does work.

I have seen messages about this happening in the event the nrpe_user has been assigned a numeric variable but as you can see in the below configuration (npre.cfg) this is not the case:

log_file=/var/log/nrpe.log
debug=1
pid_file=/var/run/nagios/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,::1
dont_blame_nrpe=1
allow_bash_command_substitution=0
command_timeout=60
connection_timeout=300
command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200
include=/etc/nagios/nrpe_local.cfg
include_dir=/etc/nagios/nrpe.d/


TIA - any help would be gratefully received :)
CrazyHorse019
 
Posts: 3
Joined: Tue Mar 26, 2019 6:02 am

Re: nrpe.cfg not passing through a global environment variab

Postby mcapra » Wed Mar 27, 2019 11:46 am

CrazyHorse019 wrote:However when the nagios user is called, it ignores the environment variables configured in bash.bashrc and profile.d/appenv.sh.

Commands executed by NRPE do not run in a shell -- Bash or otherwise. They are executed via popen. They therefore do not inherit environment variables defined in a particular shell.

Here's the environment variables that get set on the latest NRPE version:
https://github.com/NagiosEnterprises/nr ... #L320-L338
Code: Select all
setenv("PATH", path, 1);
setenv("IFS", " \t\n", 1);
setenv("LOGNAME", nrpe_user, 0);
setenv("USER", nrpe_user, 0);

...

setenv("HOME", pw->pw_dir, 0);
setenv("SHELL", pw->pw_shell, 0);


It would be more appropriate to set your required environment variables via the init system. For systemd on CentOS 7, with NRPE running under xinetd:
Code: Select all
[Unit]
Description=Xinetd A Powerful Replacement For Inetd
After=syslog.target network.target NetworkManager-wait-online.service

[Service]
Type=forking
PIDFile=/var/run/xinetd.pid
EnvironmentFile=-/etc/sysconfig/xinetd
ExecStart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid "$EXTRAOPTIONS"
ExecReload=/usr/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target


We are providing an EnvironmentFile to xinetd via systemd. I can update this environment file (/etc/sysconfig/xinetd) with a value:
Code: Select all
FOO="bar"


Have a dummy command in my nrpe.cfg:
Code: Select all
command[echo_foo]=/usr/bin/echo hello $FOO; exit 0


Then restart the daemon (xinetd in this case) and the NRPE server picks it up just fine:
Code: Select all
[root@capra_nag ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c echo_foo
hello bar


But only because I'm explicitly including certain environments and variables in the parent process (xinetd), which are inherited by the child process (NRPE).

TL;DR you can totally do this, but it depends on how both your operating system and your init system are handling environment variables because the NRPE process itself ... doesn't aside from the few I mentioned :)
Former Nagios employee
http://www.mcapra.com/
User avatar
mcapra
 
Posts: 3600
Joined: Thu May 05, 2016 3:54 pm

Re: nrpe.cfg not passing through a global environment variab

Postby npolovenko » Wed Mar 27, 2019 12:32 pm

Thanks, @mcapra!
@CrazyHorse019, Let us know if you have any questions.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
npolovenko
Support Tech
 
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: nrpe.cfg not passing through a global environment variab

Postby CrazyHorse019 » Thu Mar 28, 2019 4:48 am

Hi,

Thanks for the replies.

I should have mentioned the versions of services and OS that I am currently using - apologies that you've had to make assumptions.

So I am using nagios-nrpe-server service on ubuntu 18.04 - neither inetd or xinetd are installed on the system. The purpose of using nagios nrpe is to to serve my icinga 1.x installation in conjunction with pnp4nagios.

I am using nagios-nrpe-server 3.2.1-1ubuntu1.

Are there any caveats to this installation or do the same instructions still apply - would I just install xinetd and add the environment variable and have it referenced in the nrpe config?
CrazyHorse019
 
Posts: 3
Joined: Tue Mar 26, 2019 6:02 am

Re: nrpe.cfg not passing through a global environment variab

Postby scottwilkerson » Thu Mar 28, 2019 7:46 am

As mcapra mentioned
mcapra wrote:Commands executed by NRPE do not run in a shell -- Bash or otherwise. They are executed via popen. They therefore do not inherit environment variables defined in a particular shell.


If you want to add variables they need to be added to the command, such as

Code: Select all
command[echo_foo]=/usr/bin/env FOO="bar" /usr/bin/echo hello $FOO; exit 0


Additionally, this is a Nagios forum and we cannot support icinga installations.

Locking thread
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
User avatar
scottwilkerson
DevOps Engineer
 
Posts: 17598
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises


Return to Nagios Core

Who is online

Users browsing this forum: Google [Bot], helix, MSN [Bot] and 44 guests