8
8
import aiohttp
9
9
10
10
# Import app libraries
11
- from switchmap import API_POLLER_POST_URI
11
+ from switchmap import API_POLLER_POST_URI , API_PREFIX
12
12
from switchmap .poller .snmp import async_poller
13
13
from switchmap .poller .update import device as udevice
14
14
from switchmap .poller .configuration import ConfigPoller
@@ -35,13 +35,16 @@ async def devices(max_concurrent_devices=None):
35
35
config = ConfigPoller ()
36
36
37
37
# 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
40
41
41
42
# Create a list of polling objects
42
43
zones = sorted (config .zones (), key = lambda z : z .name )
43
44
44
45
for zone in zones :
46
+ if not zone .hostnames :
47
+ continue
45
48
arguments .extend (
46
49
_META (zone = zone .name , hostname = _ , config = config )
47
50
for _ in zone .hostnames
@@ -60,8 +63,9 @@ async def devices(max_concurrent_devices=None):
60
63
61
64
# Semaphore to limit concurrent devices
62
65
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 :
65
69
tasks = [
66
70
device (argument , device_semaphore , session , post = True )
67
71
for argument in arguments
@@ -149,8 +153,13 @@ async def device(poll_meta, device_semaphore, session, post=True):
149
153
150
154
if post :
151
155
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
+
152
161
async with session .post (
153
- API_POLLER_POST_URI , json = data
162
+ url , json = data
154
163
) as res :
155
164
if res .status == 200 :
156
165
log_message = (
@@ -168,7 +177,7 @@ async def device(poll_meta, device_semaphore, session, post=True):
168
177
log_message = (
169
178
f"HTTP error posting data for { hostname } : { e } "
170
179
)
171
- log .log2exception (1415 , log_message )
180
+ log .log2warning (1415 , log_message )
172
181
return False
173
182
174
183
else :
@@ -183,9 +192,13 @@ async def device(poll_meta, device_semaphore, session, post=True):
183
192
log .log2debug (1408 , log_message )
184
193
return False
185
194
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
186
199
except Exception as e :
187
200
log_message = f"Unexpected error polling device { hostname } : { e } "
188
- log .log2exception (1409 , log_message )
201
+ log .log2warning (1409 , log_message )
189
202
return False
190
203
191
204
@@ -209,6 +222,8 @@ async def cli_device(hostname):
209
222
210
223
# Create a list of arguments
211
224
for zone in zones :
225
+ if not zone .hostnames :
226
+ continue
212
227
for next_hostname in zone .hostnames :
213
228
if next_hostname == hostname :
214
229
arguments .append (
0 commit comments