-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrafPushr.rb
executable file
·71 lines (62 loc) · 2.36 KB
/
GrafPushr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
require 'trollop'
require 'rest-client'
require 'json'
class DashboardPushr
# Username and Password to authenticate against Grafana
@@username = '[username]'
@@password = '[password]'
def get_template(dashboard)
# Open a given template JSON file to be passed into add_dashboard method
return File.read("templates/#{dashboard}.json")
end
def list_dashboards (url)
request = RestClient::Request.execute(:url => "http://#{@@username}:#{@@password}@#{url}/api/search", :method => :get)
# Parsing JSON to be manipulated.
response = JSON.parse(request)
# Iterate over each block of JSON in the response and print out the Dashboard Title
response.each do |dashboard|
puts "#{dashboard['title']} - #{dashboard['uri']}"
end
end
def add_dashboard (url, dashboard)
# Using the method above to pull in the template JSON file
template = get_template(dashboard)
request = RestClient::Request.execute(:url => "http://#{@@username}:#{@@password}@#{url}/api/dashboards/db", :method => :post, :payload => template, headers: {:content_type => 'application/json'})
return request
end
def delete_dashboard (url, dashboard)
request = RestClient::Request.execute(:url => "http://#{@@username}:#{@@password}@#{url}/api/dashboards/db/#{dashboard}", :method => :delete)
return request
end
end
opts = Trollop::options do
version "Grafana Dashboard Pusher 0.1 - Ben Groves <[email protected]>"
banner <<-EOS
The Dashboard Pusher is a simple Ruby wrapper for the Grafana API.
It is only desinged for Dashboards and has 3 functions LIST, ADD, DELETE.
Example:
$ ./GrafPushr.rb -o list -u h1grf01-v01.devops.stg2.ovp.bskyb.com -d system-healthcheck
Usage:
ruby api_tool.rb [options]
where [options] are:
EOS
opt :option, 'LIST, ADD, DELETE', :required => true, type: :string
opt :url, 'Grafana URL', :required => true, type: :string
opt :dashboard, 'Name of Dashboard JSON file saved in ./templates', :required => true, type: :string
end
object = DashboardPushr. new
cmd = opts[:option]
case cmd
when 'ADD', 'add', 'a'
puts 'ADDING Dashboard'
puts object.add_dashboard(opts[:url], opts[:dashboard])
when 'DELETE', 'delete', 'd'
puts 'DELETED Dashboard:'
puts object.delete_dashboard(opts[:url], opts[:dashboard])
when 'LIST', 'list', 'l'
puts 'Listing all dashboards:'
object.list_dashboards(opts[:url])
else
puts 'Not a valid option'
end