forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tagger_list.go
115 lines (92 loc) · 3.03 KB
/
tagger_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package app
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/StackVista/stackstate-agent/cmd/agent/api/response"
"github.com/StackVista/stackstate-agent/cmd/agent/common"
"github.com/StackVista/stackstate-agent/pkg/api/util"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
func init() {
AgentCmd.AddCommand(taggerListCommand)
}
var taggerListCommand = &cobra.Command{
Use: "tagger-list",
Short: "Print the tagger content of a running agent",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if flagNoColor {
color.NoColor = true
}
err := common.SetupConfigWithoutSecrets(confFilePath, "")
if err != nil {
return fmt.Errorf("unable to set up global agent configuration: %v", err)
}
err = config.SetupLogger(loggerName, config.GetEnvDefault("DD_LOG_LEVEL", "off"), "", "", false, true, false)
if err != nil {
fmt.Printf("Cannot setup logger, exiting: %v\n", err)
return err
}
c := util.GetClient(false) // FIX: get certificates right then make this true
// Set session token
err = util.SetAuthToken()
if err != nil {
return err
}
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return err
}
r, err := util.DoGet(c, fmt.Sprintf("https://%v:%v/agent/tagger-list", ipcAddress, config.Datadog.GetInt("cmd_port")))
if err != nil {
if r != nil && string(r) != "" {
fmt.Fprintln(color.Output, fmt.Sprintf("The agent ran into an error while getting tags list: %s", string(r)))
} else {
fmt.Fprintln(color.Output, fmt.Sprintf("Failed to query the agent (running?): %s", err))
}
}
tr := response.TaggerListResponse{}
err = json.Unmarshal(r, &tr)
if err != nil {
return err
}
for entity, tagItem := range tr.Entities {
fmt.Fprintln(color.Output, fmt.Sprintf("\n=== Entity %s ===", color.GreenString(entity)))
sources := make([]string, 0, len(tagItem.Tags))
for source := range tagItem.Tags {
sources = append(sources, source)
}
// sort sources for deterministic output
sort.Slice(sources, func(i, j int) bool {
return sources[i] < sources[j]
})
for _, source := range sources {
fmt.Fprintln(color.Output, fmt.Sprintf("== Source %s ==", source))
fmt.Fprint(color.Output, "Tags: [")
// sort tags for easy comparison
tags := tagItem.Tags[source]
sort.Slice(tags, func(i, j int) bool {
return tags[i] < tags[j]
})
for i, tag := range tags {
tagInfo := strings.Split(tag, ":")
fmt.Fprintf(color.Output, fmt.Sprintf("%s:%s", color.BlueString(tagInfo[0]), color.CyanString(strings.Join(tagInfo[1:], ":"))))
if i != len(tags)-1 {
fmt.Fprintf(color.Output, " ")
}
}
fmt.Fprintln(color.Output, "]")
}
fmt.Fprintln(color.Output, "===")
}
return nil
},
}