-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (67 loc) · 1.59 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
resticCheckExitCode = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "restic_check_exit_code",
Help: "restic check command exit code result.",
},
[]string{"prefix"},
)
)
func resticCheck() {
// override restic executable path
resticExecPath := os.Getenv("RESTIC_BINARY_PATH")
if resticExecPath == "" {
resticExecPath = "/usr/local/bin/restic"
}
godotenv.Load("/etc/default/restic")
godotenv.Load("/etc/default/restic_exporter")
prefixes := strings.Split(os.Getenv("RESTIC_CHECK_PREFIXES"), ",")
go func() {
for {
for _, prefix := range prefixes {
os.Setenv(
"RESTIC_REPOSITORY",
fmt.Sprintf("%s/%s", os.Getenv("RESTIC_REPOSITORY_BUCKET"), prefix))
// 5 attempts before failed state return
for i := 0; i < 5; i++ {
cmd := exec.Command(resticExecPath, "check")
err := cmd.Run()
if err != nil {
log.Printf("check failed: %s\n", err)
} else {
resticCheckExitCode.WithLabelValues(prefix).Set(0)
break
}
if i < 4 {
time.Sleep(5 * time.Second)
} else {
resticCheckExitCode.WithLabelValues(prefix).Set(1)
}
}
}
time.Sleep(30 * time.Second)
}
}()
}
func init() {
prometheus.MustRegister(resticCheckExitCode)
}
func main() {
resticCheck()
http.Handle("/metrics", promhttp.Handler())
log.Println("exporter started")
http.ListenAndServe(":9707", nil)
}