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
25
# 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]
  @total = {type: "total", stats: [{"name" => "_total_"}]}
end

Instance Method Details

#write(stats, total = true) ⇒ Object



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

def write(stats, total = true)
  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].each do |stat|
    add_to_total(stat) if total
    request.body = line = stat_to_line(stat, type)
    puts line if @debug
    response = http.request(request)
    yield(stat, response) if block_given?
  end
  if total
    write(@total, total = false) {|stat, response| yield(stat, response)}
  end
end