-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
252 lines (201 loc) · 5.25 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/spf13/pflag"
"github.com/fsnotify/fsnotify"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
flagUsageMessage = `Supported env variables:
SUDO_BINARY_PATH - path to sudo binary (default: /usr/bin/sudo)
BARMAN_BINARY_PATH - path to barman binary path (default: /usr/bin/barman)
BARMAN_USER_NAME - username, which used for barman execution (default: barman)
BARMAN_CONFIG_DIR - path to dir with user defined config files (default: /etc/barman.d)
Allowed flags:`
)
var (
sudoExecPath, barmanExecPath, barmanUserName, barmanConfigDir string
mux sync.Mutex
version string
commitID string
checkExitCode *prometheus.GaugeVec = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "barman_check_exit_code",
Help: "barman check command exit code result.",
},
[]string{"backup"},
)
flagSet *pflag.FlagSet = pflag.NewFlagSet("barman_exporter", pflag.ExitOnError)
flagVersion bool
flagParallelCheck bool
flagScrapeInterval int
)
// periodic task for metrics values update
func periodicCheck() {
for {
// mux.Lock()
barmanUpdateMetrics(0)
// mux.Unlock()
time.Sleep(time.Duration(flagScrapeInterval) * time.Second)
}
}
func resetMetrics(event fsnotify.Event) {
log.Println(event.Op)
if event.Op <= 4 {
timestamp := time.Now().Unix()
log.Printf("[%d] reset metrics started", timestamp)
mux.Lock()
// reset metrics if files changed
checkExitCode.Reset()
mux.Unlock()
// and then update metrics
barmanUpdateMetrics(timestamp)
log.Printf("[%d] reset metrics completed", timestamp)
}
}
// check config directory changes
// and reset metrics if files removed or added
func configDirectoryCheck() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan struct{})
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("fsnotify event:", event)
resetMetrics(event)
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("fsnotify error:", err)
}
}
}()
err = watcher.Add(barmanConfigDir)
if err != nil {
log.Fatal(err)
}
<-done
}
// write new metrics values
func writeMetircValue(timestamp int64, db string) {
log.Printf("[%d] get metric for db: %s", timestamp, db)
cmd := exec.Command(
sudoExecPath,
fmt.Sprintf("--user=%s", barmanUserName),
barmanExecPath,
"check", db)
err := cmd.Run()
log.Printf("[%d] got metric for db: %s", timestamp, db)
mux.Lock()
if err != nil {
log.Printf("[%d] check failed: %s\n", timestamp, err)
checkExitCode.WithLabelValues(db).Set(1)
} else {
checkExitCode.WithLabelValues(db).Set(0)
}
mux.Unlock()
}
// create metrics or override them's values
func barmanUpdateMetrics(timestamp int64) {
if timestamp == 0 {
timestamp = time.Now().Unix()
}
log.Printf(
"[%d] execute: %s --user=%s %s list-server --minimal\n",
timestamp,
sudoExecPath,
barmanUserName,
barmanExecPath,
)
cmd := exec.Command(
sudoExecPath,
fmt.Sprintf("--user=%s", barmanUserName),
barmanExecPath,
"list-server", "--minimal")
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("[%d] giving databases list failed: %s\n", timestamp, err)
}
dbs := strings.Split(string(out), "\n")
dbs = dbs[:len(dbs)-1]
log.Printf("[%d] prepared backups list: %v\n", timestamp, dbs)
for _, db := range dbs {
if flagParallelCheck {
go writeMetircValue(timestamp, db)
} else {
writeMetircValue(timestamp, db)
}
}
log.Printf("[%d] check completed", timestamp)
}
// initialize
func init() {
// override sudo executable path
sudoExecPath = os.Getenv("SUDO_BINARY_PATH")
if sudoExecPath == "" {
sudoExecPath = "/usr/bin/sudo"
}
// override barman executable path
barmanExecPath = os.Getenv("BARMAN_BINARY_PATH")
if barmanExecPath == "" {
barmanExecPath = "/usr/bin/barman"
}
// override barman user name
barmanUserName = os.Getenv("BARMAN_USER_NAME")
if barmanUserName == "" {
barmanUserName = "barman"
}
// override /etc/barman.d
barmanConfigDir = os.Getenv("BARMAN_CONFIG_DIR")
if barmanConfigDir == "" {
barmanConfigDir = "/etc/barman.d"
}
prometheus.MustRegister(checkExitCode)
}
// func flagUsage(f *pflag.FlagSet) {
func flagUsage() {
fmt.Println(flagUsageMessage)
flagSet.PrintDefaults()
}
func main() {
flagSet.Usage = flagUsage
flagSet.BoolVar(&flagVersion, "version", false, "show current version")
flagSet.BoolVar(&flagParallelCheck, "parallel-check", false, "check different databases in parallel")
flagSet.IntVar(&flagScrapeInterval, "scrape-interval", 60, "exporter metrics update interval")
flagSet.Parse(os.Args[1:])
if flagVersion {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
if version != "" {
fmt.Fprintf(w, "version:\t%s\n", version)
}
fmt.Fprintf(w, "git commit:\t%s\n", commitID)
w.Flush()
os.Exit(0)
}
go configDirectoryCheck()
go periodicCheck()
http.Handle("/metrics", promhttp.Handler())
log.Println("exporter started")
err := http.ListenAndServe(":9706", nil)
if err != nil {
log.Printf("http listener failed with error: %s\n", err)
}
}