LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-29-2007, 09:40 AM   #1
lord-fu
Member
 
Registered: Apr 2005
Location: Ohio
Distribution: Slackware && freeBSD
Posts: 676

Rep: Reputation: 30
perl pass regex check as an argument


Hello and mtia.

I have a small script I have written that does some pattern matching. I would like to pass this as an argument to the script. The script works perfectly hard coded with the pattern matching, I receive errors when passing as an argument due to what I believe is the "\ | [ ] etc" , so how could I pass this stuff as an argument so it just takes it as a string? I have the argument defined as a string in the script.

Like so

script.pl --regex /F\|(6|7)[0-9]/

Any ideas? Thank You.

Heres the script in it's entirety

Code:
#!/usr/bin/perl
#########################################################################
# check_temperature plugin for nagios
# $Version .6-BETA $
#
# This plugin will allow the checking of a temperature range if the remote
# server returns a properly formatted string that this plugin can regex.
#
# TO DO:
# Allow the use of crit and warn options
#
#
#########################################################################
use warnings;
use strict;
use Getopt::Long;
use LWP::UserAgent;

my $PROGNAME = "check_temperature";
my $revision = ".6-BETA";

my $options = { 'host', 'port', 'crit' => 90, 'warn' => 78, 'regex'};
GetOptions($options, "host=s" => \my $host, "port=s" => \my $port, "crit=i", "warn=i", "regex=s" => \my $regex, "help", "version");

if (defined $options->{'help'}) {
        print <<FOO;
$0: monitor web based remote temperature readings

 check_temperature.pl [ --host <host> ] [ --port <port> ] [ --crit <temperature> ] [ --warn <temperature> ]

  --host  <host>        - The host to which the request is sent
  --port  <port>        - The port the request is sent to
  --crit  <positions>   - Critical if temperature is equal to or above this - NOT ACTIVE IN BETA
  --warn  <positions>   - Warn if temperature is equal to or above this - NOT ACTIVE IN BETA
  --regex <expression>  - The expression to be used during the comparison
  --help                - This help page


FOO
exit;
}


if (defined $options->{'version'}) {
        print <<FOO;
        $PROGNAME$revision
FOO
exit;
}

my $url = 'http://'.$host.':'.$port;
my $ua = new LWP::UserAgent;
my $request = new HTTP::Request ('GET' => $url);
my $response = $ua->request($request);

if ($response->is_success) {

my $state = "Temperature Monitor: ";

        if ($response->content =~ $regex){
                print "OK: " . $host . ' ' . $state.$&;
                exit 0;
        }else{
                print "WARN: " . $host . ' ' . $state.$&;
                exit 1;
        }

} else {
        print "CRITICAL: " . $response->status_line;
        exit 2;

Last edited by lord-fu; 08-29-2007 at 09:43 AM. Reason: fixing some stuff screwed in the copy and paste
 
Old 08-29-2007, 11:17 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
If calling the script from a shell command line, enclose the parts with offending characters in single quotes.
Code:
script.pl --regex '/F\|(6|7)[0-9]/'
--- rod.
 
Old 08-29-2007, 02:55 PM   #3
lord-fu
Member
 
Registered: Apr 2005
Location: Ohio
Distribution: Slackware && freeBSD
Posts: 676

Original Poster
Rep: Reputation: 30
theNbomr thank you for the response.Sorry for the late reply, internet went down for awhile here today. I figured it out, was a few odd little details I had askew. Here is the final result that works as it should. This is a plugin for Nagios if anyone was wondering. Allows regular expressions to be passed and used for whatever you would like.



Code:
 ./check_regex.pl --host 192.168.1.1 --port 39683 --regex "F\|(1|2|3|4|5|6|7)[0-9]" --warn "F\|8[0-9]"

Code:
#!/usr/bin/perl
#########################################################################
# check_regex plugin for nagios
# $Version 1.0rc1 $
#
# A regular expression parser.
#
#
#########################################################################
use warnings;
use strict;
use Getopt::Long;
use LWP::UserAgent;

my $PROGNAME = "check_regex";
my $revision = "1.0rc1";

my $options = { 'host', 'port', 'regex', 'warn' };
GetOptions($options, "host=s" => \my $host, "port=s" => \my $port, "regex=s" => \my $regex, "warn=s" => \my $warn, "help", "version");

if (defined $options->{'help'}) {
        print <<FOO;
$0: A regular expression parser

 check_regex.pl [ --host <host> ] [ --port <port> ] [ --regex <expression> ] [ --warn <temperature> ] [ --help ]
[ --version ]

  --host  <host>              - The host to which the request is sent
  --port  <port>              - The port the request is sent to
  --regex <normal expression> - Regular expression representing normal
  --warn  <warn expression>   - Regular expression representing warning
  --help                      - This help page
  --version                   - Version Information

FOO
exit;
}

my $url = 'http://'.$host.':'.$port;
my $ua = new LWP::UserAgent;
my $request = new HTTP::Request ('GET' => $url);
my $response = $ua->request($request);

if ($response->is_success) {

my $state = "Temperature Monitor: ";

        if ($response->content =~ /$regex/){
                print "OK: " . $host . ' ' . $state.$&;
                exit 0;
        }
        elsif($response->content =~ /$warn/){
                print "WARN: " . $host . ' ' . $state.$&;
                exit 1;
        }else{
                print "CRITICAL: " . $host . ' ' . $state.$&;
                exit 2;
        }

} else {
        print "CRITICAL: " . $response->status_line;
        exit 2;
}

Last edited by lord-fu; 08-29-2007 at 02:58 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl regex $ ShaqDiesel Programming 6 08-18-2006 02:40 PM
Pass argument between php scripts gandalf_the_grey Linux - Newbie 1 02-03-2006 02:53 PM
regex Perl help igotlongestname Programming 2 09-14-2005 07:51 PM
How to pass an argument to DHCPCD ? shazam75 Linux - Networking 3 02-27-2005 10:34 AM
PHP pass argument to shell script monzter Programming 2 08-14-2004 06:16 AM


All times are GMT -5. The time now is 07:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration