Title: Implementing
1Implementing SNMP with Microsoft
Windows Presented by William C. Kratville COSC
5140 Spring 2001
2Purpose
Simple Network Management Protocol (SNMP) is a
service that supports computers running TCP/IP
and IPX protocols.
You can use SNMP to configure remote devices,
monitor network performance, audit network usage,
and detect network faults or inappropriate
access.
3Usage
SNMP uses a distributed architecture consisting
of management applications and agent
applications.
The SNMP service implements an SNMP agent.
You can use third-party SNMP management
software, or you can develop your own SNMP
management software application.
4SNMP Management APIs
SNMP Management API
WinSNMP API version 2.0 for the Windows 2000
operating environment.
SNMP Extension Agent API
5Windows SNMP Service
Windows CE supports two versions of the SNMP
protocol SNMPv1 and SNMPv2c.
The SNMP service uses the unique host names, IP
addresses, or IPX addresses of devices to
recognize the host(s) to which it reports
information and from which it receives requests.
6Pre SNMP Installation
Create or obtain from the network administrator
The Community names used in your network.
Trap destination for each community.
IP addresses, IPX addresses, or computer names
for SNMP management hosts.
7SNMP Service Setup
SNMP is installed as part of Custom Setup
when you install TCP/IP on Windows NT.
8SNMPUtil.exe
After installation the utility SNMPUTIL.EXE can
be used to check that it is operating correctly.
At the command prompt type the following snmputil
getnext localhost public .1.3.6.1.4.1.77.1
The response should be similar to Variable
iso.org.dod.internet.private.enterprises.lanmanage
r.lanmgr-2.common.comVersionMaj.0Value OCTET
STRING - 4
9Windows NT files installed for SNMP
DHCPMIB.DLL - DHCP extension-agent DLL. Only
installed on DHCP servers. This DLL is
available for Windows NT 3.5 and
later. INETMIB1.DLL Internet MIB II
extension-agent DLL LMMIB2.DLL LAN Manager
MIB 2 extension- agent DLL
10Windows NT files ...
MGMTAPI.DLL - SNMP Management API
library MIB.BIN - Binary file containing all of
the MIBs SNMP.EXE - SNMP Agent
Service proxy agent that listens for requests
and hands them off to the appropriate SNMP
DLL
11Windows NT files ...
SNMPTRAP.EXE - Receives SNMP traps and
forwards them to MGMTAPI.DLL WINSMIB.DLL
- WINS extension-agent DLL. Only
installed on WINS servers. This DLL is
available for Windows NT 3.5 and later.
12Turning SNMP On and Off
You can start and stop SNMP from the MS-DOS
console window or by using the Services icon in
Control Panel.
To start from the console net start snmp
To obtain help net help start snmp
13SDK SNMP Sample Programs
SnmpUtil Management application example.
Testdll Agent extension example.
14SNMPUtil.exe
On the Windows Systems Developers Kit (SDK) in
the directory \MSTOOLS\SAMPLES\WIN32\WINNT\SNMP\
SNMPUTIL The following file is
found SNMPUTIL.C It is an Example of how to
code management applications using the SNMP
Management APIs for Windows NT.
15SNMPUtil.exe commands
get Gets the current value of the specified
oid(s). getnext Gets the current value of the
item in the MIB that follows the item whose oid
is specified. walk Steps through the MIB and
retrieves the values of all items in the branch
of the MIB specified by oid. Trap Tells
SNMPUTIL to listen for traps.
16SNMPUtil.exe includes
// Necessary includes. include
ltwindows.hgt include ltstdio.hgt include
ltstring.hgt include ltmalloc.hgt include
ltsnmp.hgt include ltmgmtapi.hgt
17Get OIDs
RFC1157VarBindList variableBindings while(--argum
entCount) AsnObjectIdentifier reqObject
argumentVector // Convert the string
representation to an internal representation.
if (!SnmpMgrStrToOid(argumentVector,
reqObject)) // error else
// Since successful, add to the variable bindings
list. variableBindings.len
variableBindings.listvariableBindings.len -
1.name reqObject variableBindings.li
stvariableBindings.len - 1.value.asnType
ASN_NULL // end while()
18Open SNMP Session
// Establish a SNMP session to communicate with
the remote agent. The // community,
communications timeout, and communications retry
count // for the session are also required. if
((session SnmpMgrOpen(agent, community,
timeout,
retries)) NULL)
printf("error on SnmpMgrOpen d\n",
GetLastError()) return 1 // agent is
the address of the requested agent (ip).
Note Skip this if requesting a Trap operation.
19SNMP Get
// Get and GetNext are relatively simple
operations to perform. // Simply initiate the
request and process the result and/or // possible
error conditions. if (operation GET)
requestType ASN_RFC1157_GETREQUEST else
requestType ASN_RFC1157_GETNEXTREQUEST //
Request that the API carry out the desired
operation. if (!SnmpMgrRequest(session,
requestType, variableBindings,
errorStatus, errorIndex))
// The API is indicating an error.
printf("error on SnmpMgrRequest d\n",
GetLastError()) else ...
20 else // The API succeeded, errors may be
indicated from the remote // agent. if
(errorStatus gt 0) printf("Error
errorStatusd, errorIndexd\n", errorStatus,
errorIndex) else //
Display the resulting variable bindings.
UINT i char string NULL for(i0 i
lt variableBindings.len i)
SnmpMgrOidToStr(variableBindings.listi.name,
string) printf("Variable s\n",
string) if (string)
SNMP_free(string) printf("Value
") SnmpUtilPrintAsnAny(variableBindin
gs.listi.value) printf("\n")
// end for()
21 // Free the variable bindings that
have been allocated. SnmpUtilVarBindListF
ree(variableBindings)
22SNMP Walk
if (operation WALK) // Walk is a common
term used to indicate that all MIB variables
// under a given OID are to be traversed and
displayed. This is // a more complex
operation requiring tests and looping in
addition // to the steps for get/getnext
above. AsnObjectIdentifier root
AsnObjectIdentifier tempOid
SnmpUtilOidCpy(root, variableBindings.list0.na
me) requestType ASN_RFC1157_GETNEXTREQUEST
23 while(1) if (!SnmpMgrRequest(session
, requestType, variableBindings,
errorStatus,
errorIndex)) // The API is indicating
an error. printf("error on
SnmpMgrRequest d\n", GetLastError())
break else // The API
succeeded, errors may be indicated from the
remote agent. // Test for end of
subtree or end of MIB. if (errorStatus
SNMP_ERRORSTATUS_NOSUCHNAME
SnmpUtilOidNCmp(variableBin
dings.list0.name,
root, root.idLength))
printf("End of MIB subtree.\n\n")
break
24 // Test for general error conditions or
success. . . . // Prepare for the
next iteration. Make sure returned oid is
// preserved and the returned value is freed.
SnmpUtilOidCpy(tempOid, variableBindings.lis
t0.name) SnmpUtilVarBindFree(variableB
indings.list0) SnmpUtilOidCpy(variable
Bindings.list0.name, tempOid)
variableBindings.list0.value.asnType
ASN_NULL SnmpUtilOidFree(tempOid) //
end while()
25SNMP Close
if (operation ! TRAP) // Close SNMP session
with the remote agent. if
(!SnmpMgrClose(session))
printf("error on SnmpMgrClose d\n",
GetLastError()) return 1
26SNMP Trap
// Trap handling can be done two different ways
event driven or // polled. The following code
illustrates the steps to use event // driven
trap reception in a management application.
HANDLE hNewTraps NULL if (!SnmpMgrTrapListen(
hNewTraps)) printf("error on
SnmpMgrTrapListen d\n", GetLastError())
else printf("snmputil listening for
traps...\n")
27while(1) DWORD dwResult if ((dwResult
WaitForSingleObject(hNewTraps, 0xffffffff))
0xffffffff) printf("error on
WaitForSingleObject d\n", GetLastError())
else if (!ResetEvent(hNewTraps))
printf("error on ResetEvent d\n",
GetLastError()) else
AsnObjectIdentifier enterprise
AsnNetworkAddress IPAddress
AsnInteger genericTrap
AsnInteger specificTrap
AsnTimeticks timeStamp
RFC1157VarBindList variableBindings
UINT i char string NULL
28 while(SnmpMgrGetTrap(enterprise,
IPAddress, genericTrap,
specificTrap, timeStamp, variableBindings))
printf("snmputil trap genericd
specificd\n",
genericTrap, specificTrap) if
(IPAddress.dynamic) SNMP_free(IPAddress.stream)
for(i0 i lt
variableBindings.len i)
// Display results // end for()
SnmpUtilOidFree(enterprise)
SnmpUtilVarBindListFree(variableBindings)
// end while()
29SNMPUtil.exe API Data types
RFC1157VarBindList
AsnObjectIdentifier AsnNetworkAddress AsnIntege
r AsnTimeticks
30SNMPUtil.exe API Functions
SnmpMgrStrToOid
SnmpMgrOpen
SnmpMgrRequest
SnmpMgrOidToStr
SnmpUtilOidNCmp
SNMP_free
SnmpUtilVarBindListFree
SnmpUtilPrintAsnAny
SnmpMgrClose
SnmpMgrTrapListen
SnmpMgrGetTrap
31TestDLL.dll
On the Windows Systems Developers Kit (SDK) in
the directory \MSTOOLS\SAMPLES\WIN32\WINNT\SNMP\T
ESTDLL The following files are
found Testdll.c Testdll.def TestMIB.c TestMI
B.h This DLL implements the toaster MIB.
32TestDLL.dll Overview
Microsoft's Extendible Agent for Windows NT is
implemented by dynamically linking to Extension
Agent DLLs that implement portions of the MIB.
These Extension Agents are configured in the
Windows NT Registration Database. When the
Extendible Agent Service is started, it queries
the registry to determine which Extension Agent
DLLs have been installed and need to be loaded
and initialized.
The Extendible Agent invokes various DLL entry
points to request MIB queries and obtain
Extension Agent generated traps.
33TestDLL.dll includes
// Necessary includes. include
ltwindows.hgt include ltsnmp.hgt include
"testmib.h"
34TestDLL.dll Elapsed Time
Extension Agent DLLs need access to the elapsed
time an agent has been active. This is
implemented by initializing the Extension Agent
with a time zero reference, and allowing the
agent to compute elapsed time by subtracting the
time zero reference from the current system time.
This example Extension Agent implements this
reference with dwTimeZero. DWORD dwTimeZero 0
35Extension Agent DLLs that generate traps must
create a Win32 Event object to communicate
occurrence of traps to the Extendible Agent.
The event handle is given to the Extendible Agent
when the Extension Agent is initialized, it
should be NULL if traps will not be generated.
This example Extension Agent simulates the
occurrence of traps with hSimulateTrap. HANDLE
hSimulateTrap NULL
36Agent DLL Initialization
Extension Agent DLLs provide the following entry
point to coordinate the initializations of the
Extension Agent and the Extendible Agent. The
Extendible Agent provides the Extension Agent
with a time zero reference and the Extension
Agent provides the Extendible Agent with an
Event handle for communicating occurrence of
traps, and an object identifier representing the
root of the MIB subtree that the Extension Agent
supports.
BOOL WINAPI SnmpExtensionInit(
IN DWORD dwTimeZeroReference,
OUT HANDLE hPollForTrapEvent,
OUT AsnObjectIdentifier
supportedView)
37Agent Traps
Create an Event that will be used to communicate
the occurrence of traps to the Extendible Agent.
The Extension Agent will assert this Event when a
trap has occurred. This will be explained
later. if ((hPollForTrapEvent
CreateEvent(NULL, FALSE, FALSE, NULL)) !
NULL) hSimulateTrap
hPollForTrapEvent
38Agent MIB supported
Indicate the MIB view supported by this
Extension Agent, an object identifier
representing the sub root of the MIB that is
supported. supportedView MIB_OidPrefix
39Agent SNMP Query
Extension Agent DLLs provide the following entry
point to resolve queries for MIB variables in
their supported MIB view The requestType is
Get/GetNext/Set.
BOOL WINAPI SnmpExtensionQuery( IN BYTE
requestType, IN OUT RFC1157VarBindList
variableBindings, OUT AsnInteger
errorStatus, OUT AsnInteger errorIndex)
40Agent SNMP Traps
Extension Agent DLLs provide the following entry
point to communicate traps to the Extendible
Agent. The Extendible Agent will query this
entry point when the trap Event (supplied at
initialization time) is asserted, which indicates
that zero or more traps may have occurred. The
Extendible Agent will repeatedly call this entry
point until FALSE is returned, indicating that
all outstanding traps have been processed.
BOOL WINAPI SnmpExtensionTrap( OUT
AsnObjectIdentifier enterprise, OUT
AsnInteger genericTrap, OUT
AsnInteger specificTrap, OUT
AsnTimeticks timeStamp, OUT
RFC1157VarBindList variableBindings)
41Initiating an Agent SNMP Trap
SetEvent(hSimulateTrap)
42Defining an MIB
MIBCC.EXE The SNMP MIB Compiler
OID Object Identifier. This is a unique ID
that is used to identify system objects for
instance, .1.3.6.1.4.1.311 identifies the
Microsoft enterprise.
43 MIB Registration Tree
root
ccitt(0)
iso(1)
joint-iso-ccitt(2)
member body(2)
identified organization(3)
standard(0)
registration authority(1)
dod(6)
Internet(1)
44 Internet subtree
internet(1)
directory(1)
mgmt(2)
experimental(3)
private(4)
enterprises(1)
mib(1)
cisco(9)
microsoft(311)
45 MIB subtree
internet(1)
mgmt(2)
mib(1)
system(1) interfaces(2) at(3) ip(4) icmp(5)
tcp(6) udp(7) egp(8) transmission(10)
snmp(11)
iso.org.dod.internet.mgmt.mib.system.sysDescr
1. 3. 6. 1. 2 . 1.
1. 1
46 MIB Getnext example
C\gtsnmputil getnext DHCPServer public
1.3.6.1.4.1.311.1.3.2.1.1.2. 157.55.80.0
OID 1.3.6.1.4.1.311.1.3.2.1.1.2.172.31.200.0
.iso.org.dod.internet.private.enterprises.microsof
t.software.dhcp .dhcpScope.scopeTable.scopeTableEn
try.noAddInUse.172.31.200.0 COUNTER 9
47 MIB Files example
TOASTER-MIB DEFINITIONS BEGIN IMPORTS
enterprises FROM RFC1155-SMI OBJECT-TYPE FROM
RFC-1212 DisplayString FROM RFC-1213 epilogue
OBJECT IDENTIFIER enterprises 12 toaster
OBJECT IDENTIFIER epilogue 2
toasterManufacturer OBJECT-TYPE SYNTAX
DisplayString ACCESS read-only STATUS
mandatory DESCRIPTION "The name of the toaster's
manufacturer. For instance, Microsoft Toaster.
toaster 1
48MIB Files creation
MIB.BIN - Compiled MIB information used by
MGMTAPI.DLL.
Mib.cc - SNMP MIB compiler that is used to
produce a new Mib.bin file that includes
additional MIBs Perf2Mib.exe MIB builder tool
49SNMP Registry requirements
The windows registry requires entries for SNMP
extension dlls.
The ExtensionAgents registry key describes the
extension agents to load HKEY_LOCAL_MACHINE\SYSTE
M\CurrentControlSet\ Services\SNMP\Parameters\Exte
nsionAgents
50Registry keys
Registry key Type Value
Default Description Unique
String Registry NA
Contains the
subkey. registry subkey
backslashes
from which the must
be used parameters
for in pairs.
the extension agent can be
read.
51Registry key example
Example Registry key 1
SOFTWARE\\Microsoft\\MIBII\\CurrentVersion 2
SOFTWARE\\Microsoft\\HostMIB\\CurrentVer
sion 3 SOFTWARE\\MyCompany\\MyMIB\\C
urrentVersion
A complete entry with key SOFTWARE\\MyCompany\\My
MIB\\CurrentVersion PathName MyMib.dll
52Summary
The Windows platform provides the interfaces
necessary to implement an SNMP MIB without the
need to implement any low level protocol
specifics. It has the ability to be accessed
from third party SNMP management software and to
facilitate Traps.
53References
http//msdn.microsoft.com/library/techart/snmpmib.
htm. (2001) Writing Your Own SNMP Management
Information Base for Microsoft Windows CE 3.0
http//msdn.microsoft.com/library/devprods/vs6/vis
ualc/vcsample/vcsmptestdll.htm. (2001) Testdll
Sample SNMP Extension Agent http//msdn.microsoft.
com/library/default.asp?URL/library/wcedoc/wcecom
m/snmp_36.htm. (2001) SNMP Extension
Agent http//msdn.microsoft.com/library/default.as
p?URL/library/wcedoc/wcecomm/snmp_36.htm. (2001)
Simple Network Management Protocol Agent Support