grafana-rb 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dc7f51380b937fa32c5800be68e2e200520f167
4
- data.tar.gz: 345ad01d4b454bc1cc49bd24e428241b9b7f208b
3
+ metadata.gz: 0e6c02cb82164ba72d54f357f7666261006ce9f8
4
+ data.tar.gz: 777d42766e7c1fcce465b5f019cc5a8afe61825f
5
5
  SHA512:
6
- metadata.gz: ac85c8eb209c986dd6dad22eec536ba1083b5cb830b817b89dc14be11d790c7528bf5125a95ff5d8ffc9fc3ae2d2bf000f606faee86d78fd3d4d9c70dc371a00
7
- data.tar.gz: faa5c3e7ac4f4c1aa312f9a83706668b3dd95cf99b44709789550e51c9c8ed310aaa2b2023b894d1fedb8e6445443190b49d2e7b147ec112bdb00ee0ca832c8a
6
+ metadata.gz: 64a23476e8a9ea937fe249474a9773a448347e3c222adf5b39a2ff99a88be5bb5fecaebc07fa3ab19e05ed61bfccf100e0cc23b710405334339270767071ba95
7
+ data.tar.gz: 89c5539c887a303761e40868c8f9577d0b0cb8cfa939277631b0afbdb7ebe1b62d57ca4af392b4c18df05a1ea223d3cbfc0158636bd79a1fe200a94e8231d6f5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## Grafana-rb 0.15.0 (April 11, 2017) ##
2
+
3
+ * Support of multiple datasources and notifications.
4
+
1
5
  ## Grafana-rb 0.14.0 (April 10, 2017) ##
2
6
 
3
7
  * Add text panel support.
@@ -79,13 +79,11 @@ module GrafanaRb
79
79
  puts "---"
80
80
  puts "tool_version: #{GrafanaRb::VERSION}"
81
81
  puts "grafana_url: monitoring.your-domain.com:3000 # required"
82
- puts "prometheus_url: monitoring.your-domain.com:9090 # required"
83
82
  puts "grafana_user: user # 'admin' if missed"
84
83
  puts "grafana_password: secret # 'admin' if missed"
85
- puts "slack_channel: #alerts # no slack notifications if missed"
86
- puts "slack_url: https://hooks.slack.com/services/... # no slack notifications if missed"
87
- puts "slack_mention: @john, @michelle # optional"
88
84
  puts "require: [main.yml, dash*.yml] # optional, [*.yml] by default"
85
+ puts "datasources: ... # datasources"
86
+ puts "notifications: ... # notifications"
89
87
  puts ""
90
88
  die
91
89
  end
@@ -117,39 +115,35 @@ module GrafanaRb
117
115
  }
118
116
  end
119
117
 
120
- def create_or_update_prometheus_datasource
121
- payload = {
122
- name: "prometheus",
123
- type: "prometheus",
124
- url: "http://" + config["prometheus_url"],
125
- access: "proxy"
126
- }
127
- if @datasource_id = request(:get, "/api/datasources/id/prometheus")["id"]
128
- puts "update datasource"
129
- request(:put, "/api/datasources/#{@datasource_id}", payload)
130
- else
131
- puts "create datasource"
132
- @datasource_id = request(:post, "/api/datasources", payload)["id"]
118
+ def create_or_update_datasources
119
+ @datasource_ids = {}
120
+ (config["datasources"] || []).each do |payload|
121
+ if @datasource_ids[payload["name"]] = request(:get, "/api/datasources/id/#{payload["name"]}")["id"]
122
+ puts "update datasource: #{payload["name"]}"
123
+ request(:put, "/api/datasources/#{@datasource_ids[payload["name"]]}", payload)
124
+ else
125
+ puts "create datasource: #{payload["name"]}"
126
+ @datasource_ids[payload["name"]] = request(:post, "/api/datasources", payload)["id"]
127
+ end
133
128
  end
134
129
  end
135
130
 
136
131
  def create_or_update_slack_notifications
137
- notif = request(:get, "/api/alert-notifications").find{ |s| s["name"] == "slack" }
138
- if notif
139
- method = :put
140
- url = "/api/alert-notifications/#{notif["id"]}"
141
- opts = {id: notif["id"]}
142
- else
143
- method = :post
144
- url = "/api/alert-notifications"
145
- opts = {}
132
+ @notif_ids = {}
133
+ (config["notifications"] || []).each do |payload|
134
+ notif = request(:get, "/api/alert-notifications").find{ |s| s["name"] == payload["name"] }
135
+ if notif
136
+ method = :put
137
+ url = "/api/alert-notifications/#{notif["id"]}"
138
+ opts = {id: notif["id"]}
139
+ else
140
+ method = :post
141
+ url = "/api/alert-notifications"
142
+ opts = {}
143
+ end
144
+ puts "create/update slack notifications: #{payload["name"]}"
145
+ @notif_ids[payload["name"]] = request(method, url, opts.merge(payload))["id"]
146
146
  end
