Skip to content

Instantly share code, notes, and snippets.

@camathieu
Created August 30, 2017 15:07
Show Gist options
  • Save camathieu/9ff9d9a8c35c587f29e6be315f33a014 to your computer and use it in GitHub Desktop.
Save camathieu/9ff9d9a8c35c587f29e6be315f33a014 to your computer and use it in GitHub Desktop.
Full mesh monitoring
package main
import (
"log"
"flag"
"net"
"strings"
"time"
"os"
"os/signal"
serf2 "github.com/hashicorp/serf/serf"
)
func main() {
listen := flag.String("listen", "", "Listen address")
advertise := flag.String("advertise", "", "Advertise address")
bootstrap := flag.String("bootstrap","","List of bootstrap server")
flag.Parse()
events := make(chan serf2.Event, 0)
serfConfig := serf2.DefaultConfig()
serfConfig.EventCh = events
serfConfig.MemberlistConfig.DisableTcpPings = true
if *listen != "" {
serfConfig.NodeName = *listen
addr, err := net.ResolveUDPAddr("udp", *listen)
if err != nil {
log.Fatalf("Unable to resolve bind address : %v", err)
}
serfConfig.MemberlistConfig.BindAddr = addr.IP.String()
serfConfig.MemberlistConfig.BindPort = addr.Port
}
if *advertise != "" {
addr, err := net.ResolveUDPAddr("udp", *listen)
if err != nil {
log.Fatalf("Unable to resolve advertise address : %v", err)
}
serfConfig.MemberlistConfig.AdvertiseAddr = addr.IP.String()
serfConfig.MemberlistConfig.AdvertisePort = addr.Port
}
serf, err := serf2.Create(serfConfig)
if err != nil {
log.Fatalf("Unable to start serf : %v", err)
}
/*
* Handle SIGINT
*/
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for {
<-c
log.Println("SIGING detected")
err = serf.Shutdown()
if err != nil {
log.Printf("Unable to shutdown serf : %v", err)
os.Exit(1)
}
os.Exit(0)
}
}()
if *bootstrap != "" {
serf.Join(strings.Split(*bootstrap,","), false)
}
go func() {
for {
for _, member := range serf.Members() {
rtt, err := serf.Memberlist().Ping(member.Name,net.Addr(&net.UDPAddr{IP: member.Addr, Port: int(member.Port)}))
if err == nil {
log.Printf("node %s rtt is %s", member.Name, rtt)
} else {
log.Printf("node %s rtt is %v", member.Name, err)
}
}
time.Sleep(10 * time.Second)
}
}()
select {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment