...

Source file src/github.com/prometheus/alertmanager/cli/format/format_extended.go

Documentation: github.com/prometheus/alertmanager/cli/format

     1  // Copyright 2018 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    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  	"github.com/prometheus/alertmanager/pkg/labels"
    26  )
    27  
    28  type ExtendedFormatter struct {
    29  	writer io.Writer
    30  }
    31  
    32  func init() {
    33  	Formatters["extended"] = &ExtendedFormatter{writer: os.Stdout}
    34  }
    35  
    36  func (formatter *ExtendedFormatter) SetOutput(writer io.Writer) {
    37  	formatter.writer = writer
    38  }
    39  
    40  // FormatSilences formats the silences into a readable string
    41  func (formatter *ExtendedFormatter) FormatSilences(silences []models.GettableSilence) error {
    42  	w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
    43  	sort.Sort(ByEndAt(silences))
    44  	fmt.Fprintln(w, "ID\tMatchers\tStarts At\tEnds At\tUpdated At\tCreated By\tComment\t")
    45  	for _, silence := range silences {
    46  		fmt.Fprintf(
    47  			w,
    48  			"%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
    49  			*silence.ID,
    50  			extendedFormatMatchers(silence.Matchers),
    51  			FormatDate(*silence.Silence.StartsAt),
    52  			FormatDate(*silence.Silence.EndsAt),
    53  			FormatDate(*silence.UpdatedAt),
    54  			*silence.CreatedBy,
    55  			*silence.Comment,
    56  		)
    57  	}
    58  	return w.Flush()
    59  }
    60  
    61  // FormatAlerts formats the alerts into a readable string
    62  func (formatter *ExtendedFormatter) FormatAlerts(alerts []*models.GettableAlert) error {
    63  	w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
    64  	sort.Sort(ByStartsAt(alerts))
    65  	fmt.Fprintln(w, "Labels\tAnnotations\tStarts At\tEnds At\tGenerator URL\tState\t")
    66  	for _, alert := range alerts {
    67  		fmt.Fprintf(
    68  			w,
    69  			"%s\t%s\t%s\t%s\t%s\t%s\t\n",
    70  			extendedFormatLabels(alert.Labels),
    71  			extendedFormatAnnotations(alert.Annotations),
    72  			FormatDate(*alert.StartsAt),
    73  			FormatDate(*alert.EndsAt),
    74  			alert.GeneratorURL,
    75  			*alert.Status.State,
    76  		)
    77  	}
    78  	return w.Flush()
    79  }
    80  
    81  // FormatConfig formats the alertmanager status information into a readable string
    82  func (formatter *ExtendedFormatter) FormatConfig(status *models.AlertmanagerStatus) error {
    83  	fmt.Fprintln(formatter.writer, status.Config.Original)
    84  	fmt.Fprintln(formatter.writer, "buildUser", status.VersionInfo.BuildUser)
    85  	fmt.Fprintln(formatter.writer, "goVersion", status.VersionInfo.GoVersion)
    86  	fmt.Fprintln(formatter.writer, "revision", status.VersionInfo.Revision)
    87  	fmt.Fprintln(formatter.writer, "version", status.VersionInfo.Version)
    88  	fmt.Fprintln(formatter.writer, "branch", status.VersionInfo.Branch)
    89  	fmt.Fprintln(formatter.writer, "buildDate", status.VersionInfo.BuildDate)
    90  	fmt.Fprintln(formatter.writer, "uptime", status.Uptime)
    91  	return nil
    92  }
    93  
    94  // FormatClusterStatus formats the cluster status with peers into a readable string.
    95  func (formatter *ExtendedFormatter) FormatClusterStatus(status *models.ClusterStatus) error {
    96  	w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
    97  	fmt.Fprintf(w,
    98  		"Cluster Status:\t%s\nNode Name:\t%s\n\n",
    99  		*status.Status,
   100  		status.Name,
   101  	)
   102  	fmt.Fprintln(w, "Address\tName")
   103  	sort.Sort(ByAddress(status.Peers))
   104  	for _, peer := range status.Peers {
   105  		fmt.Fprintf(
   106  			w,
   107  			"%s\t%s\t\n",
   108  			*peer.Address,
   109  			*peer.Name,
   110  		)
   111  	}
   112  	return w.Flush()
   113  }
   114  
   115  func extendedFormatLabels(labels models.LabelSet) string {
   116  	output := []string{}
   117  	for name, value := range labels {
   118  		output = append(output, fmt.Sprintf("%s=\"%s\"", name, value))
   119  	}
   120  	sort.Strings(output)
   121  	return strings.Join(output, " ")
   122  }
   123  
   124  func extendedFormatAnnotations(labels models.LabelSet) string {
   125  	output := []string{}
   126  	for name, value := range labels {
   127  		output = append(output, fmt.Sprintf("%s=\"%s\"", name, value))
   128  	}
   129  	sort.Strings(output)
   130  	return strings.Join(output, " ")
   131  }
   132  
   133  func extendedFormatMatchers(matchers models.Matchers) string {
   134  	lms := labels.Matchers{}
   135  	for _, matcher := range matchers {
   136  		lms = append(lms, labelsMatcher(*matcher))
   137  	}
   138  	return lms.String()
   139  }
   140  

View as plain text