Skip to content

Commit

Permalink
(stream/lib) sc_event: add new event outputs options (#97)
Browse files Browse the repository at this point in the history
* add new output management options

* better pagerduty event format
  • Loading branch information
tanguyvda committed Mar 16, 2022
1 parent bc0360a commit 64cc9d9
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function EventQueue:format_accepted_event()
event_type = "host",
timestamp = self.sc_event.event.last_check,
host = self.sc_event.event.cache.host.name,
output = string.gsub(self.sc_event.event.output, "\n", " "),
output = self.sc_event.event.output,
status = self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
state = self.sc_event.event.state,
state_type = self.sc_event.event.state_type
Expand All @@ -151,7 +151,7 @@ function EventQueue:format_accepted_event()
status = self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
state = self.sc_event.event.state,
state_type = self.sc_event.event.state_type,
output = string.gsub(self.sc_event.event.output, "\n", " "),
output = self.sc_event.event.output,
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function EventQueue:format_host_status()
self.sc_event.event.formated_event = {
host = tostring(self.sc_event.event.cache.host.name),
state = self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
output = self.sc_common:ifnil_or_empty(string.match(string.gsub(self.sc_event.event.output, '\\', "_"), "^(.*)\n"), "no output"),
output = self.sc_common:ifnil_or_empty(string.gsub(self.sc_event.event.output, '\\', "_"), "no output"),
}
end

Expand All @@ -149,7 +149,7 @@ function EventQueue:format_service_status()
host = tostring(self.sc_event.event.cache.host.name),
service = tostring(self.sc_event.event.cache.service.description),
state = self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
output = self.sc_common:ifnil_or_empty(string.match(string.gsub(self.sc_event.event.output, '\\', "_"), "^(.*)\n"), "no output")
output = self.sc_common:ifnil_or_empty(string.gsub(self.sc_event.event.output, '\\', "_"), "no output")
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ function EventQueue:format_event_host()
pdy_custom_details['Hostseverity'] = host_severity
end

pdy_custom_details["Output"] = self.sc_common:ifnil_or_empty(event.output, "no output")

self.sc_event.event.formated_event = {
payload = {
summary = tostring(event.cache.host.name) .. ": " .. self.sc_common:ifnil_or_empty(string.match(event.output, "^(.*)\n"), 'no output'),
summary = tostring(event.cache.host.name) .. ": " .. self.sc_params.params.status_mapping[event.category][event.element][event.state],
timestamp = new_from_timestamp(event.last_update):rfc_3339(),
severity = self.state_to_severity_mapping[event.state].severity,
source = self.sc_params.params.pdy_source or tostring(event.cache.host.name),
Expand Down Expand Up @@ -261,9 +262,11 @@ function EventQueue:format_event_service()
pdy_custom_details["Serviceseverity"] = service_severity
end

pdy_custom_details["Output"] = self.sc_common:ifnil_or_empty(event.output, "no output")

self.sc_event.event.formated_event = {
payload = {
summary = tostring(event.cache.host.name) .. "/" .. tostring(event.cache.service.description) .. ": " .. self.sc_common:ifnil_or_empty(string.match(event.output, "^(.*)\n"), 'no output'),
summary = tostring(event.cache.host.name) .. "/" .. tostring(event.cache.service.description) .. ": " .. self.sc_params.params.status_mapping[event.category][event.element][event.state],
timestamp = new_from_timestamp(event.last_update):rfc_3339(),
severity = self.state_to_severity_mapping[event.state].severity,
source = self.sc_params.params.pdy_source or tostring(event.cache.host.name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function EventQueue:format_event_host()
EventType = "HOST",
Date = self.sc_macros:transform_date(self.sc_event.event.last_check),
Host = self.sc_event.event.cache.host.name,
Message = string.gsub(self.sc_event.event.output, "\n", " "),
Message = self.sc_event.event.output,
Status = self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
Title = "HOST ALERT:" .. self.sc_event.event.cache.host.name .. " is " .. self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
["X-S4-SourceSystem"] = self.sc_params.params.x_s4_source_system,
Expand All @@ -150,7 +150,7 @@ function EventQueue:format_event_service()
Date = self.sc_macros:transform_date(self.sc_event.event.last_check),
Host = self.sc_event.event.cache.host.name,
Service = self.sc_event.event.cache.service.description,
Message = string.gsub(self.sc_event.event.output, "\n", " "),
Message = self.sc_event.event.output,
Status = self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
Title = "SERVICE ALERT:" .. self.sc_event.event.cache.host.name .. "/" .. self.sc_event.event.cache.service.description .. " is " .. self.sc_params.params.status_mapping[self.sc_event.event.category][self.sc_event.event.element][self.sc_event.event.state],
["X-S4-SourceSystem"] = self.sc_params.params.x_s4_source_system,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function EventQueue:format_event_host()
state = self.sc_event.event.state,
state_type = self.sc_event.event.state_type,
hostname = self.sc_event.event.cache.host.name,
output = string.gsub(self.sc_event.event.output, "\n", ""),
output = self.sc_event.event.output,
}
end

Expand All @@ -140,7 +140,7 @@ function EventQueue:format_event_service()
state_type = self.sc_event.event.state_type,
hostname = self.sc_event.event.cache.host.name,
service_description = self.sc_event.event.cache.service.description,
output = string.gsub(self.sc_event.event.output, "\n", ""),
output = self.sc_event.event.output,
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ function ScEvent:is_valid_host_status_event()
return false
end

self:build_outputs()

return true
end

Expand Down Expand Up @@ -217,6 +219,8 @@ function ScEvent:is_valid_service_status_event()
return false
end

self:build_outputs()

return true
end

Expand Down Expand Up @@ -1099,6 +1103,28 @@ function ScEvent:is_valid_downtime_event_end()
return false
end

--- build_outputs: adds short_output and long_output entries in the event table. output entry will be equal to one or another depending on the use_longoutput param
function ScEvent:build_outputs()
self.event.long_output = self.event.output
self.event.long_output = self.event.output

-- no short output if there is no line break
local short_output = string.match(self.event.output, "^(.*)\n")
if short_output then
self.event.short_output = short_output
end

-- use shortoutput if it exists
if self.params.use_long_output == 0 and short_output then
self.event.output = short_output

-- replace line break if asked to and we are not already using a short output
elseif not short_output and self.params.remove_line_break_in_output == 1 then
self.event.output = string.gsub(self.event.output, "\n", self.params.output_line_break_replacement_character)
end

end

--- is_valid_storage: DEPRECATED method, use NEB category to get metric data instead
-- @return true (boolean)
function ScEvent:is_valid_storage_event()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function sc_params.new(common, logger)

-- event formatting parameters
format_file = "",
use_long_output = 1,
remove_line_break_in_output = 1,
output_line_break_replacement_character = " ",

-- time parameters
local_time_diff_from_utc = os.difftime(os.time(), os.time(os.date("!*t", os.time()))),
Expand Down Expand Up @@ -659,6 +662,9 @@ function ScParams:check_params()
self.params.allow_insecure_connection = self.common:number_to_boolean(self.common:check_boolean_number_option_syntax(self.params.allow_insecure_connection, 0))
self.params.logfile = self.common:ifnil_or_empty(self.params.logfile, "/var/log/centreon-broker/stream-connector.log")
self.params.log_level = self.common:ifnil_or_empty(self.params.log_level, 1)
self.params.use_long_output = self.common:check_boolean_number_option_syntax(self.params.use_longoutput, 1)
self.params.remove_line_break_in_output = self.common:check_boolean_number_option_syntax(self.params.remove_line_break_in_output, 1)
self.params.output_line_break_replacement_character = self.common:if_wrong_type(self.params.output_line_break_replacement_character, "string", " ")
end

--- get_kafka_params: retrieve the kafka parameters and store them the self.params.kafka table
Expand Down
12 changes: 12 additions & 0 deletions stream-connectors/modules/docs/sc_event.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
- [is_valid_downtime_event_end method](#is_valid_downtime_event_end-method)
- [is_valid_downtime_event_end: returns](#is_valid_downtime_event_end-returns)
- [is_valid_downtime_event_end: example](#is_valid_downtime_event_end-example)
- [build_outputs method](#build_outputs-method)
- [build_outputs: example](#build_outputs-example)
- [is_valid_storage_event method](#is_valid_storage_event-method)

## Introduction
Expand Down Expand Up @@ -1072,6 +1074,16 @@ local result = test_event:is_valid_downtime_event_end()
--> result is true or false
```

## build_outputs method

The **build_outputs** method adds short_output and long_output entries in the event table. output entry will be equal to one or another depending on the [**use_long_output parameter](sc_param.md#default-parameters).

### build_outputs: example

```lua
local result = test_event:build_outputs()
```

## is_valid_storage_event method

**DEPRECATED** does nothing
Loading

0 comments on commit 64cc9d9

Please sign in to comment.