-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
111 lines (96 loc) · 3.74 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
package main
import (
"FlapAlerted/config"
_ "FlapAlerted/modules"
"FlapAlerted/monitor"
"flag"
"fmt"
"log/slog"
"net/netip"
"os"
"strings"
"time"
)
var Version = "v3.14.0"
func main() {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{})))
_, _ = fmt.Fprintln(os.Stderr, "FlapAlerted", Version)
monitor.SetVersion(Version)
routeChangeCounter := flag.Int("routeChangeCounter", 700, "Number of times a route path needs"+
" to change to list a prefix. Use '0' to show all route changes.")
flapPeriod := flag.Int("period", 60, "Interval in seconds within which the"+
" routeChangeCounter value is evaluated. Higher values increase memory consumption.")
asn := flag.Int("asn", 0, "Your ASN number")
routerID := flag.String("routerID", "0.0.0.51", "BGP Router ID for this program")
noPathInfo := flag.Bool("noPathInfo", false, "Disable keeping path information. (only disable if memory usage is a concern)")
pathInfoDetectedOnly := flag.Bool("pathInfoDetectedOnly", false, "Keep path information only for detected prefixes (decreases memory usage)")
disableAddPath := flag.Bool("disableAddPath", false, "Disable BGP AddPath support. (Setting must be replicated in BGP daemon)")
relevantAsnPosition := flag.Int("asnPosition", -1, "The position of the last static ASN (and for which to keep separate state for)"+
" in each path. Use of this parameter is required for special cases such as when connected to a route collector.")
minimumAge := flag.Int("minimumAge", 540, "Minimum age in seconds a prefix must be active to be detected."+
" Has no effect if the routeChangeCounter is set to zero")
bgpPort := flag.Int("bgpPort", 1790, "Port to listen on for incoming BGP connections")
enableDebug := flag.Bool("debug", false, "Enable debug mode (produces a lot of output)")
flag.Parse()
// Support environment variables
flag.VisitAll(func(f *flag.Flag) {
env := os.Getenv("FA_" + strings.ToUpper(f.Name))
if env != "" {
err := flag.Set(f.Name, env)
if err != nil {
fmt.Println("Invalid value for the environment variable", "FA_"+strings.ToUpper(f.Name))
os.Exit(1)
}
}
})
conf := config.UserConfig{}
conf.RouteChangeCounter = *routeChangeCounter
conf.FlapPeriod = *flapPeriod
conf.MinimumAge = *minimumAge
conf.Asn = uint32(*asn)
conf.KeepPathInfo = !*noPathInfo
conf.KeepPathInfoDetectedOnly = *pathInfoDetectedOnly
conf.UseAddPath = !*disableAddPath
conf.RelevantAsnPosition = *relevantAsnPosition
if conf.RelevantAsnPosition == -1 {
if conf.UseAddPath {
conf.RelevantAsnPosition = 1
} else {
conf.RelevantAsnPosition = 0
}
}
conf.Debug = *enableDebug
if conf.Asn == 0 {
fmt.Println("ASN value not specified")
os.Exit(1)
}
var err error
conf.RouterID, err = netip.ParseAddr(*routerID)
if err != nil {
fmt.Println("Invalid Router ID:", err)
os.Exit(1)
}
conf.BgpPort = *bgpPort
if conf.BgpPort < 1 || conf.BgpPort > 65535 {
fmt.Println("Invalid BGP Port:", conf.BgpPort)
os.Exit(1)
}
if conf.RouteChangeCounter == 0 {
conf.MinimumAge = 0
}
modules := monitor.GetRegisteredModuleNames()
if len(modules) != 0 {
slog.Info("Enabled", "modules", strings.Join(modules, ","))
}
if conf.Debug {
fmt.Println("Debug mode has been activated which will generate a lot of output")
fmt.Println("Waiting for 4 seconds...")
time.Sleep(4 * time.Second)
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: true})))
}
slog.Info("Program started", "parameters", fmt.Sprintf(
"Detecting a flap if the route to a prefix changes within %d seconds at least %d time(s)"+
" and remains active for at least %d seconds", conf.FlapPeriod, conf.RouteChangeCounter, conf.MinimumAge))
slog.Info("Started")
monitor.StartMonitoring(conf)
}