Exploding check commands batman ...

Andreas Ericsson ae at op5.se
Fri Jul 1 21:28:45 CEST 2005


Paul L. Allen wrote:
> Andreas Ericsson writes:
> 
>> Paul L. Allen wrote:
> 
> 
>>>  Easy for options which take string arguments and
>>> where the string "0" is never valid or sensible:
>>
>>  
>> The string "0" and str[0] = 0 are two quite different things.
> 
> 
> In C they are.  In perl there are some overlaps between "0", the
> null string, the number 0 and unitialized variables, depending on what
> exactly you do with them.
>   Variable is      Is defined?    Boolean val    Num val    String val
>   -----------      -----------    --------       -------    ----------
>   uninitialized    NO             false          0          null string
>   null string      yes            false          0          null string
>   number 0         yes            false          0          "0"
>   string "0"       yes            false          0          "0"


Note that all boolean values here are false. It gets important further down.


>   string "0.0"     yes            TRUE           0          "0.0"
>   string "00"      yes            TRUE           0          "00"


These are no longer null-strings in any sense, so they aren't very 
interesting.

> In strict mode uninitialized variables cannot be used in comparisons.
> So if somebody does manage to do something that results in a variable
> holding an option being uninitialized, as opposed to being initialized
> with the null string,

This won't be possible. I was thinking along the lines of this;

command_name  check_foo_command
command_line  check_foo -H $HOSTADDRESS$ -C "$ARG1$" -p "$ARG2$"

Where check_foo is a plugin written in perl that checks something via 
SNMP. It's reasonable to assume that most will want to give the 
community as an argument from the service-object, while most want to use 
the default port.

Nagios will translate all 32 $ARGx$ macros even if some aren't set 
(which is a bit weird, but useful for this purpose) and simply remove 
the macro-tags since their counterparts will be NULL pointers (as 
opposed to null strings).

So, the resulting command turns out to

check_foo -H host -C bar -p ""

which means the imaginary variable $port will be set to the null-string 
and thus it will be defined but will evaluate to false when being 
evaluated as a boolean expression, as it would have if it wasn't 
specified at all.

The -H argument in this case is not "overloadable" because it doesn't 
makes sense to have it that way considering how Nagios does things.

> things break.  Outside of strict mode an
> uninitialized variable is false in a boolean context, 0 in a numeric
> context and the null string in a string context; in strict mode all you
> can do with an unitialized variable without causing an error is assign
> to it.


Which is why the simple approach to make perl-scripts overloaded is to 
apply defaults after getopt parsing and conditionally of a boolean 
evaluation.

> 
>> POSIX quite clearly states that empty arguments are still arguments. 
>> Any implementation doing otherwise is decidedly broken, even if it 
>> errs on the side of prudence.
> 
> 
> There's a difference between the null string and being uninitialized.
>   check_foo -x ''


This is how it will be used, as explained above.

> Means $opt_x contains the null string.
>   check_foo -x
> IF you can somehow sneak it past the shell and the GetOpt module


The shell will handle it, along with Nagios' macro replacement code.

-- 
Andreas Ericsson                   andreas.ericsson at op5.se
OP5 AB                             www.op5.se
Lead Developer


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list