Class: Fluent::CloudStackInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_cloudstack.rb

Constant Summary collapse

INTERVAL_MIN =
300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCloudStackInput

Returns a new instance of CloudStackInput.



28
29
30
31
32
33
34
# File 'lib/fluent/plugin/in_cloudstack.rb', line 28

def initialize
  require "fog"
  require "eventmachine"
  init_eventmachine

  super
end

Instance Attribute Details

#before_events=(value) ⇒ Object (writeonly)

Sets the attribute before_events

Parameters:

  • value

    the value to set the attribute before_events to.



26
27
28
# File 'lib/fluent/plugin/in_cloudstack.rb', line 26

def before_events=(value)
  @before_events = value
end

Instance Method Details

#configure(conf) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/in_cloudstack.rb', line 36

def configure(conf)
  super

  @conf = conf

  unless @host && @apikey && @secretkey
    raise ConfigError, "'host' and 'apikey' and 'secretkey' must be all specified."
  end

  unless @debug_mode
    if @interval.to_i < INTERVAL_MIN
      raise ConfigError, "'interval' must be over #{INTERVAL_MIN}."
    end
  end

  @before_events_filepath = "#{@log_dir}/before_events.#{tag}.yml"

  if File.exist?(@before_events_filepath)
    @before_events = YAML.load_file(@before_events_filepath)
  else
    @before_events = nil
  end

  @before_usages_filepath = "#{@log_dir}/before_usages.#{tag}.yml"
  if File.exist?(@before_usages_filepath)
    @before_usages = YAML.load_file(@before_usages_filepath)
  else
    @before_usages = Hash.new
  end

  @event_output_tag = "#{@tag}.event"
  @usages_output_tag = "#{@tag}.usages"

end

#csObject



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/fluent/plugin/in_cloudstack.rb', line 198

def cs
  @cs ||= Fog::Compute.new(
      :provider => 'CloudStack',
      :cloudstack_api_key           => @apikey,
      :cloudstack_secret_access_key => @secretkey,
      :cloudstack_host              => @host,
      :cloudstack_port              => @port,
      :cloudstack_path              => @path,
      :cloudstack_scheme            => @protocol,
  )
end

#emit_new_eventsObject



97
98
99
100
101
102
103
104
105
# File 'lib/fluent/plugin/in_cloudstack.rb', line 97

def emit_new_events
  new_events = get_new_events
  new_events.each do |event|
    time = Time.parse(event["created"]).to_i
    Engine.emit(@event_output_tag, time, event)
  end

  Engine.emit("#{@usages_output_tag}", Engine.now, {"events_flow" => new_events.size})
end

#emit_usagesObject



107
108
109
110
111
# File 'lib/fluent/plugin/in_cloudstack.rb', line 107

def emit_usages
  usages = get_usages

  Engine.emit("#{@usages_output_tag}", Engine.now, get_usages)
end

#get_new_eventsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fluent/plugin/in_cloudstack.rb', line 113

def get_new_events
  if @before_events
    startdate = Time.parse(@before_events[0]["created"])
    event_responses = cs.list_events(:startdate => startdate.strftime("%Y-%m-%d %H:%M:%S"), :domainid => @domain_id)
    events = Array.new
    event_responses["listeventsresponse"]["event"].each do |event|
      unless Time.parse(event["created"]) == startdate
        events.push event
      end
    end
  else
    events = cs.list_events(:domainid => @domain_id)["listeventsresponse"]["event"]
  end

  if events.size > 0
    File.write(@before_events_filepath, events.to_yaml)
    @before_events = events
  end

  events
end

#get_usagesObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fluent/plugin/in_cloudstack.rb', line 135

def get_usages
  usages_per_service_offering   = Hash.new(0)
  usages_per_disk_offering      = Hash.new(0)
  memory_sum      = 0
  cpu_sum         = 0
  root_volume_sum = 0
  data_volume_sum = 0

  vms_responses = cs.list_virtual_machines(:domainid=>@domain_id)
  vms =  vms_responses["listvirtualmachinesresponse"]["virtualmachine"]

  if vms
    vms.each do |vm|
      memory_sum += vm["memory"].to_i * 1024 * 1024
      cpu_sum += vm["cpunumber"].to_i
      usages_per_service_offering[vm["serviceofferingname"]] += 1
    end
  else
    vms = []
  end

  volumes_responses = cs.list_volumes(:domainid=>@domain_id)
  volumes =  volumes_responses["listvolumesresponse"]["volume"]

  if volumes
    volumes.each do |volume|
      case volume["type"]
      when "ROOT"
        root_volume_sum += volume["size"]
      when "DATADISK"
        data_volume_sum += volume["size"]
        usages_per_disk_offering[volume["diskofferingname"].gsub(' ','_')] += 1
      end
    end
  end

  usages = Hash.new

  usages[:vm_sum]          = vms.size
  usages[:memory_sum]      = memory_sum
  usages[:cpu_sum]         = cpu_sum
  usages[:root_volume_sum] = root_volume_sum
  usages[:data_volume_sum] = data_volume_sum

  usages_per_service_offering.each do |key,value|
    usages[key] = value
  end
  usages_per_disk_offering.each do |key,value|
    usages[key] = value
  end

  @before_usages.each do |key,value|
    unless usages.key?(key)
      usages[key] = 0
    end
  end

  File.write(@before_usages_filepath, usages.to_yaml)
  @before_usages = usages

  usages
end

#runObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/in_cloudstack.rb', line 85

def run
  EM.add_periodic_timer(@interval) do
    begin
      emit_new_events
      emit_usages
    rescue => ex
      log.warn("EM.periodic_timer loop error.")
      log.warn("#{ex}, tracelog : \n#{ex.backtrace.join("\n")}")
    end
  end
end

#shutdownObject



78
79
80
81
82
83
# File 'lib/fluent/plugin/in_cloudstack.rb', line 78

def shutdown
  super
  @thread.join
  EM.stop if EM.reactor_running?
  @reactor_thread.join if @reactor_thread
end

#startObject



71
72
73
74
75
76
# File 'lib/fluent/plugin/in_cloudstack.rb', line 71

def start
  super
  run_reactor_thread
  @thread = Thread.new(&method(:run))
  log.info "listening cloudstack api on #{@host}"
end