Module: PyHeartBeat—Detecting Inactive Computers
Credit: Nicola Larosa
When we have a number of computers connected by a TCP/IP network, we are often interested in monitoring their working state. The pair of programs presented in Example 10-1 and Example 10-2 help you detect when a computer stops working, while having minimal impact on network traffic and requiring very little setup. Note that this does not monitor the working state of single, specific services running on a machine, just that of the TCP/IP stack and the underlying operating system and hardware components.
PyHeartBeat is made up of two files:
PyHBClient.py
sends UDP packets, while
PyHBServer.py
listens for such packets and detects inactive clients. The client
program, running on any number of computers, periodically sends an
UDP packet to the server program that runs on one central computer.
In the server program, one thread dynamically builds and updates a
dictionary that stores the IP numbers of the client computers and the
timestamp of the last packet received from each. At the same time,
the main thread of the server program periodically checks the
dictionary, noting whether any of the timestamps is older than a
defined timeout.
In this kind of application, there is no need to use reliable TCP connections, since the loss of a packet now and then does not produce false alarms, given that the server-checking timeout is kept suitably larger than the client-sending period. On the other hand, if we have hundreds of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access