Class: ManageEngine::APMConnector

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

Instance Method Summary collapse

Constructor Details

#initializeAPMConnector

Returns a new instance of APMConnector.



10
11
12
13
14
# File 'lib/agent/server/am_connector.rb', line 10

def initialize 
  @obj = ManageEngine::APMObjectHolder.instance
  @pretry =0
  @gretry =0
end

Instance Method Details

#connection(url) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/agent/server/am_connector.rb', line 100

def connection(url)

  if (@obj.config.proxyneeded)
    @obj.log.debug "[connect] Through Proxy"
    con = Net::HTTP::Proxy(@obj.config.proxy_host, @obj.config.proxy_port,@obj.config.proxy_user,@obj.config.proxy_pass).new(url.host, url.port)    
  else
    #@obj.log.info "Proxy Not Needed #{url.host} #{url.port}"
    con = Net::HTTP.new(url.host, url.port)
    #con.use_ssl=true
    #con.verify_mode=OpenSSL::SSL::VERIFY_NONE
    #@obj.log.info "connection = #{con}"
  end
  con=getScheme(con, url)
  con.open_timeout = @obj.constants.connection_open_timeout
  con.read_timeout = @obj.constants.connection_read_timeout
  con
end

#deleteAgentObject



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/agent/server/am_connector.rb', line 226

def deleteAgent
  uManage = Hash.new
  uManage["agent.id"]=@obj.config.instance_id
  uManage["agent.enabled"]=false 
  @obj.config.updateAgentInfoFile uManage
  begin
    File.delete(@obj.constants.agent_conf)
  rescue Exceptione=>e
    @obj.log.logException "#{e.message}",e
  end
end

#get(uri) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/agent/server/am_connector.rb', line 46

def get uri
  @gretry = @gretry +1
  begin
    u = url uri
    #@obj.log.info "[connector] [ GET ]  START"
    @obj.log.debug "[connector] [ GET]  : \n#{u}\n"
    req = Net::HTTP::Get.new(u.request_uri)
    resp = con.request(req)
    #@obj.log.info "[connector] [ GET ]  END"
  rescue Exception=>e
    @obj.log.logException "[connector] [ GET]  Exception while connecting server  - Data not sent ",e
    if @pretry >=@obj.config.connection_retry
      #@obj.shutdown= true
    else
      @obj.log.info "[connector] Exception found in Get request - Retrying - Count - #{@gretry}"
      return get uri
    end
  end
  @gretry = 0
end

#getScheme(con, url) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/agent/server/am_connector.rb', line 118

def getScheme(con, url)
  if(url.to_s.start_with?("https"))
    #@obj.log.info "[connect] Secured"
    #con = Net::HTTP::Proxy(@obj.config.proxy_host, @obj.config.proxy_port,@obj.config.proxy_user,@obj.config.proxy_pass).new(url.host, url.port)   
    con.use_ssl=true
    con.verify_mode=OpenSSL::SSL::VERIFY_NONE
  end
  con
end

#manageObject



219
220
221
222
223
224
# File 'lib/agent/server/am_connector.rb', line 219

def manage
  uManage = Hash.new
  uManage["agent.id"]=@obj.config.instance_id
  uManage["agent.enabled"]=true  
  @obj.config.updateAgentInfoFile uManage
end

#post(uri, data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/agent/server/am_connector.rb', line 16

def post uri,data
  @pretry = @pretry +1
  begin

    u = url uri
    #@obj.log.info "[connector] [ POST]  START"
    @obj.log.debug "[connector] [ POST]  : #{u}\n#{data}"
    con =  connection(u)
    req = Net::HTTP::Post.new(u.request_uri,initheader = {'Content-Type' =>'application/json'})
    req.body=data.to_json
    resp = con.request(req)
    @obj.log.debug "[connector] [POST ] \n Response : #{resp} \nResponse Code : #{resp.code}\nMessage : #{resp.message}\nBody : #{resp.body}"
    rdata = responseParser resp
    @pretry = 0
    #@obj.log.info "[connector] [ POST]  END"
    return rdata
  rescue Exception=>e
    @obj.log.logException "[connector] Exception while connecting server- Data not sent \n",e
    if @pretry >=@obj.config.connection_retry
      #@obj.shutdown= true
      return nil
    else
      @obj.log.info "[connector] Exception found in Post request - Retrying - Count - #{@pretry}"
      return post uri,data
    end

  end
  @pretry = 0
