A perl regexp wrapper for a C custom monitorI've just written a custom Monitor in C. However, it can only check for an exact value - it would be much more powerful if I could check the output against a range of values - or even better a regular expression. Adding a regular expression library to a custom monitor program written in C is a real pain though. But, wait a minute! ZXTM ships with a MiniPerl implementation that contains full Perl regular expressions. We can just wrap our C monitor in a perl script. Usage
Any standard output from the monitor will be passed back to the Perl wrapper and checked against the regular expression. Any missmatches will be reported. Example To test it create a custom monitor with these arguments:
and (assuming you have a running MySQL server) these settings.
Note that you can also pass it username, password, database etc. To see the full code download the Zip Archive.
#!/bin/sh
# Bootstrap into the version of perl provided by Zeus
exec $ZEUSHOME/perl/miniperl -wx $0 ${1+"$@"}
if 0;
# -*- perl -*-
#!/usr/bin/perl
#line 9
# This monitor will run a seperate script/program and check it's
# output against a regular expression. It is intended to be used
# to make the creation of monitors simpler in languages without
# built-in regular expressions e.g. C
#
# Required parameters:
#
# --program : the program to run
# --regexp : the regular expression to check the result against
#
# all other parameters will be passed through to the script/program
BEGIN { unshift @INC, "$ENV{ZEUSHOME}/zxtm/lib/perl",
"$ENV{ZEUSHOME}/zxtmadmin/lib/perl"; }
use Zeus::ZXTM::Monitor qw( ParseArguments MonitorWorked MonitorFailed Log );
# Process the arguments
my %args = ParseArguments();
if(!$args{program}) {
MonitorFailed("No program");
}
if(!$args{regexp}) {
MonitorFailed("No regexp");
}
my $options ="";
for my $arg (keys %args) {
next if $arg eq "verbose"; # ignore verbose mode
next if $arg eq "program";
next if $arg eq "regexp";
$options .= "\"--" . $arg . "=" . $args{$arg} . "\" ";
}
my $cmd;
if ($args{program} !~ /^\//) {
$cmd = "$ENV{ZEUSHOME}/zxtm/monitors/$args{program} $options";
} else {
$cmd = "$args{program} $options";
}
Log( "Running $cmd" );
# Now run the program
open( PROG, "$cmd 2>&1 |" );
my $response = '';
while (<PROG>) {
$response .= $_;
}
close PROG;
Log( "Output: $response" );
if( $response !~ /$args{regexp}/ ) {
MonitorFailed( "Unexpected result: $response" );
}
MonitorWorked();
Sambeau
[Zeus Dev Team] 02 March 2006
|
Recent Articles
Other Resources
|




