class Fluent::Plugin::CalyptiaAPI::Requester

Public Class Methods

new(endpoint, api_key, log, worker_id) click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 28
def initialize(endpoint, api_key, log, worker_id)
  @endpoint = endpoint
  @api_key = api_key
  @log = log
  @worker_id = worker_id
  @machine_id = Fluent::Plugin::CalyptiaMonitoringMachineId.new(worker_id, log)
end

Public Instance Methods

add_metrics(metrics, agent_token, agent_id) click to toggle source

POST /v1/agents/:agent_id/metrics Authorization: X-Agent-Token

# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 110
def add_metrics(metrics, agent_token, agent_id)
  url = URI("#{@endpoint}/v1/agents/#{agent_id}/metrics")

  https = if proxy = proxies
            proxy_uri = URI.parse(proxy)
            Net::NTTP.new(uri.host, uri.port,
                          proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
          else
            Net::HTTP.new(url.host, url.port)
          end
  https.use_ssl = (url.scheme == "https")

  @log.debug "send adding agent metrics request"
  request = Net::HTTP::Post.new(url)
  request["X-Agent-Token"] = agent_token
  request["Content-Type"] = "application/x-msgpack"

  request.body = metrics

  response = https.request(request)
  return [response.code, Yajl.load(response.read_body)]
end
agent_metadata(current_config) click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 45
def agent_metadata(current_config)
  metadata = {
    "name" => Socket.gethostname,
    "type" => "fluentd",
    "rawConfig" => current_config,
    "version" => create_go_semver(Fluent::VERSION),
    "edition" => "community".freeze,
  }
  if system_config.workers.to_i > 1
    metadata["flags"] = ["number_of_workers=#{system_config.workers}", "worker_id=#{@worker_id}"]
  end
  metadata
end
create_agent(current_config) click to toggle source

POST /v1/agents Authorization: X-Project-Token

# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 61
def create_agent(current_config)
  url = URI("#{@endpoint}/v1/agents")

  https = if proxy = proxies
            proxy_uri = URI.parse(proxy)
            Net::NTTP.new(uri.host, uri.port,
                          proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
          else
            Net::HTTP.new(url.host, url.port)
          end
  https.use_ssl = (url.scheme == "https")
  machine_id = @machine_id.id
  @log.debug "send creating agent request"
  request = Net::HTTP::Post.new(url)
  request["X-Project-Token"] = @api_key
  request["Content-Type"] = "application/json"
  request.body = Yajl.dump(agent_metadata(current_config).merge("machineID" => machine_id))
  response = https.request(request)
  agent = Yajl.load(response.read_body)
  return [response.code, agent, machine_id]
end
create_go_semver(version) click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 40
def create_go_semver(version)
  version.gsub(/.(?<prever>(rc|alpha|beta|pre))/,
               '-\k<prever>')
end
proxies() click to toggle source
# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 36
def proxies
  ENV['HTTPS_PROXY'] || ENV['HTTP_PROXY'] || ENV['http_proxy'] || ENV['https_proxy']
end
update_agent(current_config, agent, machine_id) click to toggle source

PATCH /v1/agents/:agent_id Authorization: X-Agent-Token

# File lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb, line 85
def update_agent(current_config, agent, machine_id)
  url = URI("#{@endpoint}/v1/agents/#{agent['id']}")

  https = if proxy = proxies
            proxy_uri = URI.parse(proxy)
            Net::NTTP.new(uri.host, uri.port,
                          proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
          else
            Net::HTTP.new(url.host, url.port)
          end
  https.use_ssl = (url.scheme == "https")

  @log.debug "send updating agent request"
  request = Net::HTTP::Patch.new(url)
  request["X-Agent-Token"] = agent['token']
  request["Content-Type"] = "application/json"

  request.body = Yajl.dump(agent_metadata(current_config).merge("machineID" => machine_id))
  response = https.request(request)
  body = response.read_body
  return [response.code, body]
end