Skip to content

Commit

Permalink
cmd_process_service_check_result uses of string::unscape instead of a…
Browse files Browse the repository at this point in the history
…bseil version (#1515)

REFS: MON51121
  • Loading branch information
jean-christophe81 authored Jul 22, 2024
1 parent 92e80e9 commit 7e1d4c7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
5 changes: 3 additions & 2 deletions engine/inc/com/centreon/engine/string.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <sstream>
#include <string>


namespace com::centreon::engine {

namespace string {
Expand Down Expand Up @@ -205,6 +204,8 @@ std::string& remove_thresholds(std::string& perfdata) noexcept;

void unescape(char* buffer);

void unescape(std::string& str);

/**
* @brief this class is a thread safe replacement for my_strtok
* An instance is not thread safe but sevaral instances can be used in different
Expand All @@ -226,6 +227,6 @@ class c_strtok {

} // namespace string

}
} // namespace com::centreon::engine

#endif // !CCE_MISC_STRING_HH
6 changes: 6 additions & 0 deletions engine/src/command_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ int command_manager::process_passive_service_check(
if (!found->second->passive_checks_enabled())
return ERROR;

SPDLOG_LOGGER_DEBUG(runtime_logger,
"process_passive_service_check check_time={}, "
"host_name={}, service={}, return_code={}, output={}",
check_time, host_name, svc_description, return_code,
output);

timeval tv;
gettimeofday(&tv, nullptr);

Expand Down
4 changes: 2 additions & 2 deletions engine/src/commands/commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ int cmd_process_service_check_result(int cmd [[maybe_unused]],
++ait;

// replace \\n with \n
std::string output;
absl::CUnescape(*ait, &output);
std::string output(ait->data(), ait->size());
string::unescape(output);

timeval tv;
gettimeofday(&tv, nullptr);
Expand Down
13 changes: 13 additions & 0 deletions engine/src/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,16 @@ void string::unescape(char* buffer) {
*buffer = 0;
}
}

/**
* @brief * @brief Unescape the string buffer. Works with \t, \n, \r and \\.
* The buffer is directly changed. No copy is made.
*
* @param str in out modified string
*/
void string::unescape(std::string& str) {
boost::replace_all(str, "\\n", "\n");
boost::replace_all(str, "\\r", "\r");
boost::replace_all(str, "\\t", "\t");
boost::replace_all(str, "\\\\", "\\");
}
39 changes: 39 additions & 0 deletions tests/broker-engine/external-commands2.robot
Original file line number Diff line number Diff line change
Expand Up @@ -1412,3 +1412,42 @@ BEHOSTCHECK
Ctn Schedule Forced Host Check host_1
${result} Ctn Check Host Check With Timeout host_1 30 ${VarRoot}/lib/centreon-engine/check.pl --id 0
Should Be True ${result} hosts table not updated


BE_BACKSLASH_CHECK_RESULT
[Documentation] external command PROCESS_SERVICE_CHECK_RESULT with \:
[Tags] broker engine services extcmd MON-51121
Ctn Config Engine ${1} ${50} ${20}
Ctn Set Services Passive ${0} service_.*
Ctn Config Broker rrd
Ctn Config Broker central
Ctn Config Broker module ${1}
Ctn ConfigBBDO3 1
Ctn Config Broker Sql Output central unified_sql
Ctn Clear Retention
Ctn Broker Config Log central sql debug
FOR ${use_grpc} IN RANGE 0 2
Log To Console external command PROCESS_SERVICE_CHECK_RESULT use_grpc=${use_grpc}
Ctn Clear Retention
${start} Get Current Date
Ctn Start Broker
Ctn Start engine
Ctn Wait For Engine To Be Ready ${start} ${1}

${start} Ctn Get Round Current Date
Ctn Process Service Check Result host_1 service_1 0 output ok D: \\: Total: 1.205TB - Used: 1.203TB (100%) - Free: 2.541GB (0%) ${use_grpc} config0 ${use_grpc}

${result} Ctn Check Service Output Resource Status With Timeout
... host_1
... service_1
... 35
... ${start}
... 0
... HARD
... output ok D: \\: Total: 1.205TB - Used: 1.203TB (100%) - Free: 2.541GB (0%) ${use_grpc}
Should Be True ${result} resources table not updated


Ctn Stop engine
Ctn Kindly Stop Broker
END
6 changes: 4 additions & 2 deletions tests/resources/Engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2823,17 +2823,19 @@ def ctn_process_service_check_result(hst: str, svc: str, state: int, output: str
0 on success.
"""
if use_grpc > 0:
ts = Timestamp()
ts.GetCurrentTime()
port = 50001 + int(config[6:])
with grpc.insecure_channel(f"127.0.0.1:{port}") as channel:
stub = engine_pb2_grpc.EngineStub(channel)
if nb_check > 1:
for i in range(nb_check):
indexed_output = f"{output}_{i}"
stub.ProcessServiceCheckResult(engine_pb2.Check(
host_name=hst, svc_desc=svc, output=indexed_output, code=state))
host_name=hst, svc_desc=svc, check_time=ts, output=indexed_output, code=state))
else:
stub.ProcessServiceCheckResult(engine_pb2.Check(
host_name=hst, svc_desc=svc, output=output, code=state))
host_name=hst, svc_desc=svc, check_time=ts, output=output, code=state))

else:
now = int(time.time())
Expand Down

0 comments on commit 7e1d4c7

Please sign in to comment.