2019-08-16 19:43:15 +00:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-08-27 17:29:49 +00:00
|
|
|
"fmt"
|
2019-08-16 19:43:15 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/flux/ast"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/notification/endpoint"
|
|
|
|
"github.com/influxdata/influxdb/v2/notification/flux"
|
2019-08-16 19:43:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HTTP is the notification rule config of http.
|
|
|
|
type HTTP struct {
|
|
|
|
Base
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateFlux generates a flux script for the http notification rule.
|
|
|
|
func (s *HTTP) GenerateFlux(e influxdb.NotificationEndpoint) (string, error) {
|
2019-08-27 17:29:49 +00:00
|
|
|
httpEndpoint, ok := e.(*endpoint.HTTP)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("endpoint provided is a %s, not an HTTP endpoint", e.Type())
|
|
|
|
}
|
|
|
|
p, err := s.GenerateFluxAST(httpEndpoint)
|
2019-08-16 19:43:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return ast.Format(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateFluxAST generates a flux AST for the http notification rule.
|
2019-08-27 17:29:49 +00:00
|
|
|
func (s *HTTP) GenerateFluxAST(e *endpoint.HTTP) (*ast.Package, error) {
|
2019-08-16 19:43:15 +00:00
|
|
|
f := flux.File(
|
|
|
|
s.Name,
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
s.imports(e),
|
2019-08-27 17:29:49 +00:00
|
|
|
s.generateFluxASTBody(e),
|
2019-08-16 19:43:15 +00:00
|
|
|
)
|
|
|
|
return &ast.Package{Package: "main", Files: []*ast.File{f}}, nil
|
|
|
|
}
|
|
|
|
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
func (s *HTTP) imports(e *endpoint.HTTP) []*ast.ImportDeclaration {
|
|
|
|
packages := []string{
|
|
|
|
"influxdata/influxdb/monitor",
|
|
|
|
"http",
|
|
|
|
"json",
|
|
|
|
"experimental",
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.AuthMethod == "bearer" || e.AuthMethod == "basic" {
|
|
|
|
packages = append(packages, "influxdata/influxdb/secrets")
|
|
|
|
}
|
|
|
|
|
|
|
|
return flux.Imports(packages...)
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
func (s *HTTP) generateFluxASTBody(e *endpoint.HTTP) []ast.Statement {
|
2019-08-16 19:43:15 +00:00
|
|
|
var statements []ast.Statement
|
|
|
|
statements = append(statements, s.generateTaskOption())
|
2019-09-05 22:32:28 +00:00
|
|
|
statements = append(statements, s.generateHeaders(e))
|
2019-08-27 17:29:49 +00:00
|
|
|
statements = append(statements, s.generateFluxASTEndpoint(e))
|
|
|
|
statements = append(statements, s.generateFluxASTNotificationDefinition(e))
|
2019-08-19 11:31:46 +00:00
|
|
|
statements = append(statements, s.generateFluxASTStatuses())
|
2020-02-20 21:37:03 +00:00
|
|
|
statements = append(statements, s.generateLevelChecks()...)
|
2019-08-16 19:43:15 +00:00
|
|
|
statements = append(statements, s.generateFluxASTNotifyPipe())
|
|
|
|
|
|
|
|
return statements
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s *HTTP) generateHeaders(e *endpoint.HTTP) ast.Statement {
|
|
|
|
props := []*ast.Property{
|
|
|
|
flux.Dictionary(
|
|
|
|
"Content-Type", flux.String("application/json"),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.AuthMethod {
|
|
|
|
case "bearer":
|
|
|
|
token := flux.Call(
|
|
|
|
flux.Member("secrets", "get"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("key", flux.String(e.Token.Key)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
bearer := flux.Add(
|
|
|
|
flux.String("Bearer "),
|
|
|
|
token,
|
|
|
|
)
|
|
|
|
auth := flux.Dictionary("Authorization", bearer)
|
|
|
|
props = append(props, auth)
|
|
|
|
case "basic":
|
|
|
|
username := flux.Call(
|
|
|
|
flux.Member("secrets", "get"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("key", flux.String(e.Username.Key)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
passwd := flux.Call(
|
|
|
|
flux.Member("secrets", "get"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("key", flux.String(e.Password.Key)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
basic := flux.Call(
|
|
|
|
flux.Member("http", "basicAuth"),
|
|
|
|
flux.Object(
|
|
|
|
flux.Property("u", username),
|
|
|
|
flux.Property("p", passwd),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
auth := flux.Dictionary("Authorization", basic)
|
|
|
|
props = append(props, auth)
|
|
|
|
}
|
|
|
|
return flux.DefineVariable("headers", flux.Object(props...))
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
func (s *HTTP) generateFluxASTEndpoint(e *endpoint.HTTP) ast.Statement {
|
|
|
|
call := flux.Call(flux.Member("http", "endpoint"), flux.Object(flux.Property("url", flux.String(e.URL))))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-19 11:31:46 +00:00
|
|
|
return flux.DefineVariable("endpoint", call)
|
2019-08-16 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTP) generateFluxASTNotifyPipe() ast.Statement {
|
2019-09-05 22:32:28 +00:00
|
|
|
endpointBody := flux.Call(
|
|
|
|
flux.Member("json", "encode"),
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
flux.Object(flux.Property("v", flux.Identifier("body"))),
|
2019-09-05 22:32:28 +00:00
|
|
|
)
|
|
|
|
headers := flux.Property("headers", flux.Identifier("headers"))
|
|
|
|
|
|
|
|
endpointProps := []*ast.Property{
|
|
|
|
headers,
|
|
|
|
flux.Property("data", endpointBody),
|
|
|
|
}
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
endpointFn := flux.FuncBlock(flux.FunctionParams("r"),
|
|
|
|
s.generateBody(),
|
|
|
|
&ast.ReturnStatement{
|
|
|
|
Argument: flux.Object(endpointProps...),
|
|
|
|
},
|
|
|
|
)
|
2019-08-16 19:43:15 +00:00
|
|
|
|
|
|
|
props := []*ast.Property{}
|
2019-08-19 11:31:46 +00:00
|
|
|
props = append(props, flux.Property("data", flux.Identifier("notification")))
|
2019-08-16 19:43:15 +00:00
|
|
|
props = append(props, flux.Property("endpoint",
|
2019-08-19 11:31:46 +00:00
|
|
|
flux.Call(flux.Identifier("endpoint"), flux.Object(flux.Property("mapFn", endpointFn)))))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-23 21:46:03 +00:00
|
|
|
call := flux.Call(flux.Member("monitor", "notify"), flux.Object(props...))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
return flux.ExpressionStatement(flux.Pipe(flux.Identifier("all_statuses"), call))
|
2019-08-16 19:43:15 +00:00
|
|
|
}
|
|
|
|
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
func (s *HTTP) generateBody() ast.Statement {
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
// {r with "_version": 1}
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
props := []*ast.Property{
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
flux.Property(
|
|
|
|
"_version", flux.Integer(1),
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
),
|
|
|
|
}
|
feat(notification/rule): add _version: 1 to HTTP body (#15028)
This adds the _version: 1 correctly to the body of the HTTP POST.
Additionally, this fixes the imports when using secrets.
The POSTed JSON body now is:
```json
{
"_check_id": "046cac59e2aa3000",
"_check_name": "High CPU User Usage",
"_level": "crit",
"_measurement": "notifications",
"_message": "High CPU User Usage: rsavage.prod is crit",
"_notification_endpoint_id": "046cad0c83aec000",
"_notification_endpoint_name": "HTTP Endpoint",
"_notification_rule_id": "046dff53d4183000",
"_notification_rule_name": "HTTP Notification",
"_source_measurement": "cpu",
"_source_timestamp": 1567797375000000000,
"_start": "2019-09-06T19:15:59Z",
"_status_timestamp": 1567797376416632300,
"_stop": "2019-09-06T19:16:20.362006739Z",
"_time": "2019-09-06T19:16:20.609629338Z",
"_type": "threshold",
"_version": 1,
"cpu": "cpu-total",
"host": "rsavage.prod",
"usage_user": 91.12278069517379
}
```
2019-09-06 21:21:27 +00:00
|
|
|
|
|
|
|
body := flux.ObjectWith("r", props...)
|
|
|
|
return flux.DefineVariable("body", body)
|
feat(notification/rule): add JSON body for the HTTP POST (#14994)
The body of the JSON webhook would be:
{
"version": 1,
"rule_name": notification._notification_rule_name,
"rule_id": notification._notification_rule_id,
"endpoint_name": notification._notification_endpoint_name,
"endpoint_id": notification._notification_endpoint_id,
"check_name": r._check_name,
"check_id": r._check_id,
"check_type": r._type,
"source_measurement": r._source_measurement,
"source_timestamp": r._source_timestamp,
"level": r._level,
"message": r._message,
}
2019-09-06 01:38:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 19:43:15 +00:00
|
|
|
type httpAlias HTTP
|
|
|
|
|
|
|
|
// MarshalJSON implement json.Marshaler interface.
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s HTTP) MarshalJSON() ([]byte, error) {
|
2019-08-16 19:43:15 +00:00
|
|
|
return json.Marshal(
|
|
|
|
struct {
|
|
|
|
httpAlias
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{
|
2019-09-05 22:32:28 +00:00
|
|
|
httpAlias: httpAlias(s),
|
|
|
|
Type: s.Type(),
|
2019-08-16 19:43:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid returns where the config is valid.
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s HTTP) Valid() error {
|
|
|
|
if err := s.Base.valid(); err != nil {
|
2019-08-16 19:43:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of the rule config.
|
2019-09-05 22:32:28 +00:00
|
|
|
func (s HTTP) Type() string {
|
2019-08-16 19:43:15 +00:00
|
|
|
return "http"
|
|
|
|
}
|