forked from netobserv/netobserv-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
206 lines (166 loc) · 4.8 KB
/
root.go
File metadata and controls
206 lines (166 loc) · 4.8 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
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
package cmd
import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/netobserv/network-observability-cli/internal/pkg/kubernetes"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type captureType string
const (
Flow captureType = "Flow"
Packet captureType = "Packet"
Metric captureType = "Metric"
)
var (
log = logrus.New()
logLevel string
port int
filename string
namespace string
options string
maxTime time.Duration
maxBytes int64
currentTime = time.Now
startupTime = currentTime()
mutex = sync.Mutex{}
totalBytes = int64(0)
rootCmd = &cobra.Command{
Use: "network-observability-cli",
Short: "network-observability-cli is an interactive Flow and Packet visualizer",
Long: `An interactive Flow / PCAP collector and visualization tool`,
Run: func(_ *cobra.Command, _ []string) {
},
}
capture = Flow
collectorStarted = false
captureStarted = false
captureEnded = false
stopReceived = false
useMocks = false
isBackground = false
)
// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}
// func main() {
func init() {
cobra.OnInitialize(onInit)
rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "info", "Log level")
rootCmd.PersistentFlags().IntVarP(&port, "port", "", 9999, "TCP port to listen")
rootCmd.PersistentFlags().StringVarP(&filename, "filename", "", "", "Output file name")
rootCmd.PersistentFlags().StringVarP(&options, "options", "", "", "Options(s)")
rootCmd.PersistentFlags().DurationVarP(&maxTime, "maxtime", "", 5*time.Minute, "Maximum capture time")
rootCmd.PersistentFlags().Int64VarP(&maxBytes, "maxbytes", "", 50000000, "Maximum capture bytes")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "netobserv-cli", "Namespace where agent pods are running")
rootCmd.PersistentFlags().BoolVarP(&useMocks, "mock", "", false, "Use mock")
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
log.Info("Received SIGTERM; cleaning up...")
stopReceived = true
os.Exit(0)
}()
// flow
rootCmd.AddCommand(flowCmd)
// packet
rootCmd.AddCommand(pktCmd)
// metrics
rootCmd.AddCommand(metricCmd)
}
func onInit() {
lvl, _ := logrus.ParseLevel(logLevel)
log.SetLevel(lvl)
err := LoadConfig()
if err != nil {
log.Fatalf("can't load config from yaml: %v", err)
}
printBanner()
log.Infof("Log level: %s\nOption(s): %s", logLevel, options)
if strings.Contains(options, "background") && !strings.Contains(options, "background=false") {
isBackground = true
log.Infof("Running in background mode")
}
showKernelVersion()
if useMocks {
log.Info("Using mocks...")
go mockForever()
}
}
func printBanner() {
fmt.Print(`
------------------------------------------------------------------------
_ _ _ _ ___ _ ___
| \| |___| |_ ___| |__ ___ ___ _ ___ __ / __| | |_ _|
| .' / -_) _/ _ \ '_ (_-</ -_) '_\ V / | (__| |__ | |
|_|\_\___|\__\___/_.__/__/\___|_| \_/ \___|____|___|
------------------------------------------------------------------------
`)
}
func showKernelVersion() {
output, err := exec.Command("uname", "-r").Output()
if err != nil {
log.Errorf("Can't get kernel version: %v", err)
}
if len(output) == 0 {
log.Infof("Kernel version not found")
} else {
log.Infof("Kernel version: %s", strings.TrimSpace(string(output)))
}
}
func onLimitReached() bool {
shouldExit := false
if !captureEnded {
captureEnded = true
log.Trace("Capture ended")
if app != nil && errAdvancedDisplay == nil {
app.Stop()
}
if isBackground {
err := kubernetes.DeleteDaemonSet(context.Background(), namespace)
if err != nil {
log.Error(err)
}
fmt.Print(`Thank you for using...`)
printBanner()
if capture == "Metric" {
fmt.Print(`
- Open NetObserv / On Demand dashboard to see generated metrics
- Once finished, remove everything using 'oc netobserv cleanup'
See you soon !
`)
} else {
fmt.Print(`
- Download the generated output using 'oc netobserv copy' command
- Once finished, clean the collector pod using 'oc netobserv cleanup'
See you soon !
`)
}
} else {
shouldExit = true
}
}
return shouldExit
}
// Create output file, preventing path traversal
func createOutputFile(kind, filename string) (*os.File, error) {
base := "./output/" + kind + "/"
if err := os.MkdirAll(base, 0700); err != nil {
return nil, err
}
root, err := os.OpenRoot(base)
if err != nil {
return nil, err
}
defer root.Close()
return root.Create(filename)
}