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



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 53

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/agent/server/worker/am_worker.rb', line 75

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

#mapdb(res, dat) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/agent/server/worker/am_worker.rb', line 241

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
end

#mapdx(res, dat) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/agent/server/worker/am_worker.rb', line 225

def mapdx 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[5] = res[5]+dat[5]
	res[6] = res[6]+dat[6]
	res[7] = res[7]+dat[7]
	res[4] = (res[5].to_f + (res[6].to_f/2).to_f).to_f/res[3].to_f
	res
end

#merge(data) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/agent/server/worker/am_worker.rb', line 201

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])
				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



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

def read p
     data = Array.new
  File.open( p, "r+" ) { |f|
f.flock(File::LOCK_EX)
     	begin
        		f.each_line do |line|
	data.push(JSON.parse(line))
       		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



140
141
142
143
144
145
146
147
# File 'lib/agent/server/worker/am_worker.rb', line 140

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/agent/server/worker/am_worker.rb', line 149

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/agent/server/worker/am_worker.rb', line 112

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])
                        trdata.concat(val[1])
                end
        end
        result.push(merge(tdata))
        resp = @obj.connector.post @obj.constants.connect_data_uri+@obj.config.instance_id,result
        if trdata.size>0
                result[2]=trdata;
                resp = @obj.connector.post @obj.constants.connect_trace_uri+@obj.config.instance_id,result
        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 "woker 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
checkforagentstatus
updateConfig
                        dc
sleep (@obj.config.connect_interval).to_i
    	            end#w
                    @status= "end"
     	            @obj.log.debug "Worker thread ends"
        	end
        end
end

#stopObject



70
71
72
73
# File 'lib/agent/server/worker/am_worker.rb', line 70

def stop
    dc
			@obj.shutdown = true;
end

#updateConfigObject



44
45
46
47
48
49
50
51
# 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."
		@obj.config.lastupdatedtime=File.mtime(@obj.constants.apm_conf).to_i
		@obj.config.configureFile
		@obj.config.assignConfig
	end
end

#write(p, data) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/agent/server/worker/am_worker.rb', line 188

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