Good day. It will be about using the SNMP protocol, but a bit different from what everyone is used to. Wikipedia reports that this “protocol is commonly used in network management systems to monitor devices connected to a network for conditions that require administrator attention”. Such devices are often routers, hubs, switches, bridges, printers and, of course, computers themselves. To implement the monitoring function, on each such device there is an SNMP agent, whose immediate duties include collecting information and transferring it to the manager upon request.

Initially, this feature is implemented as an SNMP service, which takes care of all the rough work related to the details of the protocol. All information available to the agent is located in the management information bases (MIB), where each device parameter is assigned a unique identifier, called an OID. Thus, in order to read the required value of the network device parameter, the manager needs to make a request in which to indicate the OID of this parameter. The agent, having received the request, finds this parameter in the MIB and returns the value to the manager. Everything is very simple.
If we need to extend the basic functionality of the agent, then we need to write an extension agent with the required capabilities. Physically, the extension agent is a dll, which exports at least 3 functions:
')
- SnmpExtensionInit
- SnmpExtensionQuery
- SnmpExtensionTrap
I will explain the work of the extension agent in the figure:

Suppose that our extension agent is installed on a regular computer. When the OS is loaded, SnmpExtensionInit is called, as a result of which the agent prepares for work. First, it indicates the “zone of its responsibility”, i.e. range of OIDs, when queried for which this particular agent should be called. In addition, other preparatory activities can be carried out, for example, loading in memory of parameter values to be monitored by the manager, etc.
After the manager sends the request, the SNMP service establishes that the request is “under the responsibility” of our agent and “pulls” the corresponding dll. And our agent, in accordance with the request, produces either a reading of the parameter or the recording of a new value.
To register an agent in the system, you need to create a string parameter in the HKLM-> System-> CurrentControlSet-> services-> SNMP-> Parameters-> ExtensionAgents section with the next in order number.

The value of this parameter will be the registry path to the string parameter Pathname, which should be located in the Software section. For example, "Software \ Test \ SnmpExtAgent \ CurrentVersion". Those. we need to create in the Software 3 sections: Test, SnmpExtAgent and CurrentVersion. Then, create a string parameter Pathname, the value of which will be the path to our dll'ki.

Naturally, we must have the SNMP service installed on our computer. If it is not there, then go to Add or Remove Programs "->" Installing Windows Components ". There we tick the" Management and Monitoring Tools "and install it. In Win XP, you need the installation disk, in Win 7 it goes without it.
Next, go to My Computer-> Management-> Services. We find the treasured line "SNMP Service" and in the properties, on the "Security" tab, create a public community with READ WRITE rights. Restart the service, the agent is ready to work.
Now for some code.
SnmpExtensionInit function:
BOOL SNMP_FUNC_TYPE SnmpExtensionInit( DWORD dwUptimeReference, HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion) { *pFirstSupportedRegion = MIB_OidPrefix; // " " *phSubagentTrapEvent = g_hSimulateTrap;// // g_szAbout = (char*)malloc(sizeof(char)*64); strcpy(g_szAbout,"Author : Ramanan.T"); g_szName = (char*)malloc(sizeof(char)*64); strcpy(g_szName,"Your Name"); g_asnIntAge = 0; g_dwStartTime = GetTickCount(); return SNMPAPI_NOERROR; }
SnmpExtensionQuery function:
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery( BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex) { int nRet = 0; AsnObjectName; *pErrorStatus = SNMP_ERRORSTATUS_NOERROR; *pErrorIndex = 0; for(UINT i=0;i<pVarBindList->len;i++) { *pErrorStatus = SNMP_ERRORSTATUS_NOERROR; // switch(bPduType) { case SNMP_PDU_GET: // GET *pErrorStatus = GetRequest(&pVarBindList->list[i]); if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR) *pErrorIndex++; break; case SNMP_PDU_GETNEXT: // GETNEXT *pErrorStatus = GetNextRequest(&pVarBindList->list[i]); if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR) *pErrorIndex++; break; case SNMP_PDU_SET: // SET *pErrorStatus = SetRequest(&pVarBindList->list[i]); if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR) *pErrorIndex++; break; default: *pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME; *pErrorIndex++; }; } return SNMPAPI_NOERROR; }
We see that, using the
pVarBindList structure, the OID of the required parameter comes to us and in the
GetRequest and
GetNextRequest functions we write the values of these parameters in the same structure.
So, what happens in the end. We can write dll, which will be called by the system in case of receiving requests over the network. But what we are going to do inside this very
SnmpExtensionQuery is our business. That is, we, having received a request from the manager, have the opportunity to perform arbitrary actions. Considering that the SNMP service is available on almost all computers, we obtain quite wide possibilities for managing technical equipment running computers, i.e. any
Thanks for attention.
References:
1.
www.codeproject.com/Articles/9024/How-to-develop-a-SNMP-extension-agent-DLL