Beispiel #1
0
// DisplayAgents is responsible for displaying a set of Agents to the terminal
func (capi ClientApi) DisplayAgents(agents []defs.AgentAdvert, detail bool) error {

	if len(agents) == 0 {
		fmt.Println("No agents found.")
		return nil
	} else {
		if agents[0].Uuid == "" {
			fmt.Println("No agents found.")
			return nil
		}
	}

	if detail {

		// TODO(moswalt): if nothing found, API should return either null or empty slice, and client should handle this
		tmpl, err := template.New("test").Parse(
			`Agent UUID:  {{.Uuid}}
Expires:  {{.Expires}}
Collector Summary: {{.CollectorSummary}}
Facts:
{{.PPFacts}}` + "\n")

		if err != nil {
			return err
		}

		// Output retrieved data
		for i := range agents {
			err = tmpl.Execute(os.Stdout, agents[i])
			if err != nil {
				return err
			}
		}

	} else {
		w := new(tabwriter.Writer)

		// Format in tab-separated columns with a tab stop of 8.
		w.Init(os.Stdout, 0, 8, 0, '\t', 0)
		fmt.Fprintln(w, "UUID\tEXPIRES\tADDR\tFACT SUMMARY\tCOLLECTOR SUMMARY")

		for i := range agents {
			fmt.Fprintf(
				w,
				"%s\t%s\t%s\t%s\t%s\n",
				hostresources.TruncateID(agents[i].Uuid),
				agents[i].Expires,
				agents[i].DefaultAddr,
				agents[i].FactSummary(),
				agents[i].CollectorSummary(),
			)
		}
		fmt.Fprintln(w)
		w.Flush()

	}

	return nil
}
Beispiel #2
0
// Groups will query ToDD for a map containing current agent-to-group mappings
func (capi ClientApi) Groups(conf map[string]string) {

	url := fmt.Sprintf("http://%s:%s/v1/groups", conf["host"], conf["port"])

	// Build the request
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		panic(err)
	}

	// Send the request via a client
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	// Defer the closing of the body
	defer resp.Body.Close()
	// Read the content into a byte array
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	// Marshal API data into map
	var groupmap map[string]string
	err = json.Unmarshal(body, &groupmap)
	if err != nil {
		panic(err)
	}

	w := new(tabwriter.Writer)

	// Format in tab-separated columns with a tab stop of 8.
	w.Init(os.Stdout, 0, 8, 0, '\t', 0)
	fmt.Fprintln(w, "UUID\tGROUP NAME")

	for agent_uuid, group_name := range groupmap {
		fmt.Fprintf(
			w,
			"%s\t%s\n",
			hostresources.TruncateID(agent_uuid),
			group_name,
		)
	}
	fmt.Fprintln(w)
	w.Flush()

}