2017-04-20 09:04:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
"github.com/alecthomas/kingpin"
|
2017-04-20 09:04:17 +00:00
|
|
|
"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 {
|
2017-06-08 11:14:37 +00:00
|
|
|
ConfigYAML string `json:"configYAML"`
|
|
|
|
ConfigJSON config.Config `json:"configJSON"`
|
2017-04-20 09:04:17 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
// configCmd represents the config command
|
|
|
|
var configCmd = app.Command("config", "View the running config").Action(queryConfig)
|
2017-04-20 09:04:17 +00:00
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func init() {
|
|
|
|
longHelpText["config"] = `View current config
|
2017-04-20 09:04:17 +00:00
|
|
|
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
|
2017-12-22 10:17:13 +00:00
|
|
|
- Json: Print entire config object as json`
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fetchConfig() (Config, error) {
|
|
|
|
configResponse := alertmanagerStatusResponse{}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
u := GetAlertmanagerURL("/api/v1/status")
|
2017-04-20 09:04:17 +00:00
|
|
|
res, err := http.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2017-05-08 18:25:25 +00:00
|
|
|
err = json.NewDecoder(res.Body).Decode(&configResponse)
|
2017-04-20 09:04:17 +00:00
|
|
|
if err != nil {
|
2017-05-08 18:25:25 +00:00
|
|
|
return configResponse.Data, err
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if configResponse.Status != "success" {
|
|
|
|
return Config{}, fmt.Errorf("[%s] %s", configResponse.ErrorType, configResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return configResponse.Data, nil
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
func queryConfig(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
2017-04-20 09:04:17 +00:00
|
|
|
config, err := fetchConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
formatter, found := format.Formatters[*output]
|
2017-04-20 09:04:17 +00:00
|
|
|
if !found {
|
2017-11-12 16:43:48 +00:00
|
|
|
return errors.New("unknown output formatter")
|
2017-04-20 09:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := format.Config(config)
|
|
|
|
|
|
|
|
return formatter.FormatConfig(c)
|
|
|
|
}
|