Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/ntop/ntopng into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoBiscosi committed Jul 22, 2024
2 parents ad275ef + 7870f17 commit e573c63
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 33 deletions.
7 changes: 7 additions & 0 deletions include/Ntop.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class Ntop {
#ifdef HAVE_NEDGE
std::vector<PacketForwarder*> multicastForwarders;
#endif
#ifdef HAVE_SNMP_TRAP
SNMPTrap *trap_collector;
#endif

/* Local network address list */
char *local_network_names[CONST_MAX_NUM_NETWORKS];
Expand Down Expand Up @@ -823,6 +826,10 @@ class Ntop {
void decNumLuaVMs() { num_active_lua_vms--; }
inline u_int16_t getNumActiveLuaVMs() { return(num_active_lua_vms); }
const char* getPersistentCustomListNameById(u_int8_t list_id);
#ifdef HAVE_SNMP_TRAP
void initSNMPTrapCollector();
void toggleSNMPTrapCollector(bool enable);
#endif
};

extern Ntop *ntop;
Expand Down
19 changes: 1 addition & 18 deletions include/SNMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@

#define SNMP_MAX_NUM_OIDS 10

#ifdef HAVE_LIBSNMP
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#endif

/* ******************************* */

typedef enum {
Expand All @@ -40,17 +35,6 @@ typedef enum {
snmp_set_pdu,
} snmp_pdu_primitive;

#ifdef HAVE_LIBSNMP
class SNMPSession {
public:
struct snmp_session session;
void *session_ptr;

SNMPSession();
~SNMPSession();
};
#endif

class SNMP {
private:
u_int snmp_version;
Expand Down Expand Up @@ -83,8 +67,6 @@ class SNMP {
public:
SNMP();
~SNMP();

void collectTraps();

#ifdef HAVE_LIBSNMP
void handle_async_response(struct snmp_pdu *pdu, const char *agent_ip);
Expand All @@ -102,6 +84,7 @@ class SNMP {
char value_types[SNMP_MAX_NUM_OIDS],
char *values[SNMP_MAX_NUM_OIDS], bool _batch_mode);
#endif

void send_snmpv1v2c_request(char *agent_host, char *community,
snmp_pdu_primitive pduType, u_int version,
char *oid[SNMP_MAX_NUM_OIDS], bool _batch_mode);
Expand Down
43 changes: 43 additions & 0 deletions include/SNMPSession.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* (C) 2013-24 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

#ifndef _SNMP_SESSION_H_
#define _SNMP_SESSION_H_

#include "ntop_includes.h"

#ifdef HAVE_LIBSNMP

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

class SNMPSession {
public:
struct snmp_session session;
void *session_ptr;

SNMPSession();
~SNMPSession();
};

#endif

#endif /* _SNMP_SESSION_H_ */
67 changes: 67 additions & 0 deletions include/SNMPTrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* (C) 2013-24 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

#ifndef _SNMP_TRAP_H_
#define _SNMP_TRAP_H_

#include "ntop_includes.h"

#ifdef HAVE_LIBSNMP

/* ******************************* */

class SNMPTrap {
private:
netsnmp_transport *trap_transport;
SNMPSession *trap_session;
netsnmp_session *trap_session_internal;
pthread_t trap_loop;
bool trap_collection_running;

public:
SNMPTrap();
~SNMPTrap();

// Call to commence trap collection
void startTrapCollection();

// Call to cease trap collection
void stopTrapCollection();

// Call to check if trap collection is active
bool isTrapCollectionRunning();

// Trap collection loop
void trapCollection();

// Handle traps
void handleTrap(struct snmp_pdu *pdu);

// Init trap session
bool initSession();

// Release trap session data structures
void releaseSession();
};

#endif

#endif /* _SNMP_TRAP_H_ */
4 changes: 4 additions & 0 deletions include/ntop_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,10 @@ extern NtopngLuaContext* getUserdata(struct lua_State *vm);
#define MIN_NUM_ASYNC_SNMP_ENGINES NTOPNG_MAX_NUM_SNMP_DEVICES_ENT_M
#define MAX_NUM_ASYNC_SNMP_ENGINES NTOPNG_MAX_NUM_SNMP_DEVICES_ENT_XL

#if defined(NTOPNG_PRO) && defined(HAVE_LIBSNMP)
#define HAVE_SNMP_TRAP
#endif

#define MIN_NUM_HASH_WALK_ELEMS 512

#define COMPANION_QUEUE_LEN 4096
Expand Down
2 changes: 2 additions & 0 deletions include/ntop_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ using namespace std;
#include "Fingerprint.h"
#include "Prefs.h"
#include "DnsStats.h"
#include "SNMPSession.h"
#include "SNMP.h"
#include "SNMPTrap.h"
#include "NetworkDiscovery.h"
#include "ICMPstats.h"
#include "ICMPinfo.h"
Expand Down
4 changes: 4 additions & 0 deletions scripts/lua/admin/prefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ if auth.has_capability(auth.capabilities.preferences) then
ts_utils.setupAgain()
end

if _POST["toggle_snmp_trap"] then
ntop.snmpToggleTrapCollection(toboolean(_POST["toggle_snmp_trap"]))
end

page_utils.print_header_and_set_active_menu_entry(page_utils.menu_entries.preferences)

dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua")
Expand Down
18 changes: 18 additions & 0 deletions src/LuaEngineNtop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4676,6 +4676,23 @@ static int ntop_snmp_read_responses(lua_State *vm) {

/* ****************************************** */

static int ntop_snmp_toggle_trap_collection(lua_State *vm) {
bool enable = false;

if (ntop_lua_check(vm, __FUNCTION__, 1, LUA_TBOOLEAN) != CONST_LUA_OK)
return(ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_ERROR));
enable = (bool)lua_toboolean(vm, 1);

#ifdef HAVE_SNMP_TRAP
ntop->toggleSNMPTrapCollector(enable);
#endif

lua_pushnil(vm);
return(ntop_lua_return_value(vm, __FUNCTION__, CONST_LUA_OK));
}

/* ****************************************** */

#ifndef WIN32
static int ntop_syslog(lua_State *vm) {
char *msg;
Expand Down Expand Up @@ -8119,6 +8136,7 @@ static luaL_Reg _ntop_reg[] = {
{ "snmpgetbulkavailable", ntop_is_libsnmp_available },
{ "snmpMaxNumEngines", ntop_snmp_max_num_engines },
{ "snmpSetBulkMaxNumRepetitions", ntop_snmp_set_bulk_max_repetitions },
{ "snmpToggleTrapCollection", ntop_snmp_toggle_trap_collection },

/* Synchronous */
{ "snmpget", ntop_snmpget},
Expand Down
32 changes: 32 additions & 0 deletions src/Ntop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ Ntop::Ntop(const char *appName) {

startupLockFile = -1;
#endif

#ifdef HAVE_SNMP_TRAP
trap_collector = new (std::nothrow) SNMPTrap();
#endif
}

/* ******************************************* */
Expand Down Expand Up @@ -385,6 +389,9 @@ Ntop::~Ntop() {
if (radiusAcc) delete radiusAcc;
#endif

#ifdef HAVE_SNMP_TRAP
if (trap_collector) delete trap_collector;
#endif
}

/* ******************************************* */
Expand Down Expand Up @@ -1061,6 +1068,31 @@ void Ntop::loadMacManufacturers(char *dir) {

/* ******************************************* */

#ifdef HAVE_SNMP_TRAP
/* Note: the collector is already allocated in the constructor
* as the socket should be created before changing user */
void Ntop::initSNMPTrapCollector() {
if (!trap_collector) return;

if (ntop->getPrefs()->isSNMPTrapEnabled())
trap_collector->startTrapCollection();
}

/* ******************************************* */

void Ntop::toggleSNMPTrapCollector(bool enable) {
if (!trap_collector) return;

if (enable) {
trap_collector->startTrapCollection();
} else {
trap_collector->stopTrapCollection();
}
}
#endif

/* ******************************************* */

void Ntop::setWorkingDir(char *dir) {
snprintf(working_dir, sizeof(working_dir), "%s", dir);
removeTrailingSlash(working_dir);
Expand Down
15 changes: 0 additions & 15 deletions src/SNMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,12 @@ extern "C" {

/* ******************************* */

SNMPSession::SNMPSession() { session_ptr = NULL; }

/* ******************************* */

SNMPSession::~SNMPSession() {
if(session_ptr) snmp_sess_close(session_ptr);
}

/* ******************************* */
/* ******************************* */

SNMP::SNMP() {
if(trace_new_delete) ntop->getTrace()->traceEvent(TRACE_NORMAL, "[new] %s", __FILE__);
batch_mode = false;
#ifdef HAVE_LIBSNMP
init_snmp("ntopng");
#endif

getbulk_max_num_repetitions = 10;
}

Expand Down Expand Up @@ -1124,6 +1112,3 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType,

/* ******************************************* */

void SNMP::collectTraps() {
/* TODO */
}
38 changes: 38 additions & 0 deletions src/SNMPSession.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
*
* (C) 2013-24 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

#include "ntop_includes.h"

#ifdef HAVE_LIBSNMP

/* ******************************* */

SNMPSession::SNMPSession() { session_ptr = NULL; }

/* ******************************* */

SNMPSession::~SNMPSession() {
if(session_ptr) snmp_sess_close(session_ptr);
}

/* ******************************* */

#endif
Loading

0 comments on commit e573c63

Please sign in to comment.