469,306 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

heartbeats

Hi,

I need to write a heartbeat solution to monitor some external clients,
and what is different as in the examples that I have seen so far is that
I want my central server to poll the clients, and not the clients
"pinging" the central server.

In detail I need a daemon on my central server which e.g. which in a
loop pings (not really ping but you know what I mean) each 20 seconds
one of the clients.

The only thing the client has to do is to accept the connection.
(optionally sending back some bytes). If it refuses it is assumed to be
offline.

My central server, and this is important, should have a short timeout.
If one client does not respond because it's offline, after max. 10
seconds the central server should continue with the next client.
Which python functions would be the most convenient for this application?

Best regards,
Yves
Dec 9 '05 #1
4 1560
Yves Glodt enlightened us with:
In detail I need a daemon on my central server which e.g. which in a
loop pings (not really ping but you know what I mean) each 20
seconds one of the clients.
You probably mean "really a ping, just not an ICMP echo request".
The only thing the client has to do is to accept the connection.
(optionally sending back some bytes). If it refuses it is assumed to
be offline.
Ok, fair enough.
My central server, and this is important, should have a short
timeout. If one client does not respond because it's offline, after
max. 10 seconds the central server should continue with the next
client.


I'd write a single function that pings a client and waits for a
response/timeout. It then should return True if the client is online,
and False if it is offline. You can then use a list of clients and the
filter() function, to retrieve a list of online clients.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Dec 9 '05 #2
On Fri, 9 Dec 2005, Sybren Stuvel wrote:
Yves Glodt enlightened us with:
In detail I need a daemon on my central server which e.g. which in a
loop pings (not really ping but you know what I mean) each 20 seconds
one of the clients.
Do you mean pings one client every 20 sec, or each client every 20 sec?
You probably mean "really a ping, just not an ICMP echo request".


What's a real ping, if not an ICMP echo request? That's pretty much the
definitive packet for internetwork groping as far as i know. I think that
the more generic sense of ping is a later meaning (BICouldVeryWellBW).
My central server, and this is important, should have a short timeout.
If one client does not respond because it's offline, after max. 10
seconds the central server should continue with the next client.


I'd write a single function that pings a client and waits for a
response/timeout. It then should return True if the client is online,
and False if it is offline. You can then use a list of clients and the
filter() function, to retrieve a list of online clients.


That sounds like a good plan.

To do the timeouts, you want the settimeout method on socket:

import socket

def default_validate(sock):
return True

def ping(host, port, timeout=10.0, validate=default_validate):

"""Ping a specified host on the specified port. The timeout (in
seconds) and a validation function can be set; the validation
function should accept a freshly opened socket and return True if
it's okay, and False if not. This functions returns True if the
specified target can be connected to and yields a valid socket, and
False otherwise.

"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
try:
sock.connect((host, port))
except socket.error:
return False
ok = validate(sock)
sock.close()
return ok

A potential problem with this is that in the worst case, you'll be
spending a little over ten seconds on each socket; if you have a lot of
sockets, that might mean you're not getting through them fast enough.
There are two ways round this: handle several pings in parallel using
threads, or use non-blocking sockets to handle several at once with a
single thread.

tom

--
everything from live chats and the Web, to the COOLEST DISGUSTING
PORNOGRAPHY AND RADICAL MADNESS!!
Dec 9 '05 #3
Tom Anderson wrote:
On Fri, 9 Dec 2005, Sybren Stuvel wrote:
You probably mean "really a ping, just not an ICMP echo request".


What's a real ping, if not an ICMP echo request? That's pretty much the
definitive packet for internetwork groping as far as i know. I think that
the more generic sense of ping is a later meaning (BICouldVeryWellBW).


Submarines came before the 'net. ;-)

Dec 9 '05 #4
On Fri, 9 Dec 2005, Peter Hansen wrote:
Tom Anderson wrote:
On Fri, 9 Dec 2005, Sybren Stuvel wrote:
You probably mean "really a ping, just not an ICMP echo request".


What's a real ping, if not an ICMP echo request? That's pretty much the
definitive packet for internetwork groping as far as i know. I think that
the more generic sense of ping is a later meaning (BICouldVeryWellBW).


Submarines came before the 'net. ;-)


Ah, of course!

if self.ping(host):
self.depth = PERISCOPE_DEPTH
periscope.up()
self.tubes["bow"].load()

:)

tom

--
Rip and tear your guts! You are huge! That means you have huge guts! Rip
and tear! -- The Doomguy
Dec 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by GMane Python | last post: by
4 posts views Thread by bwmiller16 | last post: by
3 posts views Thread by Tal Shachar | last post: by
4 posts views Thread by Vlad Hrybok | last post: by
3 posts views Thread by Court Jester | last post: by
6 posts views Thread by dredge | last post: by
2 posts views Thread by dougmcmurtry | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.