Advertisement
jarekmor

nvidia-smi_XML_to_mqtt

Feb 8th, 2023 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2. import subprocess
  3. import xml.etree.ElementTree as ET
  4. import xmltodict
  5. import json
  6. import csv
  7. import time
  8.  
  9. # MQTT broker and topic information
  10. HOST = "192.168.1.5"
  11. TOPIC = "nvidia/params"
  12. USER = "mqtt"
  13. PASSWD = "mqtt#"
  14.  
  15.     # Capture the output of nvidia-smi
  16. def send_nvidia_mqtt():
  17.     result = subprocess.run(["nvidia-smi", "-q", "-x"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  18.     xml_output = result.stdout.decode("utf-8")
  19.    
  20.     # xml parsing
  21.     root = ET.fromstring(xml_output)
  22.    
  23.     # xml to dict conversion
  24.     data_dict = xmltodict.parse(xml_output)['nvidia_smi_log']['gpu']
  25.    
  26.     # dict to json conversion
  27.     json_dict = {
  28.         "fan_speed": data_dict['fan_speed'],
  29.         "performance_state": data_dict['performance_state'],
  30.         "fb_memory_usage": data_dict['fb_memory_usage'],
  31.         "utilization": data_dict['utilization'],
  32.         "temperature": data_dict['temperature'],
  33.         "power_readings": data_dict['power_readings'],
  34.         "clocks": data_dict['clocks'],
  35.     }
  36.  
  37.     # json object creation
  38.     json_object = json.dumps(json_dict, indent=4)
  39.        
  40.     # Create MQTT client and connect to the broker
  41.     client = mqtt.Client()
  42.     client.username_pw_set(USER, PASSWD)
  43.     client.connect(HOST, 1883, 60)
  44.    
  45.     # Publish the JSON data to the desired topic
  46.     client.publish(TOPIC, json_object)
  47.    
  48.     # Disconnect from the broker
  49.     client.disconnect()
  50.  
  51. # Main function
  52. if __name__ == "__main__":
  53.     while True:
  54.         send_nvidia_mqtt()
  55.         time.sleep(5)       # Wait for 5 seconds
  56.  
  57. =====================================================================================================
  58. # Output (JSON):
  59. {
  60.     "fan_speed": "0 %",
  61.     "performance_state": "P8",
  62.     "fb_memory_usage": {
  63.         "total": "5942 MiB",
  64.         "used": "407 MiB",
  65.         "free": "5535 MiB"
  66.     },
  67.     "utilization": {
  68.         "gpu_util": "7 %",
  69.         "memory_util": "7 %",
  70.         "encoder_util": "0 %",
  71.         "decoder_util": "0 %"
  72.     },
  73.     "temperature": {
  74.         "gpu_temp": "45 C",
  75.         "gpu_temp_max_threshold": "96 C",
  76.         "gpu_temp_slow_threshold": "93 C",
  77.         "gpu_temp_max_gpu_threshold": "91 C",
  78.         "gpu_target_temperature": "83 C",
  79.         "memory_temp": "N/A",
  80.         "gpu_temp_max_mem_threshold": "N/A"
  81.     },
  82.     "power_readings": {
  83.         "power_state": "P8",
  84.         "power_management": "Supported",
  85.         "power_draw": "17.24 W",
  86.         "power_limit": "125.00 W",
  87.         "default_power_limit": "125.00 W",
  88.         "enforced_power_limit": "125.00 W",
  89.         "min_power_limit": "70.00 W",
  90.         "max_power_limit": "150.00 W"
  91.     },
  92.     "clocks": {
  93.         "graphics_clock": "300 MHz",
  94.         "sm_clock": "300 MHz",
  95.         "mem_clock": "405 MHz",
  96.         "video_clock": "540 MHz"
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement