Tcp port monitoring

For fun, I was writing load balancing on python and trying to determine the best (right?) Way to check if the port is accessible and the remote host still exists.

I find that after connecting it becomes difficult to tell when the remote host goes down. I continued to live, but I can not get him to recognize a broken connection earlier than a minute (I understand that polling more often than a minute may be unnecessary, but let's say I wanted) even after setting various TCP_KEEPALIVE options to the lowest.

When I use non-blocking sockets, I noticed that recv () will return an error ("resource is temporarily unavailable") when it reads from a live socket, but returns "" when reading from a dead (send and recv of 0 bytes, which may be the reason ?). However, this seems like a strange way to test it, and it does not allow to determine if the connected ones died, but after sending some data.

Besides connecting / disconnecting for each check, is there something I can do? Can I manually send tcp keepalive or establish a lower level connection that allows me to test the connection without sending real data that the remote server could potentially handle?

+3
source share
5 answers

I would recommend not leaving your (single) test socket connected - create a new connection every time you need to poll. Every load / server availability system I have ever seen uses this method instead of a persistent connection.

If the remote server did not respond within a reasonable amount of time (for example, 10 seconds), mark it as “down”. Use timers and signals rather than functional response codes to handle this timeout.

+2
source

"it becomes difficult to say when the remote host drops"

. TCP. TCP - . TCP ( , TCP).

+1

ping

TCP- . , TCP , FIN. ACK FIN, , .

0

keepalive. , , , . , , .

, TCP-, - ACK. ACK, SEND .

0

Bash - TCP/UDP -, :

printf "" > /dev/tcp/example.com/80 && echo Works

, . :

nc -vl 1234 &
printf "" > /dev/tcp/localhost/1234

cron watch:

watch bash -c 'echo > /dev/tcp/localhost/1234 && echo Works || echo FAIL'

, , Monit, Nagios ..

Monit (monit):

# Verify host.
check host example with address example.com
  if failed
    port 80
    protocol http
  then alert
0

Source: https://habr.com/ru/post/1706795/


All Articles