...

Source file src/github.com/prometheus/alertmanager/cli/format/format.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  	"io"
    18  	"time"
    19  
    20  	"github.com/go-openapi/strfmt"
    21  	"gopkg.in/alecthomas/kingpin.v2"
    22  
    23  	"github.com/prometheus/alertmanager/api/v2/models"
    24  	"github.com/prometheus/alertmanager/pkg/labels"
    25  )
    26  
    27  const DefaultDateFormat = "2006-01-02 15:04:05 MST"
    28  
    29  var dateFormat *string
    30  
    31  func InitFormatFlags(app *kingpin.Application) {
    32  	dateFormat = app.Flag("date.format", "Format of date output").Default(DefaultDateFormat).String()
    33  }
    34  
    35  // Formatter needs to be implemented for each new output formatter.
    36  type Formatter interface {
    37  	SetOutput(io.Writer)
    38  	FormatSilences([]models.GettableSilence) error
    39  	FormatAlerts([]*models.GettableAlert) error
    40  	FormatConfig(*models.AlertmanagerStatus) error
    41  	FormatClusterStatus(status *models.ClusterStatus) error
    42  }
    43  
    44  // Formatters is a map of cli argument names to formatter interface object.
    45  var Formatters = map[string]Formatter{}
    46  
    47  func FormatDate(input strfmt.DateTime) string {
    48  	return time.Time(input).Format(*dateFormat)
    49  }
    50  
    51  func labelsMatcher(m models.Matcher) *labels.Matcher {
    52  	var t labels.MatchType
    53  	// Support for older alertmanager releases, which did not support isEqual.
    54  	if m.IsEqual == nil {
    55  		isEqual := true
    56  		m.IsEqual = &isEqual
    57  	}
    58  	switch {
    59  	case !*m.IsRegex && *m.IsEqual:
    60  		t = labels.MatchEqual
    61  	case !*m.IsRegex && !*m.IsEqual:
    62  		t = labels.MatchNotEqual
    63  	case *m.IsRegex && *m.IsEqual:
    64  		t = labels.MatchRegexp
    65  	case *m.IsRegex && !*m.IsEqual:
    66  		t = labels.MatchNotRegexp
    67  	}
    68  
    69  	return &labels.Matcher{Type: t, Name: *m.Name, Value: *m.Value}
    70  }
    71  

View as plain text