Class: ManageEngine::APMWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/server/worker/am_worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAPMWorker

Returns a new instance of APMWorker.



10
11
12
13
# File 'lib/agent/server/worker/am_worker.rb', line 10

def initialize
  @status = "initialized"
  @id = Process.pid
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/agent/server/worker/am_worker.rb', line 9

def id
  @id
end

Class Method Details

.getInstanceObject



37
38
39
40
41
42
# File 'lib/agent/server/worker/am_worker.rb', line 37

def self.getInstance
  if(@work==nil || @work.id!=Process.pid)
   	@work = ManageEngine::APMWorker.new
  end
  return @work
end

Instance Method Details

#checkforagentstatusObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/agent/server/worker/am_worker.rb', line 70

def checkforagentstatus
	prevState = @obj.config.agent_enabled
	@obj.config.checkAgentInfo
	if !@obj.config.agent_enabled
		@obj.log.info "Agent in Disabled State."
		if prevState
			@obj.log.info "Agent in Disabled State. Going to unsubscribe"
#					@obj.instrumenter.doUnSubscribe
		end
	else
		if !prevState
			@obj.log.info "Agent in Active State."
#					@obj.instrumenter.doSubscribe
		end
	end
end

#dcObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/agent/server/worker/am_worker.rb', line 92

def dc
  	    begin
              @obj.log.debug "[dc] collecting..."
              now = @obj.util.currenttimemillis
              result =  Array.new
              result.push(@obj.last_dispatch_time)
                              result.push(now)
              data = Array.new
              trd= nil;
              @last_dispatch_time = now
              if @obj.config.agent_enabled
                      d = @obj.parser.parse @obj.store.metrics_dup
                      if(d!=nil && d.has_key?("trace-data"))
                              trd = d.delete("trace-data");
                              #@obj.log.info "[dc] [TRACE] : #{d}"
                      end
                      #@obj.log.info "[dc] Data - #{d}"
                      if(d.length>0)
                              data =@obj.formatter.format d
                              #@obj.log.debug "[dc] Formatted Data - #{data}"
                      end
                      @obj.store.remove @obj.formatter.keysToRemove
              end #if
              fd = Array.new
              fd.push(data)
              if(trd!=nil)
              	fd.push(trd)
              end
		@obj.log.debug "[dc] data to store : #{fd}"
              send_save fd
		@obj.log.debug "[dc] collecting ends"
       rescue Exception=>e
              @obj.log.logException "[dc]  Exception during data Collection. #{e.message}",e
              @obj.shutdown=true
  	    end
end

#mapbg(res, dat) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/agent/server/worker/am_worker.rb', line 297

def mapbg res,dat
  begin
    rtData = res[0];
    rtData[0] = rtData[0]+dat[0][0];
    if dat[0][1]<rtData[1]
      rtData[1]=dat[0][1]
    end
    if dat[0][2]>rtData[2]
      rtData[2]=dat[0][2]
    end
    rtData[3] = rtData[3]+dat[0][3]
    rtData[4] = rtData[4]+dat[0][4]
    res[0] = rtData
    
    # error_rt merge 
    res_error_rt = res[1][@obj.constants.error_rt];
    dat_error_rt = dat[1][@obj.constants.error_rt];
    if (res_error_rt != nil)
      if (dat_error_rt != nil)
        res[1][@obj.constants.error_rt] = res_error_rt + dat_errror_rt;
      end
    elsif (dat_error_rt != nil)
      res[1][@obj.constants.error_rt] = dat_errror_rt;
    end         
    
    resExcepData = res[1][@obj.constants.mf_logmetric]
    excepData = dat[1][@obj.constants.mf_logmetric]
    if (resExcepData == nil)
        resExcepData = excepData
    else
      if (excepData != nil)
        resExcepData = resExcepData.merge(excepData){|key, oldval, newval| newval + oldval}
      end
    end
    
    res[1][@obj.constants.mf_logmetric] = resExcepData != nil ? resExcepData : Hash.new
    rescue Exception=>e
      @obj.log.logException "Exception while merging bg data",e
    end
  res
end

#mapdb(res, dat) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/agent/server/worker/am_worker.rb', line 338

