Skip to content

Commit

Permalink
add a new method to create shell curl commands (#130)
Browse files Browse the repository at this point in the history
* add get_curl_command method

* add proxy_protocol param

* move get_curl_command from sc_common to sc_logger

* add log_curl_commands param

* document new parameters

* add log_curl_command documentation

* encapsulate strings in log_curl_command

* add new log_curl_command method in the sc

* fix bugs with log curl command

* log curl commands for all possible sc

* fix log message

* fix bad proxy verif

* add 3.6.0 specifle
  • Loading branch information
tanguyvda authored Jan 31, 2023
1 parent 26dd594 commit bd9d19a
Show file tree
Hide file tree
Showing 21 changed files with 338 additions and 187 deletions.
29 changes: 11 additions & 18 deletions centreon-certified/capensis/canopsis2-events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function EventQueue:add()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event

self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

end
Expand All @@ -375,15 +375,21 @@ function EventQueue:send_data(payload, queue_metadata)
local params = self.sc_params.params
local url = params.sending_protocol .. "://" .. params.canopsis_user .. ":" .. params.canopsis_password
.. "@" .. params.canopsis_host .. ':' .. params.canopsis_port .. queue_metadata.event_route
local data = broker.json_encode(payload)
payload = broker.json_encode(payload)
queue_metadata.headers = {
"content-length: " .. string.len(payload),
"content-type: application/json"
}

self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload)

-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
self.sc_logger:notice("[send_data]: " .. tostring(data))
return true
end

self.sc_logger:info("[EventQueue:send_data]: Going to send the following json " .. data)
self.sc_logger:info("[EventQueue:send_data]: Going to send the following json " .. payload)
self.sc_logger:info("[EventQueue:send_data]: Canopsis address is: " .. tostring(url))

local http_response_body = ""
Expand All @@ -396,13 +402,7 @@ function EventQueue:send_data(payload, queue_metadata)
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(
curl.OPT_HTTPHEADER,
{
"content-length: " .. string.len(data),
"content-type: application/json"
}
)
:setopt(curl.OPT_HTTPHEADER, queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
Expand All @@ -427,14 +427,7 @@ function EventQueue:send_data(payload, queue_metadata)
if queue_metadata.method and queue_metadata.method == "DELETE" then
http_request:setopt(curl.OPT_CUSTOMREQUEST, queue_metadata.method)
else
http_request:setopt(
curl.OPT_HTTPHEADER,
{
"content-length: " .. string.len(data),
"content-type: application/json"
}
)
http_request:setopt_postfields(data)
http_request:setopt_postfields(payload)
end

-- performing the HTTP request
Expand Down
35 changes: 14 additions & 21 deletions centreon-certified/capensis/canopsis4-events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ function EventQueue:add()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event

self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end
end

Expand All @@ -380,15 +380,22 @@ function EventQueue:send_data(payload, queue_metadata)

local params = self.sc_params.params
local url = params.sending_protocol .. "://" .. params.canopsis_host .. ':' .. params.canopsis_port .. queue_metadata.event_route
local data = broker.json_encode(payload)

payload = broker.json_encode(payload)
queue_metadata.headers = {
"content-length: " .. string.len(payload),
"content-type: application/json",
"x-canopsis-authkey: " .. tostring(self.sc_params.params.canopsis_authkey)
}

self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload)

-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
self.sc_logger:notice("[send_data]: " .. tostring(data))
self.sc_logger:notice("[send_data]: " .. tostring(payload))
return true
end

self.sc_logger:info("[EventQueue:send_data]: Going to send the following json " .. data)
self.sc_logger:info("[EventQueue:send_data]: Going to send the following json " .. payload)
self.sc_logger:info("[EventQueue:send_data]: Canopsis address is: " .. tostring(url))

