1753804 Members
7686 Online
108805 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...