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
376d2d56
Commit
376d2d56
authored
Jan 19, 2017
by
huydx
Committed by
huydx
Jan 19, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(feature) add LINE notify to notifier
parent
e1b82e72
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
155 additions
and
0 deletions
+155
-0
pkg/metrics/metrics.go
+2
-0
pkg/models/notifications.go
+1
-0
pkg/services/alerting/notifiers/line.go
+94
-0
pkg/services/alerting/notifiers/line_test.go
+50
-0
pkg/services/notifications/notifications.go
+1
-0
pkg/services/notifications/webhook.go
+7
-0
No files found.
pkg/metrics/metrics.go
View file @
376d2d56
...
@@ -45,6 +45,7 @@ var (
...
@@ -45,6 +45,7 @@ var (
M_Alerting_Notification_Sent_Email
Counter
M_Alerting_Notification_Sent_Email
Counter
M_Alerting_Notification_Sent_Webhook
Counter
M_Alerting_Notification_Sent_Webhook
Counter
M_Alerting_Notification_Sent_PagerDuty
Counter
M_Alerting_Notification_Sent_PagerDuty
Counter
M_Alerting_Notification_Sent_LINE
Counter
M_Alerting_Notification_Sent_Victorops
Counter
M_Alerting_Notification_Sent_Victorops
Counter
M_Alerting_Notification_Sent_OpsGenie
Counter
M_Alerting_Notification_Sent_OpsGenie
Counter
M_Alerting_Notification_Sent_Telegram
Counter
M_Alerting_Notification_Sent_Telegram
Counter
...
@@ -118,6 +119,7 @@ func initMetricVars(settings *MetricSettings) {
...
@@ -118,6 +119,7 @@ func initMetricVars(settings *MetricSettings) {
M_Alerting_Notification_Sent_OpsGenie
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"opsgenie"
)
M_Alerting_Notification_Sent_OpsGenie
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"opsgenie"
)
M_Alerting_Notification_Sent_Telegram
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"telegram"
)
M_Alerting_Notification_Sent_Telegram
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"telegram"
)
M_Alerting_Notification_Sent_Sensu
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"sensu"
)
M_Alerting_Notification_Sent_Sensu
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"sensu"
)
M_Alerting_Notification_Sent_LINE
=
RegCounter
(
"alerting.notifications_sent"
,
"type"
,
"LINE"
)
M_Aws_CloudWatch_GetMetricStatistics
=
RegCounter
(
"aws.cloudwatch.get_metric_statistics"
)
M_Aws_CloudWatch_GetMetricStatistics
=
RegCounter
(
"aws.cloudwatch.get_metric_statistics"
)
M_Aws_CloudWatch_ListMetrics
=
RegCounter
(
"aws.cloudwatch.list_metrics"
)
M_Aws_CloudWatch_ListMetrics
=
RegCounter
(
"aws.cloudwatch.list_metrics"
)
...
...
pkg/models/notifications.go
View file @
376d2d56
...
@@ -23,6 +23,7 @@ type SendWebhookSync struct {
...
@@ -23,6 +23,7 @@ type SendWebhookSync struct {
Password
string
Password
string
Body
string
Body
string
HttpMethod
string
HttpMethod
string
HttpHeader
map
[
string
]
string
}
}
type
SendResetPasswordEmailCommand
struct
{
type
SendResetPasswordEmailCommand
struct
{
...
...
pkg/services/alerting/notifiers/line.go
0 → 100644
View file @
376d2d56
package
notifiers
import
(
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/services/alerting"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/metrics"
"net/url"
"fmt"
"github.com/grafana/grafana/pkg/bus"
)
func
init
()
{
alerting
.
RegisterNotifier
(
&
alerting
.
NotifierPlugin
{
Type
:
"LINE"
,
Name
:
"LINE"
,
Description
:
"Send notifications to LINE notify"
,
Factory
:
NewLINENotifier
,
OptionsTemplate
:
`
<div class="gf-form-group">
<h3 class="page-heading">LINE notify settings</h3>
<div class="gf-form">
<span class="gf-form-label width-14">Token</span>
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.token" placeholder="LINE notify token key"></input>
</div>
</div>
`
,
})
}
const
(
lineNotifyUrl
string
=
"https://notify-api.line.me/api/notify"
)
func
NewLINENotifier
(
model
*
m
.
AlertNotification
)
(
alerting
.
Notifier
,
error
)
{
token
:=
model
.
Settings
.
Get
(
"token"
)
.
MustString
()
if
token
==
""
{
return
nil
,
alerting
.
ValidationError
{
Reason
:
"Could not find token in settings"
}
}
return
&
LineNotifier
{
NotifierBase
:
NewNotifierBase
(
model
.
Id
,
model
.
IsDefault
,
model
.
Name
,
model
.
Type
,
model
.
Settings
),
Token
:
token
,
log
:
log
.
New
(
"alerting.notifier.line"
),
},
nil
}
type
LineNotifier
struct
{
NotifierBase
Token
string
log
log
.
Logger
}
func
(
this
*
LineNotifier
)
Notify
(
evalContext
*
alerting
.
EvalContext
)
error
{
this
.
log
.
Info
(
"Executing line notification"
,
"ruleId"
,
evalContext
.
Rule
.
Id
,
"notification"
,
this
.
Name
)
metrics
.
M_Alerting_Notification_Sent_LINE
.
Inc
(
1
)
var
err
error
switch
evalContext
.
Rule
.
State
{
case
m
.
AlertStateAlerting
:
err
=
this
.
createAlert
(
evalContext
)
}
return
err
}
func
(
this
*
LineNotifier
)
createAlert
(
evalContext
*
alerting
.
EvalContext
)
error
{
this
.
log
.
Info
(
"Creating Line notify"
,
"ruleId"
,
evalContext
.
Rule
.
Id
,
"notification"
,
this
.
Name
)
ruleUrl
,
err
:=
evalContext
.
GetRuleUrl
()
if
err
!=
nil
{
this
.
log
.
Error
(
"Failed get rule link"
,
"error"
,
err
)
return
err
}
form
:=
url
.
Values
{}
body
:=
fmt
.
Sprintf
(
"%s - %s
\n
%s"
,
evalContext
.
Rule
.
Name
,
ruleUrl
,
evalContext
.
Rule
.
Message
)
form
.
Add
(
"message"
,
body
)
cmd
:=
&
m
.
SendWebhookSync
{
Url
:
lineNotifyUrl
,
HttpMethod
:
"POST"
,
HttpHeader
:
map
[
string
]
string
{
"Authorization"
:
fmt
.
Sprintf
(
"Bearer %s"
,
this
.
Token
),
"Content-Type"
:
"application/x-www-form-urlencoded"
,
},
Body
:
form
.
Encode
(),
}
if
err
:=
bus
.
DispatchCtx
(
evalContext
.
Ctx
,
cmd
);
err
!=
nil
{
this
.
log
.
Error
(
"Failed to send notification to LINE"
,
"error"
,
err
,
"body"
,
string
(
body
))
return
err
}
return
nil
}
pkg/services/alerting/notifiers/line_test.go
0 → 100644
View file @
376d2d56
package
notifiers
import
(
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
m
"github.com/grafana/grafana/pkg/models"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestLineNotifier
(
t
*
testing
.
T
)
{
Convey
(
"Line notifier tests"
,
t
,
func
()
{
Convey
(
"empty settings should return error"
,
func
()
{
json
:=
`{ }`
settingsJSON
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
model
:=
&
m
.
AlertNotification
{
Name
:
"line_testing"
,
Type
:
"line"
,
Settings
:
settingsJSON
,
}
_
,
err
:=
NewLINENotifier
(
model
)
So
(
err
,
ShouldNotBeNil
)
})
Convey
(
"settings should trigger incident"
,
func
()
{
json
:=
`
{
"token": "abcdefgh0123456789"
}`
settingsJSON
,
_
:=
simplejson
.
NewJson
([]
byte
(
json
))
model
:=
&
m
.
AlertNotification
{
Name
:
"line_testing"
,
Type
:
"line"
,
Settings
:
settingsJSON
,
}
not
,
err
:=
NewLINENotifier
(
model
)
lineNotifier
:=
not
.
(
*
LineNotifier
)
So
(
err
,
ShouldBeNil
)
So
(
lineNotifier
.
Name
,
ShouldEqual
,
"line_testing"
)
So
(
lineNotifier
.
Type
,
ShouldEqual
,
"line"
)
So
(
lineNotifier
.
Token
,
ShouldEqual
,
"abcdefgh0123456789"
)
})
})
}
pkg/services/notifications/notifications.go
View file @
376d2d56
...
@@ -65,6 +65,7 @@ func SendWebhookSync(ctx context.Context, cmd *m.SendWebhookSync) error {
...
@@ -65,6 +65,7 @@ func SendWebhookSync(ctx context.Context, cmd *m.SendWebhookSync) error {
Password
:
cmd
.
Password
,
Password
:
cmd
.
Password
,
Body
:
cmd
.
Body
,
Body
:
cmd
.
Body
,
HttpMethod
:
cmd
.
HttpMethod
,
HttpMethod
:
cmd
.
HttpMethod
,
HttpHeader
:
cmd
.
HttpHeader
,
})
})
}
}
...
...
pkg/services/notifications/webhook.go
View file @
376d2d56
...
@@ -19,6 +19,7 @@ type Webhook struct {
...
@@ -19,6 +19,7 @@ type Webhook struct {
Password
string
Password
string
Body
string
Body
string
HttpMethod
string
HttpMethod
string
HttpHeader
map
[
string
]
string
}
}
var
(
var
(
...
@@ -63,6 +64,12 @@ func sendWebRequestSync(ctx context.Context, webhook *Webhook) error {
...
@@ -63,6 +64,12 @@ func sendWebRequestSync(ctx context.Context, webhook *Webhook) error {
request
.
Header
.
Add
(
"Authorization"
,
util
.
GetBasicAuthHeader
(
webhook
.
User
,
webhook
.
Password
))
request
.
Header
.
Add
(
"Authorization"
,
util
.
GetBasicAuthHeader
(
webhook
.
User
,
webhook
.
Password
))
}
}
if
len
(
webhook
.
HttpHeader
)
!=
0
{
for
k
,
v
:=
range
webhook
.
HttpHeader
{
request
.
Header
.
Set
(
k
,
v
)
}
}
resp
,
err
:=
ctxhttp
.
Do
(
ctx
,
http
.
DefaultClient
,
request
)
resp
,
err
:=
ctxhttp
.
Do
(
ctx
,
http
.
DefaultClient
,
request
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
...
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