Wanted to get some general help in terms of variable passing from Nagios into custom scripts. I'm currently attempting to use a script which will allow for sending a ticket into JIRA (through a SOAP URL), but only if the notification is being sent is an acknowledgment. It's a PHP script, for what it's worth.
As I dig into it, I see instances where it's requesting an environment variable:
getenv("NAGIOS_NOTIFICATIONTYPE")
getenv("NAGIOS_SERVICEDESC")
But, later in the script, I get an error saying that the Nagios environment variables were never set, and it errors out.
{
// Neither the NAGIOS_SERVICEDESC or NAGIOS_HOSTNAME environment variables were set - which indicates that this scripty wasn't called from Nagios
$errstr = "Script was called, but there were no Nagios environment variables present. Script was most likely not called from Nagios. Aborting.";
trigger_error($errstr, E_USER_ERROR);
exit(1);
}
I've put a copy of the script at http://pastebin.com/HFwdkAUQ. Can't attach it as PHP's aren't liked as attachments (underatandably so). It's an older script but it's worked for me in past revisions of Nagios (I'm using the most current revision of XI right now). Questions might be:
1) In a default Nagios installation, what owner/group and permissions should this script have?
2) How can I best in a PHP script figure out if variables are being passed in, and is there a better way with a PHP script for it to request the environment variables? Realize this is being called by Nagios through a command that is part of a contact (as opposed to sending an e-mail)
Any help here is appreciated, I'm kind of lost on where to look.
Nagios Not Passing Variables To Custom Commands
Re: Nagios Not Passing Variables To Custom Commands
This actually becomes a Core problem. You'll want to make sure you turn on environmental variables in the nagios.cfg file -
(https://assets.nagios.com/downloads/nag ... gmain.html)
With a quick skim of the script, it looks like all the input is being polled from those variables. Once you turn them on, restart the nagios service, and it should work properly.
1. I believe nagios user, httpd group.
2. You may be able to run a var dump - http://php.net/manual/en/function.var-dump.php - I suspect this will be fixed after allowing them as I mentioned above though.
Code: Select all
Format: enable_environment_macros=<0/1>
Example: enable_environment_macros=0
This option determines whether or not the Nagios daemon will make all standard macros available as environment variables to your check, notification, event hander, etc. commands. In large Nagios installations this can be problematic because it takes additional memory and (more importantly) CPU to compute the values of all macros and make them available to the environment.
0 = Don't make macros available as environment variables
1 = Make macros available as environment variables (default)
With a quick skim of the script, it looks like all the input is being polled from those variables. Once you turn them on, restart the nagios service, and it should work properly.
1. I believe nagios user, httpd group.
2. You may be able to run a var dump - http://php.net/manual/en/function.var-dump.php - I suspect this will be fixed after allowing them as I mentioned above though.
Former Nagios Employee
Re: Nagios Not Passing Variables To Custom Commands
Gotcha. Big question there is, if I change it at the .cfg file level, is XI going to try and overwrite that somewhere?
Re: Nagios Not Passing Variables To Custom Commands
Nope, you can make changes to the nagios.cfg safely. It isn't controlled by the DB backend.
Former Nagios Employee
Re: Nagios Not Passing Variables To Custom Commands
Sounds good. I've gone ahead and made this change.
Looks like I've only got one remaining issue on this (on the back-end command). I can tell that Nagios is calling the command by looking at the notification log for the service I'm using as a test. However, I'm not getting any indication from the script itself that it's running (when it's called, it should at least write something to a log file). Anything in the command itself I should be troubleshooting?
(The command, by the way, is /usr/bin/php -q /usr/local/nagios/libexec/to-jira/NTJ/nagios_to_jira.php --debug ... nothing too crazy)
(Scratch this for the moment, this may be a script error that it's not writing as much to the log as I would normally desire. May have commented out a bit too much)
Looks like I've only got one remaining issue on this (on the back-end command). I can tell that Nagios is calling the command by looking at the notification log for the service I'm using as a test. However, I'm not getting any indication from the script itself that it's running (when it's called, it should at least write something to a log file). Anything in the command itself I should be troubleshooting?
(The command, by the way, is /usr/bin/php -q /usr/local/nagios/libexec/to-jira/NTJ/nagios_to_jira.php --debug ... nothing too crazy)
(Scratch this for the moment, this may be a script error that it's not writing as much to the log as I would normally desire. May have commented out a bit too much)
Last edited by sav2880 on Tue Feb 07, 2017 4:40 pm, edited 1 time in total.
Re: Nagios Not Passing Variables To Custom Commands
It's hard to say as that would be specific to the script. There appears to be a few fopen's for a HISTORY_FILE - if you can figure out where that's writing to it'll probably answer your question.
The only logging on the nagios portion would be to nagios.log when it's executed via an event or notification.
The only logging on the nagios portion would be to nagios.log when it's executed via an event or notification.
Former Nagios Employee
Re: Nagios Not Passing Variables To Custom Commands
Just checking in since we have not heard from you in a while. Did @rkennedy's post clear things up or has the issue otherwise been resolved?
Former Nagios employee