Files
karma/internal/alertmanager/version.go
Łukasz Mierzwa 5d4ae47888 Convert all packages to be internal
Internal packages are supported by Go 1.5+, any package in /internal/ dir is only importable from the same repo. This will cleanup main dir a bit and provide better namespace for unsee subpackages
2017-08-04 16:21:27 -07:00

52 lines
1.4 KiB
Go

package alertmanager
import (
"time"
"github.com/cloudflare/unsee/internal/transport"
log "github.com/sirupsen/logrus"
)
// AlertmanagerVersion is what api/v1/status returns, we only use it to check
// version, so we skip all other keys (except for status)
type alertmanagerVersion struct {
Status string `json:"status"`
Data struct {
VersionInfo struct {
Version string `json:"version"`
} `json:"versionInfo"`
} `json:"data"`
}
// GetVersion returns version information of the remote Alertmanager endpoint
func GetVersion(uri string, timeout time.Duration) string {
// if everything fails assume Alertmanager is at latest possible version
defaultVersion := "999.0.0"
url, err := transport.JoinURL(uri, "api/v1/status")
if err != nil {
log.Errorf("Failed to join url '%s' and path 'api/v1/status': %s", uri, err.Error())
return defaultVersion
}
ver := alertmanagerVersion{}
err = transport.ReadJSON(url, timeout, &ver)
if err != nil {
log.Errorf("%s request failed: %s", url, err.Error())
return defaultVersion
}
if ver.Status != "success" {
log.Errorf("Request to %s returned status %s", url, ver.Status)
return defaultVersion
}
if ver.Data.VersionInfo.Version == "" {
log.Error("No version information in Alertmanager API")
return defaultVersion
}
log.Infof("Remote Alertmanager version: %s", ver.Data.VersionInfo.Version)
return ver.Data.VersionInfo.Version
}