|
5 | 5 | import time |
6 | 6 | import xml.etree.ElementTree as ET |
7 | 7 |
|
| 8 | +import fabric |
8 | 9 | import numpy as np |
9 | 10 | from src.collector.interface import ( |
10 | 11 | CollectorOutputReaderException, |
@@ -290,28 +291,29 @@ def save_csv(self, csv_file: str): |
290 | 291 | Path to CSV file. Local file, CSV will be downloaded when collector running on remote. |
291 | 292 | """ |
292 | 293 | header = CSV_HEADER_TO_ANALYZER_HEADER.values() |
293 | | - |
294 | | - batch_size = 100_000 |
295 | | - buffer = [] |
| 294 | + conn: fabric.Connection = self._executor._connection |
| 295 | + user = conn.user |
| 296 | + host = conn.host |
| 297 | + kwargs = conn.connect_kwargs |
| 298 | + |
| 299 | + if "key_filename" in kwargs: |
| 300 | + key_filename = kwargs["key_filename"] |
| 301 | + ssh_cmd = ( |
| 302 | + f"ssh -i {key_filename} {user}@{host} '{self._cmd_csv}' >> {csv_file}" |
| 303 | + ) |
| 304 | + else: |
| 305 | + password = kwargs["password"] |
| 306 | + ssh_cmd = f"sshpass -p {password} ssh {user}@{host} '{self._cmd_csv}' >> {csv_file}" |
296 | 307 |
|
297 | 308 | logging.getLogger().info( |
298 | 309 | "Preparing CSV output by calling ipfixcol2 + jq command..." |
299 | 310 | ) |
300 | 311 | start = time.time() |
301 | 312 | # write csv |
302 | | - process = AsyncTool(self._cmd_csv, executor=self._executor) |
303 | | - process.run() |
304 | | - |
305 | | - with open(csv_file, mode="w", buffering=1024**2) as f: |
| 313 | + with open(csv_file, mode="w") as f: |
306 | 314 | f.write(",".join(header) + "\n") |
307 | | - for line in process.stdout: |
308 | | - buffer.append(line + "\n") |
309 | | - if len(buffer) >= batch_size: |
310 | | - f.writelines(buffer) |
311 | | - buffer = [] |
312 | | - |
313 | | - if buffer: |
314 | | - f.writelines(buffer) |
| 315 | + |
| 316 | + Tool(ssh_cmd).run() |
315 | 317 |
|
316 | 318 | end = time.time() |
317 | 319 | logging.getLogger().info("CSV output saved in %.2f seconds.", (end - start)) |
0 commit comments