Skip to content

Commit 0d31de7

Browse files
committed
chore: cleanup & refactor logs
1 parent d8dbc7d commit 0d31de7

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

switchmap/core/configuration.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,6 @@ def __init__(self):
5555
)
5656
log.log2die_safe(1006, log_message)
5757

58-
def agent_subprocesses(self):
59-
"""Get agent_subprocesses.
60-
61-
Args:
62-
None
63-
64-
Returns:
65-
result: result
66-
67-
"""
68-
# Get threads
69-
threads = max(1, self._config_core.get("agent_subprocesses", 20))
70-
71-
# Get CPU cores
72-
cores = multiprocessing.cpu_count()
73-
desired_max_threads = max(1, cores - 1)
74-
75-
# We don't want a value that's too big that the CPU cannot cope
76-
result = min(threads, desired_max_threads)
77-
78-
# Return
79-
return result
80-
8158
def api_log_file(self, daemon):
8259
"""Get api_log_file.
8360

switchmap/poller/async_poll.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import aiohttp
99

1010
# Import app libraries
11-
from switchmap import API_POLLER_POST_URI
11+
from switchmap import API_POLLER_POST_URI,API_PREFIX
1212
from switchmap.poller.snmp import async_poller
1313
from switchmap.poller.update import device as udevice
1414
from switchmap.poller.configuration import ConfigPoller
@@ -35,13 +35,16 @@ async def devices(max_concurrent_devices=None):
3535
config = ConfigPoller()
3636

3737
# Use config value if not provided
38-
if max_concurrent_devices is None:
39-
max_concurrent_devices = config.agent_subprocesses()
38+
if not isinstance(max_concurrent_devices,int) or max_concurrent_devices < 1:
39+
log.log2warning(1401, f"Invalid concurrency={max_concurrent_devices}; defaulting to 1")
40+
max_concurrent_devices = 1
4041

4142
# Create a list of polling objects
4243
zones = sorted(config.zones(), key=lambda z: z.name)
4344

4445
for zone in zones:
46+
if not zone.hostnames:
47+
continue
4548
arguments.extend(
4649
_META(zone=zone.name, hostname=_, config=config)
4750
for _ in zone.hostnames
@@ -60,8 +63,9 @@ async def devices(max_concurrent_devices=None):
6063

6164
# Semaphore to limit concurrent devices
6265
device_semaphore = asyncio.Semaphore(max_concurrent_devices)
63-
64-
async with aiohttp.ClientSession() as session:
66+
67+
timeout = aiohttp.ClientTimeout(total=30)
68+
async with aiohttp.ClientSession(timeout=timeout) as session:
6569
tasks = [
6670
device(argument, device_semaphore, session, post=True)
6771
for argument in arguments
@@ -149,8 +153,13 @@ async def device(poll_meta, device_semaphore, session, post=True):
149153

150154
if post:
151155
try:
156+
# Construct full URL for posting
157+
url = f"{config.server_url_root()}{API_PREFIX}{API_POLLER_POST_URI}"
158+
log_message = f"Posting data for {hostname} to {url}"
159+
log.log2debug(1416, log_message)
160+
152161
async with session.post(
153-
API_POLLER_POST_URI, json=data
162+
url, json=data
154163
) as res:
155164
if res.status == 200:
156165
log_message = (
@@ -168,7 +177,7 @@ async def device(poll_meta, device_semaphore, session, post=True):
168177
log_message = (
169178
f"HTTP error posting data for {hostname}: {e}"
170179
)
171-
log.log2exception(1415, log_message)
180+
log.log2warning(1415, log_message)
172181
return False
173182

174183
else:
@@ -183,9 +192,13 @@ async def device(poll_meta, device_semaphore, session, post=True):
183192
log.log2debug(1408, log_message)
184193
return False
185194

195+
except (asyncio.TimeoutError, KeyError, ValueError) as e:
196+
log_message = f"Recoverable error polling device {hostname}: {e}"
197+
log.log2warning(1409, log_message)
198+
return False
186199
except Exception as e:
187200
log_message = f"Unexpected error polling device {hostname}: {e}"
188-
log.log2exception(1409, log_message)
201+
log.log2warning(1409, log_message)
189202
return False
190203

191204

@@ -209,6 +222,8 @@ async def cli_device(hostname):
209222

210223
# Create a list of arguments
211224
for zone in zones:
225+
if not zone.hostnames:
226+
continue
212227
for next_hostname in zone.hostnames:
213228
if next_hostname == hostname:
214229
arguments.append(

switchmap/poller/snmp/async_snmp_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async def everything(self):
5656
keys = ["misc", "system", "layer1", "layer2", "layer3"]
5757
for key, result in zip(keys, results):
5858
if isinstance(result, Exception):
59-
log.log2warning(f"{key} failed: {result}")
59+
log.log2warning(1004, f"{key} failed: {result}")
6060
elif result:
6161
data[key] = result
6262

0 commit comments

Comments
 (0)