mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
POC mongodb tracing
This commit is contained in:
37
main.go
37
main.go
@ -4,13 +4,22 @@ import (
|
||||
"beelzebub/parser"
|
||||
"beelzebub/protocols"
|
||||
"beelzebub/tracer"
|
||||
"context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var quit = make(chan struct{})
|
||||
|
||||
const mongoURI = "mongodb://root:example@mongo:27017/?maxPoolSize=20&w=majority"
|
||||
|
||||
var mongoClient *mongo.Client
|
||||
|
||||
func main() {
|
||||
parser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/")
|
||||
|
||||
@ -34,6 +43,9 @@ func main() {
|
||||
// Init protocol manager, with simple log on stout trace strategy and default protocol HTTP
|
||||
protocolManager := protocols.InitProtocolManager(traceStrategyStdout, hypertextTransferProtocolStrategy)
|
||||
|
||||
mongoClient = buildMongoClient(mongoURI)
|
||||
defer mongoClient.Disconnect(context.TODO())
|
||||
|
||||
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
|
||||
switch beelzebubServiceConfiguration.Protocol {
|
||||
case "http":
|
||||
@ -59,6 +71,17 @@ func traceStrategyStdout(event tracer.Event) {
|
||||
"status": event.Status.String(),
|
||||
"event": event,
|
||||
}).Info("New Event")
|
||||
|
||||
coll := mongoClient.Database("beelzebub").Collection("event")
|
||||
data, err := bson.Marshal(event)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = coll.InsertOne(context.TODO(), data)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func configureLoggingByConfigurations(configurations parser.Logging) *os.File {
|
||||
@ -80,3 +103,17 @@ func configureLoggingByConfigurations(configurations parser.Logging) *os.File {
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
func buildMongoClient(uri string) *mongo.Client {
|
||||
// Create a new client and connect to the server
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Ping the primary
|
||||
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Println("Successfully connected and pinged.")
|
||||
return client
|
||||
}
|
||||
|
@ -74,6 +74,7 @@ func (SSHStrategy *SecureShellStrategy) Init(beelzebubServiceConfiguration parse
|
||||
PasswordHandler: func(ctx ssh.Context, password string) bool {
|
||||
tr.TraceEvent(tracer.Event{
|
||||
Msg: "New SSH attempt",
|
||||
Protocol: tracer.SSH,
|
||||
Status: tracer.Stateless,
|
||||
User: ctx.User(),
|
||||
Password: password,
|
||||
|
Reference in New Issue
Block a user