NagiosEnterprises / ncpa

Nagios Cross-Platform Agent
Other
177 stars 95 forks source link

How to set "HOME" env. variable? #901

Open BorisTrnka opened 1 year ago

BorisTrnka commented 1 year ago

Hello, is there a way to define "HOME" environment variable for the NCPA agent (ncpa_listener service)? I tried few standard ways (default for service, ...), but no luck.

Current ENV:

root@sandbox1:/usr/local/ncpa# /usr/lib/nagios/plugins/nagios_plugins/check_ncpa.py -H localhost -M 'plugins/print-env.sh'
SHLVL=1
TCL_LIBRARY=/usr/local/ncpa/tcl
JOURNAL_STREAM=9:190959
_=/usr/local/ncpa/ncpa_listener
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
INVOCATION_ID=36d74fe4a26a441283541d6cd8533ca9
LANG=en_US.UTF-8
TK_LIBRARY=/usr/local/ncpa/tk
PWD=/

Thanks.

ccztux commented 1 year ago

On a system with systemd you can do it the following way:

  1. Create a drop-in config file /etc/systemd/system/ncpa_listener.service.d/ncpa_listener.conf and define your environment file:
    [Service]
    EnvironmentFile=-/etc/sysconfig/ncpa_listener

    Example of the environment file cat /etc/sysconfig/ncpa_listener

    MY_VAR="my value"
  2. systemctl daemon-reload
  3. You should see your dropin-config with systemctl cat ncpa_listener
    
    # /run/systemd/generator.late/ncpa_listener.service
    # Automatically generated by systemd-sysv-generator

[Unit] Documentation=man:systemd-sysv-generator(8) SourcePath=/etc/rc.d/init.d/ncpa_listener Description=LSB: This manages the NCPA Listener service Before=runlevel2.target Before=runlevel3.target Before=runlevel4.target Before=runlevel5.target Before=shutdown.target Before=nagios.service After=remote-fs.target After=network-online.target Conflicts=shutdown.target

[Service] Type=forking Restart=no TimeoutSec=5min IgnoreSIGPIPE=no KillMode=process GuessMainPID=no RemainAfterExit=yes ExecStart=/etc/rc.d/init.d/ncpa_listener start ExecStop=/etc/rc.d/init.d/ncpa_listener stop ExecReload=/etc/rc.d/init.d/ncpa_listener reload

/etc/systemd/system/ncpa_listener.service.d/ncpa_listener.conf

[Service] EnvironmentFile=-/etc/sysconfig/ncpa_listener

3. Restart ncpa_listener service `systemctl restart ncpa_listener`
4. Check if it works

./client/check_ncpa.py -H localhost -t mytoken -M plugins/print_env TK_LIBRARY=/usr/local/ncpa/tk TCL_LIBRARY=/usr/local/ncpa/tcl MYVAR=my value PATH=/sbin:/usr/sbin:/bin:/usr/bin =/bin/env PWD=/ LANG=en_US.utf8 SHLVL=2