heartbeats

Y

Yves Glodt

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
 
S

Sybren Stuvel

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
 
T

Tom Anderson

Yves Glodt enlightened us with:

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).
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
 
P

Peter Hansen

Tom said:
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. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,365
Members
48,049
Latest member
robinsonkoff

Latest Threads

Top