Builiding your own Network Management System based on net-snmp

1.- Descarga y descompresión de NET-SNMP (Version 5.4.2.1 @Julio 2009)


wmunguiam@server:~$ wget https://sourceforge.net/projects/net-snmp/files/net-snmp/net-snmp-5.4.2.1.tar.gz/download

wmunguiam@server:~$ tar -xzvf net-snmp-5.4.2.1.tar.gz

wmunguiam@server:~$ cd net-snmp-5.4.2.1


2.- Compilación e instalación de NET-SNMP:


wmunguiam@server:~/net-snmp-5.4.2.1$ sudo -s

root@server:~/net-snmp-5.4.2.1# ./configure

root@server:~/net-snmp-5.4.2.1# make

root@server:~/net-snmp-5.4.2.1# make install

root@server:~/net-snmp-5.4.2.1# exit


[root@server:~/net-snmp-5.4.2.1# make uninstall

root@server:~/net-snmp-5.4.2.1# make clean]


3.- Creación de espacio de trabajo:


wmunguiam@server:~/net-snmp-5.4.2.1$ cd ..

wmunguiam@server:~$ mkdir DiseñoGestor DiseñoGestor /development

wmunguiam@server:~$ cd DiseñoGestor/development/


4.- Creación de Archivos: (Detalle de los contenidos cd cada uno en el Anexo A)


wmunguiam@server:~/DiseñoGestor/development$ ls -l

-rw-r--r-- 1 wmunguiam wmunguiam 1147 2009-07-16 16:38 Makefile

-rw-r--r-- 1 wmunguiam wmunguiam 6743 2009-07-17 15:29 snmpdemoapp.c


5.- Compilación de archivo “snmpdemoapp” (en base al archivo Makefile) :


wmunguiam@server:~/DiseñoGestor/development$ make snmpdemoapp

Salida:

gcc -I. `net-snmp-config --cflags` -c -o snmpdemoapp.o snmpdemoapp.c

gcc -o snmpdemoapp snmpdemoapp.o `net-snmp-config --libs`


6.- Verificacion de archivos generados:


wmunguiam@server:~/DiseñoGestor/development$ ls -l

-rw-r--r-- 1 wmunguiam wmunguiam 1147 2009-07-16 16:38 Makefile

-rwxr-xr-x 1 wmunguiam wmunguiam 21138 2009-07-17 12:29 snmpdemoapp

-rw-r--r-- 1 wmunguiam wmunguiam 6521 2009-07-16 16:35 snmpdemoapp.c

-rw-r--r-- 1 wmunguiam wmunguiam 13048 2009-07-17 12:29 snmpdemoapp.o


7.- Ejecución de consulta:


wmunguiam@server:~/DiseñoGestor/development$ ./snmpdemoapp

Salida:

SNMPv2-MIB::sysDescr.0 = STRING: Linux ccserver 2.6.24-7-pve #1 SMP PREEMPT Tue Jun 2 08:00:29 CEST 2009 x86_64

value #1 is a string: Linux ccserver 2.6.24-7-pve #1 SMP PREEMPT Tue Jun 2 08:00:29 CEST 2009 x86_64


ANEXO A: Contenidos de archivos


El contenido de los archivos descritos a continuación estan basados en el ejemplo de contrucción de una “Aplicacion Simple” descrito en la Wiki de NET-SNMP y adaptados para operar en el sistema operativo Ubuntu bajo NET-SNMP version 5.4.2.1.


* Peername=Host a consultar = 200.37.45.98

* Version SNMP= v3

* Nivel de seguridad (Auth, AuthNoPriv o Priv)=AuthNoPriv

* Tipo de autenticación= MD5

* usuario= snmpv3 username = wmunguiam

* PassPhase=wmunguiamUNMSM


A.- Archivo: snmpdemoadd.c


Se resume el contenido del código:


  • [STEP 1] Llamado de Headers Net-SNMP

  • [STEP 2] Definicion de uso de SNMPv3.

  • [STEP 2a: PassPhrase] PassPhrase="wmunguiamUNMSM"

  • [STEP 3] Declaracion del Main() conteniendo las variables y rutinas

  • [STEP 4] Declaracion de Variables

  • [STEP 5] Inicializacion de libreria SNMP. Initialize the SNMP library

  • [STEP 6] Inicializacion de sesion y definicion de parametros de conexion.

  • [STEP 6a: Autenticacion] Definicion de parametros de autenticacion.

  • [STEP 6b: Autenticacion: Version de SNMP] Version: SNMPv3.

  • [STEP 6c: Autenticacion: UserName] Usuario: wmunguiam

  • [STEP 6d: Autenticacion: Security Level] Seguridad: AuthNoPriv

  • [STEP 6e: Autenticacion: Tipo] Tipo: MD5

  • [STEP 6f: Autenticacion: Conformacion de Key]

  • [STEP 7] Apertura de Sesion.

  • [STEP 8] Generacion de PDU: SNMPGET(system.sysDescr.0)

  • [STEP 9] Envio de Peticion mediante la sesion y PDU establecida.

  • [STEP 10] : Analisis de PDU de respuesta

  • [STEP 11] Clausura de Sesion.



Contenido de archivo:


/* [STEP 1] Llamado de Headers Net-SNMP */

#include

#include

#include


/* [STEP 2] Definicion de uso de SNMPv3. */

/* Change the word "define" to "undef" to try the (insecure) SNMPv1 version */

#define DEMO_USE_SNMP_VERSION_3


/* [STEP 2a: PassPhrase] PassPhrase="wmunguiamUNMSM" */

#ifdef DEMO_USE_SNMP_VERSION_3

/* const char *our_v3_passphrase = "Frase para authorizacion (MD5)"; */

const char *our_v3_passphrase = "wmunguiamUNMSM";

#endif


/* [STEP 3] Declaracion del Main() conteniendo las variables y rutinas */

int main(int argc, char ** argv)

{ /*

* [STEP 4] Declaracion de Variables

*/

netsnmp_session session, *ss;

netsnmp_pdu *pdu;

netsnmp_pdu *response;


oid anOID[MAX_OID_LEN];

size_t anOID_len;

netsnmp_variable_list *vars;

int status;

int count=1;


/*

* [STEP 5] Inicializacion de libreria SNMP. Initialize the SNMP library

*/

init_snmp("snmpdemoapp");

/*

* [STEP 6] Inicializacion de sesion y definicion de parametros de conexion.

* configuracion de SNMP:

* Peername=Host a consultar = 200.37.45.98

* Version SNMP= v3

* usuario= snmpv3 username = wmunguiam

* Nivel de seguridad (Auth, AuthNoPriv o Priv)=AuthNoPriv

* Tipo de autenticacion= MD5

*/

snmp_sess_init( &session ); /* set up defaults */

session.peername = strdup("200.37.45.98");


/* [STEP 6a: Autenticacion] Definicion de parametros de autenticacion.

* set up the authentication parameters for talking to the server */


#ifdef DEMO_USE_SNMP_VERSION_3


/* [STEP 6b: Autenticacion: Version de SNMP] Version: SNMPv3.

* Use SNMPv3 to talk to the experimental server */


/* set the SNMP version number */

session.version=SNMP_VERSION_3;

/* [STEP 6c: Autenticacion: UserName] Usuario: wmunguiam

* Set the SNMPv3 user name */

session.securityName = strdup("wmunguiam");

session.securityNameLen = strlen(session.securityName);


/* [STEP 6d: Autenticacion: Security Level] Seguridad: AuthNoPriv

* Set the security level to authenticated, but not encrypted */

session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;


/* [STEP 6e: Autenticacion: Tipo] Tipo: MD5

* Set the authentication method to MD5 */

session.securityAuthProto = usmHMACMD5AuthProtocol;

session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);

session.securityAuthKeyLen = USM_AUTH_KU_LEN;


/* [STEP 6f: Autenticacion: Conformacion de Key]

* Set the authentication key to a MD5 hashed version of our

passphrase "The Net-SNMP Demo Password" (which must be at least 8

characters long) strlen(MD5Userpw) = strlen(our_v3_passphrase)*/

if (generate_Ku(session.securityAuthProto,

session.securityAuthProtoLen,

(u_char *) our_v3_passphrase, strlen(our_v3_passphrase),

session.securityAuthKey,

&session.securityAuthKeyLen) != SNMPERR_SUCCESS) {

snmp_perror(argv[0]);

snmp_log(LOG_ERR,

"Error generating Ku from authentication pass phrase. \n");

exit(1);

}

#else /* we'll use the insecure (but simplier) SNMPv1 */


/* set the SNMP version number */

session.version = SNMP_VERSION_1;


/* set the SNMPv1 community name used for authentication */

session.community = "public";

session.community_len = strlen(session.community);


#endif /* SNMPv1 */


/* [STEP 7] Apertura de Sesion.

* Open the session (SOCK_STARTUP usado solo para Win32)

*/

/* SOCK_STARTUP; */


ss = snmp_open(&session); /* establish the session */


if (!ss) { /* Notificacion si se presenta un error*/

snmp_sess_perror("ack", &session);

SOCK_CLEANUP;

exit(1);

}

/* [STEP 8] Generacion de PDU: SNMPGET(system.sysDescr.0)

* OID a consultar: system.sysDescr.0 = .1.3.6.1.2.1.1.1.0

*

*OPTION 1: Especificacion de OID "Numerica"

* Create the PDU for the data for our request.

* 1) We're going to GET the system.sysDescr.0 node.

*/

pdu = snmp_pdu_create(SNMP_MSG_GET);


anOID_len = MAX_OID_LEN;

if (!snmp_parse_oid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len)) {

snmp_perror(".1.3.6.1.2.1.1.1.0");

SOCK_CLEANUP;

exit(1);

}