end

#response_action(rCode) ⇒ Object



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

def response_action rCode
  case rCode
  when @obj.constants.licence_expired then
    @obj.log.info "License Expired. Going to shutdown"
    raise Exception.new("License Expired. Going to shutdown")
  when @obj.constants.licence_exceeds then
    @obj.log.info "License Exceeds. Going to shutdown"
    raise Exception.new("License Exceeds. Going to shutdown")
  when @obj.constants.delete_agent then
    @obj.log.info "Action from Server - Delete the Agent. Going to shutdown and remove the Agent"
    deleteAgent
    raise Exception.new("Action from Server - Delete the Agent. Going to shutdown and remove the Agent")
  when @obj.constants.unmanage_agent then
    @obj.log.info "Action from Server - Unmanage the Agent. Going to  Stop the DC - Disabling the Agent"
    unManage
  when @obj.constants.manage_agent then
    @obj.log.info "Action from Server - Manage the Agent. Going to Sart the DC - Enabling the Agent"
    manage  
  end
end

#responseParser(resp) ⇒ Object



128
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
160
161
162
# File 'lib/agent/server/am_connector.rb', line 128

def responseParser resp
  if resp.kind_of? Net::HTTPOK
    rawData = resp.body
    if rawData.length>=2
      rBody = JSON.parse(rawData)
      result = rBody["result"]
      data = rBody["data"]
      if !@obj.util.getBooleanValue result
        if data!=nil
          if data.has_key?("exception")
            raise Exception.new("Exception from server - "+data["exception"])
          end
        end

      end
      if data!=nil
             if data.has_key?(@obj.constants.response_code)
          srCode = data[@obj.constants.response_code]
          response_action srCode
             end
             if data.has_key?(@obj.constants.custom_config_info)
              config_info = data[@obj.constants.custom_config_info]
                 if data.has_key?(@obj.constants.agent_specific_info)
                   config_info = config_info.merge(data[@obj.constants.agent_specific_info])
                 end
                 update_config config_info
             end
      end
      return data
    end
    return rawData
  else
    raise Exception.new("Http Connection Response Error #{resp.to_s}")
  end
end

#unManageObject



212
213
214
215
216
217
# File 'lib/agent/server/am_connector.rb', line 212

def unManage
  uManage = Hash.new
  uManage["agent.id"]=@obj.config.instance_id
  uManage["agent.enabled"]=false 
  @obj.config.updateAgentInfoFile uManage
end

#update_config(configInfo) ⇒ Object



187
188
189
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/am_connector.rb', line 187

def update_config configInfo
  existingConfigInfo = @obj.config.getAgentConfigData
  sendUpdate = "false"
  existingConfigInfo.each do|key,value|
    if key != "last.modified.time"
      newValue = configInfo[key]
      if key == "sql.capture.enabled" || key == "transaction.trace.enabled" || key == "transaction.trace.sql.parametrize"
        if newValue
          newValue = 1
        else
          newValue = 0
        end
      end
      if value != newValue
        sendUpdate = "true"
      end
    end
  end
  if sendUpdate == "true"
    @obj.log.info "Action from Server - Agent configuration updated from UI. Going to update the same in apminsight.conf file"
    @obj.log.info "config info = #{configInfo}"
    @obj.config.update_config configInfo
  end
end

#url(uri) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/agent/server/am_connector.rb', line 67

def url(uri)
  ru=nil
  p="https"
  if(!@obj.config.is_secured)
    p="http"
  end         
  if(@obj.config.license_key != nil)
    if(!@obj.config.license_key.empty?)
      if(@obj.config.apmhost != nil && !@obj.config.apmhost.empty?)
        u = @obj.config.apmhost+uri
      else
        u = @obj.config.site24x7url+uri
      end
    else
      #empty license key - print error
      @obj.log.info "license key is present, but empty"
    end
  else
    @obj.log.info "license key is null"
    u = p+"://"+@obj.config.apmhost+":#{@obj.config.apmport}/"+uri
  end
  begin
    ru = URI.parse(u)
  rescue
    raise URI::InvalidURIError, "Invalid url '#{ru}'"
  end

  if (ru.class != URI::HTTP && ru.class != URI::HTTPS)
    raise URI::InvalidURIError, "Invalid url '#{u}'"
  end
  ru
end