Skip to content

Commit

Permalink
Fix splunk (#62)
Browse files Browse the repository at this point in the history
* add rockspec files for 1.3.0 release

* fix options in stream connector

* add number to boolean method

* handle basic connection parameters

* add rockspec file for 1.4.0 release
  • Loading branch information
tanguyvda authored and kduret committed Feb 14, 2023
1 parent 0757686 commit a8629e4
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ function EventQueue:new (params)
self.sc_params.params.client_secret = params.client_secret
self.sc_params.params.username = params.username
self.sc_params.params.password = params.password
self.sc_params.params.proxy_address = params.proxy_address or ''
self.sc_params.params.proxy_port = params.proxy_port or ''
self.sc_params.params.proxy_username = params.proxy_username or ''
self.sc_params.params.proxy_password = params.proxy_password or ''

-- checking mandatory parameters and setting a fail flag
if not self.sc_params:is_mandatory_config_set(mandatory_parameters, params) then
Expand Down Expand Up @@ -200,6 +196,7 @@ function EventQueue:call (url, method, data, authToken)
:setopt_writefunction(function (response)
res = res .. tostring(response)
end)
:setopt(curl.OPT_TIMEOUT, self.sc_params.params.connection_timeout)

self.sc_logger:debug("EventQueue:call: Request initialize")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function EventQueue.new(params)

local mandatory_parameters = {
"http_server_url",
"splunk_token",
"splunk_index"
"splunk_token"
}

self.fail = false
Expand All @@ -54,11 +53,8 @@ function EventQueue.new(params)
end

-- overriding default parameters for this stream connector if the default values doesn't suit the basic needs
self.sc_params.params.proxy_address = params.proxy_address
self.sc_params.params.proxy_port = params.proxy_port
self.sc_params.params.proxy_username = params.proxy_username
self.sc_params.params.proxy_password = params.proxy_password
self.sc_params.params.splunk_source = params.splunk_source
self.sc_params.params.splunk_index = params.splunk_index or ""
self.sc_params.params.splunk_source = params.splunk_source or ""
self.sc_params.params.splunk_sourcetype = params.splunk_sourcetype or "_json"
self.sc_params.params.splunk_host = params.splunk_host or "Central"
self.sc_params.params.accepted_categories = params.accepted_categories or "neb"
Expand Down Expand Up @@ -196,7 +192,8 @@ function EventQueue:send_data(data, element)
http_response_body = http_response_body .. tostring(response)
end
)
:setopt(curl.OPT_TIMEOUT, self.http_timeout)
: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,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ function EventQueue.new(params)

local mandatory_parameters = {
"http_server_url",
"splunk_token",
"splunk_index"
"splunk_token"
}

self.fail = false
Expand All @@ -52,15 +51,12 @@ function EventQueue.new(params)
end

-- overriding default parameters for this stream connector if the default values doesn't suit the basic needs
self.sc_params.params.proxy_address = params.proxy_address
self.sc_params.params.proxy_port = params.proxy_port
self.sc_params.params.proxy_username = params.proxy_username
self.sc_params.params.proxy_password = params.proxy_password
self.sc_params.params.splunk_source = params.splunk_source
self.sc_params.params.splunk_index = params.splunk_index or ""
self.sc_params.params.splunk_source = params.splunk_source or ""
self.sc_params.params.splunk_sourcetype = params.splunk_sourcetype or "_json"
self.sc_params.params.splunk_host = params.splunk_host or "Central"
self.sc_params.params.accetepd_categories = params.accepted_categories or "neb"
self.sc_params.params.accetepd_elements = params.accepted_elements or "host_status,service_status"
self.sc_params.params.accepted_categories = params.accepted_categories or "neb"
self.sc_params.params.accepted_elements = params.accepted_elements or "host_status,service_status"
self.sc_params.params.hard_only = params.hard_only or 0
self.sc_params.params.enable_host_status_dedup = params.enable_host_status_dedup or 0
self.sc_params.params.enable_service_status_dedup = params.enable_service_status_dedup or 0
Expand Down Expand Up @@ -192,7 +188,8 @@ function EventQueue:send_data(data, element)
http_response_body = http_response_body .. tostring(response)
end
)
:setopt(curl.OPT_TIMEOUT, self.http_timeout)
: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,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ function ScCommon:boolean_to_number(boolean)
return boolean and 1 or 0
end

