flags.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package flags
  2. import (
  3. "fmt"
  4. "net/url"
  5. "os"
  6. "strings"
  7. "github.com/common-nighthawk/go-figure"
  8. "gopkg.in/alecthomas/kingpin.v2"
  9. "gopkg.in/ini.v1"
  10. )
  11. var (
  12. // apm
  13. ConfigServer = kingpin.Flag("config-server", "The URL of the endpoint to send traces to").Envar("CONFIG_SERVER").Default("http://10.0.16.250:18080").String()
  14. DataServer = kingpin.Flag("data-server", "The URL of the endpoint to send traces to").Envar("DATA_SERVER").Default("http://10.0.16.250:18080").String()
  15. DumpApps = kingpin.Flag("dump", "Dump app snap").Short('d').Default("false").Bool()
  16. DumpRules = kingpin.Flag("dr", "Dump rule snap").Default("false").Bool()
  17. PrintFormat = kingpin.Flag("output", "Output format (table|json)").Short('o').Default("table").String()
  18. Version = kingpin.Flag("version", "show app version").Short('v').Bool()
  19. LogLevel = kingpin.Flag("log-level", "Log level").Envar("LOG_LEVEL").Default("info").String()
  20. ConsoleLog = kingpin.Flag("console-log", "Console log").Envar("CONSOLE_LOG").Default("false").Bool()
  21. EbpfFilePath = kingpin.Flag("ebpf-path", "Set ebpf file path").Envar("EBPF_FILE").Default("").String()
  22. CommonIni = kingpin.Flag("common.ini", "Set ebpf file path").Envar("COMMON_INI").Default("/opt/cloudwise/omniagent/conf/common.ini").String()
  23. ServerPrefix = kingpin.Flag("server-prefix", "server-prefix").Envar("SERVER_PREFIX").Default("").String()
  24. DisableRegisterHost = kingpin.Flag("disable-reg-host", "Disable reg host").Default("false").Envar("DISABLE_REG_HOST").Bool()
  25. // agent
  26. DisableE2ETracing = kingpin.Flag("disable-e2e-tracing", "Disable e2e tracing").Default("true").Envar("DISABLE_E2E_TRACING").Bool()
  27. DisableStackTracing = kingpin.Flag("disable-stack-tracing", "Disable stack tracing").Default("true").Envar("DISABLE_STACK_TRACING").Bool()
  28. LicenseKey = kingpin.Flag("license-key", "Apm API key").Default("J45Engw88NeHUZ4Q7qNsK8L47FTH**QvgW113IEnsNaBNMR5zZ**oj/g!!!!").Envar("LICENSE_KEY").String()
  29. RunInContainer = kingpin.Flag("run-in-container", "run in container").Default("false").Envar("RUN_IN_CONTAINER").Bool()
  30. RunInOmniagent = kingpin.Flag("run-in-omniagent", "run in omniagent").Default("false").Envar("RUN_IN_OMNIAGENT").Bool()
  31. ListenAddress = kingpin.Flag("listen", "Listen address - ip:port or :port").Default("0.0.0.0:8123").Envar("LISTEN").String()
  32. CgroupRoot = kingpin.Flag("cgroupfs-root", "The mount point of the host cgroupfs root").Default("/sys/fs/cgroup").Envar("CGROUPFS_ROOT").String()
  33. DisableLogParsing = kingpin.Flag("disable-log-parsing", "Disable container log parsing").Default("true").Envar("DISABLE_LOG_PARSING").Bool()
  34. DisablePinger = kingpin.Flag("disable-pinger", "Don't ping upstreams").Default("false").Envar("DISABLE_PINGER").Bool()
  35. DisableL7Tracing = kingpin.Flag("disable-l7-tracing", "Disable L7 tracing").Default("false").Envar("DISABLE_L7_TRACING").Bool()
  36. ExternalNetworksWhitelist = kingpin.
  37. Flag("track-public-network", "Allow track connections to the specified IP networks, all private networks are allowed by default (e.g., Y.Y.Y.Y/mask)").
  38. Envar("TRACK_PUBLIC_NETWORK").
  39. Default("0.0.0.0/0").
  40. Strings()
  41. EphemeralPortRange = kingpin.Flag("ephemeral-port-range", "Destination and Listen TCP ports from this range will be skipped").Default("").Envar("EPHEMERAL_PORT_RANGE").String()
  42. Provider = kingpin.Flag("provider", "`provider` label for `node_cloud_info` metric").Envar("PROVIDER").String()
  43. Region = kingpin.Flag("region", "`region` label for `node_cloud_info` metric").Envar("REGION").String()
  44. AvailabilityZone = kingpin.Flag("availability-zone", "`availability_zone` label for `node_cloud_info` metric").Envar("AVAILABILITY_ZONE").String()
  45. InstanceType = kingpin.Flag("instance-type", "`instance_type` label for `node_cloud_info` metric").Envar("INSTANCE_TYPE").String()
  46. InstanceLifeCycle = kingpin.Flag("instance-life-cycle", "`instance_life_cycle` label for `node_cloud_info` metric").Envar("INSTANCE_LIFE_CYCLE").String()
  47. LogPerSecond = kingpin.Flag("log-per-second", "The number of logs per second").Default("10.0").Envar("LOG_PER_SECOND").Float64()
  48. LogBurst = kingpin.Flag("log-burst", "The maximum number of tokens that can be consumed in a single call to allow").Default("100").Envar("LOG_BURST").Int()
  49. CollectorEndpoint = kingpin.Flag("collector-endpoint", "A base endpoint URL for metrics, traces, logs, and profiles").Envar("COLLECTOR_ENDPOINT").URL()
  50. ApiKey = kingpin.Flag("api-key", "Coroot API key").Envar("API_KEY").String()
  51. MetricsEndpoint = kingpin.Flag("metrics-endpoint", "The URL of the endpoint to send metrics to").Envar("METRICS_ENDPOINT").URL()
  52. TracesEndpoint = kingpin.Flag("traces-endpoint", "The URL of the endpoint to send traces to").Envar("TRACES_ENDPOINT").URL()
  53. LogsEndpoint = kingpin.Flag("logs-endpoint", "The URL of the endpoint to send logs to").Envar("LOGS_ENDPOINT").URL()
  54. ProfilesEndpoint = kingpin.Flag("profiles-endpoint", "The URL of the endpoint to send profiles to").Envar("PROFILES_ENDPOINT").URL()
  55. InsecureSkipVerify = kingpin.Flag("insecure-skip-verify", "whether to skip verifying the certificate or not").Envar("INSECURE_SKIP_VERIFY").Default("false").Bool()
  56. ScrapeInterval = kingpin.Flag("scrape-interval", "How often to gather metrics from the agent").Default("15s").Envar("SCRAPE_INTERVAL").Duration()
  57. WalDir = kingpin.Flag("wal-dir", "Path to where the agent stores data (e.g. the metrics Write-Ahead Log)").Default("/tmp/coroot-node-agent").Envar("WAL_DIR").String()
  58. HostDirPathPrefix = kingpin.Flag("host-dir-path-prefix", "Set the prefix of path about the mount point of the host directory").Envar("HOST_DIR_PATH_PREFIX").Default("").String()
  59. FuseTryMax = kingpin.Flag("fuse_try_max", "The maximum number of the fuse operation try").Default("3").Envar("FUSE_TRY_MAX").Int()
  60. // debug
  61. Test = kingpin.Flag("test", "Only test").Default("false").Envar("TEST").Bool()
  62. // op 新增配置
  63. SysTag = kingpin.Flag("sys-tag", "sys tag").Envar("CW_SYS").Default("").String()
  64. // l7
  65. MysqlDefault = kingpin.Flag("mysql-default", "Default MySQL protocol when port-based detection fails").Envar("MYSQL_DEFAULT").Default("mysql").String()
  66. // 端口白名单配置
  67. MysqlPortWhitelist = kingpin.Flag("mysql-ports", "Comma-separated list of ports to identify as MySQL").Envar("MYSQL_PORTS").Default("").String()
  68. MariadbPortWhitelist = kingpin.Flag("mariadb-ports", "Comma-separated list of ports to identify as MariaDB").Envar("MARIADB_PORTS").Default("").String()
  69. //APP注册到DOOP
  70. RegisterAppToDoop = kingpin.Flag("register-app-to-doop", "Register the app-info to the doop").Default("false").Envar("REGISTER_APP_TO_DOOP").Bool()
  71. //是否向平台发送网络数据
  72. SendNetData = kingpin.Flag("send-net-data", "Send the net data to platform").Default("false").Envar("SEND_NET_DATA").Bool()
  73. )
  74. var AgentName = "euspace"
  75. var AgentVersion = "0.0.1"
  76. var GitCommit = ""
  77. var BuildDate = ""
  78. func GetString(fl *string) string {
  79. if fl == nil {
  80. return ""
  81. }
  82. return *fl
  83. }
  84. func init() {
  85. if strings.HasSuffix(os.Args[0], ".test") {
  86. return
  87. }
  88. kingpin.HelpFlag.Short('h').Hidden()
  89. kingpin.Parse()
  90. if *Test {
  91. euspace := figure.NewFigure("TEST", "", true)
  92. euspace.Print()
  93. } else {
  94. euspace := figure.NewColorFigure("Euspace", "slant", "blue", true)
  95. euspace.Print()
  96. }
  97. if *Version {
  98. ShowVersion()
  99. }
  100. if *DumpApps {
  101. DumpTableFeatures(*PrintFormat)
  102. }
  103. if *DumpRules {
  104. DumpRuleTableFeatures(*PrintFormat)
  105. }
  106. if *CollectorEndpoint != nil {
  107. u := *CollectorEndpoint
  108. if *MetricsEndpoint == nil {
  109. *MetricsEndpoint = u.JoinPath("/v1/metrics")
  110. }
  111. if *TracesEndpoint == nil {
  112. *TracesEndpoint = u.JoinPath("/v1/traces")
  113. }
  114. if *LogsEndpoint == nil {
  115. *LogsEndpoint = u.JoinPath("/v1/logs")
  116. }
  117. if *ProfilesEndpoint == nil {
  118. *ProfilesEndpoint = u.JoinPath("/v1/profiles")
  119. }
  120. }
  121. if *MetricsEndpoint != nil {
  122. *ListenAddress = "127.0.0.1:10300"
  123. }
  124. // set ServerPrefix
  125. // set ConfigServer
  126. // set DataServer
  127. if *RunInOmniagent {
  128. iniData, err := ini.Load(*CommonIni)
  129. if err == nil && iniData != nil {
  130. *ServerPrefix = "/apm"
  131. *ConfigServer = iniData.Section("common").Key("config_server").String()
  132. *DataServer = iniData.Section("common").Key("data_server").String()
  133. }
  134. }
  135. // set TracesEndpoint
  136. if *TracesEndpoint == nil && *DataServer != "" {
  137. if !strings.HasPrefix(*DataServer, "http") {
  138. *DataServer = fmt.Sprintf("http://%s", *DataServer)
  139. }
  140. dataServer, err := url.Parse(*DataServer)
  141. if err == nil && dataServer != nil {
  142. *TracesEndpoint = dataServer.JoinPath(*ServerPrefix + "/api/v2/data/receive")
  143. }
  144. }
  145. // set tiny-agent config
  146. //nativeConf := utils.GetDefaultAgentsPath("NativeAgent", "conf", "agent.properties")
  147. //confData, err := ini.Load(nativeConf)
  148. //if err == nil {
  149. // configServer := *ConfigServer
  150. // if !strings.HasPrefix(*ConfigServer, "http") {
  151. // configServer = fmt.Sprintf("http://%s", *ConfigServer)
  152. // }
  153. // confData.Section(ini.DEFAULT_SECTION).Key("ConfigDomain").SetValue(configServer + *ServerPrefix)
  154. // confData.Section(ini.DEFAULT_SECTION).Key("SendDomain").SetValue(*DataServer + *ServerPrefix)
  155. // err = confData.SaveTo(nativeConf)
  156. // if err != nil {
  157. // klog.Error(err)
  158. // }
  159. //}
  160. if *RunInContainer {
  161. if *HostDirPathPrefix == "" {
  162. *HostDirPathPrefix = "/host"
  163. }
  164. } else {
  165. *HostDirPathPrefix = ""
  166. }
  167. // 初始化端口白名单
  168. InitPortWhitelists()
  169. }
  170. func ShowVersion() {
  171. fmt.Printf("%-20s %-8s\n", "Name:", AgentName)
  172. fmt.Printf("%-20s %-8s\n", "Version:", AgentVersion)
  173. fmt.Printf("%-20s %-8s\n", "Commit:", GitCommit)
  174. fmt.Printf("%-20s %-8s\n", "Date:", BuildDate)
  175. os.Exit(0)
  176. }