logo.png

Mib Snmp

LibSnmp

Grammaire

Intégration

Home Up


Contents



1 Introduction

# yum install net-snmp-libs
# gcc -o go -lnetsnmp ?
# gcc -o go /usr/lib/libnetsnmp.so.15 ??


2 API SNMP

fichier snmp.h :

#ifndef __SNMP_H
#define __SNMP_H

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

typedef struct snmp_session  Session;
typedef struct snmp_pdu      Pdu;
typedef struct variable_list List;       /* /usr/include/net-snmp/library/snmp_api.h:518 */

/* API */
Session* connectSnmp(char* ipAddress);      /* Warning: use 'admin' priviledges */
Session* disconnectSnmp(Session*); 
Pdu*     getSnmp(Session*, char* requestedOid);
int      setSnmp(Session*, char* requestedOid, char types, char *values);

int      getASN_OCTET_STR(List*, char**);
int      getASN_INTEGER(List*);
float    getASN_OPAQUE_TAG2_FLOAT(List*);
/* int      getASN_BIT8(List*); */
/* int      getASN_BOOLEAN(List*); */

void     showAsnTypeForDebug(List* var);

#define boolError    2
#define intError    -32767
#define floatError  -1.
#define doubleError -1.

/* see exemple in section _utMAIN in snmp.c */

#endif /*  __SNMP_H */


3 API WIENER

fichier wiener.h :

#ifndef __WIENER_H
#define __WIENER_H

#include "global.h"
#include "snmp.h"

#define DEFAULT_IP_ALIM "192.168.1.86"

int setWienerOff(const char* ipAddress);
int setWienerOn(const char* ipAddress);

#endif  /*  __WIENER_H */


4 Fonctions WIENER

fichier wiener.c :

int setWienerOff(const char* ipAddress)
{
  Session* p_session = (Session*) 0;
  Pdu *response = (Pdu*)0;
  int rc = FALSE;

  if (ipAddress == (char*)0)
    ipAddress = DEFAULT_IP_ALIM;

  if ((p_session = connectSnmp(ipAddress)) 
      == (Session*)0)
    goto error;
  
  /* switch off */
  if (setSnmp(p_session, ".1.3.6.1.4.1.19947.1.1.1.0", 'i', "0\0") 
      != TRUE) 
    goto error;
  
  rc = TRUE;
 error:
  return rc;
}

int setWienerOn(const char* ipAddress)
{
  Session* p_session = (Session*) 0;
  Pdu *response = (Pdu*)0;

  /* outV, minV and MaxV */
  static char voltagesOids[][32] = 
    {
      ".1.3.6.1.4.1.19947.1.3.2.1.10. ", /* outputVoltage */
      ".1.3.6.1.4.1.19947.1.3.2.1.16. ", /* minVoltage */
      ".1.3.6.1.4.1.19947.1.3.2.1.17. "  /* maxVoltage */
    };

  const int indexOfUnitsInVoltagesOids = 30;

  /* outV, minV and MaxV */
  float voltages[3]; 

  static char unit[]={'1', '2', '4', '5', '\0'};  

  int try = 0;
  int nbConsecutiveValidatedTries = 0;
  int rcOnLoop = TRUE;
  int u, i;
  int rc = FALSE;

  if (ipAddress == (char*)0)
    ipAddress = DEFAULT_IP_ALIM;

  if ((p_session = connectSnmp(ipAddress)) 
      == (Session*)0)
    goto error;
  
  /* switch on */
  if (setSnmp(p_session, ".1.3.6.1.4.1.19947.1.1.1.0", 'i', "1\0") 
      != TRUE)  
    goto error;
  
  /* validate 3 time output voltage */
  do
    {
      /* loop on each unit */
      for (rcOnLoop = TRUE, u=0; rcOnLoop && unit[u] != '\0'; ++u)
      {
	/* get outputVoltage, minVoltage and maxVoltage for unit u*/
	for (i=0; rcOnLoop && i<3; ++i)
	  {
	    voltagesOids[i][indexOfUnitsInVoltagesOids] = unit[u];
	    //printf("   %s\n", voltagesOids[i]);
	    rcOnLoop &= ((response = getSnmp(p_session, voltagesOids[i])) != (Pdu*)0);
	    rcOnLoop &= ((voltages[i] = getASN_OPAQUE_TAG2_FLOAT(response->variables)) != floatError);
	    snmp_free_pdu(response);
	  }

	/* validate output voltage for this unit */
	//printf("outputVoltage = %f < %f < %f\n", voltages[1], voltages[0], voltages[2]);
	rcOnLoop &= (voltages[1] < voltages[0] && voltages[0] < voltages[2]);
      }

    /* validate output voltage once */
    if (rcOnLoop) 
	++nbConsecutiveValidatedTries;
    
    response = (Pdu *)0;
  }  
  while(nbConsecutiveValidatedTries < NB_CONSECUTIVE_VALIDATED_TRIES_NEEDED 
	&& ++try < NB_MAX_TRY);
  
  rc = (nbConsecutiveValidatedTries == NB_CONSECUTIVE_VALIDATED_TRIES_NEEDED);
 error:
  return rc;
}


5 Code source

Je cherche à me déplacer dans les sous arbres, ce qui ne me semble pas trivial au regards des headers installés via le paquet libsnmp-dev.

A priori il n'y a rien de prévu à cet effet ?!

  • fichier /etc/apt/sources.list :
    deb-src http://ftp.fr.debian.org/debian lenny main contrib non-free
    

  • insatalation du code source de /usr/bin/snmpwalk
    # mkdir rangerLesSourcesIci && cd rangerLesSourcesIci
    # apt-get source net-snmp
    # less apps/snmpwalk.c
    

Home Up

This document is also available in PDF and PostScript format.



2016-02-15