Class: CloudstackStats::Feed

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Feed

Returns a new instance of Feed.



17
18
19
20
21
22
23
24
# File 'lib/cloudstack_stats/feed.rb', line 17

def initialize(opts = {})
  @database = opts[:database]
  @url = opts[:influx_url] ||
    "http://localhost:8086/"
  @user = opts[:influx_user]
  @password = opts[:influx_password]
  @debug = opts[:debug]
end

Instance Method Details

#write(stats) ⇒ Object



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
# File 'lib/cloudstack_stats/feed.rb', line 26

def write(stats)
  uri = URI.parse(
    URI.join(
      @url,
      "write?db=#{@database}&precision=m"
    ).to_s
  )
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  request = Net::HTTP::Post.new(uri.request_uri)
  request.content_type = "application/octet-stream"
  request.basic_auth(@user, @password) if @user && @password


  type = stats[:type]
  stats[:stats].map do |stat|
    request.body = line = stat_to_line(stat, type)
    puts line if @debug
    response = http.request(request)
    yield(stat, response) if block_given?
  end
end