/
list_silence.go
93 lines (78 loc) · 2.27 KB
/
list_silence.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
package silence
import (
"encoding/json"
"fmt"
"log"
"github.com/openshift/osdctl/cmd/common"
"github.com/spf13/cobra"
)
type ID struct {
ID string `json:"id"`
}
type Matchers struct {
Name string `json:"name"`
Value string `json:"value"`
}
type Status struct {
State string `json:"state"`
}
type Silence struct {
ID string `json:"id"`
Matchers []Matchers `json:"matchers"`
Status Status `json:"status"`
Comment string `json:"comment"`
CreatedBy string `json:"createdBy"`
EndsAt string `json:"endsAt"`
StartsAt string `json:"startsAt"`
}
func NewCmdListSilence() *cobra.Command {
return &cobra.Command{
Use: "list <cluster-id>",
Short: "List all silences",
Long: `print the list of silences`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
ListSilence(args[0])
},
}
}
func ListSilence(clusterID string) {
var silences []Silence
silenceCmd := []string{"amtool", "silence", "--alertmanager.url", LocalHostUrl, "-o", "json"}
_, kubeconfig, clientset, err := common.GetKubeConfigAndClient(clusterID)
if err != nil {
log.Fatal(err)
}
op, err := ExecInPod(kubeconfig, clientset, silenceCmd)
if err != nil {
fmt.Println(err)
}
opSlice := []byte(op)
err = json.Unmarshal(opSlice, &silences)
if err != nil {
fmt.Println("Error in unmarshaling the data", err)
}
fmt.Printf("Silence Information:\n")
if len(silences) > 0 {
for _, silence := range silences {
printSilence(silence)
}
} else {
fmt.Println("No silences found, all silence has been cleared.")
}
}
func printSilence(silence Silence) {
id, matchers, status, created, starts, end, comment := silence.ID, silence.Matchers, silence.Status, silence.CreatedBy, silence.StartsAt, silence.EndsAt, silence.Comment
fmt.Println("-------------------------------------------")
for _, matcher := range matchers {
fmt.Printf(" SilenceID: %s\n", id)
fmt.Printf(" Status: %s\n", status.State)
fmt.Printf(" Created By: %s\n", created)
fmt.Printf(" Starts At: %s\n", starts)
fmt.Printf(" Ends At: %s\n", end)
fmt.Printf(" Comment: %s\n", comment)
fmt.Printf(" AlertName: %s\n", matcher.Value)
}
fmt.Println("---------------------------------------------")
}