Operating System - HP-UX
1833756 Members
3019 Online
110063 Solutions
New Discussion

Re: Script to check the connection to the server

 
Hunki
Super Advisor

Script to check the connection to the server

I have a need to monitor the the connection to a particular server on port 80 in a loop and the script shud send me the status when its not able to make a connection. Has anybody has any ideas on how to achieve this.

telnet x.x.x.x 80

thanks,
hunki
12 REPLIES 12
Steven E. Protter
Exalted Contributor

Re: Script to check the connection to the server

Shalom hunki,

got an idea.

There is a text based web browser called elinks.

Its an option in linux installations. It is very good for doing this type of work.

run it remotely against the web server and check return codes. Get a non-zero have the script pop someone and email.

also a wget for index.html is another good way and wget runs on hp-ux albeit sometimes badly.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Paul Sperry
Honored Contributor

Re: Script to check the connection to the server

I use BigBrother to monitor all sorts of connections and much more. I use the free version. Check it out here:

http://bb4.com
Hunki
Super Advisor

Re: Script to check the connection to the server

Can you guys do it through a script. I am not really into installation at this point.
Paul Sperry
Honored Contributor

Re: Script to check the connection to the server

Opps sorry you can download the free version here:

http://www.bb4.org
spex
Honored Contributor

Re: Script to check the connection to the server

Hi Hunki,

Personally, I would use nmap, nc, curl, or wget for this, but you asked for a script...

# cat mon_port.sh

#!/usr/bin/sh
HOST=$1
PORT=$2

{
telnet ${HOST} ${PORT} << _EOF_

exit
_EOF_
} > /tmp/telnet.$$.txt

if (! grep -q 'Connected' /tmp/telnet.$$.txt)
then
echo "${HOST}:${PORT} unreachable at $(date)" | mailx -m -s "${HOST}:${PORT} ALERT!" your@email.addr
fi

[ -f /tmp/telnet.$$.txt ] && rm /tmp/telnet.$$.txt

exit

# mon_port.sh www.google.com 80

PCS
Hunki
Super Advisor

Re: Script to check the connection to the server

its failing at :

if (! grep -q 'Connected' /tmp/telnet.$$.txt)

any idea why its not able to find "!"

This is the error that I get:
!: not found
Dave La Mar
Honored Contributor

Re: Script to check the connection to the server

Hate to over simplify, but what is wrong with
ping xxxx.xxxx.xxxx.xxxx 80 -n 5> ping_file.txt

Then check ping_file.txt for reply.

Do this in a while loop.

while [ `date +%H` -lt 20 ]
do
echo "`date`" >> ping_file.txt

ping xxxx.xxxx.xxxx.xxxx 80 -n 5 >> ping_file.txt
echo "========================" >> ping_file.txt

grep 'bytes from' ping_file.txt

echo $? | read RC
If [ $RC -ne 0 ]
then
"Send some sort of notification."
exit 1
fi
done

The notification can be an error message, an email, whatever is your desire.

Regards,

-dl

"I'm not dumb. I just have a command of thoroughly useless information."
spex
Honored Contributor

Re: Script to check the connection to the server

Dave,

ICMP operates below the trasport layer at the network layer, so there is no notion of a port. 'ping x.x.x.x 80 -n 5' will send five, 80-byte packets to x.x.x.x.

Hunki,

Try this:

#!/usr/bin/sh

HOST=$1
typeset -i PORT=$2
typeset -i STAT=0

{
telnet ${HOST} ${PORT} << _EOF_


exit
_EOF_
} > /tmp/telnet.$$.txt &
sleep 5
if [[ "$(jobs|wc -l)" -gt "0" ]]
then
kill %$(jobs | awk 'NR==1 {print $1}' | tr -d '\[\]')
STAT=1
elif [[ "$(grep -q 'Connected' /tmp/telnet.$$.txt)" -eq "0" ]]
then
rm /tmp/telnet.$$.txt
exit ${STAT}
else
STAT=2
fi

echo "${HOST}:${PORT} unreachable at $(date)" | mailx -m -s "${HOST}:${PORT} ALERT!" your@email.addr

rm /tmp/telnet.$$.txt
exit ${STAT}

PCS
Ralph Grothe
Honored Contributor

Re: Script to check the connection to the server

There are hundreds of ways to check a webserever,
though I wouldn't consider the plain telnet method the most appropiate for the job
(it's usually ok for a quick command line check if you have no thin agent at hand).
If you have a fairly recent Perl installed there comes with it a neat sample script of LWP API usage, which is called lwp-request.
Depending on your installation you might even have symlinks to it from GET, HEAD, POST,
but they aren't necessary.
For instance you could do a HEAD request for the index page of your webserver like

$ lwp-request -m head -H "Cache-Control: no-cache" http://localhost/
200 OK
Connection: close
Date: Thu, 26 Oct 2006 12:35:11 GMT
Accept-Ranges: bytes
ETag: "8f3d-2028-303d3cc0"
Server: Apache/2.0.55 HP-UX_Apache-based_Web_Server (Unix) DAV/2 PHP/5.0.4 mod_jk/1.2.10
Content-Length: 8232
Content-Type: text/html
Last-Modified: Fri, 10 Feb 2006 14:03:07 GMT
Client-Date: Thu, 26 Oct 2006 12:35:11 GMT
Client-Peer: 127.0.0.1:80
Client-Response-Num: 1

lwp-request has many options like if you need to authenticate or go over a proxy etc.
You can easily introduce your own custom HTTP headers like the no-cache attribute in the above example,
though it really only makes sense if you need to go over gateways such like a proxy.
The HTTP RFC demands those network devices to respect these headers.
You get a nice manpage if you issue "perldoc lwp-request".

If you want to do some serious monitoring I would suggest to set up a Nagios server.
The Nagios plug-ins include quite a good monitor called check_http which also allows all kind of HTTP options conceivable.
Nagios does the monitoring excellently with a sophisticated alerting and event handling system.




Madness, thy name is system administration
Steven Schweda
Honored Contributor

Re: Script to check the connection to the server

I too would use curl or wget.

I do this on my VMS system with a DCL script
which uses wget.

> Can you guys do it through a script. I am
> not really into installation at this point.

I'd prefer to do it the easy way, and I find
wget to be useful for other things, too.
Dave La Mar
Honored Contributor

Re: Script to check the connection to the server

Sorry, should have read your post a bit closer.
There is always the ndd command, albeit more cryptic to work with.
Since port 80 [hex e2e0] should be in a listen state, you could -

ndd -get /dev/tcp tcp_status | grep TCP_LISTEN | grep -i e2e0

A $? -ne 0 would give you a heads up.

Regards,

-dl

"I'm not dumb. I just have a command of thoroughly useless information."
Steven Schweda
Honored Contributor

Re: Script to check the connection to the server

> [...] ndd [...]

Knowing that somone is LISTENing on a port is
much less useful than knowing that the server
will actually respond to a request.