-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·55 lines (41 loc) · 1.31 KB
/
main.py
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
import logging
import os
import time
from dotenv import load_dotenv
import psutil
import thingspeak
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("system_metrics.log"),
logging.StreamHandler()
]
)
# Load environment variables from a .env file
load_dotenv()
# Constants for Thingspeak configuration
CHANNEL_ID = os.getenv('CHANNEL_ID')
WRITE_API_KEY = os.getenv('WRITE_API_KEY')
channel = thingspeak.Channel(id=CHANNEL_ID, api_key=WRITE_API_KEY)
def update_channels():
cpu_usage = psutil.cpu_percent()
memory_usage = psutil.virtual_memory().percent
try:
# Send data to ThingSpeak
response = channel.update({1: cpu_usage, 2: memory_usage})
# Log data and response
logging.info(f"CPU Usage (%): {cpu_usage}")
logging.info(f"Memory Usage (%): {memory_usage}")
logging.info(f"ThingSpeak Response: {response}")
except Exception as ex:
logging.error(f"Connection failed: {ex}")
def main():
if not CHANNEL_ID or not WRITE_API_KEY:
raise ValueError("CHANNEL_ID and WRITE_API_KEY must be set in the environment variables.")
while True:
update_channels()
time.sleep(16)
if __name__ == "__main__":
main()