Class: Grafana::Grafana

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana/grafana.rb

Overview

Main class for handling the interaction with one specific Grafana instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, key = nil, opts = {}) ⇒ Grafana

Returns a new instance of Grafana.

Parameters:

  • base_uri (String)

    full URI pointing to the specific grafana instance without trailing slash, e.g. https://localhost:3000.

  • key (String) (defaults to: nil)

    API key for the grafana instance, if required

  • opts (Hash) (defaults to: {})

    additional options. Currently supporting :logger.



19
20
21
22
23
24
25
26
# File 'lib/grafana/grafana.rb', line 19

def initialize(base_uri, key = nil, opts = {})
  @base_uri = base_uri
  @key = key
  @dashboards = {}
  @logger = opts[:logger] || ::Logger.new(nil)

  initialize_datasources unless @base_uri.empty?
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/grafana/grafana.rb', line 12

def logger
  @logger
end

Instance Method Details

#dashboard(dashboard_uid) ⇒ Dashboard

Returns dashboard object, if it has been found.

Parameters:

  • dashboard_uid (String)

    UID of the searched Dashboard

Returns:

  • (Dashboard)

    dashboard object, if it has been found

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/grafana/grafana.rb', line 124

def dashboard(dashboard_uid)
  return @dashboards[dashboard_uid] unless @dashboards[dashboard_uid].nil?

  response = prepare_request({ relative_url: "/api/dashboards/uid/#{dashboard_uid}" }).execute
  raise DashboardDoesNotExistError, dashboard_uid unless response.is_a?(Net::HTTPOK)

  # cache dashboard for reuse
  model = JSON.parse(response.body)['dashboard']
  @dashboards[dashboard_uid] = Dashboard.new(model, self)

  @dashboards[dashboard_uid]
end

#dashboard_idsArray

Returns Array of dashboard uids within the current grafana object.

Returns:

  • (Array)

    Array of dashboard uids within the current grafana object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/grafana/grafana.rb', line 109

def dashboard_ids
  response = prepare_request({ relative_url: '/api/search' }).execute
  return [] unless response.is_a?(Net::HTTPOK)

  dashboards = JSON.parse(response.body)

  dashboards.each do |dashboard|
    @dashboards[dashboard['uid']] = nil unless @dashboards[dashboard['uid']]
  end

  @dashboards.keys
end

#datasource_by_id(datasource_id) ⇒ Datasource

Returns the datasource, which has been queried by the datasource id.

Parameters:

  • datasource_id (Integer)

    id of the searched datasource

Returns:

  • (Datasource)

    Datasource for the specified datasource id

Raises:



100
101
102
103
104
105
106
# File 'lib/grafana/grafana.rb', line 100

def datasource_by_id(datasource_id)
  clean_nil_datasources
  datasource = @datasources.select { |name, ds| ds.id == datasource_id.to_i }.values.first
  raise DatasourceDoesNotExistError.new('id', datasource_id) unless datasource

  datasource
end

#datasource_by_name(datasource_name) ⇒ Datasource

Returns the datasource, which has been queried by the datasource name.

Parameters:

  • datasource_name (String)

    name of the searched datasource

Returns:

  • (Datasource)

    Datasource for the specified datasource name

Raises:



75
76
77
78
79
80
81
82
# File 'lib/grafana/grafana.rb', line 75

def datasource_by_name(datasource_name)
  datasource_name = 'default' if datasource_name.to_s.empty?
  # TODO: PRIO add support for grafana builtin datasource types
  return UnsupportedDatasource.new(nil) if datasource_name.to_s =~ /-- (?:Mixed|Dashboard|Grafana) --/
  raise DatasourceDoesNotExistError.new('name', datasource_name) unless @datasources[datasource_name]

  @datasources[datasource_name]
end

#datasource_by_uid(datasource_uid) ⇒ Datasource

Returns the datasource, which has been queried by the datasource uid.

Parameters:

  • datasource_uid (String)

    unique id of the searched datasource

Returns:

  • (Datasource)

    Datasource for the specified datasource unique id

Raises:



88
89
90
91
92
93
94
# File 'lib/grafana/grafana.rb', line 88

def datasource_by_uid(datasource_uid)
  clean_nil_datasources
  datasource = @datasources.select { |ds_name, ds| ds.uid == datasource_uid }.values.first
  raise DatasourceDoesNotExistError.new('uid', datasource_uid) unless datasource

  datasource
end

#organizationHash

Returns Information about the current organization.

Returns:

  • (Hash)

    Information about the current organization



29
30
31
32
33
34
35
36
37
38
# File 'lib/grafana/grafana.rb', line 29

def organization
  return @organization if @organization

  response = prepare_request({ relative_url: '/api/org/' }).execute
  if response.is_a?(Net::HTTPOK)
    @organization = JSON.parse(response.body)
  end

  @organization
end

#prepare_request(options = {}) ⇒ WebRequest

Prepares a WebRequest object for the current Grafana::Grafana instance, which may be enriched with further properties and can then run WebRequest#execute.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :relative_url (Hash)

    relative URL with a leading slash, which shall be queried

  • :accept (Hash)
  • :body (Hash)
  • :content_type (Hash)

Returns:

  • (WebRequest)

    webrequest prepared for execution



145
146
147
148
# File 'lib/grafana/grafana.rb', line 145

def prepare_request(options = {})
  auth = @key ? { authorization: "Bearer #{@key}" } : {}
  WebRequest.new(@base_uri, auth.merge({ logger: @logger }).merge(options))
end

#test_connectionString

Used to test a connection to the grafana instance.

Running this function also determines, if the API configured here has Admin or NON-Admin privileges, or even fails on connecting to grafana.

Returns:

  • (String)

    Admin, NON-Admin or Failed is returned, depending on the test results



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/grafana/grafana.rb', line 58

def test_connection
  if prepare_request({ relative_url: '/api/datasources' }).execute.is_a?(Net::HTTPOK)
    # we have admin rights
    @logger.warn('Reporter is running with Admin privileges on grafana. This is a potential security risk.')
    return 'Admin'
  end
  # check if we have lower rights
  return 'Failed' unless prepare_request({ relative_url: '/api/dashboards/home' }).execute.is_a?(Net::HTTPOK)

  @logger.info('Reporter is running with NON-Admin privileges on grafana.')
  'NON-Admin'
end

#versionString

Returns grafana version.

Returns:

  • (String)

    grafana version



41
42
43
44
45
46
47
48
49
50
# File 'lib/grafana/grafana.rb', line 41

def version
  return @version if @version

  response = prepare_request({ relative_url: '/api/health' }).execute
  if response.is_a?(Net::HTTPOK)
    @version = JSON.parse(response.body)['version']
  end

  @version
end