-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_run.py
More file actions
66 lines (56 loc) · 2.1 KB
/
Copy pathagent_run.py
File metadata and controls
66 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import asyncio
import os
import requests
import json
from dataclasses import asdict
from backend.scanner import AsyncNetworkScanner
API_URL = os.getenv("OMNISIGHT_URL", "http://localhost:8000")
API_KEY = os.getenv("OMNISIGHT_API_KEY")
async def main():
if not API_KEY:
print("Error: OMNISIGHT_API_KEY is not set.")
print("Please set your API key provided by the OmniSight Dashboard.")
print("Example: set OMNISIGHT_API_KEY=your_api_key_here")
return
print(f"Starting OmniSight Agent...")
print(f"Target Cloud: {API_URL}")
print("Initializing Network Scanner... (may take a few seconds)")
scanner = AsyncNetworkScanner(interval_s=20.0)
scanner.start_background()
while True:
await asyncio.sleep(15)
devices = scanner.get_devices_snapshot()
host_info = scanner.get_host_info()
if not devices:
continue
# Convert devices to dicts
devices_data = []
for d in devices:
d_dict = asdict(d)
if d_dict.get("last_seen"):
d_dict["last_seen"] = d_dict["last_seen"].isoformat()
devices_data.append(d_dict)
payload = {
"devices": devices_data,
"host_info": host_info
}
try:
res = requests.post(
f"{API_URL}/api/agent/push",
json=payload,
headers={"x-api-key": API_KEY},
timeout=10
)
if res.status_code == 200:
print(f"[{requests.utils.time.ctime()}] Pushed {len(devices)} devices successfully.")
elif res.status_code == 401:
print(f"[{requests.utils.time.ctime()}] Error: Invalid API Key. Please verify.")
else:
print(f"[{requests.utils.time.ctime()}] Error {res.status_code}: {res.text}")
except Exception as e:
print(f"[{requests.utils.time.ctime()}] Network error pushing to cloud: {e}")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nAgent stopped.")