Class: Fluent::AppdynamicsInput

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

Overview

Read trap messages as events in to fluentd

Defined Under Namespace

Classes: MemoryStateStore, RedisStateStore, StateStore, TimerWatcher

Instance Method Summary collapse

Constructor Details

#initializeAppdynamicsInput

Initialize and bring in dependencies



141
142
143
144
145
146
147
148
# File 'lib/fluent/plugin/in_appdynamics.rb', line 141

def initialize
  super
  require 'json'
  require 'yaml'
  require 'rest-client'
  require 'pp'
  # Add any other dependencies
end

Instance Method Details

#appdynamicsEnd(startTime, endTime) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/fluent/plugin/in_appdynamics.rb', line 181

def appdynamicsEnd(startTime,endTime)
	# Setup URL Resource
	# Sample https://ep/controller/rest/applications/Prod/problems/healthrule-violations?time-range-type=BETWEEN_TIMES&output=JSON&start-time=1426270552990&end-time=1426270553000
	@url = @endpoint.to_s + "problems/healthrule-violations?time-range-type=BETWEEN_TIMES&output=JSON" + "&start-time=" + startTime.to_s + "&end-time=" + endTime.to_s
	$log.info @url
  RestClient::Resource.new(@url,@user+"@"+@account,@pass)
end

#appdynamicsEntEnd(entityId) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/fluent/plugin/in_appdynamics.rb', line 189

def appdynamicsEntEnd(entityId)
		# Setup URL Resource
		# Sample https://ep/controller/rest/applications/Prod/nodes/81376?output=JSON
		@urlEntity = @endpoint.to_s + "nodes/" + entityId.to_s + "?output=JSON"
		$log.info @urlEntity
		RestClient::Resource.new(@urlEntity,@user+"@"+@account,@pass)
end

#configure(conf) ⇒ Object

Load internal and external configs



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
197
# File 'lib/fluent/plugin/in_appdynamics.rb', line 151

def configure(conf)
  super
  @conf = conf
  
  # State type is a must
  unless @state_type
    $log.warn "'state_type <redis/file/memory>' parameter is not set to a valid source."
    $log.warn "this parameter is highly recommended to save the last known good timestamp to resume event consuming"
	exit
  end

  # Define a handler that gets filled with 
  unless @state_file
       $log.warn "'state_file PATH' parameter is not set to a valid source."
       log.warn "this parameter is highly recommended to save the last known good timestamp to resume event consuming"
@state_store = MemoryStateStore.new
	else
if (@state_type =~ /redis/)
	@state_store = RedisStateStore.new(@state_file, @tag)
elsif (@state_type =~ /file/)
		@state_store = StateStore.new(@state_file, @tag)
else
		$log.warn "Unknown state type. Need to handle this better"
		exit
end
  end

  
  #@state_store = @state_file.nil? ? MemoryStateStore.new : StateStore.new(@state_file)

  def appdynamicsEnd(startTime,endTime)
  	# Setup URL Resource
	# Sample https://ep/controller/rest/applications/Prod/problems/healthrule-violations?time-range-type=BETWEEN_TIMES&output=JSON&start-time=1426270552990&end-time=1426270553000
	@url = @endpoint.to_s + "problems/healthrule-violations?time-range-type=BETWEEN_TIMES&output=JSON" + "&start-time=" + startTime.to_s + "&end-time=" + endTime.to_s
	$log.info @url
    RestClient::Resource.new(@url,@user+"@"+@account,@pass)
  end

  def appdynamicsEntEnd(entityId)
		# Setup URL Resource
		# Sample https://ep/controller/rest/applications/Prod/nodes/81376?output=JSON
		@urlEntity = @endpoint.to_s + "nodes/" + entityId.to_s + "?output=JSON"
		$log.info @urlEntity
		RestClient::Resource.new(@urlEntity,@user+"@"+@account,@pass)
  end
  # TO DO Add code to choke if config parameters are not there
end

#inputObject

Start appdynamics Trap listener Add the code to run this



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/fluent/plugin/in_appdynamics.rb', line 228

