Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 58 additions & 16 deletions samples/base/AlertManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ namespace darwin {
bool redis_status = false;
bool logs_status = false;

_redis = configuration.HasMember("redis_socket_path");
_redis = configuration.HasMember("redis_socket_path") or configuration.HasMember("redis_ip");
_log = configuration.HasMember("log_file_path");

if (!_redis and !_log){
DARWIN_LOG_WARNING("AlertManager:: Need at least one of these parameters to raise alerts: "
"'redis_socket_path' or 'log_file_path'");
"'redis_socket_path', 'redis_ip' or 'log_file_path'");
return false;
}

Expand Down Expand Up @@ -65,27 +65,42 @@ namespace darwin {

bool AlertManager::LoadRedisConfig(const rapidjson::Document& configuration) {
DARWIN_LOGGER;
std::string redis_socket_path;
std::string redis_socket_path, redis_ip;
unsigned int redis_port = 6379;

GetStringField(configuration, "redis_socket_path", redis_socket_path);
if (redis_socket_path.empty()) {
DARWIN_LOG_WARNING("AlertManager:: 'redis_socket_path' needs to be a non-empty string. Ignoring REDIS configuration...");
_redis = false; // Error configuring redis, disabling...
return false;
}
GetStringField(configuration, "redis_ip", redis_ip);
GetUIntField(configuration, "redis_port", redis_port);
GetStringField(configuration, "alert_redis_list_name", this->_redis_list_name);
GetStringField(configuration, "alert_redis_channel_name", this->_redis_channel_name);
if(_redis_list_name.empty() and _redis_channel_name.empty()) {

if(this->_redis_list_name.empty() and this->_redis_channel_name.empty()) {
DARWIN_LOG_WARNING("AlertManager:: if 'redis_socket_path' is provided, you need to provide at least"
" 'alert_redis_list_name' or 'alert_redis_channel_name'."
"Unable to configure REDIS. Ignoring REDIS configuration...");
_redis = false; // Error configuring redis, disabling...
return false;
}
if(!ConfigRedis(redis_socket_path)){
DARWIN_LOG_WARNING("AlertManager:: Error when configuring REDIS. Ignoring REDIS configuration...");
_redis = false; // Error configuring redis, disabling...
return false;

if(not redis_socket_path.empty()){
if (not ConfigRedis(redis_socket_path)) {
DARWIN_LOG_WARNING("AlertManager:: Error when configuring REDIS using '" + redis_socket_path +
"', ignoring REDIS configuration.");
_redis = false; // Error configuring redis, disabling...
return false;
}
} else if (not redis_ip.empty()) {
if (not ConfigRedis(redis_ip, redis_port)) {
DARWIN_LOG_WARNING("AlertManager:: Error when configuring REDIS using '" + redis_ip + ":" +
std::to_string(redis_port) + "', ignoring REDIS configuration.");
_redis = false; // Error configuring redis, disabling...
return false;
}
} else {
DARWIN_LOG_WARNING("AlertManager:: No 'redis_socket_path' or 'redis_ip' in configuration, "
"ignoring REDIS configuration.");
_redis = false; // Error configuring redis, disabling...
return false;
}
return true;
}
Expand All @@ -101,14 +116,32 @@ namespace darwin {
DARWIN_LOG_INFO(std::string("AlertManager:: '") + field_name + "' set to " + var);
return true;
} else {
DARWIN_LOG_WARNING(std::string("AlertManager:: '") + field_name + "' needs to be a non-empty string."
"Ignoring this field...");
DARWIN_LOG_WARNING(std::string("AlertManager:: '") + field_name + "' needs to be a non-empty string, "
"ignoring this field.");
}
}
return false;
}

bool AlertManager::GetUIntField(const rapidjson::Document& configuration,
const char* const field_name,
unsigned int& var) {
DARWIN_LOGGER;

if (configuration.HasMember(field_name)){
if (configuration[field_name].IsUint()) {
var = configuration[field_name].GetUint();
DARWIN_LOG_INFO(std::string("AlertManager:: '") + field_name + "' set to " + std::to_string(var));
return true;
} else {
DARWIN_LOG_WARNING(std::string("AlertManager:: '") + field_name + "' needs to be an integer, "
"ignoring this field.");
}
}
return false;
}

bool AlertManager::ConfigRedis(std::string redis_socket_path) {
bool AlertManager::ConfigRedis(const std::string redis_socket_path) {
DARWIN_LOGGER;
DARWIN_LOG_DEBUG("AlertManager:: Redis configuration...");

Expand All @@ -117,6 +150,15 @@ namespace darwin {
return redis.FindAndConnect();
}

bool AlertManager::ConfigRedis(const std::string redis_ip, const int redis_port) {
DARWIN_LOGGER;
DARWIN_LOG_DEBUG("AlertManager:: Redis configuration...");

darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance();
redis.SetIpConnection(redis_ip, redis_port);
return redis.FindAndConnect();
}

void AlertManager::Alert(const std::string& str) {
if (str.length() <= 0)
return;
Expand Down
19 changes: 17 additions & 2 deletions samples/base/AlertManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,32 @@ namespace darwin {
/// \brief Configure the RedisManager.
/// \param redis_socket_path The path to the redis socket used for alerting
/// \return True on success, false on error
bool ConfigRedis(std::string redis_socket_path);
bool ConfigRedis(const std::string redis_socket_path);

/// \brief Configure the RedisManager.
/// \param redis_ip The ip to the redis node used for alerting
/// \param redis_port The port to the redis node used for alerting
/// \return True on success, false on error
bool ConfigRedis(const std::string redis_ip, const int redis_port);

/// \brief Check and extract `field_name' of `configuration'. `field_name' *MUST* be a string.
/// \param configuration The configurations a json object.
/// \param field_name The field name. Creating a string here would be pointless.
/// \param var The string object ot fill with the extracted value. Initial value is not modified on error.
/// \param var The string object to fill with the extracted value. Initial value is not modified on error.
/// \return False on error, true on success. Logs appropriately.
static bool GetStringField(const rapidjson::Document& configuration,
const char* const field_name,
std::string& var);

/// \brief Check and extract `field_name' of `configuration'. `field_name' *MUST* be an unsigned integer.
/// \param configuration The configurations a json object.
/// \param field_name The field name. Creating a string here would be pointless.
/// \param var The unsigned integer object to fill with the extracted value. Initial value is not modified on error.
/// \return False on error, true on success. Logs appropriately.
static bool GetUIntField(const rapidjson::Document& configuration,
const char* const field_name,
unsigned int& var);

/// \brief Write the logs in file
/// \return True on success, false on error
bool WriteLogs(const std::string& logs);
Expand Down
2 changes: 1 addition & 1 deletion samples/fbuffer/Connectors/AConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool AConnector::PrepareKeysInRedis(){
DARWIN_LOG_DEBUG("AConnector::PrepareKeysInRedis:: key '"+ redis_list + "' is '" + reply + "'");

if(reply != "none") {
if(reply != "list") {
if(reply != "set") {
DARWIN_LOG_ERROR("AConnector::PrepareKeysInRedis:: key '" + redis_list + "' is already present "
"but seems to be used for something else, "
"cannot start the filter and risk overriding data in Redis");
Expand Down
56 changes: 42 additions & 14 deletions samples/fbuffer/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ bool Generator::ConfigureNetworkObject(boost::asio::io_context &context) {
for (auto &output : this->_output_configs) {
std::shared_ptr<AConnector> newOutput = _CreateOutput(context, output);
if (newOutput == nullptr) {
DARWIN_LOG_WARNING("Generator::ConfigureNetworkObject:: Unable to create the Output. Output " +
output._filter_type + " ignored.");
DARWIN_LOG_WARNING("Generator::ConfigureNetworkObject:: Unable to create the Output. Output " +
output._filter_type + " ignored.");
continue;
}
this->_outputs.push_back(newOutput);
Expand Down Expand Up @@ -51,20 +51,48 @@ bool Generator::ConfigureNetworkObject(boost::asio::io_context &context) {

bool Generator::LoadConfig(const rapidjson::Document &configuration) {
DARWIN_LOGGER;
std::string redis_socket_path, redis_ip;
unsigned int redis_port = 6379;
DARWIN_LOG_DEBUG("Generator::LoadConfig:: Loading classifier...");

if (not configuration.HasMember("redis_socket_path")) {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_socket_path' parameter missing, mandatory");
return false;
if(configuration.HasMember("redis_socket_path")) {
if (not configuration["redis_socket_path"].IsString()) {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_socket_path' needs to be a string");
return false;
} else {
redis_socket_path = configuration["redis_socket_path"].GetString();
}
}
if (not configuration["redis_socket_path"].IsString()) {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_socket_path' needs to be a string");
return false;

if(configuration.HasMember("redis_ip")) {
if (not configuration["redis_ip"].IsString()) {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_ip' needs to be a string");
return false;
} else {
redis_ip = configuration["redis_ip"].GetString();
}
}

if(configuration.HasMember("redis_port")) {
if (not configuration["redis_port"].IsUint()) {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: 'redis_port' needs to be an unsigned integer");
return false;
} else {
redis_port = configuration["redis_port"].GetUint();
}
}
std::string redis_socket_path = configuration["redis_socket_path"].GetString();

darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance();
redis.SetUnixConnection(redis_socket_path);
if (not redis_socket_path.empty()) {
redis.SetUnixConnection(redis_socket_path);
} else if (not redis_ip.empty()) {
redis.SetIpConnection(redis_ip, redis_port);
} else {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: no valid way to connect to Redis, "
"please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port').");
return false;
}

// Done in AlertManager before arriving here, but will allow better transition from redis singleton
if(not redis.FindAndConnect()) {
DARWIN_LOG_CRITICAL("Generator::LoadConfig:: Could not connect to a redis!");
Expand Down Expand Up @@ -187,14 +215,14 @@ bool Generator::LoadOutputs(const rapidjson::Value &array) {
if (not array[i].HasMember("filter_type") or not array[i]["filter_type"].IsString()) {
DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'filter_type' field missing or is not a string in outputs. Output ignored.");
continue;
}
}
if (not array[i].HasMember("filter_socket_path") or not array[i]["filter_socket_path"].IsString()) {
DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'filter_socket_path' field missing or is not a string in outputs. Output '" +
DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'filter_socket_path' field missing or is not a string in outputs. Output '" +
std::string(array[i]["filter_type"].GetString()) + "' ignored.");
continue;
}
}
if (not array[i].HasMember("interval") or not array[i]["interval"].IsInt64()) {
DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'interval' field missing or is not an integer in outputs. Output '" +
DARWIN_LOG_WARNING("Generator::LoadOutputs:: 'interval' field missing or is not an integer in outputs. Output '" +
std::string(array[i]["filter_type"].GetString()) + "' ignored.");
continue;
}
Expand Down
52 changes: 40 additions & 12 deletions samples/fconnection/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,35 @@ bool Generator::LoadConfig(const rapidjson::Document &configuration) {
DARWIN_LOGGER;
DARWIN_LOG_DEBUG("ConnectionSupervision:: Generator:: Loading classifier...");

std::string redis_socket_path;
std::string init_data_path;
std::string redis_socket_path, redis_ip, init_data_path;
unsigned int redis_port = 6379;

if (!configuration.HasMember("redis_socket_path")) {
DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: Missing parameter: 'redis_socket_path'");
return false;
if(configuration.HasMember("redis_socket_path")) {
if (not configuration["redis_socket_path"].IsString()) {
DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_socket_path' needs to be a string");
return false;
} else {
redis_socket_path = configuration["redis_socket_path"].GetString();
}
}

if (!configuration["redis_socket_path"].IsString()) {
DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_socket_path' needs to be a string");
return false;
if(configuration.HasMember("redis_ip")) {
if (not configuration["redis_ip"].IsString()) {
DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_ip' needs to be a string");
return false;
} else {
redis_ip = configuration["redis_ip"].GetString();
}
}

redis_socket_path = configuration["redis_socket_path"].GetString();
if(configuration.HasMember("redis_port")) {
if (not configuration["redis_port"].IsUint()) {
DARWIN_LOG_CRITICAL("ConnectionSupervision:: Generator:: 'redis_port' needs to be an unsigned integer");
return false;
} else {
redis_port = configuration["redis_port"].GetUint();
}
}

if (configuration.HasMember("init_data_path")) {
if (!configuration["init_data_path"].IsString()) {
Expand All @@ -70,10 +85,14 @@ bool Generator::LoadConfig(const rapidjson::Document &configuration) {
_redis_expire = configuration["redis_expire"].GetUint();
}

return ConfigRedis(redis_socket_path, init_data_path);
return ConfigRedis(redis_socket_path, redis_ip, redis_port, init_data_path);
}

bool Generator::ConfigRedis(const std::string &redis_socket_path, const std::string &init_data_path) {
bool Generator::ConfigRedis(
const std::string &redis_socket_path,
const std::string &redis_ip,
unsigned int redis_port,
const std::string &init_data_path) {
DARWIN_LOGGER;

std::ifstream init_data_stream;
Expand All @@ -82,7 +101,16 @@ bool Generator::ConfigRedis(const std::string &redis_socket_path, const std::str

darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance();
// Done in AlertManager before arriving here, but will allow better transition from redis singleton
redis.SetUnixConnection(redis_socket_path);
if (not redis_socket_path.empty()) {
redis.SetUnixConnection(redis_socket_path);
} else if (not redis_ip.empty()) {
redis.SetIpConnection(redis_ip, redis_port);
} else {
DARWIN_LOG_ERROR("ConnectionSupervision::Generator::ConfigureRedis:: no valid way to connect to Redis, "
"please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port').");
return false;
}

if(not redis.FindAndConnect()) {
DARWIN_LOG_ERROR("ConnectionSupervision::Generator::ConfigureRedis:: Could not configure Redis connection.");
return false;
Expand Down
2 changes: 2 additions & 0 deletions samples/fconnection/Generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Generator: public AGenerator {

private:
bool ConfigRedis(const std::string &redis_socket_path,
const std::string &redis_ip,
unsigned int redis_port,
const std::string &init_data_path);

unsigned int _redis_expire = 0;
Expand Down
42 changes: 34 additions & 8 deletions samples/fend/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,47 @@ bool Generator::LoadClassifier(const rapidjson::Document &configuration) {
DARWIN_LOGGER;
DARWIN_LOG_DEBUG("End:: Generator:: Loading classifier...");

if (!configuration.HasMember("redis_socket_path")) {
DARWIN_LOG_CRITICAL("End:: Generator:: Missing parameter: 'redis_socket_path'");
return false;
std::string redis_socket_path, redis_ip;
unsigned int redis_port = 6379;

if(configuration.HasMember("redis_socket_path")) {
if (not configuration["redis_socket_path"].IsString()) {
DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_socket_path' needs to be a string");
return false;
} else {
redis_socket_path = configuration["redis_socket_path"].GetString();
}
}

if (!configuration["redis_socket_path"].IsString()) {
DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_socket_path' needs to be a string");
return false;
if(configuration.HasMember("redis_ip")) {
if (not configuration["redis_ip"].IsString()) {
DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_ip' needs to be a string");
return false;
} else {
redis_ip = configuration["redis_ip"].GetString();
}
}

std::string redis_socket_path = configuration["redis_socket_path"].GetString();
if(configuration.HasMember("redis_port")) {
if (not configuration["redis_port"].IsUint()) {
DARWIN_LOG_CRITICAL("End:: Generator:: 'redis_port' needs to be an unsigned integer");
return false;
} else {
redis_port = configuration["redis_port"].GetUint();
}
}

darwin::toolkit::RedisManager& redis = darwin::toolkit::RedisManager::GetInstance();
// Done in AlertManager before arriving here, but will allow better transition from redis singleton
redis.SetUnixConnection(redis_socket_path);
if (not redis_socket_path.empty()) {
redis.SetUnixConnection(redis_socket_path);
} else if (not redis_ip.empty()) {
redis.SetIpConnection(redis_ip, redis_port);
} else {
DARWIN_LOG_ERROR("End:: Generator:: no valid way to connect to Redis, "
"please set 'redis_socket_path' or 'redis_ip' (and optionally 'redis_port').");
return false;
}
return redis.FindAndConnect();
}

Expand Down
Loading