local http_response_body = ""
Expand All @@ -401,14 +408,7 @@ function EventQueue:send_data(payload, queue_metadata)
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(
curl.OPT_HTTPHEADER,
{
"content-length: " .. string.len(data),
"content-type: application/json",
"x-canopsis-authkey: " .. tostring(self.sc_params.params.canopsis_authkey)
}
)
:setopt(curl.OPT_HTTPHEADER, queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
Expand All @@ -434,14 +434,7 @@ function EventQueue:send_data(payload, queue_metadata)
http_request:setopt(curl.OPT_CUSTOMREQUEST, queue_metadata.method)
end

http_request:setopt(
curl.OPT_HTTPHEADER,
{
"content-length: " .. string.len(data),
"content-type: application/json"
}
)
http_request:setopt_postfields(data)
http_request:setopt_postfields(payload)

-- performing the HTTP request
http_request:perform()
Expand Down
15 changes: 7 additions & 8 deletions centreon-certified/datadog/datadog-events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function EventQueue:add()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event

self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

--------------------------------------------------------------------------------
Expand All @@ -213,7 +213,12 @@ function EventQueue:send_data(payload, queue_metadata)
self.sc_logger:debug("[EventQueue:send_data]: Starting to send data")

local url = self.sc_params.params.http_server_url .. self.sc_params.params.datadog_event_endpoint
queue_metadata.headers = {
"content-type: application/json",
"DD-API-KEY:" .. self.sc_params.params.api_key
}

self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload)
-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
self.sc_logger:notice("[send_data]: " .. tostring(payload))
Expand All @@ -233,13 +238,7 @@ function EventQueue:send_data(payload, queue_metadata)
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(
curl.OPT_HTTPHEADER,
{
"content-type: application/json",
"DD-API-KEY:" .. self.sc_params.params.api_key
}
)
:setopt(curl.OPT_HTTPHEADER, queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
Expand Down
16 changes: 8 additions & 8 deletions centreon-certified/datadog/datadog-metrics-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function EventQueue:add()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event

self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

--------------------------------------------------------------------------------
Expand All @@ -260,6 +260,12 @@ function EventQueue:send_data(payload, queue_metadata)

local url = self.sc_params.params.http_server_url .. tostring(self.sc_params.params.datadog_metric_endpoint)
local payload_json = broker.json_encode(payload)
queue_metadata.headers = {
"content-type: application/json",
"DD-API-KEY:" .. self.sc_params.params.api_key
}

self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload_json)

-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
Expand All @@ -280,13 +286,7 @@ function EventQueue:send_data(payload, queue_metadata)
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(
curl.OPT_HTTPHEADER,
{
"content-type: application/json",
"DD-API-KEY:" .. self.sc_params.params.api_key
}
)
:setopt(curl.OPT_HTTPHEADER,queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
Expand Down
43 changes: 23 additions & 20 deletions centreon-certified/elasticsearch/elastic-events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ function EventQueue:format_accepted_event()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event


self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

--------------------------------------------------------------------------------
Expand All @@ -200,6 +200,15 @@ function EventQueue:format_accepted_event()
function EventQueue:send_data(payload, queue_metadata)
self.sc_logger:debug("[EventQueue:send_data]: Starting to send data")

local url = self.sc_params.params.elastic_url .. "/_bulk"
queue_metadata.headers = {
"content-type: application/json;charset=UTF-8",
"content-length: " .. string.len(payload),
"Authorization: Basic " .. (mime.b64(self.sc_params.params.elastic_username .. ":" .. self.sc_params.params.elastic_password))
}

self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload)

-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
self.sc_logger:info("[send_data]: " .. tostring(payload))
Expand All @@ -209,24 +218,18 @@ function EventQueue:format_accepted_event()
self.sc_logger:info("[EventQueue:send_data]: Going to send the following json " .. tostring(payload))
self.sc_logger:info("[EventQueue:send_data]: Elastic URL is: " .. tostring(self.sc_params.params.elastic_url) .. "/_bulk")

