source/code/plugins/agent_maintenance_script.rb
# Perform a heartbeat against the OMS endpoint
Perform a heartbeat against the OMS endpoint
Perform a heartbeat against the OMS endpoint
def heartbeat
      @load_config_return_code = load_config
      if @load_config_return_code != 0
        log_error("Error loading configuration from #{@omsadmin_conf_path}")
        return @load_config_return_code
      end
      if @WORKSPACE_ID.nil? or @AGENT_GUID.nil? or @URL_TLD.nil? or
          @WORKSPACE_ID.empty? or @AGENT_GUID.empty? or @URL_TLD.empty?
        log_error("Missing required field from configuration file: #{@omsadmin_conf_path}")
        return MISSING_CONFIG
      elsif !file_exists_nonempty(@cert_path) or !file_exists_nonempty(@key_path)
        log_error("Certificates for heartbeat request do not exist")
        return MISSING_CERTS
      end
      begin
        body_hb_xml = AgentTopologyRequestHandler.new.handle_request(@os_info, @omsadmin_conf_path,
            @AGENT_GUID, get_cert_server(@cert_path), @pid_path, telemetry=true)
        if !xml_contains_telemetry(body_hb_xml)
          log_debug("No Telemetry data was appended to the heartbeat request")
        end
      rescue => e
        log_error("Error when appending Telemetry to Heartbeat: #{e.message}")
      end
      headers = {}
      req_date = Time.now.utc.strftime("%Y-%m-%dT%T.%N%:z")
      headers[OMS::CaseSensitiveString.new("x-ms-Date")] = req_date
      headers["User-Agent"] = get_user_agent
      headers[OMS::CaseSensitiveString.new("Accept-Language")] = "en-US"
      req,http = form_post_request_and_http(headers, "https://#{@WORKSPACE_ID}.oms.#{@URL_TLD}/"\
                "AgentService.svc/LinuxAgentTopologyRequest", body_hb_xml,
                OpenSSL::X509::Certificate.new(File.open(@cert_path)),
                OpenSSL::PKey::RSA.new(File.open(@key_path)))
      log_info("Generated heartbeat request:\n#{req.body}") if @verbose
      begin
        res = nil
        res = http.start { |http_each| http.request(req) }
      rescue => e
        log_error("Error sending the heartbeat: #{e.message}")
      end
      if !res.nil?
        log_info("Heartbeat response code: #{res.code}") if @verbose
        if res.code == "200"
          cert_apply_res = apply_certificate_update_endpoint(res.body)
          dsc_apply_res = apply_dsc_endpoint(res.body)
          if cert_apply_res.class != String
            return cert_apply_res
          elsif dsc_apply_res.class != String
            return dsc_apply_res
          else
            log_info("Heartbeat success")
            return 0
          end
        else
          log_error("Error sending the heartbeat. HTTP code #{res.code}")
          return HTTP_NON_200
        end
      else
        log_error("Error sending the heartbeat. No HTTP code")
        return ERROR_SENDING_HTTP
      end
    end
def heartbeat @load_config_return_code = load_config if @load_config_return_code != 0 log_error ( "Error loading configuration from #{@omsadmin_conf_path}" ) return @load_config_return_code end if @WORKSPACE_ID . nil? or @AGENT_GUID . nil? or @URL_TLD . nil? or @WORKSPACE_ID . empty? or @AGENT_GUID . empty? or @URL_TLD . empty? log_error ( "Missing required field from configuration file: #{@omsadmin_conf_path}" ) return MISSING_CONFIG elsif ! file_exists_nonempty ( @cert_path ) or ! file_exists_nonempty ( @key_path ) log_error ( "Certificates for heartbeat request do not exist" ) return MISSING_CERTS end begin body_hb_xml = AgentTopologyRequestHandler . new . handle_request ( @os_info , @omsadmin_conf_path , @AGENT_GUID , get_cert_server ( @cert_path ) , @pid_path , telemetry = true ) if ! xml_contains_telemetry ( body_hb_xml ) log_debug ( "No Telemetry data was appended to the heartbeat request" ) end rescue => e log_error ( "Error when appending Telemetry to Heartbeat: #{e.message}" ) end headers = { } req_date = Time . now . utc . strftime ( "%Y-%m-%dT%T.%N%:z" ) headers [ OMS :: CaseSensitiveString . new ( "x-ms-Date" ) ] = req_date headers [ "User-Agent" ] = get_user_agent headers [ OMS :: CaseSensitiveString . new ( "Accept-Language" ) ] = "en-US" req , http = form_post_request_and_http ( headers , "https://#{@WORKSPACE_ID}.oms.#{@URL_TLD}/" "AgentService.svc/LinuxAgentTopologyRequest" , body_hb_xml , OpenSSL :: X509 :: Certificate . new ( File . open ( @cert_path ) ) , OpenSSL :: PKey :: RSA . new ( File . open ( @key_path ) ) ) log_info ( "Generated heartbeat request:\n#{req.body}" ) if @verbose begin res = nil res = http . start { | http_each | http . request ( req ) } rescue => e log_error ( "Error sending the heartbeat: #{e.message}" ) end if ! res . nil? log_info ( "Heartbeat response code: #{res.code}" ) if @verbose if res . code == "200" cert_apply_res = apply_certificate_update_endpoint ( res . body ) dsc_apply_res = apply_dsc_endpoint ( res . body ) if cert_apply_res . class != String return cert_apply_res elsif dsc_apply_res . class != String return dsc_apply_res else log_info ( "Heartbeat success" ) return 0 end else log_error ( "Error sending the heartbeat. HTTP code #{res.code}" ) return HTTP_NON_200 end else log_error ( "Error sending the heartbeat. No HTTP code" ) return ERROR_SENDING_HTTP end end
Perform a heartbeat against the OMS endpoint
Perform a heartbeat against the OMS endpoint
# Reload config in case of updates since last heartbeat # Check necessary inputs # Generate the request body # Form headers # Form POST request and HTTP # Submit request