53 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
# Simple Metrics Generator
 | 
						|
 | 
						|
Simple Prometheus metrics generator.
 | 
						|
 | 
						|
## Usage
 | 
						|
 | 
						|
```bash
 | 
						|
cat metadata | generate_metrics tempurature 50
 | 
						|
```
 | 
						|
 | 
						|
with your metadata file looking something like
 | 
						|
 | 
						|
```
 | 
						|
unit="F", computer="computer5"
 | 
						|
```
 | 
						|
 | 
						|
---
 | 
						|
 | 
						|
From here you could then redirect stdout into a file, then host the file with something like Caddy for Prometheus to scrape.
 | 
						|
 | 
						|
## Examples
 | 
						|
 | 
						|
A simple "down detector". Put this in a crontab (runs every minute):
 | 
						|
```bash
 | 
						|
* * * * * cat /etc/metrics_metadata | generate_metrics cloudflare $(ping -c 1 1.1.1.1 &> /dev/null ; echo $?) > /srv/metrics/cloudflare
 | 
						|
```
 | 
						|
 | 
						|
---
 | 
						|
 | 
						|
Actual down detector I'm using:
 | 
						|
 | 
						|
```bash
 | 
						|
#!/bin/bash
 | 
						|
 | 
						|
# Clear the file
 | 
						|
echo "" > /srv/metrics
 | 
						|
 | 
						|
targets=("1.1.1.1" "192.168.1.1" "oliveratkinson.net")
 | 
						|
 | 
						|
for i in "${targets[@]}"
 | 
						|
do
 | 
						|
        export TARGET=$i
 | 
						|
        envsubst < /root/metrics_metadata | /root/generate_metrics is_down $(ping -c 1 $TARGET &> /dev/null; echo $?) >> /srv/metrics
 | 
						|
done
 | 
						|
```
 | 
						|
```caddyfile
 | 
						|
:80 {
 | 
						|
        handle /metrics {
 | 
						|
                file_server /srv/metrics
 | 
						|
        }
 | 
						|
}
 | 
						|
```
 |