-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
68 lines (57 loc) · 1.99 KB
/
test.py
File metadata and controls
68 lines (57 loc) · 1.99 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
import os
import time
import psutil
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
# Set up InfluxDB connection details
token = os.environ.get("INFLUXDB_TOKEN")
org = "root"
url = "http://localhost:8086"
# Initialize InfluxDB client
write_client = InfluxDBClient(url=url, token=token, org=org)
# Set the bucket
bucket = "root"
# Initialize write API
write_api = write_client.write_api(write_options=SYNCHRONOUS)
# Define a function to get CPU and memory utilization
def get_system_stats():
cpu_percent = psutil.cpu_percent()
memory_percent = psutil.virtual_memory().percent
return cpu_percent, memory_percent
def get_number_of_clients():
try:
with open('count.txt', 'r') as file:
# Read the integer value from the file
count_value = int(file.read())
return count_value
except:
pass
return 0
def get_active_clients(filename):
try:
with open(filename, 'r') as file:
# Read the integer value from the file
count_value = int(file.read())
return count_value
except:
pass
return 0
# Write CPU and memory utilization to InfluxDB
while True:
cpu_percent, memory_percent = get_system_stats()
# Create a data point
point = (
Point("system_stats")
.tag("host", "localhost") # Tag to identify the host
.field("cpu_percent", cpu_percent)
.field("memory_percent", memory_percent)
.field("client_count", get_number_of_clients())
.field("server_active_clients", get_active_clients('server.txt'))
.field("mirror1_active_clients", get_active_clients('mirror1.txt'))
.field("mirror2_active_clients", get_active_clients('mirror2.txt'))
)
print(get_active_clients('server.txt'))
# Write the data point to InfluxDB
write_api.write(bucket=bucket, org=org, record=point)
# Wait for 1 second before next measurement
time.sleep(1)