diff --git a/config.go b/config.go index d4676bf891..772fb02afe 100644 --- a/config.go +++ b/config.go @@ -168,7 +168,7 @@ func getConfig() *types.Configuration { c.Opsgenie.MinimumPriority = checkPriority(c.Opsgenie.MinimumPriority) c.Webhook.MinimumPriority = checkPriority(c.Webhook.MinimumPriority) c.Azure.EventHub.MinimumPriority = checkPriority(c.Azure.EventHub.MinimumPriority) - c.GCP.PubSub.MinimumPriority = checkPriority(c.PubSub.EventHub.MinimumPriority) + c.GCP.PubSub.MinimumPriority = checkPriority(c.GCP.PubSub.MinimumPriority) c.Slack.MessageFormatTemplate = getMessageFormatTemplate("Slack", c.Slack.MessageFormat) c.Rocketchat.MessageFormatTemplate = getMessageFormatTemplate("Rocketchat", c.Rocketchat.MessageFormat) diff --git a/go.sum b/go.sum index 053973ff93..7b1b8b1377 100644 --- a/go.sum +++ b/go.sum @@ -234,6 +234,7 @@ github.com/prometheus/client_golang v1.7.1 h1:NTGy1Ja9pByO+xAeH/qiWnLrKtr3hJPNja github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM= diff --git a/main.go b/main.go index 96501e6335..1ecc2b269a 100644 --- a/main.go +++ b/main.go @@ -246,7 +246,7 @@ func init() { } if config.GCP.PubSub.ProjectID != "" && config.GCP.PubSub.Topic != "" && config.GCP.Credentials != "" { var err error - gcpClient, err = outputs.NewGCPClient(config, stats, statsdClient, dogstatsdClient) + gcpClient, err = outputs.NewGCPClient(config, stats, promStats, statsdClient, dogstatsdClient) if err != nil { config.GCP.Credentials = "" } else { diff --git a/outputs/gcp.go b/outputs/gcp.go index f312393db2..088d60b3fc 100644 --- a/outputs/gcp.go +++ b/outputs/gcp.go @@ -4,25 +4,17 @@ import ( "cloud.google.com/go/pubsub" "context" "encoding/base64" - "golang.org/x/oauth2/google" - "google.golang.org/api/option" - "log" - "os" - "errors" "encoding/json" + "errors" "github.com/DataDog/datadog-go/statsd" "github.com/falcosecurity/falcosidekick/types" + "golang.org/x/oauth2/google" + "google.golang.org/api/option" + "log" ) // NewGCPClient returns a new output.Client for accessing the GCP API. -func NewGCPClient(config *types.Configuration, stats *types.Statistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) { - - if config.GCP.Credentials != "" { - os.Setenv("GCP_CREDENTIALS", config.GCP.Credentials) - os.Setenv("GCP_PUBSUB_PROJECTID", config.GCP.PubSub.ProjectID) - os.Setenv("GCP_PUBSUB_TOPIC", config.GCP.PubSub.Topic) - } - +func NewGCPClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) { base64decodedCredentialsData, err := base64.StdEncoding.DecodeString(config.GCP.Credentials) if err != nil { log.Printf("[ERROR] : GCP - %v\n", "Error while base64-decoding GCP Credentials") @@ -49,6 +41,7 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, statsdCl Config: config, GCPTopicClient: topicClient, Stats: stats, + PromStats: promStats, StatsdClient: statsdClient, DogstatsdClient: dogstatsdClient, }, nil @@ -57,7 +50,6 @@ func NewGCPClient(config *types.Configuration, stats *types.Statistics, statsdCl // GCPPublishTopic sends a message to a GCP PubSub Topic func (c *Client) GCPPublishTopic(falcopayload types.FalcoPayload) { c.Stats.GCPPubSub.Add("total", 1) - go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"}) payload, _ := json.Marshal(falcopayload) message := &pubsub.Message{ @@ -66,12 +58,14 @@ func (c *Client) GCPPublishTopic(falcopayload types.FalcoPayload) { result := c.GCPTopicClient.Publish(context.Background(), message) id, err := result.Get(context.Background()) if err != nil { - log.Printf("[ERROR] : GCPPubSub - %v - %v\n", "Error while publishing message", err.Error()) + log.Printf("[ERROR] : GCPPubSub - %v - %v\n", "Error while publishing message", err.Error()) c.Stats.GCPPubSub.Add("error", 1) go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:error"}) + c.PromStats.Outputs.With(map[string]string{"destination": "gcppubsub", "status": "error"}).Inc() return } log.Printf("[INFO] : GCPPubSub - Send to topic OK (%v)\n", id) - c.Stats.GCPPubSub.Add("ok", 1) - go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"}) + c.Stats.GCPPubSub.Add("ok", 1) + go c.CountMetric("outputs", 1, []string{"output:gcppubsub", "status:ok"}) + c.PromStats.Outputs.With(map[string]string{"destination": "gcppubsub", "status": "ok"}).Inc() } diff --git a/stats.go b/stats.go index 22a877fe03..09e2840684 100644 --- a/stats.go +++ b/stats.go @@ -21,27 +21,27 @@ func getInitStats() *types.Statistics { FIFO: getInputNewMap("fifo"), GRPC: getInputNewMap("grpc"), Falco: expvar.NewMap("falco.priority"), - Slack: expvar.NewMap("outputs.slack"), - Rocketchat: expvar.NewMap("outputs.rocketchat"), - Mattermost: expvar.NewMap("outputs.mattermost"), - Teams: expvar.NewMap("outputs.teams"), - Datadog: expvar.NewMap("outputs.datadog"), - Discord: expvar.NewMap("outputs.discord"), - Alertmanager: expvar.NewMap("outputs.alertmanager"), - Elasticsearch: expvar.NewMap("outputs.elasticsearch"), - Loki: expvar.NewMap("outputs.loki"), - Nats: expvar.NewMap("outputs.nats"), - Influxdb: expvar.NewMap("outputs.influxdb"), - AWSLambda: expvar.NewMap("outputs.awslambda"), - AWSSQS: expvar.NewMap("outputs.awssqs"), - AWSSNS: expvar.NewMap("outputs.awssns"), - SMTP: expvar.NewMap("outputs.smtp"), - Opsgenie: expvar.NewMap("outputs.opsgenie"), - Statsd: expvar.NewMap("outputs.statsd"), - Dogstatsd: expvar.NewMap("outputs.dogstatsd"), - Webhook: expvar.NewMap("outputs.webhook"), - AzureEventHub: expvar.NewMap("outputs.azureeventhub"), - GCPPubSub: expvar.NewMap("outputs.gcppubsub"), + Slack: getOutputNewMap("slack"), + Rocketchat: getOutputNewMap("rocketchat"), + Mattermost: getOutputNewMap("mattermost"), + Teams: getOutputNewMap("teams"), + Datadog: getOutputNewMap("datadog"), + Discord: getOutputNewMap("discord"), + Alertmanager: getOutputNewMap("alertmanager"), + Elasticsearch: getOutputNewMap("elasticsearch"), + Loki: getOutputNewMap("loki"), + Nats: getOutputNewMap("nats"), + Influxdb: getOutputNewMap("influxdb"), + AWSLambda: getOutputNewMap("awslambda"), + AWSSQS: getOutputNewMap("awssqs"), + AWSSNS: getOutputNewMap("awssns"), + SMTP: getOutputNewMap("smtp"), + Opsgenie: getOutputNewMap("opsgenie"), + Statsd: getOutputNewMap("statsd"), + Dogstatsd: getOutputNewMap("dogstatsd"), + Webhook: getOutputNewMap("webhook"), + AzureEventHub: getOutputNewMap("azureeventhub"), + GCPPubSub: getOutputNewMap("gcppubsub"), } stats.Falco.Add("emergency", 0) stats.Falco.Add("alert", 0) @@ -52,69 +52,7 @@ func getInitStats() *types.Statistics { stats.Falco.Add("informational", 0) stats.Falco.Add("debug", 0) stats.Falco.Add("unknown", 0) - stats.Slack.Add("total", 0) - stats.Slack.Add("error", 0) - stats.Slack.Add("ok", 0) - stats.Rocketchat.Add("total", 0) - stats.Rocketchat.Add("error", 0) - stats.Rocketchat.Add("ok", 0) - stats.Mattermost.Add("total", 0) - stats.Mattermost.Add("error", 0) - stats.Mattermost.Add("ok", 0) - stats.Teams.Add("total", 0) - stats.Teams.Add("error", 0) - stats.Teams.Add("ok", 0) - stats.Datadog.Add("total", 0) - stats.Datadog.Add("error", 0) - stats.Datadog.Add("ok", 0) - stats.Discord.Add("total", 0) - stats.Discord.Add("error", 0) - stats.Discord.Add("ok", 0) - stats.Alertmanager.Add("total", 0) - stats.Alertmanager.Add("error", 0) - stats.Alertmanager.Add("ok", 0) - stats.Elasticsearch.Add("total", 0) - stats.Elasticsearch.Add("error", 0) - stats.Elasticsearch.Add("ok", 0) - stats.Influxdb.Add("total", 0) - stats.Influxdb.Add("error", 0) - stats.Influxdb.Add("ok", 0) - stats.Loki.Add("total", 0) - stats.Loki.Add("error", 0) - stats.Loki.Add("ok", 0) - stats.Nats.Add("total", 0) - stats.Nats.Add("error", 0) - stats.Nats.Add("ok", 0) - stats.AWSLambda.Add("total", 0) - stats.AWSLambda.Add("error", 0) - stats.AWSLambda.Add("ok", 0) - stats.AWSSQS.Add("total", 0) - stats.AWSSQS.Add("error", 0) - stats.AWSSQS.Add("ok", 0) - stats.AWSSNS.Add("total", 0) - stats.AWSSNS.Add("error", 0) - stats.AWSSNS.Add("ok", 0) - stats.SMTP.Add("total", 0) - stats.SMTP.Add("error", 0) - stats.SMTP.Add("ok", 0) - stats.Opsgenie.Add("total", 0) - stats.Opsgenie.Add("error", 0) - stats.Opsgenie.Add("ok", 0) - stats.Statsd.Add("total", 0) - stats.Statsd.Add("error", 0) - stats.Statsd.Add("ok", 0) - stats.Dogstatsd.Add("total", 0) - stats.Dogstatsd.Add("error", 0) - stats.Dogstatsd.Add("ok", 0) - stats.Webhook.Add("total", 0) - stats.Webhook.Add("error", 0) - stats.Webhook.Add("ok", 0) - stats.AzureEventHub.Add("total", 0) - stats.AzureEventHub.Add("error", 0) - stats.AzureEventHub.Add("ok", 0) - stats.GCPPubSub.Add("total", 0) - stats.GCPPubSub.Add("error", 0) - stats.GCPPubSub.Add("ok", 0) + return stats }