Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

dhcp.vbs - vbscript script by ActiveXperts Software

dhcp.vbs checks for maximum number of IP addresses in use.

Use dhcp.vbs directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select dhcp.vbs. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


dhcp.vbs script code

' ///////////////////////////////////////////////////////////////////////////////
' // ActiveXperts Network Monitor  - VBScript based checks
' // For more information about ActiveXperts Network Monitor and VBScript, visit
' // http://www.activexperts.com/support/network-monitor/online/vbscript/
' ///////////////////////////////////////////////////////////////////////////////

Option Explicit

' Declaration of global variables
Dim   SYSDATA, SYSEXPLANATION   ' SYSDATA is displayed in the 'Data' column in the Manager; SYSEXPLANATION in the 'LastResponse' column

' Constants - return values
Const retvalUnknown = 1         ' ActiveXperts Network Monitor functions should always return True (-1, Success), False (0, Error) or retvalUnknown (1, Uncertain)


' // To test a function outside Network Monitor (e.g. using CSCRIPT from the
' // command line), remove the comment character (') in the following lines:
' Dim bResult
' bResult = CheckDhcpMaxInUse( "dell06", "192.168.31.0", 200 )
' WScript.Echo "Return value: [" & bResult & "]"
' WScript.Echo "SYSDATA: [" & SYSDATA & "]"
' WScript.Echo "SYSEXPLANATION: [" & SYSEXPLANATION & "]"


Function CheckDhcpMaxInUse( strDhcpServer, strSubnet, numMaxAllowed  )
' Description: 
'     Check for maximum number of IP addresses in use.
'     Requires SNMP running on the DHCP server and monitoring system.
'     This function uses ActiveXperts Network Component.
'     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased
'     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
' Parameters:
'     1) strDhcpServer - Host name or IP address of the DHCP server
'     2) strSubnet - Subnet.
'     3) numMaxAllowed - maxmimum number of IP addresses allows
' Usage:
'     CheckDhcpMaxInUse( "<Hostname | IP>", "x.x.x.x", <maximum value> )
' Sample:
'     CheckDhcpMaxInUse( "localhost", "192.168.31.0", 200 )

  Dim strOID
  
  CheckDhcpMaxInUse = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA           = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION    = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strOID                 = "1.3.6.1.4.1.311.1.3.2.1.1.2." & strSubnet

  If( Not checkDhcpValue( strDhcpServer, strOID, SYSDATA, SYSEXPLANATION ) ) Then
    CheckDhcpMaxInUse  = retvalUnknown
    Exit Function
  End If

  SYSEXPLANATION         = "IP's in use: [" & SYSDATA & "]"

  If( CInt( SYSDATA ) <= CInt( numMaxAllowed ) ) Then
    CheckDhcpMaxInUse  = True
  Else
    CheckDhcpMaxInUse  = False
  End If

End Function

' //////////////////////////////////////////////////////////////////////////////

Function CheckDhcpMaxOffered( strDhcpServer, strSubnet, numMaxAllowed  )
' Description: 
'     Check for maximum number of IP addresses offered
'     Requires SNMP running on the DHCP server and monitoring system.
'     This function uses ActiveXperts Network Component.
'     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased
'     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
' Parameters:
'     1) strDhcpServer - Host name or IP address of the DHCP server
'     2) strSubnet - Subnet.
'     3) numMaxAllowed - maxmimum number of IP addresses allows
' Usage:
'     CheckDhcpMaxOffered( "<Hostname | IP>", "x.x.x.x", <maximum value> )
' Sample:
'     CheckDhcpMaxOffered( "localhost", "192.168.31.0", 5 )

  Dim strOID
  
  CheckDhcpMaxOffered = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA             = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION      = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
 
  strOID                 = "1.3.6.1.4.1.311.1.3.2.1.1.4." & strSubnet

  If( Not checkDhcpValue( strDhcpServer, strOID, SYSDATA, SYSEXPLANATION ) ) Then
    CheckDhcpMaxOffered= retvalUnknown
    Exit Function
  End If

  SYSEXPLANATION         = "IP's offered: [" & SYSDATA & "]"

  If( CInt( SYSDATA ) <= CInt( numMaxAllowed ) ) Then
    CheckDhcpMaxOffered  = True
  Else
    CheckDhcpMaxOffered  = False
  End If

