[Nagiosplug-devel] Nagios:Plugin feedback

Thomas Guyot-Sionnest dermoth at aei.ca
Fri Jan 19 13:27:51 CET 2007


On 19/01/07 01:08 AM, Gavin Carr wrote:
> On Fri, Jan 19, 2007 at 12:09:51AM -0500, Thomas Guyot-Sionnest wrote:
>> Oh BTW I meant :
>>
>>   check => &$namecheck
> 
> You meant: check => \&namecheck

I guess so ;)

>> [...]
>>
>> if $failure is a scalar, then just test the sub ($check). if test fails
>> (return 0) exit with $failure text.
>>
>> if $failure is an array, test the sub ($ret being the return code). If
>> $failure[$ret] is defined, exit with failure[$ret] text
>>
>> I.e.:
>>
>> $p->add_arg(
>>   spec => 'name|n=s',
>>   help => qq{-n, --name=<name>)
>>   check => sub { if bad return 1; if invalid return 2; return 0 },
>>   failure => (undef, 'Bad name', 'Invalid name'),
>> );
>>
>> What do you think? Do you have a better idea?
> 
> I guess I'm not sure this actually adds much. Why not just do your checks
> after the argument parsing:
> 
>   die "Bad name\n" if bad($p->name);
>   die "Invalid name\n" if invalid($p->name);

Thats true and users could have the choice. I believe my proposal
encourage cleaner code as people will more likely write subs to check
arguments. It will also encourage users to test the arguments as they
define them, or to keep appropriate check code when taking code from
others plugins. Let's say there's a is_shellchean function (as I
suggested in another reply), one could write:

$p->add_arg(
  spec => 'name|n=s',
  help => '-n, --name=<name>',
  check => sub { shift; return 1 if (is_shellclean); return 0 },
  failure => 'Name must not contain any shell reserved characters.',
);

When you see the definition for --name, you see right away what check is
ran against it.

Thomas




More information about the Devel mailing list