prometheus
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| prometheus [2024/02/07 17:34] – ssm2017 | prometheus [2025/09/09 09:33] (current) – [rpi temperature] ssm2017 | ||
|---|---|---|---|
| Line 197: | Line 197: | ||
| security_level: | security_level: | ||
| auth_protocol: | auth_protocol: | ||
| - | username: | + | username: |
| - | password: | + | password: |
| priv_protocol: | priv_protocol: | ||
| - | priv_password: | + | priv_password: |
| modules: | modules: | ||
| synology: | synology: | ||
| Line 2969: | Line 2969: | ||
| community: synology | community: synology | ||
| </ | </ | ||
| + | ==== rpi temperature ==== | ||
| + | source : brave | ||
| + | prompt : | ||
| + | < | ||
| + | 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="/ | ||
| + | OUTPUT_FILE=" | ||
| + | |||
| + | # Check if output directory exists | ||
| + | if [ ! -d " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # Check if we can write to the directory | ||
| + | if [ ! -w " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # Read raw temperature (in millidegrees Celsius) | ||
| + | TEMP_MILLI=$(cat / | ||
| + | |||
| + | if [ -z " | ||
| + | echo " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # Convert to degrees Celsius | ||
| + | TEMP_C=$(echo " | ||
| + | |||
| + | # Write metric to temporary file then move (atomic write) | ||
| + | TEMP_FILE=" | ||
| + | cat > " | ||
| + | # 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 " | ||
| + | |||
| + | exit 0 | ||
| + | </ | ||
| + | |||
| + | === ✅ Setup Instructions === | ||
| + | |||
| + | - 1. **Save the script** (e.g., as `/ | ||
| + | |||
| + | <sxh bash> | ||
| + | sudo nano / | ||
| + | </ | ||
| + | |||
| + | - 2. **Make it executable**: | ||
| + | |||
| + | <sxh bash> | ||
| + | sudo chmod +x / | ||
| + | </ | ||
| + | |||
| + | - 3. **Ensure the textfile collector directory exists**: | ||
| + | |||
| + | <sxh bash> | ||
| + | sudo mkdir -p / | ||
| + | </ | ||
| + | |||
| + | - 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 | ||
| + | </ | ||
| + | |||
| + | You should see something like: | ||
| + | <sxh> | ||
| + | | ||
| + | </ | ||
| + | then | ||
| + | <sxh bash> | ||
| + | systemctl daemon-reexec | ||
| + | </ | ||
| + | |||
| + | - 5. **Run the script manually to test**: | ||
| + | |||
| + | <sxh bash> | ||
| + | sudo / | ||
| + | </ | ||
| + | |||
| + | Then check the output: | ||
| + | |||
| + | <sxh bash> | ||
| + | cat / | ||
| + | </ | ||
| + | |||
| + | 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 | ||
| + | </ | ||
| + | |||
| + | - 6. **Set up a cron job to run every minute**: | ||
| + | |||
| + | <sxh> | ||
| + | sudo crontab -e | ||
| + | </ | ||
| + | |||
| + | Add this line: | ||
| + | |||
| + | <sxh> | ||
| + | * * * * * / | ||
| + | </ | ||
| + | |||
| + | ⚠️ Run as root or a user with access to '/ | ||
| + | |||
| + | === ✅ Prometheus Metric === | ||
| + | |||
| + | After setup, the metric will be available at your ' | ||
| + | |||
| + | <sxh> | ||
| + | rpi_cpu_temperature_celsius | ||
| + | </ | ||
| + | |||
| + | You can now graph or alert on this in Prometheus or Grafana. | ||
| + | |||
| + | === 📝 Notes === | ||
| + | |||
| + | * The default `node_exporter` textfile directory is often `/ | ||
| + | * Ensure the user running the script can read `/ | ||
| + | * Using `bc` allows for decimal math. Install it with `sudo apt install bc` if needed. | ||
| + | |||
prometheus.1707323668.txt.gz · Last modified: by ssm2017
