alertmanager/cli/config.go
Simon Pasquier 0c086e3b12 cli: extract client bindings of the v1 API (#1278)
* cli: extract client bindings of the v1 API from amtool

This is a continuation of [1] but the code is kept in the alertmanage
repository rather than having it in client_golang.

[1] https://github.com/prometheus/client_golang/pull/333

Co-Authored-By: Fabian Reinartz <fab.reinartz@gmail.com>
Co-Authored-By: Tristan Colgate <tcolgate@gmail.com>
Co-Authored-By: Corin Lawson <corin@responsight.com>
Co-Authored-By: stuart nelson <stuartnelson3@gmail.com>

* cli: fix httpSilenceAPI.Set() method

* vendor: remove github.com/prometheus/client_golang/api/alertmanager

* cli: don't use the model.Alert type
2018-03-28 19:19:04 +02:00

81 lines
2.0 KiB
Go

package cli
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/alecthomas/kingpin"
"github.com/prometheus/alertmanager/cli/format"
"github.com/prometheus/alertmanager/config"
)
// Config is the response type of alertmanager config endpoint
// Duped in cli/format needs to be moved to common/model
type Config struct {
ConfigYAML string `json:"configYAML"`
ConfigJSON config.Config `json:"configJSON"`
MeshStatus map[string]interface{} `json:"meshStatus"`
VersionInfo map[string]string `json:"versionInfo"`
Uptime time.Time `json:"uptime"`
}
type alertmanagerStatusResponse struct {
Status string `json:"status"`
Data Config `json:"data,omitempty"`
ErrorType string `json:"errorType,omitempty"`
Error string `json:"error,omitempty"`
}
// configCmd represents the config command
var configCmd = app.Command("config", "View the running config").Action(queryConfig)
func init() {
longHelpText["config"] = `View current config
The amount of output is controlled by the output selection flag:
- Simple: Print just the running config
- Extended: Print the running config as well as uptime and all version info
- Json: Print entire config object as json`
}
func fetchConfig() (Config, error) {
configResponse := alertmanagerStatusResponse{}
u := GetAlertmanagerURL("/api/v1/status")
res, err := http.Get(u.String())
if err != nil {
return Config{}, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&configResponse)
if err != nil {
return configResponse.Data, err
}
if configResponse.Status != "success" {
return Config{}, fmt.Errorf("[%s] %s", configResponse.ErrorType, configResponse.Error)
}
return configResponse.Data, nil
}
func queryConfig(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
config, err := fetchConfig()
if err != nil {
return err
}
formatter, found := format.Formatters[*output]
if !found {
return errors.New("unknown output formatter")
}
c := format.Config(config)
return formatter.FormatConfig(c)
}