snmp学习笔记之二trap——分析notification.c文件 November 19th, 2008 BianJiang Leave a comment Go to comments
By: Bian Jiang (borderj@gmail.com) From: http://www.b0rder.com Date: 2008.11.11
需要文件:
NET-SNMP-EXAMPLES-MIB.txt notification.c
这两个文件都在net-snmp源码包里,我的版本是5.4.1
NET-SNMP-EXAMPLES-MIB.txt net-snmp-5.4.1.2/mibs目录下 notification.c net-snmp-5.4.1.2/agent/mibgroup/examples目录下
1. 安装notification:
2. 3.
./configure --with-mib-modules=\"examples/notification\" make
sudo make install
4. 配置snmp.conf文件,在文件中增加NET-SNMP-EXAMPLES-MIB mib库
sudo vim /usr/local/share/snmp/snmp.conf
在文件中增加: mibs +NET-SNMP-EXAMPLES-MIB
5. 验证netSnmpExampleNotifications mib库是否正常加载:
6. 7.
snmptranslate -IR -Tp netSnmpExampleNotifications +--netSnmpExampleNotifications(3)
8. 9.
|
+--netSnmpExampleNotificationPrefix(0)
10. | |
11. | +--netSnmpExampleHeartbeatNotification(1) 12. |
13. +-- ---N String netSnmpExampleNotification(1) 14. | Textual Convention: SnmpAdminString 15. | Size: 0..255 16. |
17. +--netSnmpExampleNotificationObjects(2) 18. |
19. +-- ---N Integer32 netSnmpExampleHeartbeatRate(1) 20. +-- ---N String netSnmpExampleHeartbeatName(2) 21. Textual Convention: SnmpAdminString Size: 0..255
22. 配置snmptrapd.conf
建立/usr/share/snmp/snmptrapd.conf(我的机器上是这个,不同机器不同,可能有的放在/etc/snmp,/usr/local/share/snmp/下,视不同情况慢慢实验),加入以下一行:
authcommunity execute,log,net public
设置所有用户的访问权限:可执行,记录,传递,
如果相对接受到的信息处理可以增加:
traphandle .1.3.6.1.4.1.2021.251.2 page_me down # 默认处理函数
traphandle default log_it
1. agent自动产生trap
配置agent的snmpd.conf,加入以下几行:(参考:
http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17 ):
# From: http://www.net-snmp.org/wiki/index.php/FAQ:Agent_17 # send v1 traps
trapsink 127.0.0.1:162 # also send v2 traps trap2sink 127.0.0.1:162 informsink 127.0.0.1:162
1. 启动snmptrapd
sudo snmptrapd –d –f –Lo
2. 启动snmpd
sudo snmpd -f -L
snmpd 会每隔30秒给snmptrapd发送一个信息。收到的信息如下:
Received 64 bytes from UDP: [127.0.0.1]:56929
0000: 30 3E 02 01 00 04 06 70 75 62 6C 69 63 A4 31 06 0>.....public.1.
0016: 09 2B 06 01 04 01 BF 08 02 03 40 04 AC 10 81 01 .+........@.....
0032: 02 01 06 02 01 01 43 03 03 CC BC 30 13 30 11 06 ......C....0.0..
0048: 0C 2B 06 01 04 01 BF 08 02 03 02 01 00 02 01 1E .+..............
2008-11-11 15:43:11 172.16.129.1(via UDP: [127.0.0.1]:56929) TRAP, SNMP v1, community public
NET-SNMP-EXAMPLES-MIB::netSnmpExampleNotifications Enterprise Specific Trap
(NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification) Uptime: 0:41:30.20
NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate.0 = INTEGER: 30
1. notification.c 源码如下:
2. 3.
/** @example notification.c
* This example shows how to send a notification from inside the
4. * agent. In this case we do something really boring to decide
5. * whether to send a notification or not: we simply sleep for 30
6. * seconds and send it, then we sleep for 30 more and send it again.
7. * We do this through the snmp_alarm mechanisms (which are safe to
8. * use within the agent. Don't use the system alarm() call, it won't
9. * work properly). Normally, you would probably want to do something
10. * to test whether or not to send an alarm, based on the type of mib
11. * module you were creating. 12. *
13. * When this module is compiled into the agent (run configure with
14. * --with-mib-modules=\"examples/notification\") then it should send
15. * out traps, which when received by the snmptrapd demon will look
16. * roughly like: 17. *
18. * 可以通过 --with-mib-modules=\"examples/notification\" 把这个模块
19. * 编译到agent模块中,snmptrapd可以接收到他发送的traps, 接收到的信息 20. * 如下: 21. *
22. * 2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]:
23. * sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 \\ 24. * snmpTrapOID.0 = OID: netSnmpExampleNotification 25. * 26. */ 27. 28. /*
29. * start be including the appropriate header files
30. */
31. #include 33. #include 36. * contains prototypes 37. */ 38. #include \"notification.h\" 39. 40. /* 41. * our initialization routine 初始化 42. * (to get called, the function name must match init_FILENAME() 43. * 函数的名字必须是 init_FILENAME() 这种格式 44. */ 45. void 46. init_notification(void) 47. { 48. DEBUGMSGTL((\"example_notification\ 49. \"initializing (setting callback alarm)\\n\")); 50. snmp_alarm_register(30, /* seconds, 秒 */ 51. SA_REPEAT, /* repeat (every 30 seconds). 每隔30秒发送一个trap*/ 52. send_example_notification, /* our callback 我们的回调函数 */ 53. NULL /* no callback data needed */ 54. ); 55. } 56. 57. /** here we send a SNMP v2 trap (which can be sent through snmpv3 and 58. * snmpv1 as well) and send it out. 59. * 60. * The various \"send_trap()\" calls allow you to specify traps in different 61. * formats. And the various \"trapsink\" directives allow you to specify 62. * destinations to receive different formats. 63. * But *all* traps are sent to *all* destinations, regardless of how they 64. * were specified. 65. * 66. * 67. * I.e. it's 68. * @verbatim 69. * ___ trapsink 70. * / 71. * send_easy_trap \\___ [ Trap ] ____ trap2sink 72. * ___ [ Generator ] 73. * send_v2trap / [ ] ----- informsink 74. * \\____ 75. * trapsess 76. * 77. * *Not* 78. * send_easy_trap -------------------> trapsink 79. * send_v2trap -------------------> trap2sink 80. * ???? -------------------> informsink 81. * ???? -------------------> trapsess 82. * @endverbatim 83. */ 84. void 85. send_example_notification(unsigned int clientreg, void *clientarg) 86. { 87. /* 88. * define the OID for the notification we're going to send 89. * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification 90. */ 91. oid notification_oid[] = 92. { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 }; 93. size_t notification_oid_len = OID_LENGTH(notification_oid); 94. static u_long count = 0; 95. 96. /* 97. * In the notification, we have to assign our notification OID to 98. * the snmpTrapOID.0 object. Here is it's definition. 99. */ 100. oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; 101. size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap); 102. 103. /* 104. * define the OIDs for the varbinds we're going to include 105. * with the notification - 106. * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate and 107. * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName 108. */ 109. oid hbeat_rate_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 }; 110. size_t hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid); 111. oid hbeat_name_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 }; 112. size_t hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid); 113. 114. /* 115. * here is where we store the variables to be sent in the trap 116. */ 117. netsnmp_variable_list *notification_vars = NULL; 118. const char *heartbeat_name = \"A girl named Maria\"; 119. #ifdef RANDOM_HEARTBEAT 120. int heartbeat_rate = rand() % 60; 121. #else 122. int heartbeat_rate = 30; 123. #endif 124. 125. DEBUGMSGTL((\"example_notification\\")); 126. 127. /* 128. * add in the trap definition object 129. */ 130. snmp_varlist_add_variable(¬ification_vars, 131. /* 132. * the snmpTrapOID.0 variable 133. */ 134. objid_snmptrap, objid_snmptrap_len, 135. /* 136. * value type is an OID 137. */ 138. ASN_OBJECT_ID, 139. /* 140. * value contents is our notification OID 141. */ 142. (u_char *) notification_oid, 143. /* 144. * size in bytes = oid length * sizeof(oid) 145. */ 146. notification_oid_len * sizeof(oid)); 147. 148. /* 149. * add in the additional objects defined as part of the trap 150. */ 151. 152. snmp_varlist_add_variable(¬ification_vars, 153. hbeat_rate_oid, hbeat_rate_oid_len, 154. ASN_INTEGER, 155. (u_char *)&heartbeat_rate, 156. sizeof(heartbeat_rate)); 157. 158. /* 159. * if we want to insert additional objects, we do it here 160. */ 161. if (heartbeat_rate < 30 ) { 162. snmp_varlist_add_variable(¬ification_vars, 163. hbeat_name_oid, hbeat_name_oid_len, 164. ASN_OCTET_STR, 165. heartbeat_name, strlen(heartbeat_name)); 166. } 167. 168. /* 169. * send the trap out. This will send it to all registered 170. * receivers (see the \"SETTING UP TRAP AND/OR INFORM DESTINATIONS\" 171. * section of the snmpd.conf manual page. 172. */ 173. ++count; 174. DEBUGMSGTL((\"example_notification\count)); 175. send_v2trap(notification_vars); // 发送snmpv2的trap 176. 177. /* 178. * free the created notification variable list 179. */ 180. DEBUGMSGTL((\"example_notification\181. snmp_free_varbind(notification_vars); } 因篇幅问题不能全部显示,请点此查看更多更全内容