diff --git a/include/Prefs.h b/include/Prefs.h index 404143f1d8..55d56407b6 100644 --- a/include/Prefs.h +++ b/include/Prefs.h @@ -198,8 +198,8 @@ class Prefs { char* test_runtime_script_path; char* test_post_script_path; - char* message_broker_url; - char* message_broker; + char *message_broker_url, *message_broker; + bool message_broker_enabled; #ifdef NTOPNG_PRO bool print_maintenance, print_license; char *lic_mgr_config_file; @@ -375,6 +375,9 @@ class Prefs { inline const char* get_message_broker() { return(message_broker); }; + inline bool is_message_broker_enabled() { + return(message_broker_enabled); + }; inline char* get_export_endpoint() { return (export_endpoint); }; inline char* get_export_zmq_encryption_key() { return (export_zmq_encryption_key); diff --git a/include/ntop_defines.h b/include/ntop_defines.h index 9b2b4759e7..045f8f2a1b 100644 --- a/include/ntop_defines.h +++ b/include/ntop_defines.h @@ -1118,10 +1118,12 @@ /* MESSAGE BROKER CONSTS */ -#define CONST_PREFS_MESSAGE_BROKER_URL \ - NTOPNG_PREFS_PREFIX ".message_broker_url" +#define CONST_PREFS_MESSAGE_BROKER_URL NTOPNG_PREFS_PREFIX ".message_broker_url" #define DEFAULT_MESSAGE_BROKER_URL "0.0.0.0:4222" +#define CONST_PREFS_MESSAGE_BROKER_ENABLED NTOPNG_PREFS_PREFIX ".toggle_message_broker" +#define DEFAULT_MESSAGE_BROKER_ENABLED false + #define BROKER_RPC_CALL_DEFAULT_TIMEOUT_MS (u_int64_t)10000 #define BROKER_RPC_CALL_MAX_RSP_LEN (u_int64_t)256 diff --git a/scripts/lua/admin/prefs.lua b/scripts/lua/admin/prefs.lua index a319289c16..e6951a24ad 100644 --- a/scripts/lua/admin/prefs.lua +++ b/scripts/lua/admin/prefs.lua @@ -2205,12 +2205,13 @@ if auth.has_capability(auth.capabilities.preferences) then showElement, default_broker_id --[[show]] ) prefsInputFieldPrefs(subpage_active.entries["message_broker_url"].title, - subpage_active.entries["message_broker_url"].description, "ntopng.prefs.", "message_broker_url", "", "text", + subpage_active.entries["message_broker_url"].description, "ntopng.prefs.", "message_broker_url", prefs.message_broker_url, "text", showElement, nil, nil, { attributes = { spellcheck = "false" }, - pattern = "[^\\s]+" + pattern = "[^\\s]+", + required = true }) prefsInputFieldPrefs(subpage_active.entries["message_broker_username"].title, diff --git a/scripts/lua/modules/check_redis_prefs.lua b/scripts/lua/modules/check_redis_prefs.lua index 82414bb23e..4994663c8b 100644 --- a/scripts/lua/modules/check_redis_prefs.lua +++ b/scripts/lua/modules/check_redis_prefs.lua @@ -28,7 +28,7 @@ end -- ########################################### -function areHostTimeseriesEnabled(ifid) +function areHostTimeseriesEnabled() local rv = ntop.getPref("ntopng.prefs.hosts_ts_creation") if isEmptyString(rv) then rv = "light" diff --git a/src/Ntop.cpp b/src/Ntop.cpp index 9c0d59935b..ae330ad9bb 100644 --- a/src/Ntop.cpp +++ b/src/Ntop.cpp @@ -4276,10 +4276,12 @@ void Ntop::incBlacklisHits(std::string listname) { #ifdef NTOPNG_PRO void Ntop::connectMessageBroker() { #ifdef HAVE_NATS - const char *m_broker_id = prefs->get_message_broker(); + if (getPrefs() && getPrefs()->is_message_broker_enabled()) { + const char *m_broker_id = prefs->get_message_broker(); - if (!strcmp(m_broker_id, CONST_NATS_M_BROKER_ID)) { - message_broker = new (std::nothrow) NatsBroker(); + if (!strcmp(m_broker_id, CONST_NATS_M_BROKER_ID)) { + message_broker = new (std::nothrow) NatsBroker(); + } } #endif /* HAVE NATS */ // TODO: add MQTT diff --git a/src/Prefs.cpp b/src/Prefs.cpp index e0dddce197..6a24d24a86 100644 --- a/src/Prefs.cpp +++ b/src/Prefs.cpp @@ -77,7 +77,8 @@ Prefs::Prefs(Ntop *_ntop) { enable_informative_captive_portal = false, enable_external_auth_captive_portal = false, override_dst_with_post_nat_dst = false, - override_src_with_post_nat_src = false; + override_src_with_post_nat_src = false, + message_broker_enabled = false; hostMask = no_host_mask, collect_blacklist_stats = false; enable_asn_behaviour_analysis = enable_network_behaviour_analysis = enable_iface_l7_behaviour_analysis = false; @@ -839,7 +840,7 @@ void Prefs::getDefaultStringPrefsValue(const char *pref_key, char **buffer, const char *default_value) { char rsp[MAX_PATH]; - if ((ntop->getRedis()->get((char *)pref_key, rsp, sizeof(rsp)) == 0) && (rsp[0] != '\0')) + if ((ntop->getRedis()->get((char *)pref_key, rsp, sizeof(rsp)) == 0) && (rsp[0] != '\0') && strcmp(rsp, "nil")) *buffer = strdup(rsp); else *buffer = strdup(default_value); @@ -947,6 +948,7 @@ void Prefs::reloadPrefsFromRedis() { enable_top_talkers = getDefaultBoolPrefsValue(CONST_TOP_TALKERS_ENABLED, CONST_DEFAULT_TOP_TALKERS_ENABLED), enable_sites_collection = getDefaultBoolPrefsValue(CONST_SITES_COLLECTION_ENABLED, CONST_DEFAULT_SITES_COLLECTION_ENABLED), + message_broker_enabled = getDefaultBoolPrefsValue(CONST_PREFS_MESSAGE_BROKER_ENABLED, DEFAULT_MESSAGE_BROKER_ENABLED), enable_dns_cache = getDefaultBoolPrefsValue(CONST_DNS_CACHE_ENABLED, CONST_DEFAULT_DNS_CACHE_ENABLED), enable_active_local_hosts_cache = getDefaultBoolPrefsValue(CONST_RUNTIME_ACTIVE_LOCAL_HOSTS_CACHE_ENABLED, CONST_DEFAULT_IS_ACTIVE_LOCAL_HOSTS_CACHE_ENABLED), @@ -1030,9 +1032,12 @@ void Prefs::reloadPrefsFromRedis() { free(aux); } - getDefaultStringPrefsValue(CONST_PREFS_MESSAGE_BROKER_URL, &aux, DEFAULT_MESSAGE_BROKER_URL); - if(message_broker_url) free(message_broker_url); - message_broker_url = aux; + getDefaultStringPrefsValue(CONST_PREFS_MESSAGE_BROKER_URL, &tmp, DEFAULT_MESSAGE_BROKER_URL); + if (tmp) { + if (message_broker_url) free(message_broker_url); + message_broker_url = strdup(tmp); + free(tmp); + } getDefaultStringPrefsValue(CONST_PREFS_MESSAGE_BROKER, &tmp, DEFAULT_MESSAGE_BROKER); if (message_broker) free(message_broker); @@ -2841,6 +2846,7 @@ void Prefs::lua(lua_State *vm) { enable_external_auth_captive_portal); lua_push_uint64_table_entry(vm, "max_ui_strlen", max_ui_strlen); + lua_push_str_table_entry(vm, "config_file", config_file_path ? config_file_path : (char *)""); @@ -2857,6 +2863,10 @@ void Prefs::lua(lua_State *vm) { lua_push_str_table_entry(vm, "capture_direction", Utils::captureDirection2Str(captureDirection)); + lua_push_str_table_entry(vm, "message_broker_url", message_broker_url ? message_broker_url : ""); + lua_push_str_table_entry(vm, "message_broker", message_broker ? message_broker : ""); + lua_push_bool_table_entry(vm, "toggle_message_broker", message_broker_enabled); + lua_push_str_table_entry(vm, "zmq_publish_events_url", zmq_publish_events_url); lua_push_bool_table_entry(vm, "limited_resources_mode", limited_resources_mode); }