Class: Grafana::Client

Overview

Abstract base class for the API calls. Provides some helper methods

Author:

  • Bodo Schulz

Constant Summary

Constants included from Version

Version::MAJOR, Version::MINOR, Version::TINY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Playlist

#create_playlist, #delete_playlist, #playlist, #playlist_dashboards, #playlist_items, #playlists, #update_playlist

Methods included from FolderSearch

#folder_and_dashboard_search

Methods included from FolderPermissions

#folder_permissions, #update_folder_permissions

Methods included from Folder

#create_folder, #delete_folder, #folder, #folders, #update_folder

Methods included from Alerts

#alert, #alert_notifications, #alert_pause, #alerts, #create_alert_notification, #delete_alert_notification, #update_alert_notification

Methods included from Snapshot

#create_snapshot, #delete_snapshot, #snapshot

Methods included from DashboardPermissions

#dashboard_permissions, #update_dashboad_permissions

Methods included from DashboardVersions

#compare_dashboard_version, #dashboard_all_versions, #dashboard_version, #restore_dashboard

Methods included from Dashboard

#create_dashboard, #dashboard, #dashboard_by_uid, #dashboard_tags, #delete_dashboard, #home_dashboard, #import_dashboards_from_directory, #search_dashboards

Methods included from Organizations

#add_user_to_organization, #create_organisation, #delete_organisation, #delete_user_from_organization, #organization, #organization_users, #organizations, #update_organization, #update_organization_user

Methods included from Organization

#add_user_to_current_organization, #current_organization, #current_organization_users, #update_current_organization

Methods included from Datasource

#create_datasource, #datasource, #datasources, #delete_datasource, #update_datasource

Methods included from Teams

#add_team, #add_team_member, #delete_team, #remove_team_member, #search_team, #team, #team_members, #update_team

Methods included from Users

#search_for_users_by, #update_user, #user, #user_organizations, #users

Methods included from User

#add_dashboard_star, #current_user, #current_user_oganizations, #remove_dashboard_star, #switch_current_user_organization, #update_current_user_password

Methods included from Preferences

#org_preferences, #update_org_preferences, #update_user_preferences, #user_preferences

Methods included from Annotations

#create_annotation, #create_annotation_graphite, #delete_annotation, #delete_annotation_by_region, #find_annotation, #update_annotation

Methods included from Admin

#add_user, #admin_settings, #admin_stats, #delete_user, #pause_all_alerts, #update_user_password, #update_user_permissions

Methods included from Tools

#regenerate_template_ids, #slug, #valid_json?

Methods included from Network

#delete, #get, #patch, #post, #put

Methods included from Login

#headers, #health, #login, #ping_session

Methods included from Validator

#validate, #validate_hash

Methods included from Auth

#api_key, #api_keys, #create_api_key, #delete_api_key

Methods included from Logging

configure_logger_for, #logger, logger_for

Constructor Details

#initialize(settings) ⇒ Client

Create a new instance of Class

Examples:

to create an new Instance

config = {
  grafana: {
    host: '192.168.33.5',
    port: 3000,
    url_path: '/grafana',
    ssl: false,
    timeout: 10,
    open_timeout: 10,
    debug: true
}

@grafana = Grafana::Client.new(config)

Parameters:

  • settings (Hash, #read)

    the settings for Grafana

Options Hash (settings):

  • :host (String) — default: 'localhost'

    the Grafana Hostname

  • :port (Integer) — default: 3000

    the Grafana HTTP Port

  • :url_path (String) — default: ''
  • :ssl (Bool) — default: false
  • :timeout (Integer) — default: 5
  • :open_timeout (Integer) — default: 5
  • :http_headers (Hash) — default: {}
  • :debug (Bool) — default: false

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/grafana/client.rb', line 106

def initialize( settings )

  raise ArgumentError.new('only Hash are allowed') unless( settings.is_a?(Hash) )
  raise ArgumentError.new('missing settings') if( settings.size.zero? )

  host                = settings.dig(:grafana, :host)          || 'localhost'
  port                = settings.dig(:grafana, :port)          || 3000
  url_path            = settings.dig(:grafana, :url_path)      || ''
  ssl                 = settings.dig(:grafana, :ssl)           || false
  @timeout            = settings.dig(:grafana, :timeout)       || 5
  @open_timeout       = settings.dig(:grafana, :open_timeout)  || 5
  @api_user           = settings.dig(:grafana, :api, :user)    || 'admin'
  @api_password       = settings.dig(:grafana, :api, :password)
  @http_headers       = settings.dig(:grafana, :http_headers)  || { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
  @debug              = settings.dig(:debug)                   || false
  @debug = false
  @headers            = {}

  # Token Support for Grafana 6
  @api_key            = nil
  @api_token_name     = settings.dig(:grafana, :api, :token, :name ) || (0..10).to_a.map{|_a| rand(16).to_s(16)}.join
  @api_token_lifetime = settings.dig(:grafana, :api, :token, :lifetime ) || 0

  raise ArgumentError.new('missing \'host\'') if( host.nil? )

  raise ArgumentError.new(format('wrong type. \'port\' must be an Integer, given \'%s\'', port.class.to_s)) unless( port.is_a?(Integer) )
  raise ArgumentError.new(format('wrong type. \'url_path\' must be an String, given \'%s\'', url_path.class.to_s)) unless( url_path.is_a?(String) )
  raise ArgumentError.new(format('wrong type. \'ssl\' must be an Boolean, given \'%s\'', ssl.class.to_s)) unless( ssl.is_a?(Boolean) )
  raise ArgumentError.new(format('wrong type. \'timeout\' must be an Integer, given \'%s\'', @timeout.class.to_s)) unless( @timeout.is_a?(Integer) )
  raise ArgumentError.new(format('wrong type. \'open_timeout\' must be an Integer, given \'%s\'', @open_timeout.class.to_s)) unless( @open_timeout.is_a?(Integer) )

  protocoll = ssl == true ? 'https' : 'http'

  @url      = format( '%s://%s:%d%s', protocoll, host, port, url_path )
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



77
78
79
# File 'lib/grafana/client.rb', line 77

def debug
  @debug
end

Instance Method Details

#create_instanceObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/grafana/client.rb', line 143

def create_instance

  logger.debug( "create_instance" )
  logger.debug( "@api_key: #{@api_key} #{@api_key.class}" )

  params = { timeout: @timeout.to_i, open_timeout: @open_timeout.to_i, headers: @http_headers, verify_ssl: false }
  params = { timeout: @timeout.to_i, open_timeout: @open_timeout.to_i, headers: @http_headers, verify_ssl: false, user: @api_user, password: @api_password } if( @api_key.nil? )

  logger.debug( "url   : #{@url}" )
  logger.debug( "params: #{params}" )

  begin
    RestClient::Resource.new( @url, params )
  rescue => error
    logger.error( error ) # if @debug
    logger.debug( e.backtrace.join("\n") ) #if @debug
    false
  end
end

#settingsObject



168
169
170
171
172
# File 'lib/grafana/client.rb', line 168

def settings
  endpoint = '/api/frontend/settings'
  @logger.debug("Getting all settings (GET #{endpoint})") if @debug
  get(endpoint)
end

#versionObject



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/grafana/client.rb', line 175

def version
  s = settings

  status = s.dig('status')
  if( status.to_i == 200 )
    @version =  s.dig('buildInfo','version')
    @major_version = @version.split('.').first.to_i

    { version: @version, major_version: @major_version }
  else
    s
  end
end