class CloudflareAccess

Constants

DEFAULT_FIELDS

Attributes

auth_email[RW]
auth_key[RW]
domain[RW]
fields[RW]
metadata_file[RW]

Public Class Methods

new(arguments) click to toggle source
# File lib/logstash/inputs/cloudflare-logs.rb, line 14
def initialize(arguments)
  @auth_email = arguments[:auth_email]
  @auth_key = arguments[:auth_key]
  @domain = arguments[:domain]
  @fields = arguments[:fields] || DEFAULT_FIELDS
  @metadata_file = arguments[:metadata_file]
end

Public Instance Methods

end_time() click to toggle source
# File lib/logstash/inputs/cloudflare-logs.rb, line 31
def end_time
  @end_time ||= (Time.now - (5 * 60)).to_datetime.rfc3339
end
logs() click to toggle source
# File lib/logstash/inputs/cloudflare-logs.rb, line 51
def logs
  results = RestClient.get("https://api.cloudflare.com/client/v4/zones/#{domain}/logs/received?start=#{start_time}&end=#{end_time}&fields=#{fields.join(',')}", 'X-Auth-Email' => auth_email, 'X-Auth-Key' => auth_key)
  results.body.split("\n").collect { |raw_log| JSON.parse(raw_log) }
end
start_time() click to toggle source
# File lib/logstash/inputs/cloudflare-logs.rb, line 22
def start_time
  if File.exist?(metadata_file)
    start_time = Time.parse(JSON.parse(File.read(metadata_file))['start_time']).to_datetime.rfc3339
  else
    start_time = (Time.now - (15 * 60)).to_datetime.rfc3339
  end
  start_time
end
update_metadata_file(key, value) click to toggle source
# File lib/logstash/inputs/cloudflare-logs.rb, line 35
def update_metadata_file(key, value)
  key = key.to_s
  meta_data = {}

  if File.exist?(metadata_file)
    begin
      meta_data = JSON.parse(File.read(metadata_file))
    rescue
    end
  end

  meta_data[key] = value

  File.open(metadata_file, 'w+') { |file| file.write(meta_data.to_json) }
end