SnmpOpenConnection Function

Action

This function opens a new connection to a target machine with access to SNMP data for the provided MIB. It returns a handle required by all other BDL SNMP extension functions. This function only supports SNMP version 1/2c.

Include file

Snmp.bdh

Syntax

SnmpOpenConnection( in sMachine   : string,
                    in sMib       : string,
                    in sCommunity : string,
                    in sVersion   : string ): number;

Return value

Handle for open connection ( 0 on failure )

Parameter Description
sMachine Specifies target machine and port on which the smnp server is listening.
sMib Specifies the name of the MIB.
sCommunity Specifies the community of the SNMP data.
sVersion Specifies the version of the SNMP data.

Example

benchmark Collect 

use "pdce.bdh"  
use "snmp.bdh" 

const
  MEASURE_COMMUNITY   := "public";
  MEASURE_VERSION     := "V1";
  MEASURE_NAME        := "System/Services";
  MEASURE_MACHINE     := "machineName:161";
  MEASURE_MIB         := "RFC1213-MIB";
  MEASURE_OID_NAME    := "system.sysServices";      

var
  nInterval         : number;
  hClient1          : number;
  hMeasure1         : number;
    sKey1             : string(1024); 

dcluser
user
  Collect
transactions
  NoTRT_TStartup : begin;
  NoTRT_TCollect : 5;
  NoTRT_TEnd     : end;                              

dclfunc
  function PdceGetSnmpKey(sKey : string;
                          sMachine : string;
                          sMib : string;
                          sCommunity : string;
                          sVersion : string;
                          sOidName : string;
                          nInstance : number optional) : boolean        

  var
    sMibOid        : string(1024);
    sInstanceOid   : string(1024);
    sValue         : string(1024);
    sOid           : string(1024);
    hSnmp          : number;        // SNMP connection handle
  begin

    hSnmp := SnmpOpenConnection(sMachine, sMib, sCommunity, sVersion);

    if ( hSnmp <> 0 ) then
      SnmpGetMibOid(hSnmp, sOidName, sMibOid);                   

      Print("OID for " + sOidName + " : " + sMibOid);  
                             
      SnmpGetFirstInstanceOid(hSnmp, sMibOid, sInstanceOid);
      sOid := sMibOid + "." + sInstanceOid;                     

      Print("OID with first instance: " + sOid);

      if SnmpGetValue(hSnmp, sOid, sValue) then        
        Print("Value: " + sValue);             
      end;        
       
      sKey :=  "SNMP:" + sOid +"@" + MEASURE_MACHINE + "?" + SNMP_COMMUNITY_PUBLIC + "&" + SNMP_VERSION_1;
      PdceGetSnmpKey := true;                            

      SnmpCloseConnection( hSnmp )    
    end;                        

    PdceGetSnmpKey := false;                  
  end PdceGetSnmpKey;
                                         

dcltrans
  transaction NoTRT_TStartup  
  begin                   

      PdceGetSnmpKey( sKey1, MEASURE_Machine, MEASURE_MIB, MEASURE_COMMUNITY, MEASURE_VERSION, MEASURE_OID_NAME);
           
      PdceStartUp();
      PdceRegisterClient(hClient1, nInterval);
      PdceAddMeasure(hClient1, MEASURE_NAME, sKey1, hMeasure1);           

      Wait(float(2*nInterval));
    end NoTRT_TStartup;   

    transaction NoTRT_TCollect      
    var
    fLastValue, fSum, fMin, fMax, fAvg, fStDev : float;
    uTimeStamp, uStatus  : number;
  begin
    PdceGetMeasureValue(hMeasure1, MEASURE_NAME);    
    if (GetWorkloadModel() <> WORKLOAD_MONITORING) then      
      Wait(float(nInterval));    
    end;           

    if PdceMeasureQueryValue(hMeasure1, fLastValue, fSum, fMin, fMax, fAvg, fStDev, uTimeStamp, uStatus) then      
      Print("Query value: " + String(fLastValue) );
    end;
  end NoTRT_TCollect;

  transaction NoTRT_TEnd
  begin
    PdceMeasureUnSubscribe(hMeasure1);
    PdceClientUnRegister(hClient1);
    PdceCleanUp();
  end NoTRT_TEnd;