--- number_to_boolean: convert a 0, 1 number to its boolean counterpart
-- @param number (number) the number to convert
-- @return (boolean) true if param is 1, false if param is 0
function ScCommon:number_to_boolean(number)
if number ~= 0 and number ~= 1 then
self.sc_logger:error("[sc_common:number_to_boolean]: number is not 1 or 0. Returning nil. Parameter value is: " .. tostring(number))
return nil
end

if number == 1 then
return true
end

return false
end


--- check_boolean_number_option_syntax: make sure the number is either 1 or 0
-- @param number (number) the boolean number that must be validated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ function sc_params.new(common, logger)
max_buffer_size = 1,
max_buffer_age = 5,

-- connection parameters
connection_timeout = 60,
allow_insecure_connection = 0,

-- proxy parameters
proxy_address = "",
proxy_port = "",
proxy_username = "",
proxy_password = "",

-- event formatting parameters
format_file = "",

Expand Down Expand Up @@ -624,6 +634,12 @@ function ScParams:check_params()
self.params.enable_host_status_dedup = self.common:check_boolean_number_option_syntax(self.params.enable_host_status_dedup, 0)
self.params.enable_service_status_dedup = self.common:check_boolean_number_option_syntax(self.params.enable_service_status_dedup, 0)
self.params.send_data_test = self.common:check_boolean_number_option_syntax(self.params.send_data_test, 0)
self.params.proxy_address = self.common:if_wrong_type(self.params.proxy_address, "string", "")
self.params.proxy_address = self.common:if_wrong_type(self.params.proxy_port, "number", "")
self.params.proxy_username = self.common:if_wrong_type(self.params.proxy_username, "string", "")
self.params.proxy_password = self.common:if_wrong_type(self.params.proxy_password, "string", "")
self.params.connection_timeout = self.common:if_wrong_type(self.params.connection_timeout, "number", 60)
self.params.allow_insecure_connection = self.common:number_to_boolean(self.common:check_boolean_number_option_syntax(self.params.allow_insecure_connection, 0))
end

