-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (108 loc) · 2.81 KB
/
Copy pathmain.go
File metadata and controls
125 lines (108 loc) · 2.81 KB
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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
)
// For version
var (
version = "dev"
commit = "none"
buildDate = ""
)
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
if len(os.Args) < 2 {
usage()
os.Exit(2)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var err error
switch os.Args[1] {
case "ambient":
err = runAmbientCommand(ctx, os.Args[2:])
case "attack":
err = runAttackCommand(ctx, os.Args[2:])
case "validate":
err = runValidateCommand(os.Args[2:])
case "help", "-h", "--help":
usage()
return
default:
usage()
err = fmt.Errorf("unknown command %q", os.Args[1])
}
if err != nil && !errors.Is(err, context.Canceled) {
log.Printf("error: %v", err)
os.Exit(1)
}
}
func runAmbientCommand(ctx context.Context, args []string) error {
fs := flag.NewFlagSet("ambient", flag.ContinueOnError)
configPath := fs.String("config", "ambient.yaml", "path to ambient YAML configuration")
if err := fs.Parse(args); err != nil {
return err
}
cfg, err := loadAmbientConfig(*configPath)
if err != nil {
return err
}
return runAmbient(ctx, cfg)
}
func runAttackCommand(ctx context.Context, args []string) error {
fs := flag.NewFlagSet("attack", flag.ContinueOnError)
configPath := fs.String("config", "attack.yaml", "path to attack YAML configuration")
if err := fs.Parse(args); err != nil {
return err
}
cfg, err := loadAttackConfig(*configPath)
if err != nil {
return err
}
return runAttack(ctx, cfg)
}
func runValidateCommand(args []string) error {
fs := flag.NewFlagSet("validate", flag.ContinueOnError)
mode := fs.String("mode", "", "configuration mode: ambient or attack")
configPath := fs.String("config", "", "path to YAML configuration")
if err := fs.Parse(args); err != nil {
return err
}
if *configPath == "" {
return errors.New("--config is required")
}
switch *mode {
case "ambient":
_, err := loadAmbientConfig(*configPath)
if err != nil {
return err
}
case "attack":
_, err := loadAttackConfig(*configPath)
if err != nil {
return err
}
default:
return errors.New("--mode must be ambient or attack")
}
fmt.Printf("configuration %s is valid\n", *configPath)
return nil
}
func usage() {
fmt.Fprintf(os.Stderr, `sflowgen emits synthetic sFlow v5 samples.
Usage:
sflowgen ambient --config examples/ambient.yaml
sflowgen attack --config examples/attack-ipv4.yaml
sflowgen validate --mode ambient --config examples/ambient.yaml
The ambient command runs indefinitely by default and stops on SIGINT/SIGTERM.
The attack command adds a separate finite traffic profile and exits after its
ramp-up, hold, and ramp-down phases.
`)
fmt.Fprintf(os.Stderr, "\nVersion: %s commit: %s build_date: %s", version, commit, buildDate)
}