-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup-exporter.go
63 lines (49 loc) · 1.3 KB
/
backup-exporter.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
package main
import (
"backup_exporter/checkfiles"
confs "backup_exporter/config"
"backup_exporter/prommetrics"
"log"
"net/http"
"path/filepath"
"sync"
"fmt"
)
var (
configuration confs.Configurations
)
//RunExporter expose metrics to web server
func RunExporter(res http.ResponseWriter, req *http.Request) {
var wg sync.WaitGroup
wg.Add(len(configuration.DailyBackups))
metricPrefix := "backup"
sizeMetricName := metricPrefix + "_size"
dateMetricName := metricPrefix + "_date"
go func() {
writeM := func(mType string, m string) {
fmt.Fprintf(res,mType)
fmt.Fprintf(res,m)
}
for _, bkAddress := range configuration.DailyBackups {
files, err := filepath.Glob(bkAddress)
if err != nil {
fmt.Println("Error:", err)
return
}
for _, fileBackup := range files {
pathName, size, date := checkfiles.Run(fileBackup)
writeM(prommetrics.Generate(sizeMetricName, metricPrefix,pathName, size))
writeM(prommetrics.Generate(dateMetricName, metricPrefix,pathName, date))
}
wg.Done()
}
}()
wg.Wait()
}
func main() {
confs.ReadConfs(&configuration,"config")
mux := http.NewServeMux()
mux.HandleFunc("/metrics", RunExporter)
log.Print("Server is running on: ", configuration.Server.Port)
log.Fatal(http.ListenAndServe(":"+configuration.Server.Port, mux))
}