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
# File 'lib/agent/server/am_agent.rb', line 9

def initialize
      @obj = ManageEngine::APMObjectHolder.instance
      @obj.log.debug "Agent Initialization - START"
      doConnect

      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



78
79
80
81
82
83
84
85
86
# File 'lib/agent/server/am_agent.rb', line 78

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



33
34
35
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
70
71
72
73
74
75
# File 'lib/agent/server/am_agent.rb', line 33

def doConnect
  begin  
    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}"
        if @obj.config.site24x7
          resp = startConnect "?license.key="+@obj.config.license_key+"&instance_id="+@obj.config.instance_id,agentInfo
        else
          resp = startConnect "?instance_id="+@obj.config.instance_id,agentInfo
        end
      else
        @obj.log.debug "[doConnect] Going to connect - New "
        if @obj.config.site24x7
          resp = startConnect "?license.key="+@obj.config.license_key,agentInfo
        else
          resp = startConnect "",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



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
128
129
130
131
# File 'lib/agent/server/am_agent.rb', line 93

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



89
90
91
# File 'lib/agent/server/am_agent.rb', line 89

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