Created
October 5, 2016 09:01
-
-
Save freman/9e21953e31b00747055b66d7f2383332 to your computer and use it in GitHub Desktop.
etcdlogd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"time" | |
"github.com/coreos/etcd/client" | |
) | |
var etcdDefaultPeers = "http://alpha:4001" | |
func init() { | |
for _, variable := range []string{"ARC_ETCD_ENDPOINTS", "ETCD_ENDPOINTS", "ETCDCTL_ENDPOINTS", "ETCDCTL_PEERS"} { | |
if test := os.Getenv(variable); test != "" { | |
etcdDefaultPeers = test | |
break | |
} | |
} | |
} | |
func main() { | |
fPeers := flag.String("peers", etcdDefaultPeers, "What are we connecting to, comma seperated") | |
flag.Parse() | |
clientConfig := client.Config{ | |
Endpoints: strings.Split(*fPeers, ","), | |
Transport: client.DefaultTransport, | |
// set timeout per request to fail fast when the target endpoint is unavailable | |
HeaderTimeoutPerRequest: time.Second, | |
} | |
etcd, err := client.New(clientConfig) | |
if err != nil { | |
panic(err) | |
} | |
kapi := client.NewKeysAPI(etcd) | |
watcher := kapi.Watcher("/", &client.WatcherOptions{ | |
AfterIndex: 0, | |
Recursive: true, | |
}) | |
for { | |
etcdResponse, err := watcher.Next(context.Background()) | |
if err != nil { | |
fmt.Println("That's not right", err) | |
continue | |
} | |
msg := "" | |
if etcdResponse.Node.Dir { | |
msg = fmt.Sprintf("Directory: %s", etcdResponse.Node.Key) | |
} else { | |
if etcdResponse.PrevNode != nil { | |
msg = fmt.Sprintf("%s was %s is %s ", etcdResponse.Node.Key, etcdResponse.PrevNode.Value, etcdResponse.Node.Value) | |
} else { | |
msg = fmt.Sprintf("%s is %s", etcdResponse.Node.Key, etcdResponse.Node.Value) | |
} | |
} | |
log.Printf("[%d] act: %s, %s", etcdResponse.Index, etcdResponse.Action, msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment