doc/developers/README.Add_startup_param.md
This guide explains how to add a new command-line startup parameter to ntopng, using the --db-archive-dir parameter implementation as a reference example.
Adding a new startup parameter in ntopng requires modifications to three main files:
include/Prefs.h - Header file containing the Prefs class declarationsrc/Prefs.cpp - Implementation of preferences handling and command-line parsingsrc/LuaEngineNtop.cpp - Lua engine integration (if the parameter needs to be accessible from Lua scripts)include/Prefs.h)Add a new member variable to store the parameter value in the private section of the Prefs class:
char *data_dir, *install_dir, *docs_dir, *scripts_dir, *callbacks_dir,
*pcap_dir, *clickhouse_archive_dir // Add your new parameter here
Add a public getter method to access the parameter value:
inline const char* get_clickhouse_archive_dir() { return (clickhouse_archive_dir); };
Note: Use inline const char* for string parameters that return directory paths or simple string values.
src/Prefs.cpp)In the Prefs::Prefs(Ntop *_ntop) constructor, initialize your new member variable to NULL:
pcap_dir = NULL;
clickhouse_archive_dir = NULL; // Add initialization here
In the Prefs::~Prefs() destructor, add memory cleanup for your parameter:
if(pcap_dir) free(pcap_dir);
if(clickhouse_archive_dir) free(clickhouse_archive_dir); // Add cleanup here
Add your parameter's documentation to the usage() function:
"[--db-archive-dir|-6] <path> | Directory used for archiving historical flows\n"
" | recorded on ClickHouse when data retention is over.\n"
" | Default: %s\n"
Make sure to add the corresponding format argument in the printf-style call:
CONST_DEFAULT_DOCS_DIR, CONST_DEFAULT_SCRIPTS_DIR,
CONST_DEFAULT_CALLBACKS_DIR, CONST_DEFAULT_DATA_DIR,
CONST_DEFAULT_DATA_DIR, // Add default value here
Add your parameter to the long_options array:
{"pcap-dir", required_argument, NULL, '5'},
{"db-archive-dir", required_argument, NULL, '6'}, // Add your option here
Important:
required_argument if the parameter requires a valueno_argument if it's a boolean flagAdd your option character to the option string in loadFromCLI():
"k:eg:hi:w:r:sg:m:n:p:qd:t:x:y:1:2:3:4:5:6:l:L:uv:zA:B:c:CD:E:F:MN:G:I:O:Q:"
// ^ Add your character here
Note: Add : after the character if the parameter requires an argument.
Add a case in the setOption() function to handle your parameter:
case '6':
if(clickhouse_archive_dir) free(clickhouse_archive_dir);
clickhouse_archive_dir = strdup(optarg);
break;
In the checkOptions() function, set a default value if none was provided:
if(!pcap_dir) pcap_dir = strdup(ntop->get_working_dir());
if(!clickhouse_archive_dir) clickhouse_archive_dir = strdup(ntop->get_working_dir());
Add path validation for directory parameters:
docs_dir = ntop->getValidPath(docs_dir);
scripts_dir = ntop->getValidPath(scripts_dir);
callbacks_dir = ntop->getValidPath(callbacks_dir);
pcap_dir = ntop->getValidPath(pcap_dir);
clickhouse_archive_dir = ntop->getValidPath(clickhouse_archive_dir);
Add validation to ensure the directory exists:
if(!pcap_dir[0]) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to locate pcap dir");
return (-1);
}
if(!clickhouse_archive_dir[0]) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to locate ClickHouse archive dir");
return (-1);
}
Remove trailing slashes from directory paths:
ntop->removeTrailingSlash(docs_dir);
ntop->removeTrailingSlash(scripts_dir);
ntop->removeTrailingSlash(callbacks_dir);
ntop->removeTrailingSlash(pcap_dir);
ntop->removeTrailingSlash(clickhouse_archive_dir);
If your parameter needs to be accessible from Lua scripts, modify src/LuaEngineNtop.cpp:
ntop_get_dirs() FunctionAdd your parameter to the Lua table in the ntop_get_dirs() function:
lua_push_str_table_entry(vm, "callbacksdir",
ntop->getPrefs()->get_callbacks_dir());
lua_push_str_table_entry(vm, "pcapdir", ntop->getPrefs()->get_pcap_dir());
lua_push_str_table_entry(vm, "dbarchivedir", ntop->getPrefs()->get_clickhouse_archive_dir());
Note: Use a descriptive key name (like "dbarchivedir") that will be used in Lua scripts.
--db-archive-dir)NULL in the constructorstrdup() to copy string argumentsntop->getValidPath()After implementing your new parameter:
./ntopng --help should show your new parameter./ntopng --db-archive-dir /path/to/archiveOnce implemented, the parameter can be used as:
# Long form
./ntopng --db-archive-dir /var/lib/ntopng/archive
# Short form
./ntopng -6 /var/lib/ntopng/archive
And accessed from Lua scripts as:
local dirs = ntop.getDirs()
local archive_dir = dirs["dbarchivedir"]