###########################################################

# Description: Canopy SM traffic monitor

# Copyright 2005 NetMechanica - All rights reserved

###########################################################

string sAgentAddress = "169.254.4.1"

string community = "public"

int nRetries = 1

int nTimeout = 3000     

int nPollingInterval = 10 # seconds

# Canopy AP whispLinkTable

string sIfOutOctetsOID = "1.3.6.1.4.1.161.19.3.1.4.1.13"

string sIfInOctetsOID = "1.3.6.1.4.1.161.19.3.1.4.1.7"

string sIfPhysAddressOID = "1.3.6.1.4.1.161.19.3.1.4.1.20"

snmpvar In = CreateVar(sIfInOctetsOID)

snmpvar Out = CreateVar(sIfOutOctetsOID)

snmpvar PhysAddress = CreateVar(sIfPhysAddressOID)

snmppdu pdu = CreatePdu(In,Out,PhysAddress)  

array InOctetsArray

array OutOctetsArray

int nFirstPoll = TRUE

snmppdu response

snmpvar InOctets  

snmpvar OutOctets 

snmpvar IfPhysAddress   

int nInOctets 

int nOutOctets 

int nInDelta 

int nOutDelta  

string sIfPhysAddressValue

int nStatus

string sTableEndOID

                 

# Database variables

string sConnectionString = "DSN=Traffic"

string sInsertStatementTemplate = "insert into Traffic (PhysicalAddress, InBytes, OutBytes) values ('"

string sInsertStatement

int nDatabaseID = DBOpen(sConnectionString)

if (nDatabaseID == -1)

  # Place error handling/reporting code here

  Println("Failed to open database")

  stop

endif

 

while TRUE

  while TRUE

     response = SnmpGetNext(sAgentAddress, community, nRetries, nTimeout, pdu, SNMPV2)          

     nStatus = GetPduErrorStatus(response)

     # let's check snmp request status

     if (nStatus != OK)

       Println(GetPduErrorString(nStatus))

       break

     else                           

       InOctets = GetVarAt(response, 0)

       OutOctets = GetVarAt(response, 1)    

       IfPhysAddress = GetVarAt(response, 2

       # Check if the end of table is reached

       sTableEndOID = GetVarOID(InOctets)

       if(Find(sTableEndOID, sIfInOctetsOID, 0) == -1)

          nFirstPoll = FALSE

          break

       else

          sIfPhysAddressValue =  ToString(GetVarValue(IfPhysAddress))

          nInOctets = ToInteger(GetVarValue(InOctets))

          nOutOctets = ToInteger(GetVarValue(OutOctets))

          if(!nFirstPoll)

            nInDelta = nInOctets - InOctetsArray[sIfPhysAddressValue]

            nOutDelta = nOutOctets - OutOctetsArray[sIfPhysAddressValue]

            sInsertStatement = sInsertStatementTemplate + sIfPhysAddressValue + "', '" + ToString(nInDelta) + "', '" + ToString(nOutDelta) + "');"

            # insert into database

            DBExecuteSQL(nDatabaseID, sInsertStatement)

            if (DBGetSQLStatus(nDatabaseID) != SQLOK)

              # Place error handling/reporting code here

              Println(DBGetSQLErrorString(nDatabaseID))

              stop

            endif

          endif

          InOctetsArray[sIfPhysAddressValue] = nInOctets

          OutOctetsArray[sIfPhysAddressValue] = nOutOctets

          pdu = response

       endif      

     endif

  endwhile # end of getnext cycle

  Println("===================== Polling cycle completed at ", GetTimeString() + " =======================")

  Sleep(nPollingInterval)

  pdu = CreatePdu(In,Out,PhysAddress)

endwhile # end of polling cycle

DBClose(nDatabaseID)