-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (126 loc) · 3.93 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
package main
import (
"backupmonitor/configuration"
"backupmonitor/datapoint"
"flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"sort"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type metrics struct {
lastBackupSize prometheus.Gauge
lastBackupAge prometheus.Gauge
lastRunAge prometheus.Gauge
lastRunDuration prometheus.Gauge
}
type backupData struct {
metrics *metrics
config configuration.BackupConfig
// unix seconds
lastRunTime int64
}
type data struct {
backups map[string]*backupData
}
func main() {
configFile := flag.String("configuration-file", "config.yml", "The configuration file")
// parse flags
flag.Parse()
// read configuration
config := configuration.ParseConfig(*configFile)
data := data{
backups: map[string]*backupData{},
}
// setup metrics
setupData(&data, config)
// setup handlers for backup scripts
http.Handle("/start", newStartHandler(&data))
http.Handle("/finish", newFinishHandler(&data))
http.Handle("/metrics", promhttp.Handler())
go func() {
for {
loop(&data)
time.Sleep(time.Duration(config.CheckIntervalSeconds) * time.Second)
}
}()
log.Infof("Starting webserver on addr %s", config.ListenAddr)
err := http.ListenAndServe(config.ListenAddr, nil)
if err != nil {
log.Fatalln("Error while listening", err)
}
}
func loop(data *data) {
log.Debugln("Running loop to update metrics")
// main runtime for updating metrics
for i, backup := range data.backups {
now := time.Now().Unix()
if backup.lastRunTime > 0 {
// update last run age
age := now - backup.lastRunTime
backup.metrics.lastRunAge.Set(float64(age))
}
fileInfos, err := ioutil.ReadDir(backup.config.BackupDirectory)
if err != nil {
log.Errorln("Failed to read backup directory, ignoring...", err.Error())
break
}
var datapoints []*datapoint.Datapoint
for _, fileInfo := range fileInfos {
dp, err := datapoint.NewDatapoint(fileInfo, backup.config)
if err != nil {
log.Errorln("Invalid file in backup folder", err)
break
}
datapoints = append(datapoints, dp)
}
sort.SliceStable(datapoints, func(i, j int) bool {
return datapoints[i].Age < datapoints[j].Age
})
if len(datapoints) == 0 {
log.Warningf("No backups found for %s in '%s'", backup.config.Name, backup.config.BackupDirectory)
break
}
var newest = datapoints[0]
backup.metrics.lastBackupSize.Set(float64(newest.Size))
backup.metrics.lastBackupAge.Set(float64(newest.Age))
log.Debugf("Updated metrics of %s", i)
}
}
func setupData(data *data, config configuration.Config) {
for _, backup := range config.Backups {
data.backups[backup.Name] = &backupData{
config: backup,
lastRunTime: -1,
metrics: &metrics{
lastBackupAge: promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "backup_monitor",
Name: "last_backup_age_seconds",
Help: "Age in seconds of the last backup file",
ConstLabels: prometheus.Labels{"backup_name": backup.Name},
}),
lastBackupSize: promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "backup_monitor",
Name: "last_backup_size_bytes",
Help: "Size in bytes of the last backup file",
ConstLabels: prometheus.Labels{"backup_name": backup.Name},
}),
lastRunAge: promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "backup_monitor",
Name: "last_backup_run_age_seconds",
Help: "How long since the backup script was executed the last time",
ConstLabels: prometheus.Labels{"backup_name": backup.Name},
}),
lastRunDuration: promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "backup_monitor",
Name: "last_backup_run_duration_seconds",
Help: "The last execution time of the backup script in seconds",
ConstLabels: prometheus.Labels{"backup_name": backup.Name},
}),
}}
}
}