User Tools

Site Tools


prometheus

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
prometheus [2024/02/07 17:34] ssm2017prometheus [2025/09/09 09:33] (current) – [rpi temperature] ssm2017
Line 197: Line 197:
     security_level: authPriv     security_level: authPriv
     auth_protocol: MD5     auth_protocol: MD5
-    username: snmp-exporter +    username: myusername 
-    password: 5nmp-3xp02732+    password: mypassword
     priv_protocol: DES     priv_protocol: DES
-    priv_password: P21v-p2070c0l+    priv_password: otherpassword
 modules: modules:
   synology:   synology:
Line 2969: Line 2969:
       community: synology       community: synology
 </sxh> </sxh>
 +==== rpi temperature ====
 +source : brave
 +prompt :
 +<sxh>write  a script with node_exporter's textfile collector. This script reads the temperature from /sys/class/thermal/thermal_zone0/temp and writes it to a file in a format that node_exporter can scrape like /var/log/node_exporter</sxh>
 +answer :
 +Here's a simple Bash script that reads the Raspberry Pi's CPU temperature and writes it in Prometheus format to a file that `node_exporter` can scrape via the **textfile collector**:
 +
 +<sxh bash>
 +#!/bin/bash
 +
 +# Output directory for node_exporter textfile collector
 +OUTPUT_DIR="/var/lib/node_exporter/textfile_collector"
 +OUTPUT_FILE="$OUTPUT_DIR/rpi_cpu_temp.prom"
 +
 +# Check if output directory exists
 +if [ ! -d "$OUTPUT_DIR" ]; then
 +  echo "Error: Directory $OUTPUT_DIR does not exist. Please create it and ensure node_exporter has read access."
 +  exit 1
 +fi
 +
 +# Check if we can write to the directory
 +if [ ! -w "$OUTPUT_DIR" ]; then
 +  echo "Error: No write permission to $OUTPUT_DIR"
 +  exit 1
 +fi
 +
 +# Read raw temperature (in millidegrees Celsius)
 +TEMP_MILLI=$(cat /sys/class/thermal/thermal_zone0/temp 2>/dev/null)
 +
 +if [ -z "$TEMP_MILLI" ]; then
 +  echo "Error: Could not read temperature from /sys/class/thermal/thermal_zone0/temp"
 +  exit 1
 +fi
 +
 +# Convert to degrees Celsius
 +TEMP_C=$(echo "scale=2; $TEMP_MILLI / 1000" | bc -l)
 +
 +# Write metric to temporary file then move (atomic write)
 +TEMP_FILE="$OUTPUT_FILE.tmp"
 +cat > "$TEMP_FILE" << EOF
 +# HELP rpi_cpu_temperature_celsius CPU temperature of the Raspberry Pi
 +# TYPE rpi_cpu_temperature_celsius gauge
 +rpi_cpu_temperature_celsius $TEMP_C
 +EOF
 +
 +mv "$TEMP_FILE" "$OUTPUT_FILE"
 +
 +exit 0
 +</sxh>
 +
 +=== ✅ Setup Instructions ===
 +
 +  - 1. **Save the script** (e.g., as `/usr/local/bin/rpi_temp.sh`):
 +
 +<sxh bash>
 +sudo nano /usr/local/bin/rpi_temp.sh
 +</sxh>
 +
 +  - 2. **Make it executable**:
 +
 +<sxh bash>
 +sudo chmod +x /usr/local/bin/rpi_temp.sh
 +</sxh>
 +
 +  - 3. **Ensure the textfile collector directory exists**:
 +
 +<sxh bash>
 +sudo mkdir -p /var/lib/node_exporter/textfile_collector
 +</sxh>
 +
 +  - 4. **Verify node_exporter is running with the textfile collector enabled** (it is enabled by default). You can confirm with:
 +
 +<sxh bash>
 +node_exporter --help | grep textfile
 +</sxh>
 +
 +You should see something like:
 +<sxh>
 +   --collector.textfile.directory string   If set, will source metrics via text files in this directory, one per file.
 +</sxh>
 +then
 +<sxh bash>
 +systemctl daemon-reexec
 +</sxh>
 +
 +  - 5. **Run the script manually to test**:
 +
 +<sxh bash>
 +sudo /usr/local/bin/rpi_temp.sh
 +</sxh>
 +
 +Then check the output:
 +
 +<sxh bash>
 +cat /var/lib/node_exporter/textfile_collector/rpi_cpu_temp.prom
 +</sxh>
 +
 +Output should look like:
 +
 +<sxh>
 +# HELP rpi_cpu_temperature_celsius CPU temperature of the Raspberry Pi
 +# TYPE rpi_cpu_temperature_celsius gauge
 +rpi_cpu_temperature_celsius 42.3
 +</sxh>
 +
 +  - 6. **Set up a cron job to run every minute**:
 +
 +<sxh>
 +sudo crontab -e
 +</sxh>
 +
 +Add this line:
 +
 +<sxh>
 +* * * * * /usr/local/bin/rpi_temp.sh 2>&1
 +</sxh>
 +
 +⚠️ Run as root or a user with access to '/sys/class/thermal/thermal_zone0/temp'.
 +
 +=== ✅ Prometheus Metric ===
 +
 +After setup, the metric will be available at your 'node_exporter' metrics endpoint (usually 'http://<pi-ip>:9100/metrics') as:
 +
 +<sxh>
 +rpi_cpu_temperature_celsius
 +</sxh>
 +
 +You can now graph or alert on this in Prometheus or Grafana.
 +
 +=== 📝 Notes ===
 +
 +  * The default `node_exporter` textfile directory is often `/var/lib/node_exporter/textfile_collector`. Adjust if your system uses a different path (e.g., `/var/log/node_exporter`).
 +  * Ensure the user running the script can read `/sys/class/thermal/thermal_zone0/temp`.
 +  * Using `bc` allows for decimal math. Install it with `sudo apt install bc` if needed.
 +
prometheus.1707323668.txt.gz · Last modified: by ssm2017