def mapdb res,dat
	res[0] = res[0]+dat[0];
		if dat[1]<res[1]
		res[1]=dat[1]
	end
	if dat[2]>res[2]
		res[2]=dat[2]
	end
	res[3] = res[3]+dat[3]
	res[4] = res[4]+dat[4]
	res
end

#mapdx(res, dat) ⇒ Object



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
# File 'lib/agent/server/worker/am_worker.rb', line 252

def mapdx res,dat
  begin
		  rtData = res[0];
			rtData[0] = rtData[0]+dat[0][0];
			if dat[0][1]<rtData[1]
			  rtData[1]=dat[0][1]
			end
			if dat[0][2]>rtData[2]
			  rtData[2]=dat[0][2]
			end
			rtData[3] = rtData[3]+dat[0][3]
			rtData[5] = rtData[5]+dat[0][5]
			rtData[6] = rtData[6]+dat[0][6]
			rtData[7] = rtData[7]+dat[0][7]
			rtData[4] = rtData[3] != 0 ? (rtData[5].to_f + (rtData[6].to_f/2).to_f).to_f/rtData[3].to_f : 0
			res[0] = rtData
			
			# error_rt merge 
			res_error_rt = res[1][@obj.constants.error_rt];
			dat_error_rt = dat[1][@obj.constants.error_rt];
			if (res_error_rt != nil)
			  if (dat_error_rt != nil)
          res[1][@obj.constants.error_rt] = res_error_rt + dat_error_rt;
        end
      elsif (dat_error_rt != nil)
        res[1][@obj.constants.error_rt] = dat_errror_rt;
      end
			  
			resExcepData = res[1][@obj.constants.mf_logmetric]
			excepData = dat[1][@obj.constants.mf_logmetric]
			if (resExcepData == nil)
			    resExcepData = excepData
			else
			  if (excepData != nil)
			    resExcepData = resExcepData.merge(excepData){|key, oldval, newval| newval + oldval}
			  end
			end
			
			res[1][@obj.constants.mf_logmetric] = resExcepData != nil ? resExcepData : Hash.new
			rescue Exception=>e
			  @obj.log.logException "Exception while merging data",e
			end
	res
end

#merge(data) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/agent/server/worker/am_worker.rb', line 226

def merge data
#                 @obj.log.info "BEFORE MERGE : #{data}"
		tdata =Hash.new ;
		data.each do |sd|
			name= sd[0]["ns"] + sd[0]["name"];
			if tdata.has_key?(name)
				if (sd[0]["name"]=="apdex")
					tdata[name][1] = mapdx(tdata[name][1],sd[1])
				elsif (sd[0]["name"]=="bckgrnd")
					tdata[name][1] = mapbg(tdata[name][1],sd[1])  
				else
					tdata[name][1] = mapdb(tdata[name][1],sd[1])
				end
			else
				tdata[name]=sd;
			end
		end
#@obj.log.info "MERGED DATA : #{tdata}"
		res = Array.new;
		tdata.each do|key,value|
			res.push(value);
	   end
	res
end

#read(p) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/agent/server/worker/am_worker.rb', line 190

def read p
     data = Array.new
  File.open( p, "r+" ) { |f|
f.flock(File::LOCK_EX)
     	begin
        		f.each_line do |line|
        		   begin
        		     data.push(JSON.parse(line))
        		   rescue Exception=>ex
        		     @obj.log.logException "Error Parsing data, Skipping line #{line}", ex
        		   end
       		end
   f.truncate 0
   		rescue Exception=>e
         	@obj.log.logException "Exception while reading data #{e}",e
  		ensure
         	f.flock(File::LOCK_UN)
 		end
 	}
     data
end

#save(fd) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/agent/server/worker/am_worker.rb', line 161

def save fd
        begin
        	data = fd.to_json;
        	write @obj.constants.agent_store,data
        rescue Exception=>e
        	@obj.log.logException "[dc]  Exception during save. #{e.message}",e
        end
end

#send_save(data) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/agent/server/worker/am_worker.rb', line 170

