snmp trap程式設計之分析notification.c
需要檔案:
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目錄下
-
安裝notification:
./configure --with-mib-modules="examples/notification" make sudo make install
-
配置snmp.conf檔案,在檔案中增加NET-SNMP-EXAMPLES-MIB mib庫
sudo vim /usr/local/share/snmp/snmp.conf
在檔案中增加: mibs +NET-SNMP-EXAMPLES-MIB
-
驗證netSnmpExampleNotifications mib庫是否正常載入:
snmptranslate -IR -Tp netSnmpExampleNotifications +--netSnmpExampleNotifications(3) | +--netSnmpExampleNotificationPrefix(0) | | | +--netSnmpExampleHeartbeatNotification(1) | +-- ---N String netSnmpExampleNotification(1) | Textual Convention: SnmpAdminString | Size: 0..255 | +--netSnmpExampleNotificationObjects(2) | +-- ---N Integer32 netSnmpExampleHeartbeatRate(1) +-- ---N String netSnmpExampleHeartbeatName(2) Textual Convention: SnmpAdminString Size: 0..255
-
配置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
- agent自動產生trap
# 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
-
啟動snmptrapd
sudo snmptrapd –d –f –Lo
-
啟動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 [email protected] 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
-
notification.c 原始碼如下:
/** @example notification.c * This example shows how to send a notification from inside the * agent. In this case we do something really boring to decide * whether to send a notification or not: we simply sleep for 30 * seconds and send it, then we sleep for 30 more and send it again. * We do this through the snmp_alarm mechanisms (which are safe to * use within the agent. Don't use the system alarm() call, it won't * work properly). Normally, you would probably want to do something * to test whether or not to send an alarm, based on the type of mib * module you were creating. * * When this module is compiled into the agent (run configure with * --with-mib-modules="examples/notification") then it should send * out traps, which when received by the snmptrapd demon will look * roughly like: * * 可以通過 --with-mib-modules="examples/notification" 把這個模組 * 編譯到agent模組中,snmptrapd可以接收到他傳送的traps, 接收到的資訊 * 如下: * * 2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]: * sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 \ * snmpTrapOID.0 = OID: netSnmpExampleNotification * */ /* * start be including the appropriate header files */ #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-includes.h> #include <net-snmp/agent/net-snmp-agent-includes.h> /* * contains prototypes */ #include "notification.h" /* * our initialization routine 初始化 * (to get called, the function name must match init_FILENAME() * 函式的名字必須是 init_FILENAME() 這種格式 */ void init_notification(void) { DEBUGMSGTL(("example_notification", "initializing (setting callback alarm)\n")); snmp_alarm_register(30, /* seconds, 秒 */ SA_REPEAT, /* repeat (every 30 seconds). 每隔30秒傳送一個trap*/ send_example_notification, /* our callback 我們的回撥函式 */ NULL /* no callback data needed */ ); } /** here we send a SNMP v2 trap (which can be sent through snmpv3 and * snmpv1 as well) and send it out. * * The various "send_trap()" calls allow you to specify traps in different * formats. And the various "trapsink" directives allow you to specify * destinations to receive different formats. * But *all* traps are sent to *all* destinations, regardless of how they * were specified. * * * I.e. it's * @verbatim * ___ trapsink * / * send_easy_trap \___ [ Trap ] ____ trap2sink * ___ [ Generator ] * send_v2trap / [ ] ----- informsink * \____ * trapsess * * *Not* * send_easy_trap -------------------> trapsink * send_v2trap -------------------> trap2sink * ???? -------------------> informsink * ???? -------------------> trapsess * @endverbatim */ void send_example_notification(unsigned int clientreg, void *clientarg) { /* * define the OID for the notification we're going to send * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification */ oid notification_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 }; size_t notification_oid_len = OID_LENGTH(notification_oid); static u_long count = 0; /* * In the notification, we have to assign our notification OID to * the snmpTrapOID.0 object. Here is it's definition. */ oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 }; size_t objid_snmptrap_len = OID_LENGTH(objid_snmptrap); /* * define the OIDs for the varbinds we're going to include * with the notification - * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate and * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName */ oid hbeat_rate_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 }; size_t hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid); oid hbeat_name_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 }; size_t hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid); /* * here is where we store the variables to be sent in the trap */ netsnmp_variable_list *notification_vars = NULL; const char *heartbeat_name = "A girl named Maria"; #ifdef RANDOM_HEARTBEAT int heartbeat_rate = rand() % 60; #else int heartbeat_rate = 30; #endif DEBUGMSGTL(("example_notification", "defining the trap\n")); /* * add in the trap definition object */ snmp_varlist_add_variable(¬ification_vars, /* * the snmpTrapOID.0 variable */ objid_snmptrap, objid_snmptrap_len, /* * value type is an OID */ ASN_OBJECT_ID, /* * value contents is our notification OID */ (u_char *) notification_oid, /* * size in bytes = oid length * sizeof(oid) */ notification_oid_len * sizeof(oid)); /* * add in the additional objects defined as part of the trap */ snmp_varlist_add_variable(¬ification_vars, hbeat_rate_oid, hbeat_rate_oid_len, ASN_INTEGER, (u_char *)&heartbeat_rate, sizeof(heartbeat_rate)); /* * if we want to insert additional objects, we do it here */ if (heartbeat_rate < 30 ) { snmp_varlist_add_variable(¬ification_vars, hbeat_name_oid, hbeat_name_oid_len, ASN_OCTET_STR, heartbeat_name, strlen(heartbeat_name)); } /* * send the trap out. This will send it to all registered * receivers (see the "SETTING UP TRAP AND/OR INFORM DESTINATIONS" * section of the snmpd.conf manual page. */ ++count; DEBUGMSGTL(("example_notification", "sending trap %ld\n",count)); send_v2trap(notification_vars); // 傳送snmpv2的trap /* * free the created notification variable list */ DEBUGMSGTL(("example_notification", "cleaning up\n")); snmp_free_varbind(notification_vars); }
相關推薦
snmp trap程式設計之分析notification.c
需要檔案: NET-SNMP-EXAMPLES-MIB.txt notification.c 這兩個檔案都在net-snmp原始碼包裡,我的版本是5.4.1 NET-SNMP-EXAMPLES-MIB.txt net-snmp-5.4.1.2/mibs目錄下 not
ARM彙編與C語言混合程式設計之彙編呼叫C函式
呼叫沒有引數的函式 呼叫有引數的函式 總結 本文所用硬體平臺為S3C2440開發板。通過一個點亮數碼管的程式說明ARM彙編呼叫C函式的方法。 根據C語言中函式引數的個數,可以將彙編呼叫C函式分為兩種情況,呼叫沒有引數的函式和呼叫有引數的
基於Visual C++之Windows核心程式設計程式碼分析(1)實現裝置管理器列舉裝置
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
基於visual c++之windows核心程式設計程式碼分析(21)獲取和設定環境變數
環境變數是一個具有特定名字的物件,它包含了一個或者多個應用程式所將使用到的資訊。例如path,當要求系統執行一個程式而沒有告訴它程式所在的完整路徑時,系統除了在當前目錄下面尋找此程式外,還應到path中指定的路徑去找。使用者通過設定環境變數,來更好的執行程序。 環境變數一
基於visual c++之windows核心程式設計程式碼分析(18)遠端程式碼注入執行
我們進行系統級別的安全監控的時候,防範木馬的時候,經常需要進行遠端程式碼注入執行。執行步驟如下1. 提升程序許可權,如果許可權不夠的話,很容易造成 OpenProcess 失敗;2. 確定你的宿主程序,即你所要注入程式碼的程序,這個其實很好辦,你要是不想你的木馬或者病毒被別個
基於visual c++之windows核心程式設計程式碼分析(61)打造自己的Windows輸入法
IMM(Input Method Manager)只在安裝了亞洲語言包之後才能使用。通過呼叫GetSystemMetrics(SM_IMMENABLED)知道IMM是否使能。一共由三部分組成:status window 輸入法狀態列 表示正在處於中文輸入狀態可以知道是什
硬件層監控之Zabbix-snmp-trap企業級實戰
硬件層監控監控對象: 服務器存儲交換機路由器防洪墻 監控數據采集方法: SNMP trap 目前我監控以下的硬件信息:1、cpu處理器狀態2、cpu省電模式狀態(如果開啟了省電模式,在壓力大的時候,會很卡的)3、raid狀態(比如做了哪個raid模式,raid狀態是否正常)4、內存狀態(可以查看當前
C# socket 程式設計之 accept() 函式返回值解析
accept() 函式會返回一個新的套接字,這個新的套接字在伺服器端與客戶端進行通訊。 伺服器端的繫結監聽是一個套接字,與客戶端通訊的是另一個套接字(accept函式返回的套接字,注意這裡不是返回客戶端的套接字,返回的套接字是新建立在伺服器上的,與客戶端收發訊息用的) 下面這段程式碼,是
Python3 與 C# 網路程式設計之~ 網路基礎篇
最新版本檢視:https://www.cnblogs.com/dotnetcrazy/p/9919202.html 入門篇 官方文件:https://docs.python.org/3/library/ipc.html(程序間通訊和網路) 例項程式碼:https://github.com/lotapp/
資料結構之連結串列C語言實現以及使用場景分析
連結串列是資料結構中比較基礎也是比較重要的型別之一,那麼有了陣列,為什麼我們還需要連結串列呢!或者說設計連結串列這種資料結構的初衷在哪裡? 這是因為,在我們使用陣列的時候,需要預先設定目標群體的個數,也即陣列容量的大小,然而實時情況下我們目標的個數我們是不確定的,因此我們總是要把陣列的容量設定的
程式設計菜鳥到大佬之路:C語言程式(十二)
第十二天學習精要 遞迴初步 遞迴 一個函式,自己呼叫自己,就是遞迴。 # include <iostream> using namespace std; int factorial(int n) // 函式返回n的階乘 { if (n ==
現代C語言程式設計之資料計算
現代C語言程式設計之資料計算 C語言程式設計 3.1 運算子概述 計算機最核心的任務就是完成資料的計算,C語言提供了豐富(多達34種)的運算子來實現不同資料的各種運算,之前在求資料型別的大小時已經使用過的sizeof()
認清C/C++程式設計之-----異或運算
使用異或進行資料交換,很早以前有的程式設計師使用下面的方法進行兩個資料之間的交換 a^=b;b^=a;a^=b;即a^=b^=a^=b;只有在兩個比較的位不同時其結果為1,否則為0即[兩個輸入相同時為0,不同時為1] &nbs
洞悉C++網路程式設計之tcp/ip和socket api
原文地址:https://blog.csdn.net/libaineu2004/article/details/79020403 TCP(Transmission Control Protocol) 傳輸控制協議 三次握手 TCP是主機對主機層的傳輸控制協議,提供可靠的連線服務,採用三次
Python3 與 C# 併發程式設計之~ 執行緒上篇
2.2.加強篇¶ 其實以前的Linux中是沒有執行緒這個概念的,Windows程式設計師經常使用執行緒,這一看~方便啊,然後可能是當時程式設計師偷懶了,就把程序模組改了改(這就是為什麼之前說Linux下的多程序程式設計其實沒有Win下那麼“重量級”),弄了個精簡版程序==>執行緒(核心是分不出程序
程式設計菜鳥到大佬之路:C語言程式(五)
第五天學習精要 關係運算符和邏輯表示式 關係運算符 六種關係運算符用於數值的比較:相等 ==、不等 !=、大於 >、小於 <、大於等於 >=、小於等於 <=。 比較的結果是bool型別,成立則為true,反之為false。
程式設計菜鳥到大佬之路:C語言程式(六)
第六天學習精要 if語句 條件分支結構之if 語句 有時,並非所有的程式語句都要被順序執行到,會希望滿足某種條件就執行這部分語句,滿足另一條件就執行另一部分語句,這就需要“條件分支結構”。 依次計算表示式1、表示式2…只要碰到一個表示式i為真,則執行語
程式設計菜鳥到大佬之路:C語言程式(七)
第七天學習精要 for迴圈 for迴圈語句 for迴圈一般用於將某段程式碼(語句組)重複執行若干次。 第一步:計算“表示式1”。 第二步:計算“表示式2”,若其值為true,則執行“{ }”中的語句組,然後轉到第三步;若為false,則不再執行“{}”中的
c++ 網路程式設計之socket
windows 10 structures sockaddr, sockaddr_in sockaddr 和 sockaddr_in 同樣都是為了處理網路通訊的地址,包含了地址類別(familty),地址(ip),埠資訊。 sockaddr是給機器用的,
程式設計菜鳥到大佬之路:C語言程式(八)
第八天學習精要 break語句和continue語句 break語句 可以出現在迴圈體中(for、 while、 do…while迴圈均可),其作用是跳出迴圈。 在多重迴圈的情況下,break語句只能跳出直接包含它的那一重迴圈。 例題:如果兩個不同