diff --git a/include/DB.h b/include/DB.h index ecaaa1ee5b..aef1d6e550 100644 --- a/include/DB.h +++ b/include/DB.h @@ -30,9 +30,11 @@ class AggregatedFlow; class DB { protected: + FILE *log_fd; NetworkInterface *iface; Mutex *m; + void open_log(); public: DB(NetworkInterface *_iface); virtual ~DB(); diff --git a/include/Prefs.h b/include/Prefs.h index d99b43516c..d8252a4901 100644 --- a/include/Prefs.h +++ b/include/Prefs.h @@ -48,7 +48,7 @@ class Prefs { enable_auto_logout, use_promiscuous_mode, enable_ixia_timestamps, enable_vss_apcon_timestamps, enable_users_login, disable_localhost_login, online_license_check, - service_license_check, enable_access_log, log_to_file, + service_license_check, enable_sql_log, enable_access_log, log_to_file, flow_aggregation_enabled, enable_mac_ndpi_stats; @@ -171,6 +171,7 @@ class Prefs { inline bool is_httpbl_enabled() { return(httpbl_key ? true : false); }; inline bool do_change_user() { return(change_user); }; inline void dont_change_user() { change_user = false; }; + inline bool is_sql_log_enabled() { return(enable_sql_log); }; inline bool is_access_log_enabled() { return(enable_access_log); }; inline void do_enable_access_log(bool state = true) { enable_access_log = state; }; inline bool are_ixia_timestamps_enabled() { return(enable_ixia_timestamps); }; diff --git a/include/ntop_defines.h b/include/ntop_defines.h index 532ab084f8..657d9fa7d5 100644 --- a/include/ntop_defines.h +++ b/include/ntop_defines.h @@ -418,6 +418,7 @@ #define CONST_MAX_SQL_QUERY_LEN 8192 #define CONST_ALERT_DISABLED_PREFS "ntopng.prefs.disable_alerts_generation" #define CONST_PREFS_ENABLE_ACCESS_LOG "ntopng.prefs.enable_access_log" +#define CONST_PREFS_ENABLE_SQL_LOG "ntopng.prefs.enable_sql_log" #define CONST_TOP_TALKERS_ENABLED "ntopng.prefs.host_top_sites_creation" #define CONST_SUPPRESSED_ALERT_PREFS "ntopng.prefs.alerts.ifid_%d" #define CONST_USE_NINDEX "ntopng.prefs.use_nindex" diff --git a/src/DB.cpp b/src/DB.cpp index e5eaeadc9e..ac10671eec 100644 --- a/src/DB.cpp +++ b/src/DB.cpp @@ -28,12 +28,35 @@ DB::DB(NetworkInterface *_iface) { ntop->getTrace()->traceEvent(TRACE_WARNING, "Internal error: NULL mutex. Are you running out of memory?"); iface = _iface; + log_fd = NULL; + + open_log(); } +/* ******************************* */ + +void DB::open_log() { + static char sql_log_path[MAX_PATH]; + + if(ntop->getPrefs()->is_sql_log_enabled()) { + snprintf(sql_log_path, sizeof(sql_log_path), "%s/%d/ntopng_sql.log", + ntop->get_working_dir(), iface->get_id()); + + log_fd = fopen(sql_log_path, "a"); + + if(!log_fd) + ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to create log %s", sql_log_path); + else + chmod(sql_log_path, CONST_DEFAULT_FILE_MODE); + } +} + + /* ******************************************* */ DB::~DB() { if(m) delete m; + if(log_fd) fclose(log_fd); } /* ******************************************* */ diff --git a/src/MySQLDB.cpp b/src/MySQLDB.cpp index ec087b3aa9..b85990b7e8 100644 --- a/src/MySQLDB.cpp +++ b/src/MySQLDB.cpp @@ -808,6 +808,12 @@ int MySQLDB::exec_sql_query(lua_State *vm, char *sql, bool limitRows, bool wait_ if(m) m->lock(__FILE__, __LINE__); + + if(ntop->getPrefs()->is_sql_log_enabled() && log_fd && sql) { + fprintf(log_fd, "%s\n", sql); + fflush(log_fd); + } + if((rc = mysql_query(&mysql, sql)) != 0) { /* retry */ disconnectFromDB(&mysql); diff --git a/src/Prefs.cpp b/src/Prefs.cpp index 7738cc3820..3e4372bc74 100755 --- a/src/Prefs.cpp +++ b/src/Prefs.cpp @@ -38,7 +38,7 @@ Prefs::Prefs(Ntop *_ntop) { attacker_max_num_syn_per_sec = victim_max_num_syn_per_sec = CONST_MAX_NUM_SYN_PER_SECOND; ewma_alpha_percent = CONST_DEFAULT_EWMA_ALPHA_PERCENT; data_dir = strdup(CONST_DEFAULT_DATA_DIR); - enable_access_log = false, flow_aggregation_enabled = false; + enable_access_log = false, enable_sql_log = false, flow_aggregation_enabled = false; enable_flow_device_port_rrd_creation = false; enable_ip_reassignment_alerts = false; enable_top_talkers = false, enable_idle_local_hosts_cache = false; @@ -475,6 +475,7 @@ void Prefs::reloadPrefsFromRedis() { CONST_DEFAULT_IS_AUTOLOGOUT_ENABLED); // alert preferences enable_access_log = getDefaultBoolPrefsValue(CONST_PREFS_ENABLE_ACCESS_LOG, false); + enable_sql_log = getDefaultBoolPrefsValue(CONST_PREFS_ENABLE_SQL_LOG, false); /* Runtime Preferences */ housekeeping_frequency = getDefaultPrefsValue(CONST_RUNTIME_PREFS_HOUSEKEEPING_FREQUENCY, HOUSEKEEPING_FREQUENCY),