Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin to send notification to alertmanager #3135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cmd/notification-alertmanager/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ifeq ($(OS), Windows_NT)
SHELL := pwsh.exe
.SHELLFLAGS := -NoProfile -Command
EXT = .exe
endif

GO = go
GOBUILD = $(GO) build

BINARY_NAME = notification-alertmanager$(EXT)

build: clean
$(GOBUILD) $(LD_OPTS) -o $(BINARY_NAME)

.PHONY: clean
clean:
@$(RM) $(BINARY_NAME) $(WIN_IGNORE_ERR)
25 changes: 25 additions & 0 deletions cmd/notification-alertmanager/alertmanager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type: alertmanager # Don't change
name: alertmanager_default # Must match the registered plugin in the profile

# One of "trace", "debug", "info", "warn", "error", "off"
log_level: info

# group_wait: # Time to wait collecting alerts before relaying a message to this plugin, eg "30s"
# group_threshold: # Amount of alerts that triggers a message before <group_wait> has expired, eg "10"
# max_retry: # Number of attempts to relay messages to plugins in case of error
timeout: 20s # Time to wait for response from the plugin before considering the attempt a failure, eg "10s"

#-------------------------
# plugin-specific options

format: |
{{.|toJson}}

loglevel: "info"
host: "alertmanager_host_url"
user: "alertmanager username"
password: "alertmanager password"
basepath: "/api/v1"
schemes: ["https"]
source: "cluster_1" # source of the alert (cluster_name, server_name,...)
team: "infra_team" # the team in charge for this source or scope
50 changes: 50 additions & 0 deletions cmd/notification-alertmanager/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"os"

protobufs "github.com/crowdsecurity/crowdsec/pkg/protobufs"
hclog "github.com/hashicorp/go-hclog"
plugin "github.com/hashicorp/go-plugin"
)

var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
Name: "alertmanager-plugin",
Level: hclog.LevelFromString("DEBUG"),
Output: os.Stderr,
JSONFormat: true,
})

type PluginConfig struct {
Name string
LogLevel *string

Host string
BasePath string
Schemes []string
User string
Password string
Source string
Team string
}

func main() {
var handshake = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
}

plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"alertmanager": &protobufs.NotifierPlugin{
Impl: &AlertmanagerPlugin{
ConfigByName: make(map[string]PluginConfig),
},
},
},
GRPCServer: plugin.DefaultGRPCServer,
Logger: logger,
})
}
74 changes: 74 additions & 0 deletions cmd/notification-alertmanager/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"context"
"fmt"
"time"

protobufs "github.com/crowdsecurity/crowdsec/pkg/protobufs"
runtime "github.com/go-openapi/runtime/client"
strfmt "github.com/go-openapi/strfmt"
hclog "github.com/hashicorp/go-hclog"
alert "github.com/prometheus/alertmanager/api/v2/client/alert"
"github.com/prometheus/alertmanager/api/v2/models"
yaml "gopkg.in/yaml.v2"
)

type AlertmanagerPlugin struct {
ConfigByName map[string]PluginConfig
}

func (n *AlertmanagerPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
d := PluginConfig{}
if err := yaml.Unmarshal(config.Config, &d); err != nil {
return nil, err
}
n.ConfigByName[d.Name] = d
return &protobufs.Empty{}, nil
}

func (n *AlertmanagerPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := n.ConfigByName[notification.Name]; !ok {
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
}
cfg := n.ConfigByName[notification.Name]
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
} else {
logger.SetLevel(hclog.Info)
}

format := strfmt.NewFormats()
transport := runtime.New(cfg.Host, cfg.BasePath, cfg.Schemes)
transport.DefaultAuthentication = runtime.BasicAuth(cfg.User, cfg.Password)
client := alert.New(transport, format)

alertParams := n.createPostAlertParams(ctx, cfg, notification)
_, err := client.PostAlerts(alertParams)
if err != nil {
logger.Error("ErreurPostAlerts:", err.Error())
return nil, err
} else {
logger.Info(fmt.Sprintf(" %s ", notification.Name))
}
return &protobufs.Empty{}, nil
}

func (n *AlertmanagerPlugin) createPostAlertParams(ctx context.Context, cfg PluginConfig, notification *protobufs.Notification) *alert.PostAlertsParams {
alertParams := alert.NewPostAlertsParams()
now := time.Now()
params := &models.PostableAlert{
StartsAt: strfmt.DateTime(now),
EndsAt: strfmt.DateTime(now.Add(5 * time.Minute)),
Alert: models.Alert{
Labels: models.LabelSet{
"alertname": "crowdsec_alert",
"source": cfg.Source,
"team": cfg.Team,
"text": notification.Text,
},
},
}
alertParams.Alerts = models.PostableAlerts{params}
return alertParams
}