Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
1bf98243
Commit
1bf98243
authored
Mar 23, 2017
by
Andrzej Ressel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sending image
parent
5f3b7eaa
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
107 additions
and
32 deletions
+107
-32
pkg/components/null/float.go
+9
-0
pkg/models/notifications.go
+7
-6
pkg/services/alerting/notifiers/discord.go
+71
-12
pkg/services/notifications/notifications.go
+7
-6
pkg/services/notifications/webhook.go
+13
-8
No files found.
pkg/components/null/float.go
View file @
1bf98243
...
...
@@ -106,6 +106,15 @@ func (f Float) String() string {
return
fmt
.
Sprintf
(
"%1.3f"
,
f
.
Float64
)
}
// FullString returns float as string in full precision
func
(
f
Float
)
FullString
()
string
{
if
!
f
.
Valid
{
return
"null"
}
return
fmt
.
Sprintf
(
"%f"
,
f
.
Float64
)
}
// SetValid changes this Float's value and also sets it to be non-null.
func
(
f
*
Float
)
SetValid
(
n
float64
)
{
f
.
Float64
=
n
...
...
pkg/models/notifications.go
View file @
1bf98243
...
...
@@ -18,12 +18,13 @@ type SendEmailCommandSync struct {
}
type
SendWebhookSync
struct
{
Url
string
User
string
Password
string
Body
string
HttpMethod
string
HttpHeader
map
[
string
]
string
Url
string
User
string
Password
string
Body
string
HttpMethod
string
HttpHeader
map
[
string
]
string
ContentType
string
}
type
SendResetPasswordEmailCommand
struct
{
...
...
pkg/services/alerting/notifiers/discord.go
View file @
1bf98243
package
notifiers
import
(
"bytes"
"io"
"mime/multipart"
"os"
"strconv"
"strings"
...
...
@@ -64,9 +68,10 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
fields
:=
make
([]
map
[
string
]
interface
{},
0
)
for
_
,
evt
:=
range
evalContext
.
EvalMatches
{
fields
=
append
(
fields
,
map
[
string
]
interface
{}{
"name"
:
evt
.
Metric
,
"value"
:
evt
.
Value
,
"value"
:
evt
.
Value
.
FullString
()
,
"inline"
:
true
,
})
}
...
...
@@ -78,10 +83,6 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
color
,
_
:=
strconv
.
ParseInt
(
strings
.
TrimLeft
(
evalContext
.
GetStateModel
()
.
Color
,
"#"
),
16
,
0
)
image
:=
map
[
string
]
interface
{}{
"url"
:
evalContext
.
ImagePublicUrl
,
}
embed
:=
simplejson
.
New
()
embed
.
Set
(
"title"
,
evalContext
.
GetNotificationTitle
())
//Discord takes integer for color
...
...
@@ -91,22 +92,80 @@ func (this *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
embed
.
Set
(
"type"
,
"rich"
)
embed
.
Set
(
"fields"
,
fields
)
embed
.
Set
(
"footer"
,
footer
)
embed
.
Set
(
"image"
,
image
)
var
image
=
make
(
map
[
string
]
interface
{})
var
embeddedImage
=
false
if
evalContext
.
ImagePublicUrl
!=
""
{
image
=
map
[
string
]
interface
{}{
"url"
:
evalContext
.
ImagePublicUrl
,
}
embed
.
Set
(
"image"
,
image
)
}
else
{
image
=
map
[
string
]
interface
{}{
"url"
:
"attachment://graph.png"
,
}
embed
.
Set
(
"image"
,
image
)
embeddedImage
=
true
}
bodyJSON
.
Set
(
"embeds"
,
[]
interface
{}{
embed
})
body
,
_
:=
bodyJSON
.
MarshalJSON
()
json
,
_
:=
bodyJSON
.
MarshalJSON
()
content_type
:=
"application/json"
var
body
[]
byte
if
embeddedImage
{
var
b
bytes
.
Buffer
w
:=
multipart
.
NewWriter
(
&
b
)
this
.
log
.
Info
(
"Message"
,
string
(
body
))
f
,
err
:=
os
.
Open
(
evalContext
.
ImageOnDiskPath
)
if
err
!=
nil
{
this
.
log
.
Error
(
"Can't open graph file"
,
err
)
return
err
}
defer
f
.
Close
()
fw
,
err
:=
w
.
CreateFormField
(
"payload_json"
)
if
err
!=
nil
{
return
err
}
if
_
,
err
=
fw
.
Write
([]
byte
(
string
(
json
)));
err
!=
nil
{
return
err
}
fw
,
err
=
w
.
CreateFormFile
(
"file"
,
"graph.png"
)
if
_
,
err
=
io
.
Copy
(
fw
,
f
);
err
!=
nil
{
return
err
}
w
.
Close
()
body
=
b
.
Bytes
()
content_type
=
w
.
FormDataContentType
()
}
else
{
body
=
json
}
cmd
:=
&
m
.
SendWebhookSync
{
Url
:
this
.
WebhookURL
,
Body
:
string
(
body
),
HttpMethod
:
"POST"
,
Url
:
this
.
WebhookURL
,
Body
:
string
(
body
),
HttpMethod
:
"POST"
,
ContentType
:
content_type
,
}
if
err
:=
bus
.
DispatchCtx
(
evalContext
.
Ctx
,
cmd
);
err
!=
nil
{
this
.
log
.
Error
(
"Failed to send notification to Discord"
,
"error"
,
err
,
"body"
,
string
(
body
)
)
this
.
log
.
Error
(
"Failed to send notification to Discord"
,
"error"
,
err
)
return
err
}
...
...
pkg/services/notifications/notifications.go
View file @
1bf98243
...
...
@@ -60,12 +60,13 @@ func Init() error {
func
SendWebhookSync
(
ctx
context
.
Context
,
cmd
*
m
.
SendWebhookSync
)
error
{
return
sendWebRequestSync
(
ctx
,
&
Webhook
{
Url
:
cmd
.
Url
,
User
:
cmd
.
User
,
Password
:
cmd
.
Password
,
Body
:
cmd
.
Body
,
HttpMethod
:
cmd
.
HttpMethod
,
HttpHeader
:
cmd
.
HttpHeader
,
Url
:
cmd
.
Url
,
User
:
cmd
.
User
,
Password
:
cmd
.
Password
,
Body
:
cmd
.
Body
,
HttpMethod
:
cmd
.
HttpMethod
,
HttpHeader
:
cmd
.
HttpHeader
,
ContentType
:
cmd
.
ContentType
,
})
}
...
...
pkg/services/notifications/webhook.go
View file @
1bf98243
...
...
@@ -16,12 +16,13 @@ import (
)
type
Webhook
struct
{
Url
string
User
string
Password
string
Body
string
HttpMethod
string
HttpHeader
map
[
string
]
string
Url
string
User
string
Password
string
Body
string
HttpMethod
string
HttpHeader
map
[
string
]
string
ContentType
string
}
var
netTransport
=
&
http
.
Transport
{
...
...
@@ -61,7 +62,7 @@ func processWebhookQueue() {
}
func
sendWebRequestSync
(
ctx
context
.
Context
,
webhook
*
Webhook
)
error
{
webhookLog
.
Debug
(
"Sending webhook"
,
"url"
,
webhook
.
Url
,
"http method"
,
webhook
.
HttpMethod
)
webhookLog
.
Debug
(
"Sending webhook"
,
"url"
,
webhook
.
Url
,
"http method"
,
webhook
.
HttpMethod
,
"content type"
,
webhook
.
ContentType
)
if
webhook
.
HttpMethod
==
""
{
webhook
.
HttpMethod
=
http
.
MethodPost
...
...
@@ -72,7 +73,11 @@ func sendWebRequestSync(ctx context.Context, webhook *Webhook) error {
return
err
}
request
.
Header
.
Add
(
"Content-Type"
,
"application/json"
)
if
webhook
.
ContentType
==
""
{
webhook
.
ContentType
=
"application/json"
}
request
.
Header
.
Add
(
"Content-Type"
,
webhook
.
ContentType
)
request
.
Header
.
Add
(
"User-Agent"
,
"Grafana"
)
if
webhook
.
User
!=
""
&&
webhook
.
Password
!=
""
{
request
.
Header
.
Add
(
"Authorization"
,
util
.
GetBasicAuthHeader
(
webhook
.
User
,
webhook
.
Password
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment