| 43 | } |
| 44 | |
| 45 | func (s *Splunk) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) { |
| 46 | name := notification.GetName() |
| 47 | cfg, ok := s.PluginConfigByName[name] |
| 48 | |
| 49 | if !ok { |
| 50 | return &protobufs.Empty{}, fmt.Errorf("splunk invalid config name %s", name) |
| 51 | } |
| 52 | |
| 53 | if cfg.LogLevel != "" { |
| 54 | logger.SetLevel(hclog.LevelFromString(cfg.LogLevel)) |
| 55 | } |
| 56 | |
| 57 | logger.Info(fmt.Sprintf("received notify signal for %s config", name)) |
| 58 | |
| 59 | p := Payload{Event: notification.GetText()} |
| 60 | |
| 61 | data, err := json.Marshal(p) |
| 62 | if err != nil { |
| 63 | return &protobufs.Empty{}, err |
| 64 | } |
| 65 | |
| 66 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, cfg.URL, strings.NewReader(string(data))) |
| 67 | if err != nil { |
| 68 | return &protobufs.Empty{}, err |
| 69 | } |
| 70 | |
| 71 | req.Header.Add("Authorization", fmt.Sprintf("Splunk %s", cfg.Token)) |
| 72 | logger.Debug(fmt.Sprintf("posting event %s to %s", string(data), req.URL)) |
| 73 | |
| 74 | resp, err := s.Client.Do(req) |
| 75 | if err != nil { |
| 76 | return &protobufs.Empty{}, err |
| 77 | } |
| 78 | defer resp.Body.Close() |
| 79 | |
| 80 | if resp.StatusCode != http.StatusOK { |
| 81 | content, err := io.ReadAll(resp.Body) |
| 82 | if err != nil { |
| 83 | return &protobufs.Empty{}, fmt.Errorf("got non 200 response and failed to read error %w", err) |
| 84 | } |
| 85 | |
| 86 | return &protobufs.Empty{}, fmt.Errorf("got non 200 response %s", string(content)) |
| 87 | } |
| 88 | |
| 89 | respData, err := io.ReadAll(resp.Body) |
| 90 | if err != nil { |
| 91 | return &protobufs.Empty{}, fmt.Errorf("failed to read response body got error %w", err) |
| 92 | } |
| 93 | |
| 94 | logger.Debug(fmt.Sprintf("got response %s", string(respData))) |
| 95 | |
| 96 | return &protobufs.Empty{}, nil |
| 97 | } |
| 98 | |
| 99 | func (s *Splunk) Configure(_ context.Context, config *protobufs.Config) (*protobufs.Empty, error) { |
| 100 | d := PluginConfig{} |