--- get_kafka_params: retrieve the kafka parameters and store them the self.params.kafka table
Expand Down
1 change: 1 addition & 0 deletions stream-connectors/modules/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
| ifnil_or_empty | check if a variable is empty or nil and replace it with a default value if it is the case | [Documentation](sc_common.md#ifnil_or_empty-method) |
| if_wrong_type | check the type of a variable, if it is wrong, replace the variable with a default value | [Documentation](sc_common.md#if_wrong_type-method) |
| boolean_to_number | change a true/false boolean to a 1/0 value | [Documentation](sc_common.md#boolean_to_number-method) |
| number_to_boolean | change a 0/1 number to a false/true value | [Documentation](sc_common.md#number_to_boolean-method) |
| check_boolean_number_option_syntax | make sure that a boolean is 0 or 1, if that's not the case, replace it with a default value | [Documentation](sc_common.md#check_boolean_number_option_syntax-method) |
| split | split a string using a separator (default is ",") and store each part in a table | [Documentation](sc_common.md#split-method) |
| compare_numbers | compare two numbers using the given mathematical operator and return true or false | [Documentation](sc_common.md#compare_numbers-method) |
Expand Down
30 changes: 30 additions & 0 deletions stream-connectors/modules/docs/sc_common.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- [boolean_to_number: parameters](#boolean_to_number-parameters)
- [boolean_to_number: returns](#boolean_to_number-returns)
- [boolean_to_number: example](#boolean_to_number-example)
- [number_to_boolean method](#number_to_boolean-method)
- [number_to_boolean: parameters](#number_to_boolean-parameters)
- [number_to_boolean: returns](#number_to_boolean-returns)
- [number_to_boolean: example](#number_to_boolean-example)
- [check_boolean_number_option_syntax method](#check_boolean_number_option_syntax-method)
- [check_boolean_number_option_syntax: parameters](#check_boolean_number_option_syntax-parameters)
- [check_boolean_number_option_syntax: returns](#check_boolean_number_option_syntax-returns)
Expand Down Expand Up @@ -163,6 +167,32 @@ local result = test_common:boolean_to_number(my_boolean)
--> result is 1
```

## number_to_boolean method

The **number_to_boolean** method converts a number to its boolean equivalent.

### number_to_boolean: parameters

| parameter | type | optional | default value |
| ----------------- | ------ | -------- | ------------- |
| a number (0 or 1) | number | no | |

### number_to_boolean: returns

| return | type | always | condition |
| ------------------------- | ------- | ------ | -------------------------- |
| a boolean (true or false) | boolean | no | if parameter is 0 or 1 |
| nil | nil | no | if parameter is not 0 or 1 |

### number_to_boolean: example

```lua
local my_number = 1

local result = test_common:number_to_boolean(my_number)
--> result is true
```

## check_boolean_number_option_syntax method

The **check_boolean_number_option_syntax** method checks if the first paramter is a boolean number (0 or 1) and if that is not the case, returns the second parameter
Expand Down
5 changes: 5 additions & 0 deletions stream-connectors/modules/docs/sc_param.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ The sc_param module provides methods to help you handle parameters for your stre
| timestamp_conversion_format | string | %Y-%m-%d %X | | the date format used to convert timestamps. Default value will print dates like this: 2021-06-11 10:43:38 | all | (date format information)[https://www.lua.org/pil/22.1.html] |
| send_data_test | number | 0 | | When set to 1, send data in the logfile of the stream connector instead of sending it where the stream connector was designed to | all | |
| format_file | string | | | Path to a file that will be used as a template to format events instead of using default format | only usable for events stream connectors (\*-events-apiv2.lua) and not metrics stream connectors (\*-metrics-apiv2.lua) you should put the file in /etc/centreon-broker to keep your broker configuration in a single place. [**See documentation for more information**](templating.md) |
| proxy_address | string | | | address of the proxy | |
| proxy_port | number | | | port of the proxy | |
| proxy_username | string | | | user for the proxy | |
| proxy_password | string | | | pasword of the proxy user | |
| connection_timeout | number | 60 | | time to wait in second when opening connection | |

## Module initialization

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package = "centreon-stream-connectors-lib"
version = "1.4.0-1"
source = {
url = "git+https://github.com/centreon/centreon-stream-connector-scripts",
tag = "1.4.0-1"
}
description = {
summary = "Centreon stream connectors lua modules",
detailed = [[
Those modules provides helpful methods to create
stream connectors for Centreon
]],
license = ""
}
dependencies = {
"lua >= 5.1, < 5.4",
"luasocket >= 3.0rc1-2"
}
build = {
type = "builtin",
modules = {
["centreon-stream-connectors-lib.sc_broker"] = "modules/centreon-stream-connectors-lib/sc_broker.lua",
["centreon-stream-connectors-lib.sc_common"] = "modules/centreon-stream-connectors-lib/sc_common.lua",
["centreon-stream-connectors-lib.sc_event"] = "modules/centreon-stream-connectors-lib/sc_event.lua",
["centreon-stream-connectors-lib.sc_logger"] = "modules/centreon-stream-connectors-lib/sc_logger.lua",
["centreon-stream-connectors-lib.sc_params"] = "modules/centreon-stream-connectors-lib/sc_params.lua",
["centreon-stream-connectors-lib.sc_test"] = "modules/centreon-stream-connectors-lib/sc_test.lua",
["centreon-stream-connectors-lib.sc_macros"] = "modules/centreon-stream-connectors-lib/sc_macros.lua",
["centreon-stream-connectors-lib.sc_flush"] = "modules/centreon-stream-connectors-lib/sc_flush.lua",
["centreon-stream-connectors-lib.sc_metrics"] = "modules/centreon-stream-connectors-lib/sc_metrics.lua",
["centreon-stream-connectors-lib.rdkafka.config"] = "modules/centreon-stream-connectors-lib/rdkafka/config.lua",
["centreon-stream-connectors-lib.rdkafka.librdkafka"] = "modules/centreon-stream-connectors-lib/rdkafka/librdkafka.lua",
["centreon-stream-connectors-lib.rdkafka.producer"] = "modules/centreon-stream-connectors-lib/rdkafka/producer.lua",
["centreon-stream-connectors-lib.rdkafka.topic_config"] = "modules/centreon-stream-connectors-lib/rdkafka/topic_config.lua",
["centreon-stream-connectors-lib.rdkafka.topic"] = "modules/centreon-stream-connectors-lib/rdkafka/topic.lua",
["centreon-stream-connectors-lib.google.auth.oauth"] = "modules/centreon-stream-connectors-lib/google/auth/oauth.lua",
["centreon-stream-connectors-lib.google.bigquery.bigquery"] = "modules/centreon-stream-connectors-lib/google/bigquery/bigquery.lua"
}
}

0 comments on commit a8629e4

Please sign in to comment.