#if OTHER_METHODS

/*

*OPTION 2: Especificacion de OID "basado en Nombres"

* These are alternatives to the 'snmp_parse_oid' call above,

* e.g. specifying the OID by name rather than numerically.

*/

read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);

get_node("sysDescr.0", anOID, &anOID_len);

read_objid("system.sysDescr.0", anOID, &anOID_len);

#endif


/*

*NOTA: Relleno con NULL a los SNMP de peticion (GET, GETNEXT, GETBULK, etc)*/

snmp_add_null_var(pdu, anOID, anOID_len);


/* [STEP 9] Envio de Peticion mediante la sesion y PDU establecida.

* Send the Request out.

*/

status = snmp_synch_response(ss, pdu, &response);


/* [STEP 10] : Analisis de PDU de respuesta

* Process the response.

*/

if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {


/*

* RECEPCION EXITOSA: Impresion de resultados

* SUCCESS: Print the result variables

*/

for(vars = response->variables; vars; vars = vars->next_variable)

print_variable(vars->name, vars->name_length, vars);


/* manipuate the information ourselves */

for(vars = response->variables; vars; vars = vars->next_variable) {

if (vars->type == ASN_OCTET_STR) {

char *sp = (char *)malloc(1 + vars->val_len);

memcpy(sp, vars->val.string, vars->val_len);

sp[vars->val_len] = '\0';

printf("value #%d is a string: %s\n", count++, sp);

free(sp);

}

else

printf("value #%d is NOT a string! Ack!\n", count++);

}

} else {


/*

* RECEPCION FALLIDA: Impresion de resultados

* FAILURE: print what went wrong!

*/


if (status == STAT_SUCCESS)

fprintf(stderr, "Error en paquete\nCausa: %s\n",

snmp_errstring(response->errstat));

else if (status == STAT_TIMEOUT)

fprintf(stderr, "Timeout: No hay respuesta desde %s.\n",

session.peername);

else

snmp_sess_perror("snmpdemoapp", ss);

}


/* [STEP 11] Clausura de Sesion.

*

*

* Clean up:

* 1) free the response.

* 2) close the session.

*/

if (response)

snmp_free_pdu(response);

snmp_close(ss);

/*Codigo para Win32*/

/*SOCK_CLEANUP;*/

return (0);

} /* main() */



B.- Archivo: Makefile


#

# Warning: you may need more libraries than are included here on the

# build line. The agent frequently needs various libraries in order

# to compile pieces of it, but is OS dependent and we can't list all

# the combinations here. Instead, look at the libraries that were

# used when linking the snmpd master agent and copy those to this

# file.

#


CC=gcc


OBJS1=snmpdemoapp.o

OBJS2=example-demon.o nstAgentSubagentObject.o

OBJS3=asyncapp.o

TARGETS=example-demon snmpdemoapp asyncapp


CFLAGS=-I. `net-snmp-config --cflags`

BUILDLIBS=`net-snmp-config --libs`

BUILDAGENTLIBS=`net-snmp-config --agent-libs`


# shared library flags (assumes gcc)

DLFLAGS=-fPIC -shared


all: $(TARGETS)


snmpdemoapp: $(OBJS1)

$(CC) -o snmpdemoapp $(OBJS1) $(BUILDLIBS)


asyncapp: $(OBJS3)

$(CC) -o asyncapp $(OBJS3) $(BUILDLIBS)

example-demon: $(OBJS2)

$(CC) -o example-demon $(OBJS2) $(BUILDAGENTLIBS)


clean:

rm $(OBJS2) $(OBJS2) $(TARGETS)


nstAgentPluginObject.so: nstAgentPluginObject.c Makefile

$(CC) $(CFLAGS) $(DLFLAGS) -c -o nstAgentPluginObject.o nstAgentPluginObject.c

$(CC) $(CFLAGS) $(DLFLAGS) -o nstAgentPluginObject.so nstAgentPluginObject.o


Comments