-
-
Notifications
You must be signed in to change notification settings - Fork 203
added support for Grafana and Graylog alerting #633
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
Open
hvdort
wants to merge
2
commits into
bbernhard:master
Choose a base branch
from
hvdort:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,211 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"strconv" | ||
"strings" | ||
"fmt" | ||
|
||
_ "runtime/debug" | ||
_ "github.com/yassinebenaid/godump" | ||
|
||
"github.com/gin-gonic/gin" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/bbernhard/signal-cli-rest-api/client" | ||
) | ||
|
||
type AlertManagerNotification struct { | ||
Receiver string `json:"receiver"` | ||
Status string `json:"status"` | ||
Alerts []Alert `json:"alerts"` | ||
GroupLabels Labels `json:"groupLabels"` | ||
CommonLabels Labels `json:"commonLabels"` | ||
CommonAnnotations Annotations `json:"commonAnnotations"` | ||
ExternalURL string `json:"externalURL"` | ||
Version string `json:"version"` | ||
GroupKey string `json:"groupKey"` | ||
TruncatedAlerts int64 `json:"truncatedAlerts"` | ||
OrgID int64 `json:"orgId"` | ||
Title string `json:"title"` | ||
State string `json:"state"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type Alert struct { | ||
Status string `json:"status"` | ||
Labels Labels `json:"labels"` | ||
Annotations Annotations `json:"annotations"` | ||
StartsAt string `json:"startsAt"` | ||
EndsAt string `json:"endsAt"` | ||
GeneratorURL string `json:"generatorURL"` | ||
Fingerprint string `json:"fingerprint"` | ||
SilenceURL string `json:"silenceURL"` | ||
DashboardURL string `json:"dashboardURL"` | ||
PanelURL string `json:"panelURL"` | ||
Values interface{} `json:"values"` | ||
ValueString string `json:"valueString"` | ||
} | ||
|
||
type Annotations struct { | ||
Summary string `json:"summary"` | ||
} | ||
|
||
type Labels struct { | ||
Alertname string `json:"alertname"` | ||
Instance string `json:"instance"` | ||
} | ||
|
||
type GrafanaMessage struct { | ||
Number string `json:"number"` | ||
Recipients string `json:"recipients"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type GraylogNotification struct { | ||
EventDefinitionID string `json:"event_definition_id"` | ||
EventDefinitionType string `json:"event_definition_type"` | ||
EventDefinitionTitle string `json:"event_definition_title"` | ||
EventDefinitionDescription string `json:"event_definition_description"` | ||
JobDefinitionID string `json:"job_definition_id"` | ||
JobTriggerID string `json:"job_trigger_id"` | ||
Event GraylogEvent `json:"event"` | ||
Backlog []interface{} `json:"backlog"` | ||
} | ||
|
||
type GraylogEvent struct { | ||
ID string `json:"id"` | ||
EventDefinitionType string `json:"event_definition_type"` | ||
EventDefinitionID string `json:"event_definition_id"` | ||
OriginContext string `json:"origin_context"` | ||
Timestamp string `json:"timestamp"` | ||
TimestampProcessing string `json:"timestamp_processing"` | ||
TimerangeStart interface{} `json:"timerange_start"` | ||
TimerangeEnd interface{} `json:"timerange_end"` | ||
Streams []string `json:"streams"` | ||
SourceStreams []interface{} `json:"source_streams"` | ||
Message string `json:"message"` | ||
Source string `json:"source"` | ||
KeyTuple []string `json:"key_tuple"` | ||
Key string `json:"key"` | ||
Priority int64 `json:"priority"` | ||
Alert bool `json:"alert"` | ||
Fields Fields `json:"fields"` | ||
} | ||
|
||
type Fields struct { | ||
Recipients string `json:"recipients"` | ||
FromNumber string `json:"fromnumber"` | ||
Message string `json:"message"` | ||
} | ||
|
||
// @Summary Send a signal message. | ||
// @Tags Messages | ||
// @Description Send a signal message. Set the text_mode to 'styled' in case you want to add formatting to your text message. Styling Options: *italic text*, **bold text**, ~strikethrough text~. | ||
// @Accept json | ||
// @Produce json | ||
// @Success 201 {object} SendMessageResponse | ||
// @Failure 400 {object} SendMessageError | ||
// @Param data body SendMessageV2 true "Input Data" | ||
// @Router /v2/send [post] | ||
func (a *Api) SendAlertManagerV2(c *gin.Context) { | ||
var req AlertManagerNotification | ||
var msg GrafanaMessage | ||
base64Attachments := []string{} | ||
|
||
err := c.BindJSON(&req) | ||
if err != nil { | ||
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"}) | ||
log.Error(err.Error()) | ||
return | ||
} | ||
|
||
//fmt.Printf(">>>%s\n",[]byte(req.Message)) | ||
|
||
// Unmarshal or Decode the JSON to the interface. | ||
json.Unmarshal([]byte(req.Message), &msg) | ||
|
||
// timestamp, err := a.signalClient.SendV1(msg.Number, msg.Message, msg.Recipients, base64Attachments, msg.IsGroup) | ||
// if err != nil { | ||
// c.JSON(400, Error{Msg: err.Error()}) | ||
// return | ||
// } | ||
// c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt(timestamp.Timestamp, 10)}) | ||
|
||
data, err := a.signalClient.SendV2(msg.Number, msg.Message, strings.Split(msg.Recipients,","), base64Attachments, "", nil, nil, nil, nil, nil, nil, nil, nil) | ||
|
||
if err != nil { | ||
switch err.(type) { | ||
case *client.RateLimitErrorType: | ||
if rateLimitError, ok := err.(*client.RateLimitErrorType); ok { | ||
extendedError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.") | ||
c.JSON(429, SendMessageError{Msg: extendedError.Error(), ChallengeTokens: rateLimitError.ChallengeTokens, Account: msg.Number}) | ||
return | ||
} else { | ||
c.JSON(400, Error{Msg: err.Error()}) | ||
return | ||
} | ||
default: | ||
c.JSON(400, Error{Msg: err.Error()}) | ||
return | ||
} | ||
c.JSON(400, Error{Msg: err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)}) | ||
|
||
} | ||
|
||
// @Summary Send a signal message. | ||
// @Tags Messages | ||
// @Description Send a signal message. Set the text_mode to 'styled' in case you want to add formatting to your text message. Styling Options: *italic text*, **bold text**, ~strikethrough text~. | ||
// @Accept json | ||
// @Produce json | ||
// @Success 201 {object} SendMessageResponse | ||
// @Failure 400 {object} SendMessageError | ||
// @Param data body SendMessageV2 true "Input Data" | ||
// @Router /v2/send [post] | ||
func (a *Api) SendGraylogNotificationV2(c *gin.Context) { | ||
var req GraylogNotification | ||
base64Attachments := []string{} | ||
// jsonData,err2 := io.ReadAll(c.Request.Body) | ||
//if err2 != nil { | ||
// log.Error(err2.Error()) | ||
//} | ||
//fmt.Printf("<<<%s\n",jsonData) | ||
|
||
err := c.BindJSON(&req) | ||
if err != nil { | ||
c.JSON(400, gin.H{"error": "Couldn't process request - invalid requestttttt"}) | ||
log.Error(err.Error()) | ||
fmt.Printf("<<<%s\n",c.Request.Body) | ||
return | ||
} | ||
|
||
data, err := a.signalClient.SendV2(req.Event.Fields.FromNumber, req.Event.Fields.Message, strings.Split(req.Event.Fields.Recipients,","), base64Attachments, "", nil, nil, nil, nil, nil, nil, nil, nil) | ||
|
||
if err != nil { | ||
switch err.(type) { | ||
case *client.RateLimitErrorType: | ||
if rateLimitError, ok := err.(*client.RateLimitErrorType); ok { | ||
extendedError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.") | ||
c.JSON(429, SendMessageError{Msg: extendedError.Error(), ChallengeTokens: rateLimitError.ChallengeTokens, Account: req.Event.Fields.FromNumber}) | ||
return | ||
} else { | ||
c.JSON(400, Error{Msg: err.Error()}) | ||
return | ||
} | ||
default: | ||
c.JSON(400, Error{Msg: err.Error()}) | ||
return | ||
} | ||
c.JSON(400, Error{Msg: err.Error()}) | ||
return | ||
} | ||
|
||
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)}) | ||
} | ||
|
||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.