grafana 0.8.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +8 -3
- data/lib/grafana/admin.rb +39 -65
- data/lib/grafana/alerts.rb +334 -14
- data/lib/grafana/annotations.rb +284 -9
- data/lib/grafana/client.rb +38 -1
- data/lib/grafana/dashboard.rb +182 -39
- data/lib/grafana/dashboard_permissions.rb +132 -0
- data/lib/grafana/dashboard_versions.rb +101 -5
- data/lib/grafana/datasource.rb +93 -49
- data/lib/grafana/folder.rb +198 -0
- data/lib/grafana/folder_and_dashboard_search.rb +57 -0
- data/lib/grafana/folder_permissions.rb +155 -0
- data/lib/grafana/login.rb +41 -35
- data/lib/grafana/network.rb +128 -91
- data/lib/grafana/organization.rb +65 -34
- data/lib/grafana/organizations.rb +119 -175
- data/lib/grafana/playlist.rb +599 -0
- data/lib/grafana/preferences.rb +122 -0
- data/lib/grafana/tags.rb +19 -8
- data/lib/grafana/teams.rb +364 -0
- data/lib/grafana/tools.rb +44 -12
- data/lib/grafana/user.rb +78 -39
- data/lib/grafana/users.rb +104 -53
- data/lib/grafana/validator.rb +47 -2
- data/lib/grafana/version.rb +3 -3
- metadata +13 -38
- data/doc/Array.html +0 -200
- data/doc/Boolean.html +0 -122
- data/doc/FalseClass.html +0 -132
- data/doc/Grafana.html +0 -172
- data/doc/Hash.html +0 -212
- data/doc/Logging.html +0 -326
- data/doc/Object.html +0 -286
- data/doc/Time.html +0 -200
- data/doc/TrueClass.html +0 -132
- data/doc/_index.html +0 -380
- data/doc/class_list.html +0 -51
- data/doc/file.README.html +0 -117
- data/doc/file_list.html +0 -56
- data/doc/frames.html +0 -17
- data/doc/index.html +0 -117
- data/doc/method_list.html +0 -771
- data/doc/top-level-namespace.html +0 -112
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8e73fd657e1a344d3cb67b5bf01cd33f8e227447e92e6e93b575a51a0091a253
|
4
|
+
data.tar.gz: c9f41ad884326c341d9c25746bfb80967206c5d0f94ef2bf94a4de6dd7e74435
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4aa2e43dbde3b105b5f85c74ac84a4269cd67d2de6b327cc597854ef1e6e4d66ce52628e9564785a0f1729527e90514e2577c83a651d8fe6c82c765c73a8ecbc
|
7
|
+
data.tar.gz: 50df29f0e9ad79c5dd7586dd1190585badb8ae3e09b82847433721cded33a6aa562ccba34be65c12356ca00cde64a45a18bf9dde1592af614a52263d71bca2f4
|
data/README.md
CHANGED
@@ -26,9 +26,14 @@ TODO: Write usage instructions here
|
|
26
26
|
|
27
27
|
## Development
|
28
28
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
30
|
-
|
31
|
-
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
30
|
+
Then, run `rake rake` to run the tests.
|
31
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
32
|
+
|
33
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
34
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
35
|
+
which will create a git tag for the version,
|
36
|
+
push git commits and tags and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
37
|
|
33
38
|
## Contributing
|
34
39
|
|
data/lib/grafana/admin.rb
CHANGED
@@ -62,47 +62,36 @@ module Grafana
|
|
62
62
|
user_name = validate( params, required: true, var: 'user_name', type: String )
|
63
63
|
permissions = validate( params, required: true, var: 'permissions' )
|
64
64
|
raise ArgumentError.new(format('wrong type. \'permissions\' must be an String or Hash, given %s', permissions.class.to_s ) ) unless( permissions.is_a?(String) || permissions.is_a?(Hash) )
|
65
|
+
valid_roles = ['Viewer', 'Editor', 'Read Only Editor', 'Admin']
|
65
66
|
|
66
|
-
|
67
|
+
downcased = Set.new valid_roles.map(&:downcase)
|
67
68
|
|
68
|
-
if( permissions.is_a?(
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
}
|
79
|
-
|
80
|
-
elsif( permissions.is_a?(Hash) && !permissions.dig(:grafana_admin).nil? )
|
69
|
+
if( permissions.is_a?(String) )
|
70
|
+
unless( downcased.include?( permissions.downcase ) )
|
71
|
+
return {
|
72
|
+
'status' => 404,
|
73
|
+
'name' => user_name,
|
74
|
+
'permissions' => permissions,
|
75
|
+
'message' => format( 'wrong permissions. Must be one of %s, given \'%s\'', valid_roles.join(', '), permissions )
|
76
|
+
}
|
77
|
+
end
|
78
|
+
end
|
81
79
|
|
80
|
+
if( permissions.is_a?(Hash) && !permissions.dig(:grafana_admin).nil? )
|
82
81
|
grafana_admin = permissions.dig(:grafana_admin)
|
83
|
-
|
84
82
|
unless( grafana_admin.is_a?(Boolean) )
|
85
|
-
|
86
|
-
message = 'Grafana admin permission must be either \'true\' or \'false\''
|
87
|
-
# logger.warn( message )
|
88
|
-
|
89
83
|
return {
|
90
84
|
'status' => 404,
|
91
85
|
'name' => user_name,
|
92
86
|
'permissions' => permissions,
|
93
|
-
'message' =>
|
87
|
+
'message' => 'Grafana admin permission must be either \'true\' or \'false\''
|
94
88
|
}
|
95
89
|
end
|
96
90
|
end
|
97
91
|
|
98
|
-
usr =
|
92
|
+
usr = user(user_name)
|
99
93
|
|
100
|
-
if( usr.nil? || usr.dig('status').to_i != 200 )
|
101
|
-
return {
|
102
|
-
'status' => 404,
|
103
|
-
'message' => format('User \'%s\' not found', user_name)
|
104
|
-
}
|
105
|
-
end
|
94
|
+
return { 'status' => 404, 'message' => format('User \'%s\' not found', user_name) } if( usr.nil? || usr.dig('status').to_i != 200 )
|
106
95
|
|
107
96
|
user_id = usr.dig('id')
|
108
97
|
|
@@ -117,24 +106,25 @@ module Grafana
|
|
117
106
|
logger.debug(payload.to_json) if(@debug)
|
118
107
|
|
119
108
|
return put(endpoint, payload.to_json )
|
120
|
-
|
109
|
+
end
|
121
110
|
|
122
|
-
|
111
|
+
org = current_organization
|
123
112
|
|
124
|
-
|
125
|
-
logger.debug( format( 'Updating user id %s permissions', user_id ) ) if @debug
|
113
|
+
return { 'status' => 404, 'message' => 'No current Organization found' } if( org.nil? || org.dig('status').to_i != 200 )
|
126
114
|
|
127
|
-
|
128
|
-
|
129
|
-
orgId: org.dig('id'),
|
130
|
-
role: permissions.downcase.capitalize
|
131
|
-
}
|
115
|
+
endpoint = format( '/api/orgs/%s/users/%s', org.dig('id'), user_id )
|
116
|
+
logger.debug( format( 'Updating user id %s permissions', user_id ) ) if @debug
|
132
117
|
|
133
|
-
|
134
|
-
|
118
|
+
payload = {
|
119
|
+
name: org.dig('name'),
|
120
|
+
orgId: org.dig('id'),
|
121
|
+
role: permissions.downcase.capitalize
|
122
|
+
}
|
135
123
|
|
136
|
-
|
137
|
-
|
124
|
+
logger.debug("Updating user id #{user_id} permissions (PATCH #{endpoint})") if @debug
|
125
|
+
logger.debug(payload.to_json) if(@debug)
|
126
|
+
|
127
|
+
patch( endpoint, payload.to_json )
|
138
128
|
end
|
139
129
|
|
140
130
|
# Delete an Global User
|
@@ -152,27 +142,17 @@ module Grafana
|
|
152
142
|
#
|
153
143
|
def delete_user( user_id )
|
154
144
|
|
155
|
-
raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an User name) or an Integer (for an User Id), given \'%s\'', user_id.class.to_s))
|
145
|
+
raise ArgumentError.new(format('wrong type. user \'user_id\' must be an String (for an User name) or an Integer (for an User Id), given \'%s\'', user_id.class.to_s)) \
|
146
|
+
if( user_id.is_a?(String) && user_id.is_a?(Integer) )
|
156
147
|
raise ArgumentError.new('missing \'user_id\'') if( user_id.size.zero? )
|
157
148
|
|
158
149
|
if(user_id.is_a?(String))
|
159
|
-
usr =
|
150
|
+
usr = user(user_id)
|
160
151
|
user_id = usr.dig('id')
|
161
152
|
end
|
162
153
|
|
163
|
-
if( user_id.nil? )
|
164
|
-
|
165
|
-
'status' => 404,
|
166
|
-
'message' => format( 'No User \'%s\' found', user_id)
|
167
|
-
}
|
168
|
-
end
|
169
|
-
|
170
|
-
if( user_id.is_a?(Integer) && user_id.to_i.zero? )
|
171
|
-
return {
|
172
|
-
'status' => 403,
|
173
|
-
'message' => format( 'Can\'t delete user id %d (admin user)', user_id )
|
174
|
-
}
|
175
|
-
end
|
154
|
+
return { 'status' => 404, 'message' => format( 'No User \'%s\' found', user_id) } if( user_id.nil? )
|
155
|
+
return { 'status' => 403, 'message' => format( 'Can\'t delete user id %d (admin user)', user_id ) } if( user_id.is_a?(Integer) && user_id.to_i.zero? )
|
176
156
|
|
177
157
|
endpoint = format('/api/admin/users/%d', user_id )
|
178
158
|
logger.debug( "Deleting user id #{user_id} (DELETE #{endpoint})" ) if @debug
|
@@ -210,7 +190,7 @@ module Grafana
|
|
210
190
|
login_name = validate( params, required: false, var: 'login_name', type: String ) || user_name
|
211
191
|
password = validate( params, required: true, var: 'password', type: String )
|
212
192
|
|
213
|
-
usr =
|
193
|
+
usr = user(user_name)
|
214
194
|
|
215
195
|
if( usr.nil? || usr.dig('status').to_i == 200 )
|
216
196
|
return {
|
@@ -239,7 +219,6 @@ module Grafana
|
|
239
219
|
post( endpoint, payload.to_json)
|
240
220
|
end
|
241
221
|
|
242
|
-
|
243
222
|
# Change Password for User
|
244
223
|
#
|
245
224
|
# Only works with Basic Authentication (username and password).
|
@@ -264,16 +243,11 @@ module Grafana
|
|
264
243
|
raise ArgumentError.new('missing params') if( params.size.zero? )
|
265
244
|
|
266
245
|
user_name = validate( params, required: true, var: 'user_name', type: String )
|
267
|
-
password
|
246
|
+
password = validate( params, required: true, var: 'password', type: String )
|
268
247
|
|
269
|
-
usr =
|
248
|
+
usr = user(user_name)
|
270
249
|
|
271
|
-
if( usr.nil? || usr.dig('status').to_i != 200 )
|
272
|
-
return {
|
273
|
-
'status' => 404,
|
274
|
-
'message' => format('User \'%s\' not found', user_name)
|
275
|
-
}
|
276
|
-
end
|
250
|
+
return { 'status' => 404, 'message' => format('User \'%s\' not found', user_name) } if( usr.nil? || usr.dig('status').to_i != 200 )
|
277
251
|
|
278
252
|
user_id = usr.dig('id')
|
279
253
|
|
data/lib/grafana/alerts.rb
CHANGED
@@ -1,38 +1,358 @@
|
|
1
1
|
|
2
2
|
module Grafana
|
3
3
|
|
4
|
-
#
|
4
|
+
# You can use the Alerting API to get information about alerts and their states but this API cannot be used to modify the alert.
|
5
|
+
# To create new alerts or modify them you need to update the dashboard json that contains the alerts.
|
6
|
+
#
|
7
|
+
# This API can also be used to create, update and delete alert notifications.
|
8
|
+
#
|
9
|
+
# original API Documentation can be found under: http://docs.grafana.org/http_api/alerting/
|
5
10
|
#
|
6
11
|
module Alerts
|
7
12
|
|
8
13
|
# Get alerts
|
14
|
+
#
|
15
|
+
# These parameters are used as querystring parameters. For example:
|
16
|
+
#
|
17
|
+
# @param [Hash] params
|
18
|
+
# @option params [Mixed] dashboard_id alerts for a specified dashboard.
|
19
|
+
# @option params [Integer] panel_id alerts for a specified panel on a dashboard.
|
20
|
+
# @option params [Integer] limit (10) response to x number of alerts.
|
21
|
+
# @option params [Integer] state (ALL,no_data, paused, alerting, ok, pending)
|
22
|
+
# @option params [Array] alerts with one or more of the following alert states: 'ALL', 'no_data', 'paused', 'alerting', 'ok', 'pending'.
|
23
|
+
## To specify multiple states use the following format: ?state=paused&state=alerting
|
24
|
+
#
|
25
|
+
# @return [Array]
|
9
26
|
# GET /api/alerts/
|
27
|
+
# curl -H 'Content-Type: application/json;charset=UTF-8' 'http://admin:admin@127.0.0.1:3030/api/alerts'
|
10
28
|
#
|
29
|
+
def alerts( params )
|
30
|
+
|
31
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
32
|
+
# raise ArgumentError.new('missing \'params\'') if( params.size.zero? )
|
33
|
+
|
34
|
+
dashboard_id = validate( params, required: false, var: 'dashboard_id' )
|
35
|
+
panel_id = validate( params, required: false, var: 'panel_id', type: Integer )
|
36
|
+
limit = validate( params, required: false, var: 'limit', type: Integer )
|
37
|
+
alert_array = validate( params, required: false, var: 'alerts', type: Array )
|
38
|
+
valid_alerts = %w[ALL no_data paused alerting ok pending].sort
|
39
|
+
|
40
|
+
unless( alert_array.nil? )
|
41
|
+
alert_array = alert_array.sort
|
42
|
+
# valid = alert_array & valid_alerts
|
43
|
+
invalid = alert_array - valid_alerts
|
44
|
+
|
45
|
+
raise ArgumentError.new(format('wrong alerts type. only %s allowed, given \'%s\'', valid_alerts.join(', '), alert_array.join(', '))) if( invalid.count != 0 )
|
46
|
+
end
|
47
|
+
|
48
|
+
if( dashboard_id.is_a?(String) )
|
49
|
+
|
50
|
+
dashboard = search_dashboards( query: dashboard_id )
|
51
|
+
|
52
|
+
return { 'status' => 404, 'message' => format( 'No Dashboard \'%s\' found', dashboard_id) } if( dashboard.nil? || dashboard.dig('status').to_i != 200 )
|
53
|
+
|
54
|
+
dashboard = dashboard.dig('message').first unless( dashboard.nil? && dashboard.dig('status').to_i == 200 )
|
55
|
+
dashboard_id = dashboard.dig('id') unless( dashboard.nil? )
|
56
|
+
|
57
|
+
return { 'status' => 404, 'message' => format( 'No Dashboard \'%s\' found', dashboard_id) } if( dashboard_id.nil? )
|
58
|
+
end
|
59
|
+
|
60
|
+
api = []
|
61
|
+
api << format( 'dashboardId=%s', dashboard_id ) unless( dashboard_id.nil? )
|
62
|
+
api << format( 'panelId=%s', panel_id ) unless( panel_id.nil? )
|
63
|
+
api << format( 'limit=%s', limit ) unless( limit.nil? )
|
64
|
+
|
65
|
+
unless( alert_array.nil? )
|
66
|
+
alert_array = alert_array.join( '&state=' ) if( alert_array.is_a?( Array ) )
|
67
|
+
api << format( 'state=%s', alert_array )
|
68
|
+
end
|
69
|
+
api = api.join( '&' )
|
70
|
+
|
71
|
+
endpoint = format( '/api/alerts/?%s' , api )
|
72
|
+
|
73
|
+
@logger.debug("Attempting to search for alerts (GET #{endpoint})") if @debug
|
74
|
+
|
75
|
+
get( endpoint )
|
76
|
+
end
|
77
|
+
|
11
78
|
# Get one alert
|
12
|
-
# GET /api/alerts/:id
|
13
79
|
#
|
14
|
-
|
80
|
+
# @param [Mixed] alert_id Alertname (String) or Alertid (Integer)
|
81
|
+
#
|
82
|
+
# @example
|
83
|
+
# alert( 1 )
|
84
|
+
# alert( 'foo' )
|
85
|
+
#
|
86
|
+
# @return [Hash]
|
87
|
+
#
|
88
|
+
# curl -H 'Content-Type: application/json;charset=UTF-8' 'http://admin:admin@127.0.0.1:3030/api/alerts/1'
|
89
|
+
#
|
90
|
+
def alert( alert_id )
|
15
91
|
|
16
|
-
|
17
|
-
|
18
|
-
|
92
|
+
if( alert_id.is_a?(String) && alert_id.is_a?(Integer) )
|
93
|
+
raise ArgumentError.new(format('wrong type. \'alert_id\' must be an String (for an Alert name) or an Integer (for an Alert Id), given \'%s\'', alert_id.class.to_s))
|
94
|
+
end
|
95
|
+
raise ArgumentError.new('missing \'alert_id\'') if( alert_id.size.zero? )
|
96
|
+
|
97
|
+
# if(alert_id.is_a?(String))
|
98
|
+
# data = alerts( alerts: 'all' ).select { |_k,v| v['name'] == alert_id }
|
99
|
+
# alert_id = data.keys.first if( data )
|
100
|
+
# end
|
101
|
+
|
102
|
+
# puts alert_id
|
103
|
+
# GET /api/alerts/:id
|
104
|
+
|
105
|
+
|
106
|
+
endpoint = format( '/api/alerts/%d' , alert_id )
|
107
|
+
|
108
|
+
# puts endpoint
|
109
|
+
|
110
|
+
@logger.debug("Attempting get alert id #{alert_id} (GET #{endpoint})") if @debug
|
111
|
+
|
112
|
+
get( endpoint )
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
# Pause single alert
|
117
|
+
#
|
118
|
+
# @param [Mixed] alert_id Alertname (String) or Alertid (Integer)
|
119
|
+
#
|
120
|
+
# @example
|
121
|
+
# alert_pause( 1 )
|
122
|
+
# alert_pause( 'foo' )
|
123
|
+
#
|
124
|
+
# @return [Hash]
|
125
|
+
#
|
126
|
+
def alert_pause( alert_id )
|
127
|
+
|
128
|
+
if( alert_id.is_a?(String) && alert_id.is_a?(Integer) )
|
129
|
+
raise ArgumentError.new(format('wrong type. \'alert_id\' must be an String (for an Alert name) or an Integer (for an Alert Id), given \'%s\'', alert_id.class.to_s))
|
130
|
+
end
|
131
|
+
raise ArgumentError.new('missing \'alert_id\'') if( alert_id.size.zero? )
|
132
|
+
|
133
|
+
if(alert_id.is_a?(String))
|
134
|
+
data = alerts( alerts: 'all' ).select { |_k,v| v['name'] == alert_id }
|
135
|
+
alert_id = data.keys.first if( data )
|
136
|
+
end
|
137
|
+
|
138
|
+
# POST /api/alerts/:id/pause
|
139
|
+
# puts alert_id
|
140
|
+
end
|
19
141
|
|
20
142
|
# Get alert notifications
|
21
|
-
#
|
22
|
-
|
143
|
+
#
|
144
|
+
# @example
|
145
|
+
# alert_notifications
|
146
|
+
#
|
147
|
+
# @return [Hash]
|
148
|
+
#
|
149
|
+
def alert_notifications
|
150
|
+
logger.debug('Getting alert notifications') if @debug
|
151
|
+
get('/api/alert-notifications')
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
23
158
|
|
24
159
|
# Create alert notification
|
25
|
-
#
|
26
|
-
|
160
|
+
#
|
161
|
+
# @param [Hash] params
|
162
|
+
# @option params [String] name short description - required
|
163
|
+
# @option params [String] type one of 'slack', 'pagerduty','email','webhook','kafka','hipchat',
|
164
|
+
# 'victorops','sensu','opsgenie','threema','pushover','telegram','line','prometheus-alertmanager' - required
|
165
|
+
# @option params [Boolean] default (false)
|
166
|
+
# @option params [Hash] settings
|
167
|
+
#
|
168
|
+
# @example
|
169
|
+
# params = {
|
170
|
+
# name: 'new alert notification',
|
171
|
+
# type: 'email',
|
172
|
+
# default: false,
|
173
|
+
# settings: {
|
174
|
+
# addresses: 'carl@grafana.com;dev@grafana.com'
|
175
|
+
# }
|
176
|
+
# }
|
177
|
+
# create_alert_notification( params )
|
178
|
+
#
|
179
|
+
# @return [Hash]
|
180
|
+
#
|
181
|
+
def create_alert_notification( params )
|
182
|
+
|
183
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
184
|
+
raise ArgumentError.new('missing params') if( params.size.zero? )
|
185
|
+
|
186
|
+
# TODO
|
187
|
+
# type are 'email'
|
188
|
+
# and the other possible values?
|
189
|
+
name = validate( params, required: true, var: 'name', type: String )
|
190
|
+
type = validate( params, required: true, var: 'type', type: String ) || 'email'
|
191
|
+
default = validate( params, required: false, var: 'default', type: Boolean ) || false
|
192
|
+
settings = validate( params, required: false, var: 'settings', type: Hash )
|
193
|
+
|
194
|
+
unless( type.nil? )
|
195
|
+
valid_types = %w[slack pagerduty email webhook kafka hipchat victorops sensu opsgenie threema pushover telegram line prometheus-alertmanager]
|
196
|
+
raise ArgumentError.new(format('wrong notification type. only %s allowed, given \%s\'', valid_types.join(', '), type)) if( valid_types.include?(type.downcase) == false )
|
197
|
+
end
|
198
|
+
|
199
|
+
# TODO
|
200
|
+
# check if the alert 'name' already created
|
201
|
+
return { 'status' => 404, 'message' => format( 'alert notification \'%s\' alread exists', name) } if( alert_notification?(name) )
|
202
|
+
|
203
|
+
# data = alert_notifications
|
204
|
+
# data = data.dig('message').first unless( data.nil? && data.dig('status').to_i == 200 )
|
205
|
+
# data = data.select { |k| k['name'] == name }
|
206
|
+
# return { 'status' => 404, 'message' => format( 'alert notification \'%s\' alread exists', name) } if( data )
|
207
|
+
|
208
|
+
payload = {
|
209
|
+
name: name,
|
210
|
+
type: type,
|
211
|
+
isDefault: default,
|
212
|
+
settings: settings
|
213
|
+
}
|
214
|
+
payload.reject!{ |_k, v| v.nil? }
|
215
|
+
|
216
|
+
endpoint = '/api/alert-notifications'
|
217
|
+
|
218
|
+
# puts endpoint
|
219
|
+
# puts payload
|
220
|
+
|
221
|
+
post(endpoint, payload.to_json)
|
222
|
+
end
|
27
223
|
|
28
224
|
# Update alert notification
|
29
|
-
#
|
30
|
-
|
225
|
+
#
|
226
|
+
# @param [Hash] params
|
227
|
+
# @param [Mixed] alert_id Alertname (String) or Alertid (Integer) to change
|
228
|
+
# @option params [String] name short description - required
|
229
|
+
# @option params [String] type ('email') - required
|
230
|
+
# @option params [Boolean] default (false)
|
231
|
+
# @option params [Hash] settings
|
232
|
+
#
|
233
|
+
# @example
|
234
|
+
# params = {
|
235
|
+
# alert_id: 1
|
236
|
+
# name: 'new alert notification',
|
237
|
+
# type: 'email',
|
238
|
+
# default: false,
|
239
|
+
# settings: {
|
240
|
+
# addresses: 'carl@grafana.com;dev@grafana.com'
|
241
|
+
# }
|
242
|
+
# }
|
243
|
+
# update_alert_notification( params )
|
244
|
+
#
|
245
|
+
# params = {
|
246
|
+
# alert_id: 'new alert notification'
|
247
|
+
# name: 'new alert notification',
|
248
|
+
# type: 'email',
|
249
|
+
# default: false,
|
250
|
+
# settings: {
|
251
|
+
# addresses: 'carl@grafana.com;dev@grafana.com'
|
252
|
+
# }
|
253
|
+
# }
|
254
|
+
# update_alert_notification( params )
|
255
|
+
#
|
256
|
+
# @return [Hash]
|
257
|
+
#
|
258
|
+
def update_alert_notification( params )
|
259
|
+
|
260
|
+
raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
|
261
|
+
raise ArgumentError.new('missing params') if( params.size.zero? )
|
262
|
+
|
263
|
+
# TODO
|
264
|
+
# type are 'email'
|
265
|
+
# and the other possible values?
|
266
|
+
alert_id = validate( params, required: true, var: 'alert_id' )
|
267
|
+
name = validate( params, required: true, var: 'name', type: String )
|
268
|
+
type = validate( params, required: true, var: 'type', type: String ) || 'email'
|
269
|
+
default = validate( params, required: false, var: 'default', type: Boolean ) || false
|
270
|
+
settings = validate( params, required: false, var: 'settings', type: Hash )
|
271
|
+
|
272
|
+
unless( type.nil? )
|
273
|
+
valid_types = %w[slack pagerduty email webhook kafka hipchat victorops sensu opsgenie threema pushover telegram line prometheus-alertmanager]
|
274
|
+
raise ArgumentError.new(format('wrong notification type. only %s allowed, given \%s\'', valid_types.join(', '), type)) if( valid_types.include?(type.downcase) == false )
|
275
|
+
end
|
276
|
+
|
277
|
+
if( alert_id.is_a?(String) && alert_id.is_a?(Integer) )
|
278
|
+
raise ArgumentError.new(format('wrong type. user \'alert_id\' must be an String (for an Alertname) or an Integer (for an Alert Id), given \'%s\'', alert_id.class.to_s))
|
279
|
+
end
|
280
|
+
|
281
|
+
alert_id = alert_notification_id(alert_id)
|
282
|
+
return { 'status' => 404, 'message' => format( 'alert notification \'%s\' not exists', name) } if( alert_id.nil? )
|
283
|
+
|
284
|
+
payload = {
|
285
|
+
id: alert_id,
|
286
|
+
name: name,
|
287
|
+
type: type,
|
288
|
+
isDefault: default,
|
289
|
+
settings: settings
|
290
|
+
}
|
291
|
+
payload.reject!{ |_k, v| v.nil? }
|
292
|
+
|
293
|
+
endpoint = format( '/api/alert-notifications/%d', alert_id )
|
294
|
+
|
295
|
+
put(endpoint, payload.to_json)
|
296
|
+
end
|
31
297
|
|
32
298
|
# Delete alert notification
|
33
|
-
#
|
34
|
-
|
299
|
+
#
|
300
|
+
# @param [Mixed] alert_id Alertname (String) or Alertid (Integer)
|
301
|
+
#
|
302
|
+
# @example
|
303
|
+
# delete_alert_notification( 1 )
|
304
|
+
# delete_alert_notification( 'foo' )
|
305
|
+
#
|
306
|
+
# @return [Hash]
|
307
|
+
#
|
308
|
+
def delete_alert_notification( alert_id )
|
309
|
+
|
310
|
+
if( alert_id.is_a?(String) && alert_id.is_a?(Integer) )
|
311
|
+
raise ArgumentError.new(format('wrong type. user \'alert_id\' must be an String (for an Alert name) or an Integer (for an Alert Id), given \'%s\'', alert_id.class.to_s))
|
312
|
+
end
|
313
|
+
raise ArgumentError.new('missing \'alert_id\'') if( alert_id.size.zero? )
|
314
|
+
|
315
|
+
id = alert_notification_id(alert_id)
|
316
|
+
return { 'status' => 404, 'message' => format( 'alert notification \'%s\' not exists', alert_id) } if( id.nil? )
|
317
|
+
|
318
|
+
endpoint = format('/api/alert-notifications/%d', alert_id )
|
319
|
+
logger.debug( "Deleting alert id #{alert_id} (DELETE #{endpoint})" ) if @debug
|
320
|
+
|
321
|
+
delete( endpoint )
|
322
|
+
end
|
323
|
+
|
324
|
+
|
325
|
+
private
|
326
|
+
def alert_notification?( alert_id )
|
327
|
+
|
328
|
+
id = alert_notification_id(alert_id)
|
329
|
+
|
330
|
+
return true unless( id.nil? )
|
331
|
+
|
332
|
+
false
|
333
|
+
end
|
334
|
+
|
335
|
+
def alert_notification_id( alert_id )
|
336
|
+
|
337
|
+
data = alert_notifications
|
338
|
+
data = data.dig('message') unless( data.nil? && data.dig('status').to_i == 200 )
|
339
|
+
|
340
|
+
map = {}
|
341
|
+
data.each do |d|
|
342
|
+
map[d.dig('id')] = d.dig('name').downcase.split.join('_')
|
343
|
+
end
|
344
|
+
|
345
|
+
id = map.select { |key,_value| key == alert_id } if( map && alert_id.is_a?(Integer) )
|
346
|
+
id = map.select { |_key,value| value == alert_id.downcase.split.join('_') } if( map && alert_id.is_a?(String) )
|
347
|
+
|
348
|
+
id = id.keys.first unless(id.nil?)
|
349
|
+
|
350
|
+
return id if( id.is_a?(Integer) )
|
351
|
+
|
352
|
+
nil
|
353
|
+
end
|
35
354
|
|
36
355
|
end
|
37
356
|
|
38
357
|
end
|
358
|
+
|