-
-
Save kelein/6fb197fd454daf5057dc8f0bd460d183 to your computer and use it in GitHub Desktop.
Read Alertmanager NFLOG
This file contains 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 ( | |
"errors" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"github.com/gogo/protobuf/jsonpb" | |
pb "github.com/prometheus/alertmanager/nflog/nflogpb" | |
"github.com/matttproud/golang_protobuf_extensions/pbutil" | |
) | |
type state map[string]*pb.MeshEntry | |
// stateKey returns a string key for a log entry consisting of the group key | |
// and receiver. | |
func stateKey(k string, r *pb.Receiver) string { | |
return fmt.Sprintf("%s:%s", k, receiverKey(r)) | |
} | |
func receiverKey(r *pb.Receiver) string { | |
return fmt.Sprintf("%s/%s/%d", r.GroupName, r.Integration, r.Idx) | |
} | |
func main() { | |
flag.Parse() | |
r, err := os.Open(flag.Arg(0)) | |
if err != nil { | |
panic(err) | |
} | |
s, err := decodeState(r) | |
if err != nil { | |
panic(err) | |
} | |
for entry, mesh := range s { | |
m := &jsonpb.Marshaler{ | |
Indent: " ", | |
} | |
os.Stdout.Write([]byte(fmt.Sprintf("Entry: %s\n", entry))) | |
m.Marshal(os.Stdout, mesh) | |
os.Stdout.Write([]byte("\n")) | |
} | |
} | |
func decodeState(r io.Reader) (state, error) { | |
st := state{} | |
for { | |
var e pb.MeshEntry | |
_, err := pbutil.ReadDelimited(r, &e) | |
if err == nil { | |
if e.Entry == nil || e.Entry.Receiver == nil { | |
return nil, errors.New("oops") | |
} | |
st[stateKey(string(e.Entry.GroupKey), e.Entry.Receiver)] = &e | |
continue | |
} | |
if err == io.EOF { | |
break | |
} | |
return nil, err | |
} | |
return st, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment