1753821 Members
9114 Online
108805 Solutions
New Discussion

Script help.

 
SOLVED
Go to solution
allanm77
Frequent Advisor

Script help.

Hi All,

 

Created a piece of shell script, want to iterate the curl statement and if condition for multiple Services,boxes combination (within the same script) and send multiple emails if the condition is met.

 

If possible would prefer this to be converted to perl.

#!/bin/sh

SERVICE1=appnameA
BOX1=boxA.foobar.com
PORT1=5000

curl -s "http://$BOX1:$PORT1/segment/region/phase/dest?app=$SERVICE1" | grep DISABLED
if (`echo $?` eq 0 )
then 
mail -s "$SERVICE1 is down" allanm@foobar.com </dev/null
fi

 Thanks,

Allan.

 

 

11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: Script help.


@allanm77 wrote:

Hi All,

 

Created a piece of shell script, want to iterate the curl statement and if condition for multiple Services,boxes combination (within the same script) and send multiple emails if the condition is met.

 

If possible would prefer this to be converted to perl.


So, place a 'for' loop around the 'curl...if...fi' .

 

Why do you feel the need to convert this to Perl?

 

Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Script help.

Thanks JRF!

Its my never ending desire to compare how code works between shell and perl.

Thanks,
Allan.
James R. Ferguson
Acclaimed Contributor

Re: Script help.

Hi (again) Allan:

 

How about evaulating the comments here?

 

http://h30499.www3.hp.com/t5/Languages-and-Scripting/How-to-fasten-nested-quot-for-quot-loops/td-p/5306443

 

...JRF...

allanm77
Frequent Advisor

Re: Script help.

Hi JRF,

Evaluated, thanks!

Can you provide the perl equivalent of the shell script..

Thanks,
Allan.
James R. Ferguson
Acclaimed Contributor

Re: Script help.


@allanm77 wrote:
Hi JRF,
Can you provide the perl equivalent of the shell script..

OK, here's one possibility.  I am going to assume that you have some knowledge of Perl and/or that you will dig with this as an example :-)

 

#!/usr/bin/perl
use strict;
use warnings;

my $SERVICE1 = q(appnameA);
my $BOX1     = q(boxA.foobar.com);
my $PORT1    = 5000;

my $result =
    qx(curl -s "http://$BOX1:$PORT1/segment/region/phase/dest?app=$SERVICE1");
my $rc = $?;

die "Can't fetch URL; return code = ", +($rc >> 8), "\n" unless $rc == 0;

 if ( $result =~ m{DISABLED} ) { system qq( mail -s "$SERVICE1 is down" allanm\@foobar.com </dev/null ); } 1;

 Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Script help.

Thanks JRF!

 

One more issue I am thinking of is that we have more than 10 boxes in production and I need to do this on more than 5 apps so it would be wonderful if the solution is better than a for loop, maybe array?

 

Allan.

James R. Ferguson
Acclaimed Contributor

Re: Script help.


@allanm77 wrote:

Thanks JRF!

 

One more issue I am thinking of is that we have more than 10 boxes in production and I need to do this on more than 5 apps so it would be wonderful if the solution is better than a for loop, maybe array?


I like the way your requests grow as I offer a solution for each iteration ;-)  Don't forget to evaluate each solution as we go along.

 

For this last one, we will use a hash for several reasons.

 

#!/usr/bin/perl
use strict;
use warnings;

my %apps = (
    appnameA => q(boxA.foobar.com),
    appnameB => q(boxB.foobar.com),
    appnameC => q(boxC.foobar.com),
);
    
my $port = 5000;

while ( my ( $service, $box ) = each %apps ) {
    my $url    = qq(http://$box:$port/segment/region/phase/dest?app=$service);
    my $result = qx(curl -s $url);
    my $rc     = $?;

    die "Can't fetch $url; return code = ", +($rc >> 8), "\n" unless $rc == 0;

    if ( $result =~ m{DISABLED} ) {
        system qq
            ( mail -s "$service down on $box" allanm\@foobar.com </dev/null );
    }
}
1;

Notice, among other things, that I have lower-cased the variable names.  Upper-case names are generally used for constants.

 

You can add more items to the hash as necessary.

 

Regards!

 

...JRF...

James R. Ferguson
Acclaimed Contributor
Solution

Re: Script help.

Hi (again):

 

Oops, we really don't want to die() and abandon all processing when we loop through the hash and curl() returns a non-zero value.  Instead, replace this line:

 

die "Can't fetch $url; return code = ", +($rc >> 8), "\n" unless $rc == 0;

 ...with this:

 

if ( $rc != 0 ) {
    warn "Can't fetch $url; return code = ", +($rc >> 8), "\n";
    next;
}

 This reports the error and continues to process the next hash member.

 

Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Script help.

Thanks JRF!