In the first three articles, we introduced the use of net-snmp to implement the Get/Set command of the agent. The following describes how to send trap messages. When sending a trap message, the default port is 162. The following code can be used to send trap messages.
// This function sends real-time alarm information. Different from sending General Information ports
Void init_alarm_info (void)
{
DEBUGMSGTL ("dcsserver ",
"Initializing (setting callback alarm) \ n "));
// Tpssrunconfigmanager * serverInfo = tpssrunconfigmanager: GetInstance ();
Snmp_alarm_register (5,/* serverInfo-& gt; GetTrapTimeSpan (); * // * seconds, seconds */
SA_REPEAT,/* repeat (every 30 seconds). Send a trap every 30 seconds */
Send_trap,/* our callback function */
NULL/* no callback data needed */
);
}
Void send_trap2 (unsigned int clientreg, void * clientarg)
{
// Use .././2 ./
Static oid oid_cpu_Alarm [] = {1, 3, 6, 1, 4, 1, 9000, 2, 1 };
Static oid oid_memory_Alarm [] = {1, 3, 6, 1, 4, 1, 9000, 2, 2 };
Static oid oid_disk_Alarm [] = {1, 3, 6, 1, 4, 1, 9000, 2, 3 };
Netsnmp_pdu * pdu;
Int status = 0;
// Create pdu
Pdu = snmp_pdu_create (SNMP_MSG_TRAP2); // SNMP_MSG_TRAP
If (! Pdu)
{
CDcslog: GetInstance ()-& gt; dlog_error ("send_trap Failed to create trap PDU ");
Return;
}
If (status! = 0)
{
CDcslog: GetInstance ()-& gt; dlog_error ("send_trap snmp_add_var () error ");
Snmp_free_pdu (pdu );
Return;
}
// SNMPV2 Version 2
// Status = create_trap_session ("127.0.0.1", SNMP_TRAP_PORT, "public", SNMP_VERSION_2c, SNMP_MSG_TRAP2 );
// When the second parameter of SNMPV1 is SNMP_TRAP_PORT, a warning is reported. The value should be 0.
Status = create_trap_session (DEFAULT_ADDR, 0, "public", SNMP_VERSION_1, SNMP_MSG_TRAP );
// When the second parameter is SNMP_TRAP_PORT, there is a warning. It should be 0
If (status = 0)
{
CDcslog: GetInstance ()-& gt; dlog_error ("send_trap create_trap_session () error ");
Snmp_free_pdu (pdu );
Return;
}
// Add the variable to be sent here. You can add the sending logic.
Bool flag = false;
// When the cpu usage is greater than 85%, add it to the trap list
If (GetUsedCPU () & gt; 85)
{
Flag = true;
Status = snmp_add_var (pdu, oid_cpu_Alarm, OID_LENGTH (oid_cpu_Alarm),'s, "cpu Alarming ");
}
// Memory
If (GetUsedMemory () & gt; 85)
{
Flag = true;
Status = snmp_add_var (pdu, oid_memory_Alarm, OID_LENGTH (oid_memory_Alarm),'s, "memory Alarming ");
}
// Disk: c:
If (GetUsedDisk () & gt; 85)
{
Flag = true;
Status = snmp_add_var (pdu, oid_disk_Alarm, OID_LENGTH (oid_disk_Alarm),'s, "disk Alarming ");
}
// If the list contains content, the message is sent.
If (flag)
{
Send_trap_vars (1, 0, pdu-& gt; variables );
}
// Release resources
Snmpd_free_trapsinks ();
Snmp_free_pdu (pdu );
}
The trap messages sent in this way are flexible. Http://vcsky.net (havenzhao)
In the net-snmp source code, the notification. c file also sends the trap message. It uses the send_v2trap () api, but you must enable the snmp trap service to send the message successfully.
So far, we have fully introduced the technology for using net-snmp to implement agent extension in windows. Because the contact time is not long, it is inevitable that there are incorrect or negligent places, please forgive me.