1777284 Members
2379 Online
109066 Solutions
New Discussion

Script help

 
SOLVED
Go to solution
allanm77
Frequent Advisor

Script help

Hi All,

 

Have a piece of code which makes a curl to a webpage and returns a list of host which are currently up/down (down hosts are derived from grep -A1 DOWN command below and saved to a tmp file)

From that file I need to generate a URL and call it to mark the hosts as up. The URL that needs to be generated is like this -

http://$HOST:$PORT/URI/$host-from-tmp-file:$port-from-tmp-file

 

where

$host-from-tmp-file would need to come from the tmp file (/tmp/mon_prd.tmp)
$port-from-tmp-file would need to come from the tmp file (/tmp/mon_prd.tmp)

 ENV1)
    HOST=`command | awk -F: '{print $1}'`
    PORT=`command | awk -F: '{print $2}'|head -1`
    for i in $HOST
    do
       curl -s "http://$i:$PORT/virtual/hostlist" | grep "DOWN"
       curl -s "http://$i:$PORT/virtual/hostlist" | grep -A1 "DOWN" > /tmp/mon_prd.tmp
       if [ `echo $?` -eq 0 ]
       then
      mail -s "Subject" mail@mail.com < /tmp/mon_prd.tmp
      fi
       echo -n "" > /tmp/mon_prd.tmp
    done
 ;;
 *)
    echo "Invalid option"
 ;;
esac

 Thanks,

Allan.

13 REPLIES 13
James R. Ferguson
Acclaimed Contributor

Re: Script help


@allanm77 wrote:

From that file I need to generate a URL and call it to mark the hosts as up. The URL that needs to be generated is like this -

http://$HOST:$PORT/URI/$host-from-tmp-file:$port-from-tmp-file

 

where

$host-from-tmp-file would need to come from the tmp file (/tmp/mon_prd.tmp)
$port-from-tmp-file would need to come from the tmp file (/tmp/mon_prd.tmp)


Allan:

 

Post a sample (or two) of the *contents* of your '/tmp/mon_prd.tmp' file,  As usual, showing actual data leads to better solutions than making assumptions based on vague descriptions.

 

Snipping out the data you need to construct your URL should be quite simple.

 

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Script help

>PORT=`command | awk -F: '{print $2}'|head -1`

 

No need for head(1):

PORT=$(command | awk -F: '{print $2; exit}')

 

>if [ `echo $?` -eq 0 ]

 

I'm not sure why you are using echo?

if [ $? -eq 0 ]; then

 

And better to use mailx vs mail.

James R. Ferguson
Acclaimed Contributor

Re: Script help


@Dennis Handly wrote:

No need for head(1):

PORT=$(command | awk -F: '{print $2}; exit')


Right, but you meant to write that as:

 

PORT=$(command | awk -F: '{print $2;exit}')

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Script help

>but you meant to write that as:

 

Oops, history suitably retconned.  ;-)

allanm77
Frequent Advisor

Re: Script help

Hi JRF

Sample of /tmp/mon_prd.tmp -

 

 

<td>DOWN</td>
<td>host:port <a title="debug" href="URI">[I]</a> <a title="debugger" href="http://host:1port/debug">[D]</a> <a title="browse" href="http://host/corp/java/browse.jsp/wa/viewService?serviceUrl=http%3A//host%3A8000/java/Service.jsp/ww&amp;serviceUrl=http%3A//host%3A1port/service/lib">[TB]</a></td>

 

Thanks

Allan.

allanm77
Frequent Advisor

Re: Script help

Where host: host1.corp.foobar.com

 

 

allanm77
Frequent Advisor

Re: Script help

Basically need to contruct the URL based on the above file.

 

Thanks,

Allan.

allanm77
Frequent Advisor

Re: Script help

Any help is greatly appreciated!
James R. Ferguson
Acclaimed Contributor

Re: Script help


@allanm77 wrote:

Basically need to contruct the URL based on the above file.


Maybe I'm missing something. but showing *both* the input *and* the desired output would be more helpful than what you offered thus far.

 

Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Script help

Ok, here's another dig, the script above makes a call to a webpage(html based), the output that we get is like this -

<td>DOWN</td>
<td>host1.corp.foobar.com:port <a title="debug" href="URI">[I]</a> <a title="debugger" href="http://host1.corp.foobar.com:1port/debug">[D]</a> <a title="browse" href="http://host1.corp.foobar.com/corp/java/browse.jsp/wa/viewService?serviceUrl=http%3A//host%3A8000/java/Service.jsp/ww&amp;serviceUrl=http%3A//host%3A1port/service/lib">[TB]</a></td>

I need to generate a URL out of the above output which is like this -

curl http://$HOST:$PORT/URI/host1.corp.foobar.com:port

HOST is the variable defined in the script initially,
PORT is the variable defined in the script initially,
host1.corp.foobar.com is from curl's output saved to a tmp file,
port is from the curl's output saved to a tmp file.


Thanks,
Allan.

James R. Ferguson
Acclaimed Contributor

Re: Script help

Hi Allan:

 

OK, you can add this snippet to you script to capture the information you need to compose your URI.  You need merely to divide the returned variable into it's two parts, as delimited by a colon.

 

# VARS=$(perl -nle 'm{<td>(.+?):(.+?)<} and print qq($1:$2)' file)

 Regards!

 

...JRF...

allanm77
Frequent Advisor

Re: Script help

Thanks JRF, can this be constructed through sed instead?

Allan.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script help


@allanm77 wrote:
Thanks JRF, can this be constructed through sed instead?

Allan.

HI (again) Allan:

 

Well, if you force me :-) then try this (given your data):

 

sed -ne 's/<td>\(.*:\)/\1/;s/<a.*$//p' 

Regards!

 

...JRF...