flags.go 9.5 KB

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