@@ -28,7 +28,6 @@ async def devices(max_concurrent_devices=None):
28
28
Returns:
29
29
None
30
30
"""
31
-
32
31
# Initialize key variables
33
32
arguments = []
34
33
@@ -53,29 +52,36 @@ async def devices(max_concurrent_devices=None):
53
52
log .log2info (1400 , log_message )
54
53
return
55
54
56
- log_message = f"Starting async polling of { len (arguments )} devices with max concurrency: { max_concurrent_devices } "
55
+ log_message = (
56
+ f"Starting async polling of { len (arguments )} devices "
57
+ f"with max concurrency: { max_concurrent_devices } "
58
+ )
57
59
log .log2info (1401 , log_message )
58
60
59
61
# Semaphore to limit concurrent devices
60
62
device_semaphore = asyncio .Semaphore (max_concurrent_devices )
61
63
62
64
async with aiohttp .ClientSession () as session :
63
- tasks = [
64
- device (argument , device_semaphore , session , post = True ) for argument in arguments
65
+ tasks = [
66
+ device (argument , device_semaphore , session , post = True )
67
+ for argument in arguments
65
68
]
66
69
# Execute all devices concurrently
67
- start_time = time .time ()
68
- results = await asyncio .gather (* tasks , return_exceptions = True )
69
- end_time = time .time ()
70
+ start_time = time .time ()
71
+ results = await asyncio .gather (* tasks , return_exceptions = True )
72
+ end_time = time .time ()
70
73
71
74
# Process results and log summary
72
75
success_count = sum (1 for r in results if r is True )
73
76
error_count = sum (1 for r in results if isinstance (r , Exception ))
74
77
failed_count = len (results ) - success_count - error_count
75
78
76
- log_message = f"Polling completed in { end_time - start_time :.2f} s: { success_count } succeeded, { failed_count } failed, { error_count } errors"
79
+ log_message = (
80
+ f"Polling completed in { end_time - start_time :.2f} s: "
81
+ f"{ success_count } succeeded, { failed_count } failed, "
82
+ f"{ error_count } errors"
83
+ )
77
84
log .log2info (1402 , log_message )
78
-
79
85
# Log specific errors
80
86
for i , result in enumerate (results ):
81
87
if isinstance (result , Exception ):
@@ -84,18 +90,18 @@ async def devices(max_concurrent_devices=None):
84
90
log .log2warning (1403 , log_message )
85
91
86
92
87
- async def device (poll_meta , device_semaphore , session ,post = True ):
88
- """Poll each device asynchoronously .
93
+ async def device (poll_meta , device_semaphore , session , post = True ):
94
+ """Poll each device asynchronously .
89
95
90
96
Args:
91
97
poll_meta: _META object containing zone, hostname, config
92
98
device_semaphore: Semaphore to limit concurrent devices
99
+ session: aiohttp ClientSession for HTTP requests
93
100
post: Post the data if True, else just print it
94
101
95
102
Returns:
96
103
bool: True if successful, False otherwise
97
104
"""
98
-
99
105
async with device_semaphore :
100
106
# Initialize key variables
101
107
hostname = poll_meta .hostname
@@ -105,7 +111,10 @@ async def device(poll_meta, device_semaphore, session,post=True):
105
111
# Do nothing if the skip file exists
106
112
skip_file = files .skip_file (AGENT_POLLER , config )
107
113
if os .path .isfile (skip_file ):
108
- log_message = f"Skip file { skip_file } found. Aborting poll for { hostname } in zone '{ zone } '"
114
+ log_message = (
115
+ f"Skip file { skip_file } found. Aborting poll for "
116
+ f"{ hostname } in zone '{ zone } '"
117
+ )
109
118
log .log2debug (1404 , log_message )
110
119
return False
111
120
@@ -140,15 +149,25 @@ async def device(poll_meta, device_semaphore, session,post=True):
140
149
141
150
if post :
142
151
try :
143
- async with session .post (API_POLLER_POST_URI , json = data ) as res :
152
+ async with session .post (
153
+ API_POLLER_POST_URI , json = data
154
+ ) as res :
144
155
if res .status == 200 :
145
- log_message = f"Successfully polled and posted data for { hostname } "
156
+ log_message = (
157
+ f"Successfully polled and posted data "
158
+ f"for { hostname } "
159
+ )
146
160
log .log2debug (1407 , log_message )
147
161
else :
148
- log_message = f"Failed to post data for { hostname } , status={ res .status } "
162
+ log_message = (
163
+ f"Failed to post data for { hostname } , "
164
+ f"status={ res .status } "
165
+ )
149
166
log .log2warning (1414 , log_message )
150
167
except aiohttp .ClientError as e :
151
- log_message = f"HTTP error posting data for { hostname } : { e } "
168
+ log_message = (
169
+ f"HTTP error posting data for { hostname } : { e } "
170
+ )
152
171
log .log2exception (1415 , log_message )
153
172
return False
154
173
@@ -157,7 +176,10 @@ async def device(poll_meta, device_semaphore, session,post=True):
157
176
158
177
return True
159
178
else :
160
- log_message = f"Device { hostname } returns no data. Check connectivity/SNMP configuration"
179
+ log_message = (
180
+ f"Device { hostname } returns no data. Check "
181
+ f"connectivity/SNMP configuration"
182
+ )
161
183
log .log2debug (1408 , log_message )
162
184
return False
163
185
@@ -201,16 +223,20 @@ async def cli_device(hostname):
201
223
202
224
# Poll each zone occurrence
203
225
semaphore = asyncio .Semaphore (1 )
204
- tasks = [
205
- device (argument , semaphore , post = False )
206
- for argument in arguments
207
- ]
208
- results = await asyncio .gather (* tasks , return_exceptions = True )
226
+ async with aiohttp .ClientSession () as session :
227
+ tasks = [
228
+ device (argument , semaphore , session , post = False )
229
+ for argument in arguments
230
+ ]
231
+ results = await asyncio .gather (* tasks , return_exceptions = True )
209
232
210
233
# Check results
211
234
success_count = sum (1 for r in results if r is True )
212
235
if success_count > 0 :
213
- log_message = f"Successfully polled { hostname } from { success_count } /{ len (results )} zone(s)"
236
+ log_message = (
237
+ f"Successfully polled { hostname } from "
238
+ f"{ success_count } /{ len (results )} zone(s)"
239
+ )
214
240
log .log2info (1411 , log_message )
215
241
else :
216
242
log_message = f"Failed to poll { hostname } from any configured zone"
0 commit comments