88from sqlalchemy .exc import SQLAlchemyError
99from prometheus_client import Counter , Gauge
1010import os , time
11- from influxdb_client import Point , WritePrecision
1211
1312from src .logger import logger
14- from src .config import influx_client , bucket , write_api
13+ from src .influxdb_config import bucket , write_api
14+ from src .utils import get_system_username
1515# Flag to track if logging is already scheduled
1616is_logging_scheduled = False
17+ fetch_system_info_interval = 30
1718
1819# Initialize Prometheus metrics
1920metrics = {
@@ -44,7 +45,7 @@ def log_system_info():
4445
4546 log_system_info_to_db ()
4647 logger .debug ("System information logged successfully." )
47- schedule_next_log ()
48+ schedule_next_log (interval = fetch_system_info_interval )
4849
4950 except Exception as e :
5051 logger .error (f"Error during system info logging: { e } " , exc_info = True )
@@ -63,7 +64,7 @@ def is_logging_enabled():
6364 return False
6465
6566
66- def schedule_next_log (interval = 60 ):
67+ def schedule_next_log (interval = 10 ):
6768 """
6869 Schedules the next logging event after the specified interval (in seconds).
6970 """
@@ -80,8 +81,9 @@ def log_system_info_to_db():
8081
8182 # Update Prometheus metrics
8283 update_prometheus_metrics (system_info )
84+
8385 # Store system information in InfluxDB
84- store_system_info_in_influxdb (system_info )
86+ # store_system_info_in_influxdb(system_info)
8587
8688 # Store system information in the database
8789 # store_system_info_in_db(system_info)
@@ -128,34 +130,34 @@ def store_system_info_in_db(system_info):
128130 db .session .add (system_log )
129131 db .session .commit ()
130132
131- def store_system_info_in_influxdb (system_info ):
132- """
133- Stores the collected system information into the InfluxDB with proper error handling.
134- """
135- try :
136- # Create a data point for system information
137- point = (
138- Point ("system_info" )
139- .tag ("host" , os . environ . get ( "HOSTNAME" , "unknown" ))
140- .field ("cpu_percent" , system_info ["cpu_percent" ])
141- .field ("memory_percent" , system_info ["memory_percent" ])
142- .field ("battery_percent" , system_info ["battery_percent" ])
143- .field ("network_sent" , system_info ["network_sent" ])
144- .field ("network_received" , system_info ["network_received" ])
145- .field ("dashboard_memory_usage" , system_info ["dashboard_memory_usage" ])
146- .field ("cpu_frequency" , system_info ["cpu_frequency" ])
147- .field ("current_temp" , system_info ["current_temp" ])
148- .time (int (time .time () * 1_000_000_000 ), WritePrecision . NS ) # Nanosecond precision
149- )
150-
151- # Write the data point to InfluxDB
152- write_api .write (bucket = bucket , record = point )
153- logger .info ("Successfully wrote system information to InfluxDB" )
154-
155- except ValueError as ve :
156- logger .error (f"Value error while storing system info: { ve } " )
157- except Exception as e :
158- logger .error (f"An unexpected error occurred: { e } " , exc_info = True )
133+ # def store_system_info_in_influxdb(system_info):
134+ # """
135+ # Stores the collected system information into the InfluxDB with proper error handling.
136+ # """
137+ # try:
138+ # # Create a data point for system information
139+ # point = (
140+ # Point("system_info")
141+ # .tag("host", get_system_username( ))
142+ # .field("cpu_percent", system_info["cpu_percent"])
143+ # .field("memory_percent", system_info["memory_percent"])
144+ # .field("battery_percent", system_info["battery_percent"])
145+ # .field("network_sent", system_info["network_sent"])
146+ # .field("network_received", system_info["network_received"])
147+ # .field("dashboard_memory_usage", system_info["dashboard_memory_usage"])
148+ # .field("cpu_frequency", system_info["cpu_frequency"])
149+ # .field("current_temp", system_info["current_temp"])
150+ # .time(int(time.time() * 1_000_000_000))
151+ # )
152+
153+ # # Write the data point to InfluxDB
154+ # write_api.write(bucket=bucket, record=point)
155+ # logger.info("Successfully wrote system information to InfluxDB")
156+
157+ # except ValueError as ve:
158+ # logger.error(f"Value error while storing system info: {ve}")
159+ # except Exception as e:
160+ # logger.error(f"An unexpected error occurred: {e}", exc_info=True)
159161
160162
161163
0 commit comments