Define Global Environment Variables in Nagios - Fix it Now








Are you trying to Define Global Environment Variables in Nagios?

This guide will help you.


Sometimes while using Nagios, In some environments, when the plugin is executed by the monitoring engine, these environment variables are not loaded and hence the plugin does not know where to find them and fails.

Recently, we had a customer who came across a plugin error. In such a case, we define variables required for plugins globally.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Nagios queries.


Nature of defining Define Global Environment Variables in Nagios 

While we set up a check-in Nagios, we may come across an error similar to the following:

[Can't locate Monitoring/Plugin.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl
/usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/nagios/libexec/check_rabbitmq_aliveness l
BEGIN failed–compilation aborted at /usr/local/nagios/libexec/check_rabbitmq_aliveness line 15.]

However, the execution of check-in and SSH session as the Nagios user executes perfectly.

When we test the plugin in an SSH session, we use a session that has loads of environment variables from the bash profile. 

This has an extended PATH which allows bash to find the required modules.

In certain scenarios, when the plugin executes by the monitoring engine, these environment variables may not load. 

Hence the plugin fails to find them.


How to Define Global Environment Variables in Nagios ?

To define variables required for plugins globally, take note of:

i. Add the path /usr/local/important_application to the PATH environment

ii. Add the variable ORACLE_HOME=/usr/lib/oracle/11.2/client64


To begin. we edit a specific file that Nagios checks when it starts:

/etc/sysconfig/nagios

By default, this file does not exist. We create it.

Open an SSH session to Nagios host and type:

vi /etc/sysconfig/nagios

Then we add the following lines to the file:

export PATH=$PATH:/usr/local/important_application
export ORACLE_HOME=/usr/lib/oracle/11.2/client64

Now we restart Nagios:

RHEL 7+|CentOS 7+|Debian|Ubuntu 16/18/20

$ systemctl restart nagios.service


If you are using Mod-Gearman

If we have Mod-Gearman, we have to perform the following steps on all of our workers.

The methods for workers are different depending on the operating system they run on.


RHEL 7+|CentOS 7+|Debian|Ubuntu 16/18/20

Since the use systemd which has a different method, referencing existing environment variables like $PATH will not work. If we wish to define the path, we need to define the entire path variable.

To do so, we:

Re-define the PATH environment to include /usr/local/important_application

Add the variable ORACLE_HOME=/usr/lib/oracle/11.2/client64

We can do this by editing a specific file that mod-gearman2-worker checks when the service starts:

/etc/sysconfig/mod-gearman2-worker

Then we open an SSH session to Mod-Gearman worker and type:

vi /etc/sysconfig/mod-gearman2-worker

Add the following to the end of the file:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/important_application
ORACLE_HOME=/usr/lib/oracle/11.2/client64

Now we reload the daemons and restart the Mod-Gearman worker:

$ systemctl daemon-reload
$ systemctl restart mod-gearman2-worker.service


RAM Disk

If we later configure Nagios with a RAM Disk, we will lose the settings we just added. We have to re-add the environment variables and it will work again.


How Can We See The Environment Variables ?

One way is to create a simple plugin that will output them. From this, we create a command and service, and then we can see the result in the GUI.

Bash script:

#!/bin/bash
env | grep -v '|'
exit 0


[Need urgent help to define Variables in Nagios? We can help you. ]



Conclusion

This article covers how to define Global Environment Variables in Nagios. In some environments, when the plugin is executed by the monitoring engine, these environment variables are not loaded and hence the plugin does not know where to find them and fails.

Therefore, when the plugin is executed, environment variables may not load.


To Define Global Environment Variables in Nagios:

Here, you will define variables required for you plugins globally.

a. Add the path /usr/local/important_application to the PATH environment

b. Add the variable ORACLE_HOME=/usr/lib/oracle/11.2/client64

1. To do this, edit a specific file that nagios checks when it starts:

$ vi /etc/sysconfig/nagios

2. Add the following lines to the file and then save:

export PATH=$PATH:/usr/local/important_application
export ORACLE_HOME=/usr/lib/oracle/11.2/client64

3. Finally, restart Nagios:

$ systemctl restart nagios.service


If you have Mod-Gearman, 

The following is being applied:

a. Re-define the PATH environment to include /usr/local/important_application

b. Add the variable ORACLE_HOME=/usr/lib/oracle/11.2/client64

1. This is performed by editing a specific file that mod-gearman2-worker checks when the service starts:

/etc/sysconfig/mod-gearman2-worker

2. Open an SSH session to your Mod-Gearman worker.

Type:

$ vi /etc/sysconfig/mod-gearman2-worker

3. Add the following lines to the end of the file and save:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/important_application
ORACLE_HOME=/usr/lib/oracle/11.2/client64

4. Now reload the daemons and restart the Mod-Gearman worker:

$ systemctl daemon-reload
$ systemctl restart mod-gearman2-worker.service

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com