-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (105 loc) · 2.75 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
package main
import (
"context"
"errors"
"flag"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var port int
var appId string
var debug bool
var jsonLog bool
var fetchInterval int
const ripestatBase = "https://stat.ripe.net"
var (
updateDuration = promauto.NewGauge(prometheus.GaugeOpts{
Name: "update_duration",
Help: "time it takes for update to finish",
})
)
func updateStates() {
start := time.Now()
log.Debug().Msg("Updating Prefixes")
upstreamsGauge.Reset()
upstreams2Gauge.Reset()
bgpCommunitiesGauge.Reset()
ripeRISPeerRouteCount.Reset()
prefixCountGauge.Reset()
fetchRisPeer()
var wg sync.WaitGroup
wg.Add(len(monitorState))
for _, prefix := range monitorState {
go func() {
defer wg.Done()
prefix.checkVisState()
prefix.checkLGState()
}()
}
wg.Wait()
elapsed := time.Since(start)
updateDuration.Set(elapsed.Seconds())
}
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
flag.StringVar(&appId, "appid", "exporter", "provide a unique identifier to every data call")
flag.IntVar(&port, "port", 2112, "port")
flag.BoolVar(&debug, "debug", false, "debug")
flag.BoolVar(&jsonLog, "json", false, "json logging")
flag.IntVar(&fetchInterval, "i", 15, "fetch interval")
}
func main() {
flag.Parse()
if !jsonLog {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Debug log enabled")
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Info().
Str("appID", appId).
Str("Data Source", "RIPE RIS via RIPEstat API").
Msg("Starting PEERINGMON Exporter")
setUpstreamGauge()
updateStates()
go func() {
ticker := time.NewTicker(time.Duration(fetchInterval) * time.Second)
defer ticker.Stop()
for range ticker.C {
updateStates()
}
}()
http.Handle("/metrics", promhttp.Handler())
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
srv := &http.Server{
Addr: ":" + strconv.Itoa(port),
}
go func() {
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("Failed to start HTTP server")
}
}()
log.Info().Int("port", port).Msg("Started exporter")
<-done
log.Info().Msg("Stopping")
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Fatal().Err(err).Msg("Failed to gracefully stop server")
}
log.Info().Msg("Graceful Shutdown Successful, bye")
}