...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package format
15
16 import (
17 "fmt"
18 "io"
19 "os"
20 "sort"
21 "strings"
22 "text/tabwriter"
23
24 "github.com/prometheus/alertmanager/api/v2/models"
25 )
26
27 type SimpleFormatter struct {
28 writer io.Writer
29 }
30
31 func init() {
32 Formatters["simple"] = &SimpleFormatter{writer: os.Stdout}
33 }
34
35 func (formatter *SimpleFormatter) SetOutput(writer io.Writer) {
36 formatter.writer = writer
37 }
38
39 func (formatter *SimpleFormatter) FormatSilences(silences []models.GettableSilence) error {
40 w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
41 sort.Sort(ByEndAt(silences))
42 fmt.Fprintln(w, "ID\tMatchers\tEnds At\tCreated By\tComment\t")
43 for _, silence := range silences {
44 fmt.Fprintf(
45 w,
46 "%s\t%s\t%s\t%s\t%s\t\n",
47 *silence.ID,
48 simpleFormatMatchers(silence.Matchers),
49 FormatDate(*silence.EndsAt),
50 *silence.CreatedBy,
51 *silence.Comment,
52 )
53 }
54 return w.Flush()
55 }
56
57 func (formatter *SimpleFormatter) FormatAlerts(alerts []*models.GettableAlert) error {
58 w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
59 sort.Sort(ByStartsAt(alerts))
60 fmt.Fprintln(w, "Alertname\tStarts At\tSummary\tState\t")
61 for _, alert := range alerts {
62 fmt.Fprintf(
63 w,
64 "%s\t%s\t%s\t%s\t\n",
65 alert.Labels["alertname"],
66 FormatDate(*alert.StartsAt),
67 alert.Annotations["summary"],
68 *alert.Status.State,
69 )
70 }
71 return w.Flush()
72 }
73
74 func (formatter *SimpleFormatter) FormatConfig(status *models.AlertmanagerStatus) error {
75 fmt.Fprintln(formatter.writer, *status.Config.Original)
76 return nil
77 }
78
79 func (formatter *SimpleFormatter) FormatClusterStatus(status *models.ClusterStatus) error {
80 w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
81 fmt.Fprintf(w,
82 "Cluster Status:\t%s\nNode Name:\t%s\n",
83 *status.Status,
84 status.Name,
85 )
86 return w.Flush()
87 }
88
89 func simpleFormatMatchers(matchers models.Matchers) string {
90 output := []string{}
91 for _, matcher := range matchers {
92 output = append(output, simpleFormatMatcher(*matcher))
93 }
94 return strings.Join(output, " ")
95 }
96
97 func simpleFormatMatcher(m models.Matcher) string {
98 return labelsMatcher(m).String()
99 }
100
View as plain text