Exemple #1
0
func main() {

	// Usage
	usage := `wigocli

Usage:
	wigocli
	wigocli <command>
	wigocli probe <probe>
	wigocli remote <wigo>
	wigocli remote <wigo> probe <probe>

Commands:
	detail

Options
	--help
	--version
`

	// Parse args
	arguments, _ := docopt.Parse(usage, nil, true, "wigocli v0.2", false)

	for key, value := range arguments {

		if _, ok := value.(string); ok {
			if key == "<command>" {
				command = value.(string)

				if command == "detail" {
					showOnlyErrors = false

				} else {
					fmt.Printf("Unknown command %s\n", command)
					os.Exit(1)
				}
			} else if key == "<probe>" {
				probe = value.(string)
			} else if key == "<wigo>" {
				wigoHost = value.(string)
			}
		}
	}

	// Connect
	resp, err := http.Get("http://127.0.0.1:4000/api")
	if err != nil {
		fmt.Printf("Error : %s\n", err)
		os.Exit(1)
	}
	body, err := ioutil.ReadAll(resp.Body)

	// Instanciate object from json
	wigoObj, err := wigo.NewWigoFromJson(body, 0)
	if err != nil {
		fmt.Printf("Failed to parse return from host : %s\n", err)
	}

	// Print summary
	if probe != "" {
		if wigoHost == "localhost" {
			// Find probe
			if p, ok := wigoObj.GetLocalHost().Probes[probe]; ok {
				fmt.Println(p.Summary())
			} else {
				fmt.Printf("Probe %s not found in local wigo\n", probe)
			}
		} else {
			// Find wigo
			if w, ok := wigoObj.RemoteWigos[wigoHost]; ok {
				// Find probe
				if p, ok := w.GetLocalHost().Probes[probe]; ok {
					fmt.Println(p.Summary())
				} else {
					fmt.Printf("Probe %s not found on remote wigo %s\n", probe, wigoHost)
				}
			} else {
				fmt.Printf("Remote wigo %s not found\n", wigoHost)
			}
		}
	} else if wigoHost != "" && wigoHost != "localhost" {
		// Find remote
		if w, ok := wigoObj.RemoteWigos[wigoHost]; ok {
			fmt.Printf(w.GenerateSummary(showOnlyErrors))
		} else {
			fmt.Printf("Remote wigo %s not found\n", wigoHost)
		}
	} else {
		fmt.Printf(wigoObj.GenerateSummary(showOnlyErrors))
	}
}
Exemple #2
0
func launchRemoteHostCheckRoutine(Hostname wigo.AdvancedRemoteWigoConfig) {

	secondsToSleep := wigo.GetLocalWigo().GetConfig().RemoteWigos.CheckInterval
	if Hostname.CheckInterval != 0 {
		secondsToSleep = Hostname.CheckInterval
	}

	// Split host/port
	host := Hostname.Hostname
	if Hostname.Port != 0 {
		host = Hostname.Hostname + ":" + strconv.Itoa(Hostname.Port)
	} else {
		host = Hostname.Hostname + ":" + strconv.Itoa(wigo.GetLocalWigo().GetConfig().Global.ListenPort)
	}

	// Create vars
	var resp *http.Response
	var body []byte
	var err error

	// Create http client
	client := http.Client{Timeout: time.Duration(time.Second)}

	sslEnabled := wigo.GetLocalWigo().GetConfig().RemoteWigos.SslEnabled
	if Hostname.SslEnabled {
		sslEnabled = Hostname.SslEnabled
	}

	var protocol string
	if sslEnabled {
		protocol = "https://"
	} else {
		protocol = "http://"
	}
	url := protocol + host + "/api"

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Printf("RemoteHostCheckRoutine : Unable to build get request : %s ", err)
		return
	}

	login := wigo.GetLocalWigo().GetConfig().RemoteWigos.Login
	if Hostname.Login != "" {
		login = Hostname.Login
	}

	password := wigo.GetLocalWigo().GetConfig().RemoteWigos.Password
	if Hostname.Password != "" {
		password = Hostname.Password
	}

	if login != "" && password != "" {
		req.SetBasicAuth(login, password)
	}

	for {
		for i := 1; i <= 3; i++ {
			resp, err = client.Do(req)
			if err != nil {
				time.Sleep(time.Second)
			} else {
				body, _ = ioutil.ReadAll(resp.Body)
				resp.Body.Close()
				break
			}
		}

		// Can't connect to remote wigo
		if err != nil {
			log.Printf("Can't connect to %s : %s", host, err)
			time.Sleep(time.Second * time.Duration(secondsToSleep))
			continue
		}

		// Instanciate object from remote return
		wigoObj, err := wigo.NewWigoFromJson(body, Hostname.CheckRemotesDepth)
		if err != nil {
			log.Printf("Failed to parse return from host %s : %s\nReturn was : %s", host, err, body)
			time.Sleep(time.Second * time.Duration(secondsToSleep))
			continue
		}

		// Send it to main
		wigo.GetLocalWigo().AddOrUpdateRemoteWigo(wigoObj)

		// Sleep
		time.Sleep(time.Second * time.Duration(secondsToSleep))
	}
}