0

I'm developing a Nagios plugin in Perl (no Nagios::Plugin, just plain Perl). The error condition I'm checking for normally comes from a command output, called inside the plugin. However, it would be very inconvenient to create the error condition, so I'm looking for a way to feed test output to the plugin to see if it works correctly.

The easiest way I found at the moment would be with a command line option to optionally read input from a file instead of calling the command.

if($opt_f) {
  open(FILE, $opt_f);
  @output = <FILE>;
  close FILE;
}
else {
  @output = `my_command`;
}

Are there other, better ways to do this?

RobEarl
  • 7,862
  • 6
  • 35
  • 50
chiborg
  • 26,978
  • 14
  • 97
  • 115

2 Answers2

0

Or you could have a test version of the command that returns various status for you to test (via a command line argument perhaps).

You put the test version of mycommnd in some test directory (/my/nagois/tests/bin).

Then you manipulate the PATH environment variable on the command line that runs the test.

$ env PATH=/my/nagois/tests/bin:$PATH nagios_pugin.pl

The change to $PATH will only last for as long as that one command executes. The change is localized to the subshell that is spawned to run the plugin.

The backticks used to execute the command will cause the shell to use the PATH to locate the command, and that will the the test version of the command, which lives in the directory that is now the first one on the search path.

let me know if I wasn't clear.


New answer for new method.

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28
  • I like the principle of the idea. However, the [plug-in development guidelines] (http://nagiosplug.sourceforge.net/developer-guidelines.html#SYSCMDAUXFILES) state that you shouldn't execute system commands without specifying their full path. But another solution would be to have a parameter to optionally specify the executable command. – chiborg Dec 12 '12 at 10:46
0

Build a command line switch into your plugin, and if you set -t on the command line, you use your test command at /path/to/test/command, else you run the 'production' command at /path/to/production/command

The default action is production, only test it the switch indicating test mode is present.

Len Jaffe
  • 3,442
  • 1
  • 21
  • 28