ColdFusion SNMP Trap Sender Sample Source Code
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
SNMP MIB Browsing can be well integrated into ASP .NET environments. This document describes how Network Component's SNMP MIB Browser can be integrated into ASP .NET projects.
A management information base (MIB) is a database used to manage the devices in a communications network. The database is hierarchical (tree-structured) and entries are addressed through object identifiers (OID's). A MIB should contain information on these commands and on the target objects (controllable entities or potential sources of status information) with a view to tuning the network transport to the current needs. Each type of object in a MIB database has a name, a syntax, and an encoding. The name is represented uniquely as an OID. An OID is an administratively assigned name. The administrative policies used for assigning names are discussed later in this memo.
Use Network Component's 'SnmpMibBrowser' object to load a MIB database into memory and iterate over all objects and view all properties.
Step 1: Download and install Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new ColdFusion document
Create a new blank webdocument with the ".cfm" extention. First of all we are going to build the form whitch commands and properties of the device can be filled in. Then we are going to make a source code that connects to the device.
Step 3: Implementation
Before we can use the Network Component to perform lookups, we have to create an instance of the Rsh object. Use the following ColdFusion code to do this:
<cfobject class="AxNetwork.SnmpTrapManager" type="com" name="objSocket" Action="Create">
Appendix: Full source code
<!--- Setting the objects ---> <cfobject class="AxNetwork.SnmpTrap" type="com" name="objSnmpTrap" Action="Create"> <cfobject class="AxNetwork.SnmpTrapManager" type="com" name="objSnmpTrapManager" Action="Create"> <cfobject class="AxNetwork.SnmpObject" type="com" name="objSnmpObject" Action="Create"> <cfobject class="AxNetwork.NwConstants" type="com" name="objConstants" Action="Create"> <cfoutput> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>ActiveXperts Network Component SNMP Traps Using ColdFusion</title> <style> <!-- body{ text-align: center; } h2{ font-family: verdana; color: navy; font-weight: bold; } form{ margin: 0px; } .input{ font-family: verdana; font-size: 9pt; color: navy; border: 1px solid navy; background-color: white; width: 325px; } .button{ font-family: verdana; font-size: 9pt; width: 325px; } .table{ font-family: verdana; font-size: 9pt; color: navy; border: 1px solid navy; width: 500px; } div{ width: 500; height: 250px; overflow: auto; text-align: left; font-size: xx-small; font-family: verdana; border: 1px solid navy; } a{ font-family: verdana; font-size: 8pt; color: red; } --> </style> </cfoutput> <cfscript> strHost = "localhost"; strCommunity = "public"; intVersion = 2; strOid = "system.sysName.0"; strDatatype = "string"; strValue = "Hello world!"; intErrorNo = ""; strErrorDescr = ""; strLogfile = GetTempDirectory() & "SnmpTrap.log"; strComponent = "Module [" & objSnmpTrapManager.Module & "]; Build [" & objSnmpTrapManager.Build & "]"; </cfscript> <cfif IsDefined("URL.submitbutton")> <!--- Setting the logfile ---> <cfscript> if(IsDefined("URL.Logfile")){ objSnmpTrapManager.Logfile = URL.logfile; strLogfile = URL.logfile; } </cfscript> <!--- Initializing ---> <cfif objSnmpTrapManager.LastError eq 0> <cfset objSnmpTrapManager.Initialize()> </cfif> <!--- Setting the protocolversion ---> <cfscript> if(objSnmpTrapManager.LastError eq 0){ intVersion = 2; if(IsDefined("URL.version")){ objSnmpTrapManager.ProtocolVersion = URL.version; intVersion = URL.version; } } </cfscript> <!--- executing the OID ---> <cfscript> if(objSnmpTrapManager.LastError eq 0){ strOid = "system.sysName.0"; objSnmpObject.Clear(); if(IsDefined("URL.oid")){ objSnmpObject.OID = URL.oid; strOid = URL.oid; } } </cfscript> <!--- Setting the datatype ---> <cfscript> if(objSnmpTrapManager.LastError eq 0){ if(IsDefined("URL.datatype")){ strDatatype = "string"; switch(URL.datatype){ case "string": objSnmpObject.Type = objConstants.nwSNMP_TYPE_OCTETSTRING; strDatatype = "string"; break; case "number": objSnmpObject.Type = objConstants.nwSNMP_TYPE_INTEGER; strDatatype = "number"; break; } } } </cfscript> <!--- Setting the Value ---> <cfscript> if(objSnmpTrapManager.LastError eq 0){ strValue = "Hello world!"; if(IsDefined("URL.value")){ objSnmpObject.Value = URL.value; strValue = URL.value; } } </cfscript> <!--- Adding the trap and sending it ---> <cfscript> if(objSnmpTrapManager.LastError eq 0){ objSnmpTrapManager.Sleep(200); strHost = "localhost"; strCommunity = "public"; if(IsDefined("URL.host") and IsDefined("URL.community")) { strHost = URL.host; strCommunity = URL.community; } objSnmpTrap.Host = strHost; objSnmpTrap.Community = strCommunity; objSnmpTrap.AddObject ( objSnmpObject ); objSnmpTrapManager.Send(objSnmpTrap); } </cfscript> <!--- Echo the results ---> <cfscript> intErrorNo = objSnmpTrapManager.LastError; strErrorDescr = objSnmpTrapManager.GetErrorDescription(objSnmpTrapManager.LastError); </cfscript> </cfif> <cfoutput> </head> <body> <form method=get> <h2>ActiveXperts Network Component CF Sample - SNMP Trap</h2> <table class=table> <tr> <td>Component:</td> <td>#strComponent#</td> </tr> <tr> <td>Host:</td> <td><input class=input type=text name=host value="#strHost#"></td> </tr> <tr> <td>Community:</td> <td><input class=input type=text name=community value="#strCommunity#"></td> </tr> <tr> <td>Snmp Version:</td> <td> <cfif intVersion eq "1"> <select class=input name=version> <option value="1">SNMP V1</option> <option value="2">SNMP V2 (Default)</option> </select> <cfelse> <select class=input name=version> <option value="2">SNMP V2 (Default)</option> <option value="1">SNMP V1</option> </select> </cfif> </td> </tr> <tr> <td>Enter an OID:</td> <td><input class=input type=text name=oid value="#strOid#"></td> </tr> <tr> <td>OID Datatype:</td> <td> <cfif strDatatype eq "string"> <select class=input name=datatype> <option value="string">String</option> <option value="number">Number</option> </select> <cfelse> <select class=input name=datatype> <option value="number">Number</option> <option value="string">String</option> </select> </cfif> </td> </tr> <tr> <td>Value</td> <td><input class=input type=text name=value value="#strValue#"></td> </tr> <tr> <td> </td> <td><input class=button type=submit name=submitbutton value="Send trap"></td> </tr> <tr> <td>Logfile:</td> <td><input type=text name=logfile value="#strLogfile#"></td> </tr> </table> </form> <br> <table class=table> <tr> <td> <b>Results:</b> </td> <td> #intErrorNo#: #strErrorDescr# </td> </tr> </table> <br> <p>This sample is based on ActiveXperts Network Component, an <a target="blank" href="https://www.activexperts.com">ActiveXperts Software</a> product.</p> <a href="index.cfm">Back to main page</a> </body> </html> </cfoutput>
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.