-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.go
362 lines (319 loc) · 10.1 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright 2018 Ben Kochie <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/prometheus-community/pro-bing"
"github.com/superq/smokeping_prober/config"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
"golang.org/x/sync/errgroup"
)
var (
// Generated with: prometheus.ExponentialBuckets(0.00005, 2, 20)
defaultBuckets = "5e-05,0.0001,0.0002,0.0004,0.0008,0.0016,0.0032,0.0064,0.0128,0.0256,0.0512,0.1024,0.2048,0.4096,0.8192,1.6384,3.2768,6.5536,13.1072,26.2144"
logger log.Logger
sc = &config.SafeConfig{
C: &config.Config{},
}
)
type hostList []string
func (h *hostList) Set(value string) error {
if value == "" {
return fmt.Errorf("'%s' is not valid hostname", value)
}
*h = append(*h, value)
return nil
}
func (h *hostList) String() string {
return ""
}
func (h *hostList) IsCumulative() bool {
return true
}
func HostList(s kingpin.Settings) (target *[]string) {
target = new([]string)
s.SetValue((*hostList)(target))
return
}
type smokePingers struct {
started []*probing.Pinger
prepared []*probing.Pinger
g *errgroup.Group
maxInterval time.Duration
}
func (s *smokePingers) sizeOfPrepared() int {
if s.prepared != nil {
return len(s.prepared)
}
return 0
}
func (s *smokePingers) start() {
if s.sizeOfPrepared() == 0 {
return
}
if s.g != nil {
err := s.stop()
if err != nil {
level.Warn(logger).Log("msg", "At least one previous pinger failed to run", "err", err)
}
}
s.g = new(errgroup.Group)
s.started = s.prepared
splay := time.Duration(s.maxInterval.Nanoseconds() / int64(len(s.started)))
for _, pinger := range s.started {
level.Info(logger).Log("msg", "Starting prober", "address", pinger.Addr(), "interval", pinger.Interval, "size_bytes", pinger.Size, "source", pinger.Source)
s.g.Go(pinger.Run)
time.Sleep(splay)
}
s.prepared = nil
}
func (s *smokePingers) stop() error {
if s.g == nil {
return nil
}
if s.started == nil {
return nil
}
for _, pinger := range s.started {
pinger.Stop()
}
if err := s.g.Wait(); err != nil {
return fmt.Errorf("pingers failed: %v", err)
}
return nil
}
func (s *smokePingers) prepare(hosts *[]string, interval *time.Duration, privileged *bool, sizeBytes *int) error {
pingers := make([]*probing.Pinger, len(*hosts))
var pinger *probing.Pinger
var host string
for i, host := range *hosts {
pinger = probing.New(host)
err := pinger.Resolve()
if err != nil {
return fmt.Errorf("failed to resolve pinger: %v", err)
}
level.Info(logger).Log("msg", "Pinger resolved", "host", host, "ip_addr", pinger.IPAddr())
pinger.Interval = *interval
pinger.RecordRtts = false
pinger.SetPrivileged(*privileged)
pinger.Size = *sizeBytes
pingers[i] = pinger
}
maxInterval := *interval
sc.Lock()
defer sc.Unlock()
for _, targetGroup := range sc.C.Targets {
packetSize := targetGroup.Size
if packetSize < 24 || packetSize > 65535 {
return fmt.Errorf("packet size must be in the range 24-65535, but found '%d' bytes", packetSize)
}
if targetGroup.Interval > maxInterval {
maxInterval = targetGroup.Interval
}
for _, host = range targetGroup.Hosts {
pinger = probing.New(host)
pinger.Interval = targetGroup.Interval
pinger.RecordRtts = false
pinger.SetNetwork(targetGroup.Network)
pinger.Size = packetSize
pinger.Source = targetGroup.Source
if targetGroup.Protocol == "icmp" {
pinger.SetPrivileged(true)
}
err := pinger.Resolve()
if err != nil {
return fmt.Errorf("failed to resolve pinger: %v", err)
}
pingers = append(pingers, pinger)
}
}
s.prepared = pingers
s.maxInterval = maxInterval
return nil
}
func parseBuckets(buckets string) ([]float64, error) {
bucketstrings := strings.Split(buckets, ",")
bucketlist := make([]float64, len(bucketstrings))
for i := range bucketstrings {
value, err := strconv.ParseFloat(bucketstrings[i], 64)
if err != nil {
return nil, err
}
bucketlist[i] = value
}
return bucketlist, nil
}
func init() {
prometheus.MustRegister(versioncollector.NewCollector("smokeping_prober"))
}
func main() {
var (
configFile = kingpin.Flag("config.file", "Optional smokeping_prober configuration yaml file.").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
webConfig = kingpinflag.AddFlags(kingpin.CommandLine, ":9374")
buckets = kingpin.Flag("buckets", "A comma delimited list of buckets to use").Default(defaultBuckets).String()
factor = kingpin.Flag("native-histogram-factor", "The scaling factor for native histogram buckets").Hidden().Default("1.05").Float()
interval = kingpin.Flag("ping.interval", "Ping interval duration").Short('i').Default("1s").Duration()
privileged = kingpin.Flag("privileged", "Run in privileged ICMP mode").Default("true").Bool()
sizeBytes = kingpin.Flag("ping.size", "Ping packet size in bytes").Short('s').Default("56").Int()
hosts = HostList(kingpin.Arg("hosts", "List of hosts to ping"))
)
var smokePingers smokePingers
var smokepingCollector *SmokepingCollector
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("smokeping_prober"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger = promlog.New(promlogConfig)
level.Info(logger).Log("msg", "Starting smokeping_prober", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext())
if *sizeBytes < 24 || *sizeBytes > 65535 {
level.Error(logger).Log("msg", "Invalid packet size. (24-65535)", "bytes", *sizeBytes)
os.Exit(1)
}
if err := sc.ReloadConfig(*configFile); err != nil {
if errors.Is(err, os.ErrNotExist) {
level.Info(logger).Log("msg", "ignoring missing config file", "filename", *configFile)
} else {
level.Error(logger).Log("msg", "error loading config", "filename", err.Error())
os.Exit(1)
}
}
bucketlist, err := parseBuckets(*buckets)
if err != nil {
level.Error(logger).Log("msg", "Failed to parse buckets", "err", err)
os.Exit(1)
}
pingResponseSeconds := newPingResponseHistogram(bucketlist, *factor)
prometheus.MustRegister(pingResponseSeconds)
err = smokePingers.prepare(hosts, interval, privileged, sizeBytes)
if err != nil {
level.Error(logger).Log("err", "Unable to create ping", err)
os.Exit(1)
}
if smokePingers.sizeOfPrepared() == 0 {
level.Error(logger).Log("msg", "no targets specified on command line or in config file")
os.Exit(1)
}
smokePingers.start()
smokepingCollector = NewSmokepingCollector(smokePingers.started, *pingResponseSeconds)
prometheus.MustRegister(smokepingCollector)
hup := make(chan os.Signal, 1)
signal.Notify(hup, syscall.SIGHUP)
reloadCh := make(chan chan error)
go func() {
for {
var errCallback func(e error)
var successCallback func()
select {
case <-hup:
errCallback = func(e error) {}
successCallback = func() {}
case rc := <-reloadCh:
errCallback = func(e error) {
rc <- e
}
successCallback = func() {
rc <- nil
}
}
if err := sc.ReloadConfig(*configFile); err != nil {
level.Error(logger).Log("msg", "Error reloading config", "err", err)
errCallback(err)
continue
}
err = smokePingers.prepare(hosts, interval, privileged, sizeBytes)
if err != nil {
level.Error(logger).Log("msg", "Unable to create ping from config", "err", err)
errCallback(err)
continue
}
if smokePingers.sizeOfPrepared() == 0 {
level.Error(logger).Log("msg", "No targets specified on command line or in config file")
errCallback(fmt.Errorf("no targets specified"))
continue
}
smokePingers.start()
smokepingCollector.updatePingers(smokePingers.started, *pingResponseSeconds)
level.Info(logger).Log("msg", "Reloaded config file")
successCallback()
}
}()
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/-/healthy", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Healthy"))
})
http.HandleFunc("/-/reload",
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "This endpoint requires a POST request.\n")
return
}
rc := make(chan error)
reloadCh <- rc
if err := <-rc; err != nil {
http.Error(w, fmt.Sprintf("Failed to reload config: %s", err), http.StatusInternalServerError)
}
})
if *metricsPath != "/" && *metricsPath != "" {
landingConfig := web.LandingConfig{
Name: "Smokeping Prober",
Description: "Smokeping-style packet prober for Prometheus",
Version: version.Info(),
Links: []web.LandingLinks{
{
Address: *metricsPath,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
}
server := &http.Server{}
if err := web.ListenAndServe(server, webConfig, logger); err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
err = smokePingers.stop()
if err != nil {
level.Error(logger).Log("err", err)
}
}