def send_save data
        begin
        	if FileTest.exist?(@obj.constants.agent_lock)
                if Time.now.to_i - File.mtime(@obj.constants.agent_lock).to_i >= (@obj.config.connect_interval).to_i
                        @obj.log.debug "worker send signal"
                        senddata data
                else
                        @obj.log.info "worker save signal"
                        save data
                end
        else
                @obj.log.info "worker save signals"
                save data
                write @obj.constants.agent_lock,"#{Process.pid}"
        end
        rescue Exception=>e
                @obj.log.logException "Exception in decision making send or save #{e.message}",e
        end
end

#senddata(d) ⇒ Object



129
130
131
132
133
134
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
# File 'lib/agent/server/worker/am_worker.rb', line 129

def senddata d
       # @obj.log.info("Send data --- #{d}")
        result =  Array.new
        result.push( (File.mtime(@obj.constants.agent_lock).to_f*1000).to_i)
        now = @obj.util.currenttimemillis
        result.push(now)
        write @obj.constants.agent_lock ,"#{Process.pid}"
        data  = read @obj.constants.agent_store
        data.push(d);
        tdata = Array.new;
        trdata = Array.new;
        data.each do |val|
                case val.size
                when 1
                        tdata.concat(val[0])
                when 2
                        tdata.concat(val[0])
                        if (trdata.size < @obj.config.trace_overflow_t)
                          trdata.concat(val[1])
                        end
                end
        end
        result.push(merge(tdata))
        resp = @obj.connector.post @obj.constants.connect_data_uri+@obj.config.instance_id,result
        @obj.log.info "#{tdata.size} metric(s) dispatched."
        if trdata.size>0
                result[2]=trdata;
                resp = @obj.connector.post @obj.constants.connect_trace_uri+@obj.config.instance_id,result
                @obj.log.info "#{trdata.size} trace(s) dispatched."
        end
end

#startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/agent/server/worker/am_worker.rb', line 15

def start
  @obj = ManageEngine::APMObjectHolder.instance

	if @status=="working"
    @obj.log.debug "worker thread already started"
  elsif @status == "initialized"
    @obj.log.info "start worker thread for - #{Process.pid} :: #{@status}  "
    #@obj.log.info "Starting APMWorker Thread #{Process.pid} "
    @apm = Thread.new do
      @status  = 'working'
  	  while !@obj.shutdown do
        sleep (@obj.config.connect_interval).to_i
  	    checkforagentstatus
				updateConfig
        dc
      end#w
      @status= "end"
      @obj.log.debug "Worker thread ends"
    end
  end
end

#stopObject



87
88
89
90
# File 'lib/agent/server/worker/am_worker.rb', line 87

def stop
    dc
			@obj.shutdown = true;
end

#updateConfigObject



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
# File 'lib/agent/server/worker/am_worker.rb', line 44

def updateConfig
	if(@obj.config.lastupdatedtime!=File.mtime(@obj.constants.apm_conf).to_i)
		@obj.log.info "Configuration File Changed... So Updating Configuration."
		agent_config_data = @obj.config.getAgentConfigData
		@obj.config.lastupdatedtime=File.mtime(@obj.constants.apm_conf).to_i
		@obj.config.configureFile
		@obj.config.assignConfig
		new_agent_config_data = @obj.config.getAgentConfigData
		sendUpdate = "false"
		agent_config_data.each do|key,value|
			if key != "last.modified.time"
				newValue = new_agent_config_data[key]
				if value != newValue
					sendUpdate = "true"
				end
			end
		end
		if sendUpdate == "true"
			@obj.log.info "sending update to server #{new_agent_config_data}"
			data1 =  Hash.new
			data1["custom_config_info"]=new_agent_config_data
       			        resp = @obj.connector.post @obj.constants.connect_config_update_uri+@obj.config.instance_id,data1
		end
	end
end

#write(p, data) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/agent/server/worker/am_worker.rb', line 213

def write (p,  data )
    File.open( p, "a+" ) { |f|
     	f.flock(File::LOCK_EX)
        begin
					f.write "#{data}\n"
        rescue Exception=>e
                @obj.log.logException "Exception while writing data #{e.message}",e
  ensure
            f.flock(File::LOCK_UN)
        end
    }
end