Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch alertmanager to kingpin #974

Merged
merged 1 commit into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ EXPOSE 9093
VOLUME [ "/alertmanager" ]
WORKDIR /alertmanager
ENTRYPOINT [ "/bin/alertmanager" ]
CMD [ "-config.file=/etc/alertmanager/config.yml", \
"-storage.path=/alertmanager" ]
CMD [ "--config.file=/etc/alertmanager/config.yml", \
"--storage.path=/alertmanager" ]
6 changes: 3 additions & 3 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
a1: ./alertmanager -log.level=debug -storage.path=$TMPDIR/a1 -web.listen-address=:9093 -mesh.peer-id=00:00:00:00:00:01 -mesh.nickname=a -mesh.listen-address=:8001 -config.file=examples/ha/alertmanager.yaml
a2: ./alertmanager -log.level=debug -storage.path=$TMPDIR/a2 -web.listen-address=:9094 -mesh.peer-id=00:00:00:00:00:02 -mesh.nickname=b -mesh.listen-address=:8002 -mesh.peer=127.0.0.1:8001 -config.file=examples/ha/alertmanager.yaml
a3: ./alertmanager -log.level=debug -storage.path=$TMPDIR/a3 -web.listen-address=:9095 -mesh.peer-id=00:00:00:00:00:03 -mesh.nickname=c -mesh.listen-address=:8003 -mesh.peer=127.0.0.1:8001 -config.file=examples/ha/alertmanager.yaml
a1: ./alertmanager --log.level=debug --storage.path=$TMPDIR/a1 --web.listen-address=:9093 --mesh.peer-id=00:00:00:00:00:01 --mesh.nickname=a --mesh.listen-address=:8001 --config.file=examples/ha/alertmanager.yaml
a2: ./alertmanager --log.level=debug --storage.path=$TMPDIR/a2 --web.listen-address=:9094 --mesh.peer-id=00:00:00:00:00:02 --mesh.nickname=b --mesh.listen-address=:8002 --mesh.peer=127.0.0.1:8001 --config.file=examples/ha/alertmanager.yaml
a3: ./alertmanager --log.level=debug --storage.path=$TMPDIR/a3 --web.listen-address=:9095 --mesh.peer-id=00:00:00:00:00:03 --mesh.nickname=c --mesh.listen-address=:8003 --mesh.peer=127.0.0.1:8001 --config.file=examples/ha/alertmanager.yaml
wh: go run ./examples/webhook/echo.go

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ You can either `go get` it:
```
$ GO15VENDOREXPERIMENT=1 go get github.com/prometheus/alertmanager/cmd/...
# cd $GOPATH/src/github.com/prometheus/alertmanager
$ alertmanager -config.file=<your_file>
$ alertmanager --config.file=<your_file>
```

