-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (65 loc) · 2.24 KB
/
main.py
File metadata and controls
81 lines (65 loc) · 2.24 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import psutil
import requests
import time
import os
from dotenv import load_dotenv
import schedule
# Load environment variables from .env file
load_dotenv()
# URL of the server to send the POST requests
SERVER_URL = f"{os.environ.get('SERVER_URL')}/metrics"
def get_system_info():
# CPU utilization
cpu_percent = psutil.cpu_percent(interval=1)
# RAM utilization
ram = psutil.virtual_memory()
ram_total = ram.total
ram_used = ram.used
ram_percent = ram.percent
# Disk space utilization
disk_usage = psutil.disk_usage("/")
disk_total = disk_usage.total
disk_used = disk_usage.used
disk_percent = disk_usage.percent
# Network information
network_info = psutil.net_io_counters()
network_bytes_sent = network_info.bytes_sent
network_bytes_recv = network_info.bytes_recv
# System uptime
uptime = int(time.time() - psutil.boot_time())
# Battery information (for laptops and mobile devices)
battery = psutil.sensors_battery()
battery_percent = battery.percent if battery else None
system_info = {
"cpu_utilization": cpu_percent,
"ram_total": ram_total,
"ram_used": ram_used,
"ram_utilization": ram_percent,
"disk_total": disk_total,
"disk_used": disk_used,
"disk_utilization": disk_percent,
"network_bytes_sent": network_bytes_sent,
"network_bytes_recv": network_bytes_recv,
"uptime_seconds": uptime,
"authentication": os.environ.get("TOKEN"),
}
return system_info
def send_system_info():
try:
system_info = get_system_info()
response = requests.post(SERVER_URL, json=system_info)
if response.status_code == 200:
print("System information sent successfully.")
else:
print(
f"Failed to send system information. Status code: {response.status_code}"
)
except requests.exceptions.RequestException as e:
print(f"Error sending system information: {e}")
if __name__ == "__main__":
# Run the script every 10 minutes (600 seconds)
schedule.every(10).minutes.do(send_system_info)
# Keep the script running to handle scheduled tasks
while True:
schedule.run_pending()
time.sleep(1)