Reduce logging on AgentX connections

Previous logging was very noisy when the agent connection to snmpd
drops:

[ERROR   ] agentx.network - run            : Empty PDU, connection closed!
[INFO    ] agentx.network - disconnect     : Disconnecting from localhost:705
[ERROR   ] agentx.agent - run            : An exception occurred: Empty PDU, disconnecting
[ERROR   ] agentx.agent - run            : Reconnecting
[INFO    ] agentx.agent - run            : Opening AgentX connection
[INFO    ] agentx.network - connect        : Connecting to localhost:705
[ERROR   ] agentx.network - connect        : Failed to connect to localhost:705
[ERROR   ] agentx.agent - run            : An exception occurred: Not connected
[ERROR   ] agentx.agent - run            : Reconnecting
[INFO    ] agentx.agent - run            : Opening AgentX connection
[INFO    ] agentx.network - connect        : Connecting to localhost:705
[ERROR   ] agentx.network - connect        : Failed to connect to localhost:705
[ERROR   ] agentx.agent - run            : An exception occurred: Not connected
[ERROR   ] agentx.agent - run            : Reconnecting

Also, reconnects were attempted every 0.1s, but field research shows
that snmpd, if it restarts, takes ~3-5 seconds to come back (note: this
is also due to a systemd delay in restarting it upon failures).
Hammering the connection is not useful.

This change refactors the logging, to avoid redundant messages:
- sleep 1s between attempts (reducing the loop by 10x)
- Either print 'Connected to' or 'Failed to connect to', not both.
- Remove the 'reconnecting' superfluous message
This commit is contained in:
Pim van Pelt
2023-01-14 11:12:06 +00:00
parent 43551958f8
commit 684400ff9e
2 changed files with 4 additions and 5 deletions

View File

@ -71,10 +71,9 @@ class Agent(object):
try:
self._net.run()
except Exception as e:
self.logger.error("An exception occurred: %s" % e)
self.logger.error("Reconnecting")
self.logger.error("Disconnecting due to exception: %s" % e)
self._net.disconnect()
time.sleep(0.1)
time.sleep(1)
def stop(self):
self.logger.debug("Stopping")

View File

@ -44,7 +44,6 @@ class Network:
return
try:
logger.info("Connecting to %s" % self._server_address)
if self._server_address.startswith("/"):
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.socket.connect(self._server_address)
@ -55,9 +54,10 @@ class Network:
self.socket.connect((host, int(port)))
self.socket.settimeout(self._timeout)
self._connected = True
logger.info("Connected to %s" % self._server_address)
except socket.error:
logger.error("Failed to connect to %s" % self._server_address)
self._connected = False
logger.error("Failed to connect to %s" % self._server_address)
def disconnect(self):
if not self._connected: