loading snmp object name to OID mappings
Martin Janzen
janzen____pixelmetrix.com
Tue Jun 24 05:51:21 CEST 2003
> Cosmo, Patrick wrote:
>> I have an application that allows administrators to configure snmp
>> object name=value parameters. The application itself must be able to
>> lookup the OID for the objectname in order to set a value on the
>> device. So, somehow, we need to get the OID to object name mappings
>> into the snmp manager.
Frank Fock wrote:
> SNMP++ does not contain any MIB compiler code.
> Thus, if you need to parse MIBs at runtime, you
> will have to use libSMI, which is a free C library
> to access MIB information. If it is OK, to compile
> your MIBs at runtime, then you could use AgenPro
> to create appropriate C++ objects or data structures.
> AgenPro is fully customizable by code generation
> templates. See http://www.agentpp.com for more
> details.
The approach I use is to use NET_SNMP (formerly UCD_SNMP) to parse the
MIBs. Yeah, it's kind of silly to link in both libraries, but the
combination does the job pretty painlessly; see example below.
--
Martin Janzen
janzen at pixelmetrix dot com
// NET-SNMP includes, used only for parsing MIBs.
// These are all C functions, so hide them in a NetSnmp namespace.
//
// Some NET-SNMP header files do bad things with timeval, so we
// limit the damage by renaming it.
namespace NetSnmp
{
#define timeval net_snmp_timeval
#include "ucd-snmp/ucd-snmp-config.h"
#include "ucd-snmp/asn1.h"
#include "ucd-snmp/mib.h"
#include "ucd-snmp/parse.h"
#undef timeval
// Last error detected by NET-SNMP.
extern "C" { extern int snmp_errno; }
}
[...]
void
MyAgent::readMIBs()
{
// Use the NET-SNMP library to translate symbolic OIDs, as described
// in the variables(5) man page.
// The NET-SNMP library will search MIB directories in the order
// in which they appear in the MIBDIRS environment variable.
// Later filenames override earlier ones, so add default
// directories first, then more specific ones.
//
// The initial "+" tells the library to read these directories in
// addition to those containing the standard MIB definitions
// (eg. SNMPv2-MIB, TCP-MIB, etc.).
string mibdirs("+");
for ( /* each of your MIB directories */ )
mibdirs += ... ;
setenv("MIBDIRS", mibdirs.c_str(), 1);
// Now build a list of your MIB module names, and add each name
// to the MIBS environment variable, to tell the NET-SNMP library
// that this module should be loaded on startup.
// The initial "+" says that these modules should be loaded in
// addition to the standard modules.
std::set<string> moduleNames;
for ( /* each of your MIB files */ )
{
FILE* fp = fopen( /* next MIB file */, "r");
if (fp == 0) continue;
char token[MAXTOKEN];
if (NetSnmp::snmp_get_token(fp, token, MAXTOKEN) > 0)
moduleNames.insert(token);
fclose(fp);
}
string mibs("+");
std::set<string>::const_iterator mit = moduleNames.begin();
while (mit != moduleNames.end())
{
if (mibs.size() > 1) mibs += ':';
mibs += *mit++;
}
setenv("MIBS", mibs.c_str(), 1);
// Print warning messages (1=some, 2=lots) when parsing MIBs.
NetSnmp::snmp_set_mib_warnings(1);
// Now initialize the NET-SNMP library's MIB API. Among other
// things, this reads in all default MIBs, plus MIBs added above;
// see mib_api(3).
NetSnmp::init_mib();
}
/*static*/ Agentpp::Oidx
MyAgent::parseOID(const string& oidName)
{
NetSnmp::oid subID[MAX_OID_LEN];
size_t subIDLen = MAX_OID_LEN;
// Convert this object identifier from numeric or symbolic form
// to a list of subidentifiers.
if (NetSnmp::read_objid(oidName.c_str(), subID, &subIDLen))
return Agentpp::Oidx(subID, subIDLen);
// An empty OID indicates a failed parse.
return Agentpp::Oidx();
}
More information about the AGENTPP
mailing list