-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
417 lines (335 loc) · 12.6 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package main
import (
branch "MaoServerDiscovery/cmd"
"MaoServerDiscovery/util"
"errors"
"fmt"
"github.com/spf13/cobra"
"net"
"os"
"runtime"
"strings"
"net/http"
_ "net/http/pprof"
)
const (
ROOT_VERSION = "2.0"
// GENERAL_CLIENT_VERSION = "1.6"
// SERVER_VERSION = "1.6"
// ROOT_VERSION_SIGNATURE = "\nServer: " + SERVER_VERSION + " Client: " + GENERAL_CLIENT_VERSION + "\nGit: " + GIT_VERSION
)
/*
Steps to add a new parameter:
1. Add a global variant
2. Register the parameter and some introductions in the init()
3. Add a reader and some checking rules of the parameter in something like readServerArgs()
4. Add the global variant in the module entry like branch.RunServer()
5. Add some introductions for the parameter before the init()
*/
var (
GIT_VERSION = "Not set during the link time."
ROOT_VERSION_SIGNATURE = ROOT_VERSION + "\nCode Version: " + GIT_VERSION
//main_server_addr net.IP
report_server_addr net.IP
report_server_port uint32
minLogLevel util.MaoLogLevel
silent bool
web_server_addr net.IP
web_server_port uint32
cli_dump_interval uint32
refresh_interval uint32
report_interval uint32
nat66Gateway bool
nat66Persistent bool
gpsMonitor bool
gpsPersistent bool
influxdbUrl string
influxdbOrgBucket string
influxdbToken string
envTempMonitor bool
envTempPersistent bool
)
var rootCmd = &cobra.Command{
Use: "MaoServerDiscovery",
Short: "Mao-Service-Discovery, welcome to join our Github community. https://github.com/MaoJianwei/Mao_Service_Discovery",
Long: "Mao-Service-Discovery:\nService registry & Service discovery & Service keep-alive.\nWelcome to join our Github community. https://github.com/MaoJianwei/MaoServiceDiscovery",
Version: ROOT_VERSION_SIGNATURE,
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
util.MaoLog(util.ERROR, "Fail to execute rootCmd.Help(): %s", err.Error())
}
},
}
var generalClientCmd = &cobra.Command{
Use: "client",
Short: "Mao: Run general client. For common device, server, PC, laptop, Raspberry Pi, etc.",
Long: "Mao-Service-Discovery:\nRun general client. For common device, server, PC, laptop, Raspberry Pi, etc.",
// Version: GENERAL_CLIENT_VERSION,
Run: func(cmd *cobra.Command, args []string) {
if err := readGeneralClientArgs(cmd); err != nil {
util.MaoLog(util.ERROR, "Wrong Args for general client: %s", err.Error())
return
}
if nat66Gateway == true {
if runtime.GOOS != `linux` || os.Getgid() != 0 {
util.MaoLog(util.ERROR, "nat66Gateway is usable only in linux with root privilege")
return
}
}
client := &branch.GeneralClientV2{}
client.Run(&report_server_addr, report_server_port, report_interval, silent,
influxdbUrl, influxdbOrgBucket, influxdbToken,
nat66Gateway, nat66Persistent, gpsMonitor, gpsPersistent, envTempMonitor, envTempPersistent,
minLogLevel)
//branch.RunGeneralClient(&report_server_addr, report_server_port, report_interval, silent,
// influxdbUrl, influxdbOrgBucket, influxdbToken,
// nat66Gateway, nat66Persistent, gpsMonitor, gpsPersistent, envTempMonitor, envTempPersistent,
// minLogLevel)
},
}
var serverCmd = &cobra.Command{
Use: "server",
Short: "Mao: Run server.",
Long: "Mao-Service-Discovery: Run server.",
// Version: SERVER_VERSION,
Run: func(cmd *cobra.Command, args []string) {
if err := readServerArgs(cmd); err != nil {
util.MaoLog(util.ERROR, "Wrong Args for server: %s", err.Error())
return
}
//ss,_ := rootCmd.PersistentFlags().GetString("report_server_addr")
//report_server_addr = net.ParseIP(ss)
//fmt.Printf("%v\n%v\n%v\n%v\n%v\n%v\n%v\n",
// report_server_addr,
// report_server_port,
// main_server_addr,
// web_server_addr,
// web_server_port,
// cli_dump_interval,
// report_interval)
//
//fmt.Printf("---\n%v, %d\n", args, len(args))
//return
branch.RunServer(&report_server_addr, report_server_port, &web_server_addr, web_server_port,
influxdbUrl, influxdbToken, influxdbOrgBucket,
cli_dump_interval, refresh_interval, minLogLevel, silent, ROOT_VERSION)
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Mao: Show software version and code version.",
Long: "Mao-Service-Discovery: Show software version and code version.",
// Version: SERVER_VERSION,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("MaoServerDiscovery version " + ROOT_VERSION_SIGNATURE)
},
}
/**
Common:
- report_server_addr : connect to / listen on the addr, for service discovery
- report_server_port : connect to / listen on the port, for service discovery
- log : min log level. If set to INFO, DEBUG log will not be outputted.
- silent : if true, no log will be outputted. prior to the log parameter.
Server:
- web_server_addr : listen on the addr, for web control
- web_server_port : listen on the port, for web control
- cli_dump_interval : interval for dump all services info. (milliseconds)
//- refresh_interval : interval for refresh the status of clients. (milliseconds)
Client:
- report_interval : interval for report status to server. (milliseconds)
- influxdb_url :url to access influxdb database
- influxdb_org_bucket : organization and bucket names
- influxdb_token : token to access influxdb database
- enable_aux_nat66_stat : enable to pull statistics of nat66 gateway
- enable_aux_nat66_persistent : enable to persistent nat66 statistics to database
- enable_gps_monitor : enable to read GPS data via serial port from the GPS module
- enable_gps_persistent : enable to persistent GPS statistics to database
- enable_aux_env_temp_monitor : enable to monitor environment temperature
- enable_aux_env_temp_persistent : enable to upload environment temperature to Influxdb
*/
func init() {
rootCmd.PersistentFlags().String("report_server_addr","::","IP address for gRPC KA module. (e.g. 2001:db8::1)")
rootCmd.PersistentFlags().Uint32("report_server_port",28888,"Port for gRPC KA module.")
rootCmd.PersistentFlags().String("log_level", "INFO","The min level for the logs outputted. (e.g. DEBUG, INFO, WARN, ERROR, SILENT)")
rootCmd.PersistentFlags().Bool("silent", false,"Don't output the server list periodically. (default: false)")
//serverCmd.Flags().String("main_server_addr","::","::")
serverCmd.Flags().String("web_server_addr","::","IP address for Restful server.")
serverCmd.Flags().Uint32("web_server_port",29999,"Port for Restful server.")
serverCmd.Flags().Uint32("cli_dump_interval", 1000, "The interval to output all services info to the CLI, in milliseconds.")
//serverCmd.Flags().Uint32("refresh_interval", 1000, "The interval to refresh the status of clients, in milliseconds.")
serverCmd.Flags().String("influxdb_url","","URL for connecting to Influxdb. (e.g. https://<domain-or-ip>:<port>) (Optional)")
serverCmd.Flags().String("influxdb_org_bucket","","Same name for Org and Bucket. (Optional)")
serverCmd.Flags().String("influxdb_token","","Token string obtained from Influxdb. (Optional)")
generalClientCmd.Flags().Uint32("report_interval", 1000, "The interval to collect data and report to server, in milliseconds.")
generalClientCmd.Flags().String("influxdb_url","","URL for connecting to Influxdb. (e.g. https://<domain-or-ip>:<port>) (Optional)")
generalClientCmd.Flags().String("influxdb_org_bucket","","Same name for Org and Bucket. (Optional)")
generalClientCmd.Flags().String("influxdb_token","","Token string obtained from Influxdb. (Optional)")
generalClientCmd.Flags().Bool("enable_aux_nat66_stat", false, "Enable to pull statistics of nat66 gateway. Usable only in linux with root privilege. (default: false)")
generalClientCmd.Flags().Bool("enable_aux_nat66_persistent", false, "Enable to upload nat66 stat to Influxdb. (default: false)")
generalClientCmd.Flags().Bool("enable_gps_monitor", false, "Enable to read GPS data via serial port from the GPS module. (default: false)")
generalClientCmd.Flags().Bool("enable_gps_persistent", false, "Enable to upload GPS data to Influxdb. (default: false)")
generalClientCmd.Flags().Bool("enable_aux_env_temp_monitor", false, "Enable to monitor environment temperature. (default: false)")
generalClientCmd.Flags().Bool("enable_aux_env_temp_persistent", false, "Enable to upload environment temperature to Influxdb. (default: false)")
}
func readRootArgs(cmd *cobra.Command) error {
report_server_addr_str, err := rootCmd.PersistentFlags().GetString("report_server_addr")
if err != nil {
return err
}
report_server_addr = net.ParseIP(report_server_addr_str)
if report_server_addr == nil {
return errors.New("report_server_addr is invalid")
}
report_server_port, err = rootCmd.PersistentFlags().GetUint32("report_server_port")
if err != nil {
return err
}
if report_server_port < 1 || report_server_port > 65535 {
return errors.New("report_server_port is invalid")
}
min_log_level, err := rootCmd.PersistentFlags().GetString("log_level")
if err != nil {
return err
}
found := false
for i, s := range util.MaoLogLevelString {
if strings.Contains(s, min_log_level) {
minLogLevel = util.MaoLogLevel(i)
found = true
break
}
}
if !found {
return errors.New("log_level is invalid")
}
silent, err = rootCmd.PersistentFlags().GetBool("silent")
if err != nil {
return err
}
return nil
}
func readServerArgs(cmd *cobra.Command) error {
if err := readRootArgs(cmd); err != nil {
return err
}
//main_server_addr_str, err := cmd.Flags().GetString("main_server_addr")
//if err != nil {
// return err
//}
//main_server_addr = net.ParseIP(main_server_addr_str)
//if main_server_addr == nil {
// return errors.New("main_server_addr is invalid")
//}
web_server_addr_str, err := cmd.Flags().GetString("web_server_addr")
if err != nil {
return err
}
web_server_addr = net.ParseIP(web_server_addr_str)
if web_server_addr == nil {
return errors.New("web_server_addr is invalid")
}
web_server_port, err = cmd.Flags().GetUint32("web_server_port")
if err != nil {
return err
}
if web_server_port < 1 || web_server_port > 65535 {
return errors.New("web_server_port is invalid")
}
cli_dump_interval, err = cmd.Flags().GetUint32("cli_dump_interval")
if err != nil {
return err
}
if cli_dump_interval < 1 {
return errors.New("cli_dump_interval is invalid")
}
//refresh_interval, err = cmd.Flags().GetUint32("refresh_interval")
//if err != nil {
// return err
//}
//if refresh_interval < 1 {
// return errors.New("refresh_interval is invalid")
//}
refresh_interval = 1000 // deprecated and useless, this is a padding. can be deleted.
influxdbUrl, err = cmd.Flags().GetString("influxdb_url")
if err != nil {
return err
}
influxdbOrgBucket, err = cmd.Flags().GetString("influxdb_org_bucket")
if err != nil {
return err
}
influxdbToken, err = cmd.Flags().GetString("influxdb_token")
if err != nil {
return err
}
return nil
}
func readGeneralClientArgs(cmd *cobra.Command) error {
if err := readRootArgs(cmd); err != nil {
return err
}
var err error
report_interval, err = cmd.Flags().GetUint32("report_interval")
if err != nil {
return err
}
if report_interval < 1 {
return errors.New("report_interval is invalid")
}
influxdbUrl, err = cmd.Flags().GetString("influxdb_url")
if err != nil {
return err
}
influxdbOrgBucket, err = cmd.Flags().GetString("influxdb_org_bucket")
if err != nil {
return err
}
influxdbToken, err = cmd.Flags().GetString("influxdb_token")
if err != nil {
return err
}
nat66Gateway, err = cmd.Flags().GetBool("enable_aux_nat66_stat")
if err != nil {
return err
}
nat66Persistent, err = cmd.Flags().GetBool("enable_aux_nat66_persistent")
if err != nil {
return err
}
gpsMonitor, err = cmd.Flags().GetBool("enable_gps_monitor")
if err != nil {
return err
}
gpsPersistent, err = cmd.Flags().GetBool("enable_gps_persistent")
if err != nil {
return err
}
envTempMonitor, err = cmd.Flags().GetBool("enable_aux_env_temp_monitor")
if err != nil {
return err
}
envTempPersistent, err = cmd.Flags().GetBool("enable_aux_env_temp_persistent")
if err != nil {
return err
}
if (envTempPersistent || nat66Persistent) && influxdbUrl == "" {
return errors.New("influxdb_url is invalid")
}
if (envTempPersistent || nat66Persistent) && influxdbOrgBucket == "" {
return errors.New("influxdb_org_bucket is invalid")
}
if (envTempPersistent || nat66Persistent) && influxdbToken == "" {
return errors.New("influxdb_token is invalid")
}
return nil
}
func main() {
go func() {
util.MaoLog(util.INFO, "enable pprof: %v", http.ListenAndServe("0.0.0.0:39999", nil))
}()
rootCmd.AddCommand(versionCmd, generalClientCmd, serverCmd)
if err := rootCmd.Execute(); err != nil {
//util.MaoLog(util.ERROR, fmt.Sprintf("Fail to execute rootCmd: %s", err))
os.Exit(-1)
}
}