Or checkout the source code and build manually:
Expand All @@ -42,7 +42,7 @@ $ cd $GOPATH/src/github.com/prometheus
$ git clone https://github.com/prometheus/alertmanager.git
$ cd alertmanager
$ make build
$ ./alertmanager -config.file=<your_file>
$ ./alertmanager --config.file=<your_file>
```

You can also build just one of the binaries in this repo by passing a name to the build function:
Expand Down Expand Up @@ -290,10 +290,10 @@ To create a highly available cluster of the Alertmanager the instances need to
be configured to communicate with each other. This is configured using the
`-mesh.*` flags.

- `-mesh.peer-id` string: mesh peer ID (default "&lt;hardware-mac-address&gt;")
- `-mesh.listen-address` string: mesh listen address (default "0.0.0.0:6783")
- `-mesh.nickname` string: mesh peer nickname (default "&lt;machine-hostname&gt;")
- `-mesh.peer` value: initial peers (repeat flag for each additional peer)
- `--mesh.peer-id` string: mesh peer ID (default "&lt;hardware-mac-address&gt;")
- `--mesh.listen-address` string: mesh listen address (default "0.0.0.0:6783")
- `--mesh.nickname` string: mesh peer nickname (default "&lt;machine-hostname&gt;")
- `--mesh.peer` value: initial peers (repeat flag for each additional peer)

The `mesh.peer-id` flag is used as a unique ID among the peers. It defaults to
the MAC address, therefore the default value should typically be a good option.
Expand Down
85 changes: 23 additions & 62 deletions cmd/alertmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main
import (
"crypto/md5"
"encoding/binary"
"flag"
"fmt"
"net"
"net/http"
Expand All @@ -32,6 +31,7 @@ import (
"syscall"
"time"

"github.com/alecthomas/kingpin"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/alertmanager/api"
Expand Down Expand Up @@ -109,50 +109,35 @@ func main() {
runtime.SetMutexProfileFraction(20)
}

var (
showVersion = flag.Bool("version", false, "Print version information.")

configFile = flag.String("config.file", "alertmanager.yml", "Alertmanager configuration file name.")
dataDir = flag.String("storage.path", "data/", "Base path for data storage.")
retention = flag.Duration("data.retention", 5*24*time.Hour, "How long to keep data for.")
alertGCInterval = flag.Duration("alerts.gc-interval", 30*time.Minute, "Interval between alert GC.")

externalURL = flag.String("web.external-url", "", "The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy). Used for generating relative and absolute links back to Alertmanager itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Alertmanager. If omitted, relevant URL components will be derived automatically.")
routePrefix = flag.String("web.route-prefix", "", "Prefix for the internal routes of web endpoints. Defaults to path of -web.external-url.")
listenAddress = flag.String("web.listen-address", ":9093", "Address to listen on for the web interface and API.")

meshListen = flag.String("mesh.listen-address", net.JoinHostPort("0.0.0.0", strconv.Itoa(mesh.Port)), "Mesh listen address. Pass an empty string to disable.")
hwaddr = flag.String("mesh.peer-id", "", "Mesh peer ID (default: MAC address).")
nickname = flag.String("mesh.nickname", mustHostname(), "Mesh peer nickname.")
password = flag.String("mesh.password", "", "Password to join the peer network (empty password disables encryption).")
)
peers := &stringset{}
flag.Var(peers, "mesh.peer", "Initial peers (may be repeated)")

logLevel := &promlog.AllowedLevel{}
if err := logLevel.Set("info"); err != nil {
panic(err)
}
flag.Var(logLevel, "log.level", "Only log messages with the given severity or above. One of: [debug, info, warn, error]")
var (
configFile = kingpin.Flag("config.file", "Alertmanager configuration file name.").Default("alertmanager.yml").String()
dataDir = kingpin.Flag("storage.path", "Base path for data storage.").Default("data/").String()
retention = kingpin.Flag("data.retention", "How long to keep data for.").Default("120h").Duration()
alertGCInterval = kingpin.Flag("alerts.gc-interval", "Interval between alert GC.").Default("30m").Duration()
logLevelString = kingpin.Flag("log.level", "Only log messages with the given severity or above.").Default("info").Enum("debug", "info", "warn", "error")

externalURL = kingpin.Flag("web.external-url", "The URL under which Alertmanager is externally reachable (for example, if Alertmanager is served via a reverse proxy). Used for generating relative and absolute links back to Alertmanager itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Alertmanager. If omitted, relevant URL components will be derived automatically.").String()
routePrefix = kingpin.Flag("web.route-prefix", "Prefix for the internal routes of web endpoints. Defaults to path of --web.external-url.").String()
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for the web interface and API.").Default(":9093").String()

meshListen = kingpin.Flag("mesh.listen-address", "mesh listen address. Pass an empty string to disable.").Default(net.JoinHostPort("0.0.0.0", strconv.Itoa(mesh.Port))).String()
hwaddr = kingpin.Flag("mesh.peer-id", "mesh peer ID").Default(mustHardwareAddr()).String()
nickname = kingpin.Flag("mesh.nickname", "mesh peer nickname").Default(mustHostname()).String()
password = kingpin.Flag("mesh.password", "password to join the peer network (empty password disables encryption)").Default("").String()
peers = kingpin.Flag("mesh.peer", "initial peers (may be repeated)").Strings()
)

flag.Parse()
kingpin.Version(version.Print("alertmanager"))
kingpin.CommandLine.GetFlag("help").Short('h')
kingpin.Parse()

logLevel.Set(*logLevelString)
logger := promlog.New(*logLevel)

if *hwaddr == "" {
*hwaddr = mustHardwareAddr()
}

if len(flag.Args()) > 0 {
level.Error(logger).Log("msg", "Received unexpected and unparsed arguments", "arguments", strings.Join(flag.Args(), ", "))
os.Exit(1)
}

if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("alertmanager"))
os.Exit(0)
}

level.Info(logger).Log("msg", "Starting Alertmanager", "version", version.Info())
level.Info(logger).Log("build_context", version.BuildContext())

Expand Down Expand Up @@ -234,7 +219,7 @@ func main() {
// Disable mesh if empty string passed for mesh.listen-address flag.
if *meshListen != "" {
mrouter.Start()
mrouter.ConnectionMaker.InitiateConnections(peers.slice(), true)
mrouter.ConnectionMaker.InitiateConnections(*peers, true)
}

defer func() {
Expand Down Expand Up @@ -504,30 +489,6 @@ func listen(listen string, router *route.Router, logger log.Logger) {
}
}

type stringset map[string]struct{}

func (ss stringset) Set(value string) error {
for _, v := range strings.Split(value, ",") {
if v = strings.TrimSpace(v); v != "" {
ss[v] = struct{}{}
}
}
return nil
}

func (ss stringset) String() string {
return strings.Join(ss.slice(), ",")
}

func (ss stringset) slice() []string {
slice := make([]string, 0, len(ss))
for k := range ss {
slice = append(slice, k)
}
sort.Strings(slice)
return slice
}

func mustHardwareAddr() string {
// TODO(fabxc): consider a safe-guard against colliding MAC addresses.
ifaces, err := net.Interfaces()
Expand Down
14 changes: 7 additions & 7 deletions test/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,13 @@ type Alertmanager struct {
// Start the alertmanager and wait until it is ready to receive.
func (am *Alertmanager) Start() {
cmd := exec.Command("../../alertmanager",
"-config.file", am.confFile.Name(),
"-log.level", "debug",
"-web.listen-address", am.addr,
"-storage.path", am.dir,
"-mesh.listen-address", am.mesh,
"-mesh.peer-id", am.hwaddr,
"-mesh.nickname", am.nickname,
"--config.file", am.confFile.Name(),
"--log.level", "debug",
"--web.listen-address", am.addr,
"--storage.path", am.dir,
"--mesh.listen-address", am.mesh,
"--mesh.peer-id", am.hwaddr,
"--mesh.nickname", am.nickname,
)

if am.cmd == nil {
Expand Down
27 changes: 27 additions & 0 deletions vendor/github.com/alecthomas/template/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/alecthomas/template/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading