diff --git a/assets/examples/gagent.hcl b/assets/examples/gagent.hcl index bf201e4..b1fce4d 100644 --- a/assets/examples/gagent.hcl +++ b/assets/examples/gagent.hcl @@ -49,7 +49,7 @@ mode = "router" /* * This is the port to G'Agent will listen for on - * for Prometheus queries. It defaults to 9101. + * for Prometheus queries. Can be overriden by MONITOR_PORT environment variable * Monitoring will be disabled if this is set to 0. * * Optional. diff --git a/cmd/gagent/main.go b/cmd/gagent/main.go index 52cfc5a..350dbb9 100644 --- a/cmd/gagent/main.go +++ b/cmd/gagent/main.go @@ -4,6 +4,7 @@ import ( log "log" http "net/http" os "os" + strconv "strconv" sync "sync" autorestart "github.com/slayer/autorestart" @@ -46,9 +47,9 @@ import ( */ var environment struct { - Mode string `env:"GAGENT_MODE" envDefault:"setup"` - Port int `env:"PORT" envDefault:"3000"` - UUID string `env:"GAGENT_UUID" envDefault:""` + ConfigFile string `env:"GAGENT_CONFIG" envDefault:"/etc/gagent/gagent.hcl"` + Mode string `env:"GAGENT_MODE" envDefault:"setup"` + MonitorPort int `env:"MONITOR_PORT" envDefault:"0"` } /* @@ -151,7 +152,7 @@ func init() { */ config.Version = semVER - config.File = "/etc/gagent/gagent.hcl" + config.File = cfg.ConfigFile config.Mode = "setup" @@ -173,7 +174,7 @@ func init() { * G'Agent will use this port for monitoring via prometheus., If set * is set to 0, G'Agent will not listen for prometheus metrics. */ - config.MonitorPort = 9101 + config.MonitorPort = cfg.MonitorPort /* * G'Agent client will use this port to communicate with the routers. @@ -229,6 +230,20 @@ func init() { usage += " --agent= -- filename of the agent to be uploaded to the G'Agent network. Required in push mode\n" usage += "\n" + usage += "Environment Variables:\n" + usage += " GAGENT_CONFIG -- [default: /etc/gagent/gagent.hcl]\n" + usage += " GAGENT_MONITOR -- [default: 0]\n" + usage += " MODE -- [default: setup]\n" + usage += "\n" + + usage += "Examples:\n" + usage += " gagent client pull --config=/etc/gagent/gagent.hcl\n" + usage += " gagent client push --config=/etc/gagent/gagent.hcl --agent=/tmp/agent.tcl\n" + usage += " gagent router --config=/etc/gagent/gagent.hcl\n" + usage += " gagent worker --config=/etc/gagent/gagent.hcl\n" + usage += " gagent setup --config=/etc/gagent/gagent.hcl\n" + usage += "\n" + /* * Consume the usage variable and the command line arguments to create a * dictionary / map. @@ -279,17 +294,15 @@ func init() { } } - log.Printf("[DEBUG] Config is %v\n", config) - /* * Start Prometheus metrics exporter */ - // if config.MonitorPort != 0 { - // go func() { - // log.Printf("[INFO] Starting Prometheus metrics exporter on port %d\n", config.MonitorPort) - // log.Fatal(http.ListenAndServe(string(config.ListenAddr)+":"+strconv.Itoa(config.MonitorPort), nil)) - // }() - // } + if config.MonitorPort != 0 { + go func() { + log.Printf("[INFO] Starting Prometheus metrics exporter on port %d\n", config.MonitorPort) + log.Fatal(http.ListenAndServe(string(config.ListenAddr)+":"+strconv.Itoa(config.MonitorPort), nil)) + }() + } autorestart.WatchFilename = config.File autorestart.StartWatcher() diff --git a/internal/client/client.go b/internal/client/client.go index 3985de6..d679b6d 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -34,15 +34,15 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) { var err error if config.CMode { - agent.ScriptCode, err = ioutil.ReadFile(config.Agent) + agent.Script, err = ioutil.ReadFile(config.Agent) if err != nil { log.Printf("[ERROR] No such file or directory: %s", config.Agent) os.Exit(4) } - log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.ScriptCode) + log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.Script) } agent.Client = config.UUID - tmpsum := sha.Sum256([]byte(agent.ScriptCode)) + tmpsum := sha.Sum256([]byte(agent.Script)) agent.Shasum = fmt.Sprintf("%v", hex.EncodeToString(tmpsum[:])) log.Printf("[INFO] SHA256 of Agent file: %s", agent.Shasum) agent.Status = 1 @@ -72,7 +72,7 @@ func getTagsFromHints(agent gstructs.AgentDetails) []string { // Use named capture groups to extract the hints re := regexp.MustCompile(`^*set\s+GHINT\s*\[\s*split\s*"(?P[^"]+)"\s*,\s*\]`) - res := re.FindStringSubmatch(string(agent.ScriptCode)) + res := re.FindStringSubmatch(string(agent.Script)) // If we don't have at least 2 matches, we have no hints if len(res) < 2 { @@ -108,6 +108,7 @@ func sendAgent(wg *sync.WaitGroup, uuid string, connectString string, agent gstr } log.Printf("[DEBUG] Start sending agent...\n") + agent.Status = 2 status, err := sock.SendMessage(agent) if err != nil { log.Printf("[ERROR] Failed to send agent to router\n") diff --git a/internal/gstructs/gstructs.go b/internal/gstructs/gstructs.go index 851c93c..9d2917f 100644 --- a/internal/gstructs/gstructs.go +++ b/internal/gstructs/gstructs.go @@ -111,10 +111,10 @@ type WorkerDetails struct { } type AgentDetails struct { - Client string `hcl:"client"` - Shasum string `hcl:"shasum"` - Status int `hcl:"status"` - Hints []string - ScriptCode []byte - Answer []byte + Status byte `hcl:"status"` + Client string `hcl:"client"` + Shasum string `hcl:"shasum"` + Hints []string + Script []byte + Answer []byte } diff --git a/internal/router/router.go b/internal/router/router.go index 80f2a30..b961256 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -105,15 +105,23 @@ LOOP: /* * Create listener for client requests */ -func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) { +func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) error { defer wg.Done() - clientSock, _ := zmq.NewSocket(zmq.ROUTER) + clientSock, err := zmq.NewSocket(zmq.ROUTER) + if err != nil { + log.Printf("[ERROR] Error creating client socket: %s", err) + return err + } defer clientSock.Close() clientListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.ClientPort) log.Printf("[DEBUG] Binding to: %s", clientListener) - _ = clientSock.Bind(clientListener) + err = clientSock.Bind(clientListener) + if err != nil { + log.Printf("[ERROR] Error binding client socket: %s", err) + return err + } for { msg, err := clientSock.RecvMessage(0) @@ -122,6 +130,7 @@ func createClientListener(wg *sync.WaitGroup, config gstructs.GagentConfig) { } log.Printf("[DEBUG] Client message received: %s", msg) } + return nil } func unwrap(msg []string) (head string, tail []string) {