Class: DatalayerLight::InfluxDB

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

Overview

Metrics / Measurements Engine InfluxDB RestAPI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInfluxDB

Returns a new instance of InfluxDB.



198
199
200
201
202
203
204
# File 'lib/thm/datalayerlight.rb', line 198

def initialize
  @dbhost = "127.0.0.1"
  @dbuser = "threatmonitor"
  @dbpass = "dk3rbi9l"
  @dbport = 8086
  @dbname = "threatmonitor"
end

Instance Attribute Details

#dbhostObject

Returns the value of attribute dbhost.



196
197
198
# File 'lib/thm/datalayerlight.rb', line 196

def dbhost
  @dbhost
end

#dbnameObject

Returns the value of attribute dbname.



196
197
198
# File 'lib/thm/datalayerlight.rb', line 196

def dbname
  @dbname
end

#dbpassObject

Returns the value of attribute dbpass.



196
197
198
# File 'lib/thm/datalayerlight.rb', line 196

def dbpass
  @dbpass
end

#dbportObject

Returns the value of attribute dbport.



196
197
198
# File 'lib/thm/datalayerlight.rb', line 196

def dbport
  @dbport
end

#dburlObject

Returns the value of attribute dburl.



196
197
198
# File 'lib/thm/datalayerlight.rb', line 196

def dburl
  @dburl
end

#dbuserObject

Returns the value of attribute dbuser.



196
197
198
# File 'lib/thm/datalayerlight.rb', line 196

def dbuser
  @dbuser
end

Instance Method Details

#apiget(sql) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/thm/datalayerlight.rb', line 206

def apiget(sql)
  @dburl = "http://#{@dbhost}:#{@dbport}"
  sqlunicode = URI.encode(sql)
  puts "InfluxDB SQL URL: #{@dburl}/query?db=#{@dbname}&q=#{sqlunicode}"
  uri = URI.parse("#{@dburl}/query?db=#{@dbname}&q=#{sqlunicode}")
  puts "Request URI: #{uri}"
  http = Net::HTTP.new(uri.host, uri.port)
  begin
    response = http.request(Net::HTTP::Get.new(uri.request_uri))
    begin
      j = JSON.parse(response.body)
    rescue JSON::ParserError
      puts "Could not read JSON data"
    end
  rescue
    puts "Error retrieving data"
  end
end

#apipost(data) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/thm/datalayerlight.rb', line 225

def apipost(data)
  @dburl = "http://#{@dbhost}:#{@dbport}"
  #puts "InfluxDB SQL URL: #{@dburl}/query?db=#{@dbname}"
  uri = URI.parse("#{@dburl}/write?db=#{@dbname}")
  #puts "Request URI: #{uri}"
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_content_type("application/x-www-form-urlencoded")
  begin
    request.body = data unless data.empty?
    response = http.request(request)
    if response.code == "204" # Good response
      # Be quiet
      return response.code
    elsif response.code =~ %r=[200,400,500]= # 200 can be an error in some cases !!
      puts "Error code #{response.code}"
      return response.code
    end
  rescue
    puts "Error posting data"
    return "404"
  end
end

#query(sql, mode = "r") ⇒ Object



249
250
251
252
253
254
255
# File 'lib/thm/datalayerlight.rb', line 249

def query(sql, mode="r")
  if mode == "r"
    apiget("#{sql}")
  elsif mode == "w"
    apipost(sql)
  end
end