forked from pinpox/restic-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (134 loc) · 3.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"os"
"os/exec"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type resticData struct {
Stats resticStatsData
Snapshots []resticSnapshotData
}
type resticStatsData struct {
TotalSize int `json:"total_size"`
TotalFileCount int `json:"total_file_count"`
}
type resticSnapshotData struct {
Time time.Time `json:"time"`
Parent string `json:"parent"`
Tree string `json:"tree"`
Paths []string `json:"paths"`
Hostname string `json:"hostname"`
Username string `json:"username"`
ID string `json:"id"`
ShortID string `json:"short_id"`
}
var (
envResticBin = getEnvNotEmpty("RESTIC_EXPORTER_BIN")
envPort = getEnvNotEmpty("RESTIC_EXPORTER_PORT")
envAddress = getEnvNotEmpty("RESTIC_EXPORTER_ADDRESS")
)
func getEnvNotEmpty(name string) string {
if val := os.Getenv(name); len(val) > 0 {
return val
}
panic(name + " not set")
}
func main() {
log.Println("Starting exporter on http://" + envAddress + ":" + envPort + " ...")
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", func(w http.ResponseWriter, req *http.Request) {
probeHandler(w, req)
})
log.Fatal(http.ListenAndServe(envAddress+":"+envPort, nil))
}
func probeHandler(w http.ResponseWriter, r *http.Request) {
var (
snapshots_latest_time = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "restic",
Subsystem: "snapshots",
Name: "latest_time",
Help: "Time of the latest snapshot",
},
[]string{"hostname"},
)
latest_total_nfiles = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "restic",
Subsystem: "stats",
Name: "latest_total_nfiles",
Help: "Number of files",
},
[]string{"hostname"},
)
latest_total_size = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "restic",
Subsystem: "stats",
Name: "latest_total_size",
Help: "Total Size",
},
[]string{"hostname"},
)
)
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
r = r.WithContext(ctx)
// get ?target=<ip> parameter from request
target := r.URL.Query().Get("target")
if target == "" {
http.Error(w, "Target parameter is missing", http.StatusBadRequest)
return
}
// create registry containing metrics
registry := prometheus.NewPedanticRegistry()
// add metrics to registry
registry.MustRegister(latest_total_size)
registry.MustRegister(latest_total_nfiles)
registry.MustRegister(snapshots_latest_time)
resticStatsCmd := exec.Command(envResticBin, "stats", "latest", "--json", "--host", target)
resticSnapshotsCmd := exec.Command(envResticBin, "snapshots", "latest", "--json", "--host", target)
var rd resticData
if err := unmarshallFromCmd(resticStatsCmd, &rd.Stats); err != nil {
log.Println(err)
return
}
if err := unmarshallFromCmd(resticSnapshotsCmd, &rd.Snapshots); err != nil {
log.Println(err)
return
}
if len(rd.Snapshots) != 0 {
var common_labels prometheus.Labels = prometheus.Labels{"hostname": rd.Snapshots[0].Hostname}
// set metrics
latest_total_size.With(prometheus.Labels(common_labels)).Set(float64(rd.Stats.TotalSize))
latest_total_nfiles.With(prometheus.Labels(common_labels)).Set(float64(rd.Stats.TotalFileCount))
snapshots_latest_time.With(prometheus.Labels(common_labels)).Set(float64(rd.Snapshots[0].Time.Unix()))
}
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func unmarshallFromCmd(cmd *exec.Cmd, out interface{}) error {
var (
stdOut bytes.Buffer
stdErr bytes.Buffer
err error
)
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
err = cmd.Run()
if err != nil {
log.Println(stdErr.String())
return err
}
if err := json.Unmarshal(stdOut.Bytes(), &out); err != nil {
return err
}
return nil
}