End Function

' //////////////////////////////////////////////////////////////////////////////

Function CheckDhcpMinAvailable( strDhcpServer, strSubnet, numMinAvailable )
' Description: 
'     Check for minimum number of IP addresses available.
'     Requires SNMP running on the DHCP server and monitoring system.
'     This function uses ActiveXperts Network Component.
'     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased
'     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
' Parameters:
'     1) strDhcpServer - Host name or IP address of the DHCP server
'     2) strSubnet - Subnet.
'     3) numMaxAllowed - maxmimum number of IP addresses allows
' Usage:
'     CheckDhcpMinAvailable( "<Hostname | IP>", "x.x.x.x", <maximum value> )
' Sample:
'     CheckDhcpMinAvailable( "localhost", "192.168.31.0", 10 )

  Dim strOID

  CheckDhcpMinAvailable = retvalUnknown  ' Default return value, and will be shown as a yellow (uncertain) icon in the Manager
  SYSDATA               = ""             ' SYSDATA displayed in the 'Data' column in the Manager          
  SYSEXPLANATION        = ""             ' SYSEXPLANATION displayed in the 'LastResponse' column in the Manager
  
  strOID                = "1.3.6.1.4.1.311.1.3.2.1.1.3." & strSubnet

  If( Not checkDhcpValue( strDhcpServer, strOID, SYSDATA, SYSEXPLANATION ) ) Then
    CheckDhcpMinAvailable  = retvalUnknown
    Exit Function
  End If

  SYSEXPLANATION         = "IP's available: [" & SYSDATA & "]"

  If( CInt( SYSDATA ) >= CInt( numMinAvailable ) ) Then
    CheckDhcpMinAvailable  = True
  Else
    CheckDhcpMinAvailable  = False
  End If

End Function


' //////////////////////////////////////////////////////////////////////////////
' // --- Private Functions section ---
' // Private functions names should start with a lower case character, so they 
' // will not be listed in the Network Monitor's function browser.
' //////////////////////////////////////////////////////////////////////////////

Function checkDhcpValue( strHost, strOID, BYREF numSysData, BYREF strSysExplanation )
  Dim objSnmpManager, objConstants, objSnmpObject, strCommunity

  checkDhcpValue         = False         ' Default return value
  numSysData             = 0             ' Not used by this function
  strSysExplanation      = ""            ' Set initial value

  strCommunity           = "public"

  ' Create instance of SNMP object
  Set objSnmpManager     = CreateObject( "AxNetwork.SnmpManager" )
  Set objConstants       = CreateObject( "AxNetwork.NwConstants" )

  ' Initialize SNMP object
  objSnmpManager.LogFile = "c:\temp\_log.log"
  objSnmpManager.ProtocolVersion = objConstants.nwSNMP_VERSION_V2C
  objSnmpManager.Initialize
  If( objSnmpManager.LastError <> 0 ) Then
    strSysExplanation  = "Unable to initialize ActiveXperts Network Component SNMP object"
    checkDhcpValue     = False
    Exit Function
  End If

  ' Open connection to (remote) SNMP agent
  objSnmpManager.Open strHost, strCommunity
  If( objSnmpManager.LastError <> 0 ) Then
    strSysExplanation  = "Unable to connect to agent [" & strHost & "], community [" & strCommunity & "], error #" & objSnmpManager.LastError & ": " & objSnmpManager.GetErrorDescription( objSnmpManager.LastError )
    checkDhcpValue     = False
    Exit Function
  End If

  On Error Resume Next
  Set objSnmpObject      = objSnmpManager.Get( strOID )
    If( objSnmpManager.LastError <> 0 ) Then
    strSysExplanation  = "Unable to retrieve [" & strOID & ", error #" & objSnmpManager.LastError & ": " & objSnmpManager.GetErrorDescription( objSnmpManager.LastError )
    checkDhcpValue     = False
    Exit Function
  End If
  On Error Goto 0

  numSysData             = objSnmpObject.Value
  strSysExplanation      = "Value read: [" & objSnmpObject.Value & "]"
  checkDhcpValue         = True

  ' Note: type ID's are described in ActiveXperts Network Component manual, and included in the ActiveXperts Network Component samples.

  objSnmpManager.Close()
  objSnmpManager.Shutdown()

End Function