Class: LogStash::Outputs::Loganalytics

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/loganalytics.rb

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object



31
32
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
# 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

#registerObject

Raises:

  • (ArgumentError)


26
27
28
29
# 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