Skip to content

Commit

Permalink
Merge pull request #33 from cloudradar-monitoring/test-hub
Browse files Browse the repository at this point in the history
-t flag to test the HUB connection & creds
  • Loading branch information
soupdiver authored Nov 16, 2018
2 parents 01cf1e0 + 3d2c716 commit 3da4b82
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
15 changes: 14 additions & 1 deletion cmd/cagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func main() {

serviceUninstallPtr := flag.Bool("u", false, fmt.Sprintf("stop and uninstall the system service(%s)", systemManager.String()))
printConfigPtr := flag.Bool("p", false, "print the active config")
testConfigPtr := flag.Bool("t", false, "test the HUB config")
versionPtr := flag.Bool("version", false, "show the cagent version")

flag.Parse()
Expand Down Expand Up @@ -108,6 +109,18 @@ func main() {
return
}

if *testConfigPtr {
err := ca.TestHub()
if err != nil {
fmt.Printf("Cagent HUB test failed: %s\n", err.Error())
os.Exit(1)
return
}

fmt.Printf("HUB connection test succeed and credentials are correct!\n")
return
}

var osNotice string

/*if runtime.GOOS == "windows" && !cagent.CheckIfRawICMPAvailable() {
Expand Down Expand Up @@ -141,7 +154,7 @@ func main() {
if serviceInstallPtr != nil && *serviceInstallPtr || serviceInstallUserPtr != nil && *serviceInstallUserPtr != "" {
fmt.Println(" ****** Before start you need to set 'hub_url' config param at ", *cfgPathPtr)
} else {
fmt.Println("Missing output file flag(-o) hub_url in config")
fmt.Println("Missing output file flag(-o) or hub_url in the config")
flag.PrintDefaults()
return
}
Expand Down
35 changes: 33 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -15,7 +16,6 @@ import (
"strings"
"time"

"github.com/shirou/gopsutil/load"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -51,10 +51,41 @@ func (ca *Cagent) initHubHttpClient() {
}
}

func (ca *Cagent) TestHub() error {
if ca.HubURL == "" {
return fmt.Errorf("please set the hub_url config param")
}

ca.initHubHttpClient()
req, err := http.NewRequest("HEAD", ca.HubURL, nil)
if err != nil {
return err
}

req.Header.Add("User-Agent", ca.userAgent())
if ca.HubUser != "" {
req.SetBasicAuth(ca.HubUser, ca.HubPassword)
}

resp, err := ca.hubHttpClient.Do(req)
if err != nil {
return fmt.Errorf("unable to connect. %s. If you have a proxy or firewall, it may be blocking the connection", err.Error())
}

if resp.StatusCode == 401 && ca.HubUser == "" {
return fmt.Errorf("unable to authorise without credentials. Please set hub_user & hub_password in the config")
} else if resp.StatusCode == 401 && ca.HubUser != "" {
return fmt.Errorf("unable to authorise with the provided credentials. Please correct the hub_user & hub_password in the config")
} else if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return fmt.Errorf("got bad response status: %d, %s. If you have a proxy or firewall it may be blocking the connection", resp.StatusCode, resp.Status)
}

return nil
}

func (ca *Cagent) PostResultsToHub(result Result) error {
ca.initHubHttpClient()

load.Avg()
b, err := json.Marshal(result)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg-scripts/postinstall.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
/usr/bin/cagent -s cagent -c /etc/cagent/cagent.conf
/usr/bin/cagent -s cagent -c /etc/cagent/cagent.conf
/usr/bin/cagent -t || true

0 comments on commit 3da4b82

Please sign in to comment.