diff --git a/Providers/Modules/Plugins/Common/plugin/oms_common.rb b/Providers/Modules/Plugins/Common/plugin/oms_common.rb index 2518c1c02..91df4dd55 100644 --- a/Providers/Modules/Plugins/Common/plugin/oms_common.rb +++ b/Providers/Modules/Plugins/Common/plugin/oms_common.rb @@ -12,6 +12,8 @@ class Common require 'time' require 'zlib' require 'digest' + require 'date' + require 'securerandom' require_relative 'omslog' require_relative 'oms_configuration' @@ -475,10 +477,14 @@ class Common @@tzRightFolder = 'right/' class << self + # get the unified timezone id by absolute file path of the timezone file + # file path: the absolute path of the file def get_unified_timezoneid(filepath) # remove the baseFolder path tzID = filepath[@@tzBaseFolder.length..-1] if filepath.start_with?(@@tzBaseFolder) + return 'Unknown' if tzID.nil? + # if the rest starts with 'right/', remove it to unify the format tzID = tzID[@@tzRightFolder.length..-1] if tzID.start_with?(@@tzRightFolder) @@ -493,7 +499,7 @@ def get_current_timezone begin # if /etc/localtime is a symlink, check the link file's path if File.symlink?(@@tzLocalTimePath) - symlinkpath = File.readlink(@@tzLocalTimePath) + symlinkpath = File.absolute_path(File.readlink(@@tzLocalTimePath), File.dirname(@@tzLocalTimePath)) tzID = get_unified_timezoneid(symlinkpath) # look for the entry in the timezone mapping @@ -672,6 +678,23 @@ def create_ods_http(ods_uri, proxy={}) def create_ods_request(path, record, compress, extra_headers=nil, serializer=method(:parse_json_record_encoding)) headers = extra_headers.nil? ? {} : extra_headers + azure_resource_id = OMS::Configuration.azure_resource_id + if !azure_resource_id.to_s.empty? + headers[OMS::CaseSensitiveString.new("x-ms-AzureResourceId")] = azure_resource_id + end + + omscloud_id = OMS::Configuration.omscloud_id + if !omscloud_id.to_s.empty? + headers[OMS::CaseSensitiveString.new("x-ms-OMSCloudId")] = omscloud_id + end + + uuid = OMS::Configuration.uuid + if !uuid.to_s.empty? + headers[OMS::CaseSensitiveString.new("x-ms-UUID")] = uuid + end + + headers[OMS::CaseSensitiveString.new("X-Request-ID")] = SecureRandom.uuid + headers["Content-Type"] = "application/json" if compress == true headers["Content-Encoding"] = "deflate" @@ -793,7 +816,8 @@ def start_request(req, secure_http, ignore404 = false) if res.code != "200" # Retry all failure error codes... - res_summary = "(class=#{res.class.name}; code=#{res.code}; message=#{res.message}; body=#{res.body};)" + res_summary = "(request-id=#{req["X-Request-ID"]}; class=#{res.class.name}; code=#{res.code}; message=#{res.message}; body=#{res.body};)" + Log.error_once("HTTP Error: #{res_summary}") raise RetryRequestException, "HTTP error: #{res_summary}" end @@ -830,7 +854,8 @@ def get_ip(hostname) def get_ip_from_socket(hostname) begin addrinfos = Socket::getaddrinfo(hostname, "echo", Socket::AF_UNSPEC) - rescue => e + rescue => error + Log.error_once("Unable to resolve the IP of '#{hostname}': #{error}") return nil end diff --git a/Providers/Modules/Plugins/Common/plugin/oms_configuration.rb b/Providers/Modules/Plugins/Common/plugin/oms_configuration.rb index 689527984..8917ca5d2 100644 --- a/Providers/Modules/Plugins/Common/plugin/oms_configuration.rb +++ b/Providers/Modules/Plugins/Common/plugin/oms_configuration.rb @@ -15,7 +15,10 @@ class Configuration @@ODSEndpoint = nil @@GetBlobODSEndpoint = nil @@NotifyBlobODSEndpoint = nil - + @@OmsCloudId = nil + @@AzureResourceId = nil + @@UUID = nil + class << self # test the onboard file existence @@ -106,6 +109,18 @@ def load_configuration(conf_path, cert_path, key_path) return false end + File.open(conf_path).each_line do |line| + if line =~ /AZURE_RESOURCE_ID/ + @@AzureResourceId = line.sub("AZURE_RESOURCE_ID=","").strip + end + if line =~ /OMSCLOUD_ID/ + @@OmsCloudId = line.sub("OMSCLOUD_ID=","").strip + end + if line =~ /UUID/ + @@UUID = line.sub("UUID=","").strip + end + end + begin raw = File.read cert_path @@Cert = OpenSSL::X509::Certificate.new raw @@ -144,6 +159,18 @@ def notify_blob_ods_endpoint @@NotifyBlobODSEndpoint end # getter notify_blob_ods_endpoint + def azure_resource_id + @@AzureResourceId + end + + def omscloud_id + @@OmsCloudId + end + + def uuid + @@UUID + end #getter for VM uuid + end # Class methods end # class Common diff --git a/Providers/Modules/Plugins/Common/plugin/out_oms_blob.rb b/Providers/Modules/Plugins/Common/plugin/out_oms_blob.rb index bf007e57e..94a4e2255 100644 --- a/Providers/Modules/Plugins/Common/plugin/out_oms_blob.rb +++ b/Providers/Modules/Plugins/Common/plugin/out_oms_blob.rb @@ -68,7 +68,7 @@ def create_blob_get_request(uri) # file_path: string. file path # returns: # HTTPRequest. blob PUT request - def create_blob_put_request(uri, msg, file_path = nil) + def create_blob_put_request(uri, msg, request_id, file_path = nil) headers = {} headers[OMS::CaseSensitiveString.new("x-ms-meta-TimeZoneid")] = OMS::Common.get_current_timezone @@ -76,16 +76,37 @@ def create_blob_put_request(uri, msg, file_path = nil) if !file_path.nil? headers[OMS::CaseSensitiveString.new("x-ms-meta-FilePath")] = file_path end + + azure_resource_id = OMS::Configuration.azure_resource_id + if !azure_resource_id.to_s.empty? + headers[OMS::CaseSensitiveString.new("x-ms-AzureResourceId")] = azure_resource_id + end + + omscloud_id = OMS::Configuration.omscloud_id + if !omscloud_id.to_s.empty? + headers[OMS::CaseSensitiveString.new("x-ms-OMSCloudId")] = omscloud_id + end + + uuid = OMS::Configuration.uuid + if !uuid.to_s.empty? + headers[OMS::CaseSensitiveString.new("x-ms-UUID")] = uuid + end + + headers[OMS::CaseSensitiveString.new("X-Request-ID")] = request_id + headers["Content-Type"] = "application/octet-stream" headers["Content-Length"] = msg.bytesize.to_s req = Net::HTTP::Put.new(uri.request_uri, headers) req.body = msg return req + rescue OMS::RetryRequestException => e + Log.error_once("HTTP error for Request-ID: #{request_id} Error: #{e}") + raise e.message, "Request-ID: #{request_id}" end # create_blob_put_request # get the blob SAS URI from ODS - # parameters: + # parameters # container_type: string. ContainerType of the data # data_type: string. DataTypeId of the data # custom_data_type: string. CustomDataType of the CustomLog @@ -181,9 +202,10 @@ def get_committed_blocks(uri) # string. block id def upload_block(uri, msg) base64_blockid = Base64.encode64(SecureRandom.uuid) + request_id = SecureRandom.uuid append_uri = URI.parse("#{uri.to_s}&comp=block&blockid=#{base64_blockid}") - put_block_req = create_blob_put_request(append_uri, msg, nil) + put_block_req = create_blob_put_request(append_uri, msg, request_id, nil) http = OMS::Common.create_secure_http(append_uri, @proxy_config) OMS::Common.start_request(put_block_req, http) @@ -205,7 +227,8 @@ def commit_blocks(uri, blocks_committed, blocks_uncommitted, file_path) commit_msg = doc.to_s blocklist_uri = URI.parse("#{uri.to_s}&comp=blocklist") - put_blocklist_req = create_blob_put_request(blocklist_uri, commit_msg, file_path) + request_id = SecureRandom.uuid + put_blocklist_req = create_blob_put_request(blocklist_uri, commit_msg, request_id, file_path) http = OMS::Common.create_secure_http(blocklist_uri, @proxy_config) OMS::Common.start_request(put_blocklist_req, http) end # commit_blocks