Class: ManageEngine::APMAgent

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

Instance Method Summary collapse

Constructor Details

#initializeAPMAgent

Returns a new instance of APMAgent.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/agent/server/am_agent.rb', line 9

def initialize
			@obj = ManageEngine::APMObjectHolder.instance
			@obj.log.debug "Agent Initialization - START"
			if @obj.config.delayedStart
   @obj.log.info "Delaying agent handshake as per configuration.."
   @obj.agent_initialized = true # to be reset when connecting
   @apm = Thread.new do
     sleep ((@obj.config.connect_interval).to_i)/2
     doConnect
   end
 else
   doConnect
 end

			if !@obj.shutdown && @obj.agent_initialized
				@obj.log.info "Agent Initialization - DONE"
				ManageEngine::Environment.new.detect_and_instrument
				
				doDispatcherActions
				doCollect
				puts "APM Insight Ruby Agent Started. Agent files are located at #{@obj.constants.conf_location}"
				puts "Agent log file apm.log is generated at #{@obj.log.getLogFilePath}"
			else
				@obj.log.info "Agent Initialization Failed - Going to shutdown"
				#While parsing the response from /arh/connect we set instrumenter to nil on delete request
				#Server startup fails when the below instruction is executed
				#@obj.instrumenter.doUnSubscribe
				@obj.shutdownagent
			end
end

Instance Method Details

#doCollectObject



90
91
92
93
94
95
96
97
98
# File 'lib/agent/server/am_agent.rb', line 90

def doCollect
	@obj.log.info "[doCollect] Starts - Wait time : #{@obj.config.connect_interval} seconds "
	begin
		ManageEngine::APMWorker.getInstance.start
	rescue Exception=>e
		@obj.log.logException "[doCollect]  Exception during worker  startup  #{e.message}",e
		@obj.shutdown=true
	end
end

#doConnectObject



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/agent/server/am_agent.rb', line 40

def doConnect
	begin	
      @obj.agent_initialized=false #resetting, to go with the flow
      
		if @obj.shutdown
			@obj.log.info "[ Problem in Agent Startup ]"
		else
			agentInfo = @obj.config.getAgentInfo
			resp = nil
			if @obj.config.alreadyconnected
				@obj.log.debug "[doConnect] Already Connected - Make Contact - Instance id = #{@obj.config.instance_id}"
				resp = startConnect "?license.key="+@obj.config.license_key+"&instance_id="+@obj.config.instance_id,agentInfo
			else
				@obj.log.debug "[doConnect] Going to connect - New "
#						if @obj.config.site24x7
#							resp = startConnect "?license.key="[email protected]_key,agentInfo
#						else
#							resp = startConnect "",agentInfo
#						end
          # Checking the license pattern for APM as per team requirement
				if !@obj.config.site24x7 && !@obj.config.license_key.match?("APMI_[A-Fa-f0-9]{64}")
			    @obj.log.info "Invalid license key for App Manager, aborting agent init"
			    @obj.shutdown=true
				else
            resp = startConnect "?license.key="+@obj.config.license_key,agentInfo
				end
			end
			
			if (resp == nil || !resp.has_key?("instance-info"))
			  @obj.log.info "[doConnect] [ Problem in connecting server] [ Going to shutdown ]"
          @obj.shutdown=true
			else
				aData = resp["instance-info"]
				aData["agent.id"]=aData.delete("instanceid")
				aData["agent.enabled"]=true
				@obj.config.updateAgentInfoFile(aData)
				@obj.log.info "[doConnect] Agent successfully connected - InstanceID  : #{@obj.config.instance_id}"
			end

			if(!@obj.shutdown)
				@obj.agent_initialized=true
			end
		end
	rescue Exception=>e
		@obj.shutdown = true
		@obj.log.logException "[doConnect] Exception while connecting server. [ Going to shutdown ] ",e
	end
end

#doDispatcherActionsObject



105
106
107
108
109
110
111
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
139
140
141
142
143
# File 'lib/agent/server/am_agent.rb', line 105

def doDispatcherActions
  @obj.log.info "Dispatcher: #{@obj.config.app_dispatcher}"
	case @obj.config.app_dispatcher
		when 'passenger'
			#starting a new process
    				PhusionPassenger.on_event(:starting_worker_process) do |forked|
      				if forked
              			@obj.log.info "starting_worker_process : Process ID :#{Process.pid} : Creating new apm worker"
              			doCollect
      				else
              			doCollect
                	@obj.log.info "starting_worker_process : Conservative Process ID :#{Process.pid} - No new worker"
      				end
   			 	end
    				# shutting down a process.
		    PhusionPassenger.on_event(:stopping_worker_process) do
              		ManageEngine::APMWorker.getInstance.stop
              		@obj.log.info "stopping_worker_process :Process ID :#{Process.pid} ---->  #$$ "
   				end
   		when 'unicorn'
   		   Unicorn::HttpServer.class_eval do
   		     old_object = instance_method(:worker_loop)
   		     define_method(:worker_loop) do |worker|
             ::ManageEngine::APMObjectHolder.instance.agent.doCollect
   		       old_object.bind(self).call(worker)
   		     end
   		   end
		when 'rainbows'
		  Rainbows::HttpServer.class_eval do
         old_object = instance_method(:worker_loop)
         define_method(:worker_loop) do |worker|
           ::ManageEngine::APMObjectHolder.instance.agent.doCollect
           old_object.bind(self).call(worker)
         end
		 end
		else#case

		end#case
end

#startConnect(uri, data) ⇒ Object



101
102
103
# File 'lib/agent/server/am_agent.rb', line 101

def startConnect uri,data
	resp = @obj.connector.post @obj.constants.connect_uri+uri,data
end