HTTP push in Rust

Over the years I deployed a lot of sensor pushing Python scripts to ESPs, RaspberryPIs and even some PCs. With a system update a few weeks ago a virtualenv broke and I lost some data, so replacing some of the Python scripts with Rust seems like a worthwhile endeavour. I started with the nvidia-gpu metrics because this is the system I lost data from.

The original Python code looks like this:

import requests
import nvidia_smi

handle = nvidia_smi.nvmlDeviceGetHandleByIndex(0)
gpu_temp = nvidia_smi.nvmlDeviceGetTemperature(handle, 0)
res = nvidia_smi.nvmlDeviceGetUtilizationRates(handle)
data = {
    "sensordatavalues": [
        {"value_type": "gpu_temperature", "value": "{:2.0f}".format(gpu_temp)},
        {"value_type": "gpu_load", "value": "{:2.0f}".format(res.gpu)},
r ="", headers={"Sensor": "ID"}, json=data)

The datastructure is a decision from years ago (2015ish) and I don't want to change it now, so the Rust code has to do the same. All pushes happen within a VPN, so there is no auth and no https. The Rust version uses a CLI to get the url and the sensor-id. This was not necessary for the Python version, because it can be easily edited on the target system.

The code of the Rust version:

What I learned when building it:

  • when it compiles it will probably run, is a cool guarantee

  • eglot / lsp based on rust-analyzer is really nice

  • it takes a lot more time for me to write Rust code compared to Python - this will improve over time

  • a code review with an expert (a colleague of mine) helped me a lot

This fulfills my curiosity on trying something new. So next step is to deploy a version on a raspberry pi (arm64) and add more sensors.