-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #810 from forta-network/caner/forta-1277-report-do…
…cker-events-from-nodes Report Docker events as metrics
- Loading branch information
Showing
10 changed files
with
432 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package containers | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types/events" | ||
"github.com/forta-network/forta-core-go/protocol" | ||
"github.com/forta-network/forta-node/clients" | ||
"github.com/forta-network/forta-node/clients/docker" | ||
"github.com/forta-network/forta-node/services/components/metrics" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// ListenToDockerEvents creates new. | ||
func ListenToDockerEvents( | ||
ctx context.Context, dockerClient clients.DockerClient, msgClient clients.MessageClient, | ||
startFrom time.Time, | ||
) { | ||
handler := &eventHandler{ | ||
dockerClient: dockerClient, | ||
msgClient: msgClient, | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
logrus.Info("stopping docker events listener") | ||
return | ||
default: | ||
} | ||
|
||
events, errs := dockerClient.Events(ctx, startFrom) | ||
|
||
var restartListening bool | ||
for { | ||
select { | ||
case event := <-events: | ||
handler.HandleEvent(ctx, &event) | ||
|
||
case err := <-errs: | ||
logrus.WithError(err).Error("error while listening to docker events") | ||
// set the start time and restart listening | ||
startFrom = time.Now().Add(-1 * time.Second) | ||
restartListening = true | ||
|
||
case <-ctx.Done(): | ||
logrus.Info("stopping docker events listener") | ||
return | ||
} | ||
if restartListening { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
type eventHandler struct { | ||
dockerClient clients.DockerClient | ||
msgClient clients.MessageClient | ||
} | ||
|
||
func (es *eventHandler) HandleEvent(ctx context.Context, event *events.Message) { | ||
var metric *protocol.AgentMetric | ||
ts := time.Unix(0, event.TimeNano) | ||
switch event.Type { | ||
case "image": | ||
if event.Action != "pull" { | ||
return | ||
} | ||
imageRef := getEventAttribute(event, "name") | ||
metric = metrics.CreateEventMetric(ts, "system", metricNameFrom(event), imageRef) | ||
|
||
case "container", "network": | ||
if !isOneOf(event.Action, "create", "destroy", "connect", "disconnect") { | ||
return | ||
} | ||
botID, ok := getBotID(event) | ||
if !ok { | ||
botID = "system" | ||
} | ||
containerName := getEventAttribute(event, "name") | ||
metric = metrics.CreateEventMetric(ts, botID, metricNameFrom(event), containerName) | ||
|
||
default: | ||
return | ||
} | ||
|
||
metrics.SendAgentMetrics(es.msgClient, []*protocol.AgentMetric{metric}) | ||
return | ||
} | ||
|
||
func isOneOf(input string, values ...string) bool { | ||
for _, value := range values { | ||
if input == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func metricNameFrom(event *events.Message) string { | ||
return strings.Join([]string{"docker", event.Type, event.Action}, ".") | ||
} | ||
|
||
func getBotID(event *events.Message) (string, bool) { | ||
val := getEventAttribute(event, docker.LabelFortaBotID) | ||
return val, len(val) > 0 | ||
} | ||
|
||
func getEventAttribute(event *events.Message, attr string) string { | ||
if event.Actor.Attributes == nil { | ||
return "" | ||
} | ||
return event.Actor.Attributes[attr] | ||
} |
Oops, something went wrong.