Difference between revisions of "Mtr exporter"
Jump to navigation
Jump to search
(Created page with "``` package main import ( "encoding/json" "log" "net/http" "os/exec" "github.com/gorilla/mux" ) type MTRResult struct { // Define the structure to match MTR JSON outp...") |
|||
| Line 1: | Line 1: | ||
| + | |||
| + | mtr_probe.go | ||
``` | ``` | ||
package main | package main | ||
Revision as of 03:50, 14 June 2024
mtr_probe.go
package main
import (
"encoding/json"
"log"
"net/http"
"os/exec"
"github.com/gorilla/mux"
)
type MTRResult struct {
// Define the structure to match MTR JSON output
}
func runMTR(target string) (string, error) {
cmd := exec.Command("mtr", "--report", "--json", target)
out, err := cmd.Output()
if err != nil {
return "", err
}
return string(out), nil
}
func probeHandler(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
if target == "" {
target = "8.8.8.8"
}
result, err := runMTR(target)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(result))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/probe", probeHandler).Methods("GET")
http.Handle("/", r)
log.Println("Starting server on :9115")
log.Fatal(http.ListenAndServe(":9115", nil))
}
Dockerfile
# Start from the official Go image FROM golang:1.18 as builder # Set the Current Working Directory inside the container WORKDIR /app # Copy go.mod and go.sum files COPY go.mod go.sum ./ # Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . # Build the Go app RUN go build -o mtr_probe . # Use a minimal image to run the application FROM debian:bullseye-slim # Install MTR RUN apt-get update && apt-get install -y mtr-tiny # Copy the pre-built binary file from the previous stage COPY --from=builder /app/mtr_probe /usr/local/bin/mtr_probe # Expose port 9115 to the outside world EXPOSE 9115 # Command to run the executable CMD ["mtr_probe"]
go.mod
module mtr_probe go 1.18 require github.com/gorilla/mux v1.8.0
sh
# Build the Docker image docker build -t mtr-probe . # Run the Docker container docker run -d -p 9115:9115 mtr-probe
scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [mtr] # Look for an 'mtr' module in blackbox exporter config.
static_configs:
- targets:
- 'example.com'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115 # MTR prober address