def input
  if not @stop_flag
	alertEnd = Engine.now.to_i * 1000
	#alertStart = (Engine.now.to_f * 1000).to_i - @interval.to_i 
	#alertStart = Engine.now.to_i * 1000 
    if @state_store.last_records(@tag) 
      alertStart = @state_store.last_records(@tag)
      $log.info @tag + " :: Got time record from state_store - #{alertStart}" 
    else
      alertStart = alertEnd.to_i - @interval.to_i
      #$log.info "Spectrum :: Got time record from initial config - #{alertStart}"
    end
    
	$log.info "appdynamics :: Polling alerts for time period: #{alertStart.to_i} - #{alertEnd.to_i}"
	# Post to Appdynamics and parse results  

	begin
    	responsePost=appdynamicsEnd(alertStart,alertEnd).get
		#@state_store.update(pollingEnd, @tag)
		pollingDuration = alertEnd - alertStart
	rescue Exception => e
		$log.info e.message
		$log.info e.backtrace.inspect
	end
	# body is an array of hashes
	body = JSON.parse(responsePost.body)
	body.each_with_index {|val, index| 
		#pp val
		$log.debug val
		if @include_raw.to_s == "true"  
			# Deep copy
			rawObj=val.clone
    		#val << { "raw" => "#rawObj" }
			val["raw"]=rawObj
    	end
		# Need to do another call to get the hostname
		if ((val["affectedEntityDefinition"] || {})["entityId"] != nil) then
			begin
    			responsePostAffEnt=appdynamicsEntEnd(val["affectedEntityDefinition"]["entityId"]).get
bodyAffEntity = JSON.parse(responsePostAffEnt.body)
val["AffectedEntityName"]=bodyAffEntity[0]["name"]

			rescue Exception => e
$log.info e.message
$log.info e.backtrace.inspect
val["TrigerredEntityName"]=""
			end
			#pp bodyAffEntity["name"]
			#val["AffectedEntityName"]=bodyAffEntity["name"]

		end
		if ((val["triggeredEntityDefinition"] || {})["entityId"] != nil) then
			begin
    			responsePostTrigEnt=appdynamicsEntEnd(val["triggeredEntityDefinition"]["entityId"]).get
bodyTrigEnt = JSON.parse(responsePostTrigEnt.body)
val["TrigerredEntityName"]=bodyTrigEnt[0]["name"]
			rescue Exception => e
$log.info e.message
$log.info e.backtrace.inspect
val["TrigerredEntityName"]=""
			end
			#val["TrigerredEntityName"]=bodyTrigEnt["name"]
		end
		val["event_type"] = @tag.to_s
		val["receive_time_input"]=(Engine.now * 1000).to_s
		#puts "#{val} => #{index}" 
		$log.info val
		begin
      		Engine.emit(@tag, val['startTimeInMillis'].to_i,val)
			#@state_store.update
		rescue Exception => e
			$log.info e.message
			$log.info e.backtrace.inspect
		end
	}
	#pp body.class
	@state_store.update_records(alertEnd, @tag)
  end # END Stop flag
end

#runObject



218
219
220
221
222
223
224
# File 'lib/fluent/plugin/in_appdynamics.rb', line 218

def run
  @loop.run
  $log.info "Running appdynamics Input"
rescue
  $log.error "unexpected error", :error=>$!.to_s
  $log.error_backtrace
end

#shutdownObject

Stop Listener and cleanup any open connections.



211
212
213
214
215
216
# File 'lib/fluent/plugin/in_appdynamics.rb', line 211

def shutdown
  super
  @stop_flag = true
  @loop.stop
  @thread.join
end

#startObject

def configure



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

def start
  super
  @stop_flag = false
  @loop = Coolio::Loop.new
  #timer_trigger = TimerWatcher.new(@interval, true, &method(:input))
  #timer_trigger.attach(@loop)
  @loop.attach(TimerWatcher.new(@interval, true, &method(:input)))
  @thread = Thread.new(&method(:run))
  $log.info "starting appdynamics poller, interval #{@interval}"
end

#to_utf8(str) ⇒ Object

function to UTF8 encode



134
135
136
137
138
# File 'lib/fluent/plugin/in_appdynamics.rb', line 134

def to_utf8(str)
  str = str.force_encoding('UTF-8')
  return str if str.valid_encoding?
  str.encode("UTF-8", 'binary', invalid: :replace, undef: :replace, replace: '')
end