class LogStash::Outputs::Loganalytics

Public Instance Methods

receive(event) click to toggle source
# File lib/logstash/outputs/loganalytics.rb, line 31
def receive(event)
  begin
    body =  event.to_json
    #puts body
    date = Time.now.httpdate
    #puts date
    string_to_hash = "POST\n#{body.bytesize}\napplication/json\nx-ms-date:#{date}\n/api/logs"
    #puts string_to_hash
    hashed_string = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), Base64.decode64(@client_secret), string_to_hash.encode('utf-8')))
    #puts hashed_string
    headers = {}
    headers['Content-Type'] = 'application/json'
    headers['Authorization'] = "SharedKey #{@client_id}:#{hashed_string}"
    headers['Log-Type'] = @table
    headers['x-ms-date'] = date
    unless @time_generated_field.empty?
      headers['time-generated-field'] = @time_generated_field
    end
    response = RestClient.post(@uri, body, headers)
    #puts response.code
    unless response.code == 200
      #puts "DataCollector API request failure: error code: #{response.code}, data=>#{event}"
      @logger.error("DataCollector API request failure: error code: #{response.code}, data=>#{event}")
    end
  rescue Exception => ex
    #puts "Exception occured in posting to DataCollector API: '#{ex}', data=>#{event}"
    @logger.error("Exception occured in posting to DataCollector API: '#{ex}', data=>#{event}")
  end
end
register() click to toggle source
# File lib/logstash/outputs/loganalytics.rb, line 26
def register
      raise ArgumentError, 'log type must be only alpha characters and less than 100 characters' unless @table.match(/^[a-zA-Z]{1,100}$/)
  @uri = "https://#{@client_id}.ods.opinsights.azure.com/api/logs?api-version=2016-04-01"
end