Class: Fluent::Plugin::CalyptiaAPI::Requester

Inherits:
Object
  • Object
show all
Includes:
SystemConfig::Mixin
Defined in:
lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, api_key, log, worker_id) ⇒ Requester

Returns a new instance of Requester.



28
29
30
31
32
33
34
# 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

Instance Method Details

#add_metrics(metrics, agent_token, agent_id) ⇒ Object

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb', line 110

def add_metrics(metrics, agent_token, agent_id)
  url = URI("#{@endpoint}/v1/agent_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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/plugin/calyptia_monitoring_calyptia_api_requester.rb', line 45

def (current_config)
   = {
    "name" => Socket.gethostname,
    "type" => "fluentd",
    "rawConfig" => current_config,
    "version" => create_go_semver(Fluent::VERSION),
    "edition" => "community".freeze,
  }
  if system_config.workers.to_i > 1
    ["flags"] = ["number_of_workers=#{system_config.workers}", "worker_id=#{@worker_id}"]
  end
  
end

#create_agent(current_config) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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((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) ⇒ Object



40
41
42
43
# 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

#proxiesObject



36
37
38
# 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) ⇒ Object

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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((current_config).merge("machineID" => machine_id))
  response = https.request(request)
  body = response.read_body
  return [response.code, body]
end