local http_response_body = ""
local http_request = curl.easy()
:setopt_url(self.sc_params.params.elastic_url .. "/_bulk")
:setopt_writefunction(
function (response)
http_response_body = http_response_body .. tostring(response)
end
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(
curl.OPT_HTTPHEADER,
{
"content-type: application/json;charset=UTF-8",
"content-length: " .. string.len(payload),
"Authorization: Basic " .. (mime.b64(self.sc_params.params.elastic_username .. ":" .. self.sc_params.params.elastic_password))
}
)
local http_response_body = ""
local http_request = curl.easy()
:setopt_url(url)
:setopt_writefunction(
function (response)
http_response_body = http_response_body .. tostring(response)
end
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(curl.OPT_HTTPHEADER, queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
if (self.sc_params.params.proxy_port ~= '') then
Expand Down
4 changes: 2 additions & 2 deletions centreon-certified/kafka/kafka-events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ function EventQueue:add()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event


self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

--------------------------------------------------------------------------------
Expand Down
15 changes: 7 additions & 8 deletions centreon-certified/logstash/logstash-events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function EventQueue:add()
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event

self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

--------------------------------------------------------------------------------
Expand All @@ -193,6 +193,10 @@ end
function EventQueue:send_data(payload, queue_metadata)
self.sc_logger:debug("[EventQueue:send_data]: Starting to send data")

queue_metadata.headers = {"accept: application/json"}
queue_metadata.method = "PUT"
self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload)

-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
self.sc_logger:notice("[send_data]: " .. tostring(payload))
Expand All @@ -212,13 +216,8 @@ function EventQueue:send_data(payload, queue_metadata)
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(curl.OPT_CUSTOMREQUEST, "PUT")
:setopt(
curl.OPT_HTTPHEADER,
{
"accept: application/json"
}
)
:setopt(curl.OPT_CUSTOMREQUEST, queue_metadata.method)
:setopt(curl.OPT_HTTPHEADER, queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
Expand Down
24 changes: 13 additions & 11 deletions centreon-certified/omi/omi_events-apiv2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ function EventQueue:add()
self.sc_logger:debug("[EventQueue:add]: queue size before adding event: " .. tostring(#self.sc_flush.queues[category][element].events))
self.sc_flush.queues[category][element].events[#self.sc_flush.queues[category][element].events + 1] = self.sc_event.event.formated_event

self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. "max is: " .. tostring(self.sc_params.params.max_buffer_size))
self.sc_logger:info("[EventQueue:add]: queue size is now: " .. tostring(#self.sc_flush.queues[category][element].events)
.. ", max is: " .. tostring(self.sc_params.params.max_buffer_size))
end

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -229,32 +229,34 @@ end
function EventQueue:send_data(payload, queue_metadata)
self.sc_logger:debug("[EventQueue:send_data]: Starting to send data")

local url = self.sc_params.params.http_server_url
queue_metadata.headers = {
"Content-Type: text/xml",
"content-length: " .. string.len(payload)
}

self.sc_logger:log_curl_command(url, queue_metadata, self.sc_params.params, payload)

-- write payload in the logfile for test purpose
if self.sc_params.params.send_data_test == 1 then
self.sc_logger:notice("[send_data]: " .. tostring(payload))
return true
end

self.sc_logger:info("[EventQueue:send_data]: Going to send the following xml " .. tostring(payload))
self.sc_logger:info("[EventQueue:send_data]: BSM Http Server URL is: \"" .. tostring(self.sc_params.params.http_server_url .. "\""))
self.sc_logger:info("[EventQueue:send_data]: BSM Http Server URL is: \"" .. tostring(url) .. "\"")

local http_response_body = ""
local http_request = curl.easy()
:setopt_url(self.sc_params.params.http_server_url)
:setopt_url(url)
:setopt_writefunction(
function (response)
http_response_body = http_response_body .. tostring(response)
end
)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)
:setopt(curl.OPT_SSL_VERIFYPEER, self.sc_params.params.allow_insecure_connection)
:setopt(
curl.OPT_HTTPHEADER,
{
"Content-Type: text/xml",
"content-length: " .. string.len(payload)
}
)
:setopt(curl.OPT_HTTPHEADER,queue_metadata.headers)

-- set proxy address configuration
if (self.sc_params.params.proxy_address ~= '') then
Expand Down
Loading

0 comments on commit bd9d19a

Please sign in to comment.