flags.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package flags
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/coroot/coroot-node-agent/utils"
  6. "github.com/coroot/coroot-node-agent/utils/modelse"
  7. "github.com/jedib0t/go-pretty/v6/table"
  8. "gopkg.in/alecthomas/kingpin.v2"
  9. "gopkg.in/ini.v1"
  10. "net/url"
  11. "os"
  12. "path"
  13. "strings"
  14. )
  15. var (
  16. // apm
  17. 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()
  18. 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()
  19. DumpApps = kingpin.Flag("dump", "Dump app snap").Default("false").Bool()
  20. Version = kingpin.Flag("version", "show app version").Short('v').Bool()
  21. LogLevel = kingpin.Flag("log-level", "Log level").Envar("LOG_LEVEL").Default("info").String()
  22. EbpfFilePath = kingpin.Flag("ebpf-path", "Set ebpf file path").Envar("EBPF_FILE").Default("").String()
  23. CommonIni = kingpin.Flag("common.ini", "Set ebpf file path").Envar("COMMON_INI").Default("/opt/cloudwise/omniagent/conf/common.ini").String()
  24. ServerPrefix = kingpin.Flag("server-prefix", "server-prefix").Envar("SERVER_PREFIX").Default("").String()
  25. DisableRegisterHost = kingpin.Flag("disable-reg-host", "Disable reg host").Default("false").Envar("DISABLE_REG_HOST").Bool()
  26. // agent
  27. DisableE2ETracing = kingpin.Flag("disable-e2e-tracing", "Disable e2e tracing").Default("true").Envar("DISABLE_E2E_TRACING").Bool()
  28. DisableStackTracing = kingpin.Flag("disable-stack-tracing", "Disable stack tracing").Default("true").Envar("DISABLE_STACK_TRACING").Bool()
  29. LicenseKey = kingpin.Flag("license-key", "Apm API key").Default("J45Engw88NeHUZ4Q7qNsK8L47FTH**QvgW113IEnsNaBNMR5zZ**oj/g!!!!").Envar("LICENSE_KEY").String()
  30. RunInContainer = kingpin.Flag("run-in-container", "run in container").Default("false").Envar("RUN_IN_CONTAINER").Bool()
  31. RunInOmniagent = kingpin.Flag("run-in-omniagent", "run in omniagent").Default("false").Envar("RUN_IN_OMNIAGENT").Bool()
  32. ListenAddress = kingpin.Flag("listen", "Listen address - ip:port or :port").Default("0.0.0.0:8123").Envar("LISTEN").String()
  33. CgroupRoot = kingpin.Flag("cgroupfs-root", "The mount point of the host cgroupfs root").Default("/sys/fs/cgroup").Envar("CGROUPFS_ROOT").String()
  34. DisableLogParsing = kingpin.Flag("disable-log-parsing", "Disable container log parsing").Default("false").Envar("DISABLE_LOG_PARSING").Bool()
  35. DisablePinger = kingpin.Flag("disable-pinger", "Don't ping upstreams").Default("false").Envar("DISABLE_PINGER").Bool()
  36. DisableL7Tracing = kingpin.Flag("disable-l7-tracing", "Disable L7 tracing").Default("false").Envar("DISABLE_L7_TRACING").Bool()
  37. ExternalNetworksWhitelist = kingpin.
  38. 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)").
  39. Envar("TRACK_PUBLIC_NETWORK").
  40. Default("0.0.0.0/0").
  41. Strings()
  42. EphemeralPortRange = kingpin.Flag("ephemeral-port-range", "Destination and Listen TCP ports from this range will be skipped").Default("").Envar("EPHEMERAL_PORT_RANGE").String()
  43. Provider = kingpin.Flag("provider", "`provider` label for `node_cloud_info` metric").Envar("PROVIDER").String()
  44. Region = kingpin.Flag("region", "`region` label for `node_cloud_info` metric").Envar("REGION").String()
  45. AvailabilityZone = kingpin.Flag("availability-zone", "`availability_zone` label for `node_cloud_info` metric").Envar("AVAILABILITY_ZONE").String()
  46. InstanceType = kingpin.Flag("instance-type", "`instance_type` label for `node_cloud_info` metric").Envar("INSTANCE_TYPE").String()
  47. InstanceLifeCycle = kingpin.Flag("instance-life-cycle", "`instance_life_cycle` label for `node_cloud_info` metric").Envar("INSTANCE_LIFE_CYCLE").String()
  48. LogPerSecond = kingpin.Flag("log-per-second", "The number of logs per second").Default("10.0").Envar("LOG_PER_SECOND").Float64()
  49. 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()
  50. CollectorEndpoint = kingpin.Flag("collector-endpoint", "A base endpoint URL for metrics, traces, logs, and profiles").Envar("COLLECTOR_ENDPOINT").URL()
  51. ApiKey = kingpin.Flag("api-key", "Coroot API key").Envar("API_KEY").String()
  52. MetricsEndpoint = kingpin.Flag("metrics-endpoint", "The URL of the endpoint to send metrics to").Envar("METRICS_ENDPOINT").URL()
  53. TracesEndpoint = kingpin.Flag("traces-endpoint", "The URL of the endpoint to send traces to").Envar("TRACES_ENDPOINT").URL()
  54. LogsEndpoint = kingpin.Flag("logs-endpoint", "The URL of the endpoint to send logs to").Envar("LOGS_ENDPOINT").URL()
  55. ProfilesEndpoint = kingpin.Flag("profiles-endpoint", "The URL of the endpoint to send profiles to").Envar("PROFILES_ENDPOINT").URL()
  56. InsecureSkipVerify = kingpin.Flag("insecure-skip-verify", "whether to skip verifying the certificate or not").Envar("INSECURE_SKIP_VERIFY").Default("false").Bool()
  57. ScrapeInterval = kingpin.Flag("scrape-interval", "How often to gather metrics from the agent").Default("15s").Envar("SCRAPE_INTERVAL").Duration()
  58. 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()
  59. 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()
  60. FuseTryMax = kingpin.Flag("fuse_try_max", "The maximum number of the fuse operation try").Default("3").Envar("FUSE_TRY_MAX").Int()
  61. )
  62. var AgentName = "euspace"
  63. var AgentVersion = "0.0.1"
  64. var GitCommit = ""
  65. var BuildDate = ""
  66. func GetString(fl *string) string {
  67. if fl == nil {
  68. return ""
  69. }
  70. return *fl
  71. }
  72. func init() {
  73. if strings.HasSuffix(os.Args[0], ".test") {
  74. return
  75. }
  76. kingpin.HelpFlag.Short('h').Hidden()
  77. kingpin.Parse()
  78. if *Version {
  79. ShowVersion()
  80. }
  81. if *DumpApps {
  82. DumpTableFeatures()
  83. }
  84. if *CollectorEndpoint != nil {
  85. u := *CollectorEndpoint
  86. if *MetricsEndpoint == nil {
  87. *MetricsEndpoint = u.JoinPath("/v1/metrics")
  88. }
  89. if *TracesEndpoint == nil {
  90. *TracesEndpoint = u.JoinPath("/v1/traces")
  91. }
  92. if *LogsEndpoint == nil {
  93. *LogsEndpoint = u.JoinPath("/v1/logs")
  94. }
  95. if *ProfilesEndpoint == nil {
  96. *ProfilesEndpoint = u.JoinPath("/v1/profiles")
  97. }
  98. }
  99. if *MetricsEndpoint != nil {
  100. *ListenAddress = "127.0.0.1:10300"
  101. }
  102. // set ServerPrefix
  103. // set ConfigServer
  104. // set DataServer
  105. if *CommonIni != "" {
  106. iniData, err := ini.Load(*CommonIni)
  107. if err == nil && iniData != nil {
  108. *ServerPrefix = "/apm"
  109. *ConfigServer = iniData.Section("common").Key("config_server").String()
  110. *DataServer = iniData.Section("common").Key("data_server").String()
  111. }
  112. }
  113. // set TracesEndpoint
  114. if *TracesEndpoint == nil && *DataServer != "" {
  115. if !strings.HasPrefix(*DataServer, "http") {
  116. *DataServer = fmt.Sprintf("http://%s", *DataServer)
  117. }
  118. dataServer, err := url.Parse(*DataServer)
  119. if err == nil && dataServer != nil {
  120. *TracesEndpoint = dataServer.JoinPath(*ServerPrefix + "/api/v2/data/receive")
  121. }
  122. }
  123. // set tiny-agent config
  124. //nativeConf := utils.GetDefaultAgentsPath("NativeAgent", "conf", "agent.properties")
  125. //confData, err := ini.Load(nativeConf)
  126. //if err == nil {
  127. // configServer := *ConfigServer
  128. // if !strings.HasPrefix(*ConfigServer, "http") {
  129. // configServer = fmt.Sprintf("http://%s", *ConfigServer)
  130. // }
  131. // confData.Section(ini.DEFAULT_SECTION).Key("ConfigDomain").SetValue(configServer + *ServerPrefix)
  132. // confData.Section(ini.DEFAULT_SECTION).Key("SendDomain").SetValue(*DataServer + *ServerPrefix)
  133. // err = confData.SaveTo(nativeConf)
  134. // if err != nil {
  135. // klog.Error(err)
  136. // }
  137. //}
  138. if *RunInContainer {
  139. if *HostDirPathPrefix == "" {
  140. *HostDirPathPrefix = "/host"
  141. }
  142. } else {
  143. *HostDirPathPrefix = ""
  144. }
  145. }
  146. func DumpTableFeatures() {
  147. dumpPath := path.Join(utils.GetDefaultRuntimePath(), "memdump")
  148. fileName := path.Join(dumpPath, "app.snap")
  149. content, err := os.ReadFile(fileName)
  150. if err != nil {
  151. fmt.Println(err.Error())
  152. }
  153. s := make(map[uint32]modelse.AppStatusInfo)
  154. err = json.Unmarshal(content, &s)
  155. if err != nil {
  156. fmt.Println(err.Error())
  157. }
  158. t := table.NewWriter()
  159. for pid, info := range s {
  160. service := fmt.Sprintf("%s", info.Sn)
  161. t.AppendRow(table.Row{
  162. //info.AgentID,
  163. //info.UpdateAt,
  164. pid,
  165. info.ProcName,
  166. info.AppName,
  167. info.Rule,
  168. info.Language,
  169. service,
  170. info.AppID,
  171. info.RegisterAt,
  172. info.PreStatus.String(),
  173. info.Status.String(),
  174. info.StackStatus,
  175. })
  176. }
  177. t.SetAutoIndex(true)
  178. t.AppendHeader(table.Row{
  179. //"agent id",
  180. //"update at",
  181. "pid",
  182. "process",
  183. "app name",
  184. "app rule",
  185. "code",
  186. "service",
  187. "app id",
  188. "reg at",
  189. "app pre status",
  190. "app status",
  191. "stack status",
  192. })
  193. fmt.Println(t.Render())
  194. os.Exit(0)
  195. }
  196. func ShowVersion() {
  197. fmt.Printf("%-20s %-8s\n", "Name:", AgentName)
  198. fmt.Printf("%-20s %-8s\n", "Version:", AgentVersion)
  199. fmt.Printf("%-20s %-8s\n", "Commit:", GitCommit)
  200. fmt.Printf("%-20s %-8s\n", "Date:", BuildDate)
  201. os.Exit(0)
  202. }