1- """
2- Example script that will dump all the info out of the dll and print it out
3- """
4- from libre_cli .libre_hardware_monitor_reporter import LibreHardwareMonitorReporter
5- import time
6- import argparse
7- import requests
1+ from influxdb_client import InfluxDBClient , Point
2+ from influxdb_client .client .write_api import SYNCHRONOUS
3+ import datetime
84
9- def send_to_influxdb (data , influx_url , token , org , bucket ):
10- """
11- Sends sensor data to an InfluxDBv2 server.
12- """
13- headers = {
14- "Authorization" : f"Token { token } " ,
15- "Content-Type" : "text/plain; charset=utf-8"
16- }
17- # This will take the payload from here and format it into an InfluxDB request
18- #payload = ""
19- #for sensor in data:
20- # Format data in InfluxDB line protocol
21- # payload += f"{sensor[0]},type={sensor[1]}, value={sensor[2]}\n"
22- # payload += f"{sensor.Hardware.Name},type={sensor.Name} value={sensor.Value}\n"
5+ class influx_manager :
6+ def __init__ (self ,url ,token ,org ,bucket ):
7+ self .url = url
8+ self .token = token
9+ self .org = org
10+ self .bucket = bucket
2311
24- # Convert the data to line protocol
25- lines = []
12+ # Initialize client
13+ self .client = InfluxDBClient (url = self .url , token = self .token , org = self .org )
14+ self .write_api = self .client .write_api (write_options = SYNCHRONOUS )
2615
27- for sensor_name , metric_name , value in data :
28- try :
29- metric_clean = metric_name . replace ( " " , "_" ). lower ()
30- sensor_name_clean = sensor_name . replace ( " " , "_" ). lower ()
31- line = f"sensor_metrics,sensor_name= { sensor_name_clean } { metric_clean } = { float ( value ) } "
32- lines . append ( line )
33- except ValueError :
34- print ( f"Skipping invalid value: { value } for { metric_name } " )
16+ def write_data ( self , data_point ) :
17+ # POINT DATA HERE
18+ point = Point ( data_point [ 0 ]) \
19+ . tag ( "component " , data_point [ 1 ]) \
20+ . tag ( "sensor" , data_point [ 2 ]) \
21+ . field ( "value" , data_point [ 3 ]) \
22+ . time ( datetime . datetime . utcnow ())
23+ self . write_api . write ( bucket = self . bucket , org = self . org , record = point )
3524
36- payload = "\n " .join (lines )
37-
38- try :
39- response = requests .post (
40- f"{ influx_url } /api/v2/write?org={ org } &bucket={ bucket } &precision=s" ,
41- headers = headers ,
42- data = payload
43- )
44- if response .status_code == 204 :
45- print ("Data successfully written to InfluxDB." )
46- else :
47- print (f"Failed to write to InfluxDB: { response .status_code } - { response .text } " )
48- except Exception as e :
49- print (f"Error sending data to InfluxDB: { e } " )
50-
51- if __name__ == "__main__" :
52- try :
53- parser = argparse .ArgumentParser (description = 'Check to see if any flags are present' )
54- parser .add_argument ('-t' , action = 'store_true' , help = 'If -t is present, will continue indefintely' )
55- # InfluxDB stuff
56- parser .add_argument ('--influx-url' , type = str , help = 'InfluxDBv2 server URL' )
57- parser .add_argument ('--token' , type = str , help = 'InfluxDBv2 authentication token' )
58- parser .add_argument ('--org' , type = str , help = 'InfluxDBv2 organization' )
59- parser .add_argument ('--bucket' , type = str , help = 'InfluxDBv2 bucket' )
60- # Parse the arguments
61- args = parser .parse_args ()
62- # Access the value of the -t argument
63- print ("Starting LibreHardwareMonitor..." )
64- LibreHardwareMonitorReport = LibreHardwareMonitorReporter ()
65- # If arg.t is present, report indefintely
66- if args .t :
67- while True :
68- LibreHardwareMonitorReport .get_sensor_data ()
69- for i in LibreHardwareMonitorReport .results :
70- print (i )
71- time .sleep (10 )
72- # Send data to InfluxDB if credentials are provided
73- if args .influx_url and args .token and args .org and args .bucket :
74- send_to_influxdb (
75- LibreHardwareMonitorReport .results ,
76- args .influx_url ,
77- args .token ,
78- args .org ,
79- args .bucket
80- )
81- else :
82- LibreHardwareMonitorReport .get_sensor_data ()
83- for i in LibreHardwareMonitorReport .results :
84- print (i )
85- # Send data to InfluxDB if credentials are provided
86- if args .influx_url and args .token and args .org and args .bucket :
87- send_to_influxdb (
88- LibreHardwareMonitorReport .results ,
89- args .influx_url ,
90- args .token ,
91- args .org ,
92- args .bucket
93- )
94- print ("Completed" )
95-
96- except KeyboardInterrupt :
97- # When being shut down, close the handle
98- LibreHardwareMonitorReport .handler .Close ()
99- print ("LibreHardwareMonitor closed." )
100- print ("Exiting..." )
25+ def exit (self ):
26+ self .client .close ()
0 commit comments