A perl regexp wrapper for a C custom monitor

I'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
To use this script just pass it two arguments:

  1. --program The program to be run: either a script in the monitors folder or a full path.
  2. --regexp The regular expression to test the output against.

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
I have included a test monitor that queries a MySQL database and returns the (first) result to the wrapper script. The query and regexp to test can both be passed as options (along with username, password, host, database).

To test it create a custom monitor with these arguments:

RegExp Wrapper Example Settings

and (assuming you have a running MySQL server) these settings.

RegExp Wrapper Arguments

Note that you can also pass it username, password, database etc.

To see the full code download the Zip Archive.

regexp_wrapper.pl

#!/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  Permalink  
Leave a comment ...
Your email address will not be displayed.
Your URL will be displayed.
This public messageboard is not a forum for technical support. To report technical support problems, please contact our dedicated Support team using the instructions at the bottom of this page.
Options:
 
(Line breaks become <br />)
(Set cookies for name, email & url)
Download Free ZXTM Desktop Edition

Recent Articles

Other Resources



www.zeus.com