147
- puts "create/update slack notifications"
148
- request(method, url, opts.merge(type: "slack", name: "slack", isDefault: true, settings: {
149
- url: config["slack_url"],
150
- recipient: config["slack_channel"],
151
- mention: config["slack_mention"]
152
- }))
153
147
  end
154
148
 
155
149
  def gen_panel_custom(id, datasource_id, desc)
@@ -157,7 +151,7 @@ module GrafanaRb
157
151
  i = -1
158
152
  exprs = desc.key?("exprs") ? desc["exprs"] : [{"expr" => desc["expr"]}]
159
153
  {
160
- datasource: "prometheus",
154
+ datasource: @datasource_ids.invert[datasource_id],
161
155
  id: id,
162
156
  span: desc["span"] || 2,
163
157
  stack: desc["stack"],
@@ -181,7 +175,7 @@ module GrafanaRb
181
175
  }.merge(desc.key?("alert") ? {alert: desc["alert"], thresholds: desc["thresholds"]} : {})
182
176
  end
183
177
 
184
- def gen_panel(id, datasource_id, desc)
178
+ def gen_panel(id, datasource_id, notif_id, desc)
185
179
  if desc["type"] == "text"
186
180
  {
187
181
  "content": desc["content"].to_s,
@@ -258,6 +252,10 @@ module GrafanaRb
258
252
  raise "wrong expr"
259
253
  end
260
254
 
255
+ notification = desc.delete('notification')
256
+ if notification
257
+ notif_id = @notif_ids[notification]
258
+ end
261
259
  gen_panel_custom(id, datasource_id, desc.merge(JSON.load(JSON.dump({
262
260
  exprs: [{expr: expr, title: nil}],
263
261
  fill: 0,
@@ -279,7 +277,8 @@ module GrafanaRb
279
277
  ],
280
278
  executionErrorState: "alerting",
281
279
  frequency: "30s",
282
- name: desc["expr"]
280
+ name: desc["expr"],
281
+ notifications: (notif_id ? [{"id": notif_id}] : [])
283
282
  }
284
283
  }))))
285
284
 
@@ -292,16 +291,23 @@ module GrafanaRb
292
291
  editable = config.delete("editable") { true }
293
292
  duration = config.delete("duration") { "30m" }
294
293
  refresh = config.delete("refresh") { "10s" }
294
+ datasource = config.delete("datasource") { "unknown" }
295
+ notification = config.delete("notification") { nil }
295
296
  name = config.delete("name")
296
297
  puts "create/update dashboard '#{name}'"
297
298
 
299
+ unless @datasource_ids.key?(datasource)
300
+ puts "FATAL: unknown datasource for #{name} dashboard"
301
+ exit
302
+ end
303
+
298
304
  rows = config.map { |name, group|
299
305
  group = {"panels" => group} if group.is_a?(Array)
300
306
  {
301
307
  title: group["title"] || name,
302
308
  showTitle: group.delete("showTitle") { true },
303
309
  height: group["height"] || 200,
304
- panels: group["panels"].map { |panel| gen_panel((@panel_id += 1), @datasource_id, panel) }
310
+ panels: group["panels"].map { |panel| gen_panel((@panel_id += 1), @datasource_ids[datasource], @notif_ids[notification], panel) }
305
311
  }
306
312
  }
307
313
 
@@ -335,7 +341,6 @@ module GrafanaRb
335
341
  def apply
336
342
  check_config_existance
337
343
  die("missed grafana_url variable") unless config["grafana_url"]
338
- die("missed prometheus_url variable") unless config["prometheus_url"]
339
344
 
340
345
  tool_version = config["tool_version"] || GrafanaRb::VERSION
341
346
  if compare_versions(tool_version, GrafanaRb::VERSION) > 0
@@ -343,8 +348,8 @@ module GrafanaRb
343
348
  exit -1
344
349
  end
345
350
 
346
- create_or_update_prometheus_datasource
347
- create_or_update_slack_notifications if config["slack_url"] && config["slack_channel"]
351
+ create_or_update_datasources
352
+ create_or_update_slack_notifications
348
353
  dashboards.each do |dashboard|
349
354
  create_or_update_dashboard(deep_clone(dashboard))
350
355
  end
@@ -1,3 +1,3 @@
1
1
  module GrafanaRb
2
- VERSION = "0.14.0"
2
+ VERSION = "0.15.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grafana-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Vakhov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-10 00:00:00.000000000 Z
11
+ date: 2017-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler