1. 程式人生 > 實用技巧 >如何根據MIB庫自動生成SNMP Agent程式碼--mib2c

如何根據MIB庫自動生成SNMP Agent程式碼--mib2c

一、環境準備

1、linux,samba,網路已連線

通過samba登入到fedora core的虛擬機器上,啟動虛擬機器Fodera core,

ifconfig 檢視eth1網絡卡的ip地址,eth1為橋接到辦公網的網絡卡,假設IP地址為10.10.51.8

通過samba對映到windows,需要先檢查samba服務是否已經啟動

若已啟動samba服務,對映samba目錄

2、MIB庫檔案

二、操作步驟

1、編寫MIB庫檔案

編寫mib檔案,並將mib檔案拷貝至/user/local/share/snmp/mibs目錄下

編寫mib庫的時候如果是表格,不可增刪改的。不需要新增rowstatus,且index必須要設定成read-only,不能設定成not-accessiable

2、設定環境變數,使得可以用mib2c工具來生成相應的.c檔案和.h檔案

cd /user/local/share/snmp/mibs
export MIBS=ALL
或
export MIBS=+NSCRTV-ROOT
export MIBS=+NSCRTV-EPONEOC-ALARMS-MIB
export MIBS=+NSCRTV-EPONEOC-PROPERTY-MIB
export MIBS=+NSCRTV-EPONEOC-MOD-EOC-MIB
export MIBS=+XXX-MIB # 根據實際的MIB庫名稱調整

3、檢視OID是否能正常輸出

snmptranslate -Ts -m ALL //
檢視所有MIB NODE輸出情況 snmptranslate -IR XXXMIBNODE //就可以檢視到某個具體的MIB NODE的相關資訊

4、模板介紹:通過模板生成.c和.h檔案

模板可以採用預設的模板或者自己寫模板,預設的模板在/user/local/share/snmp/目錄下面
進入你要存放生成程式碼的資料夾,通過模板生成.c和.h檔案,預設的模板介紹如下:
scalars型別的節點:mib2c -c mib2c.scalar.conf XXXMIBNODE
Table型別的節點:mib2c -c mib2c.iterate_access.conf XXXMIBNODE
trap型別的節點:mib2c -c mib2c.notify.conf XXXMIBNODE


int scalars型別的節點:mib2c -c mib2c.int_watch.conf MIBNODE

承上,如果按需排序時:mib2c -c mib2c.array-user.conf MIBNODE
如果要產生Table上任一Column的header時:mib2c -c mib2c.column_defines.conf MIBNODE
如果是要產生Column上任一Enum的header時:mib2c -c mib2c.column_enums.conf MIBNODE

命令參考:

env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../../mib2c.scalar.conf eocCBATAdminGroup
env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../mib2c.iterate_access.conf eocCBATCardRFTable
env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../mib2c.iterate_access.conf eocCBATAdminSNMPCommunityTable
env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../../mib2c.notify.conf eocNotifications

5、生成具體過程:

進入你要存放生成程式碼的資料夾,所有的模板在/user/local/share/snmp/目錄下面

# mib2c -c ../../../mib2c.scalar.conf eocCBATAdminGroup
# env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../mib2c.iterate_access.conf eocCBATCardRFTable
# env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../../mib2c.iterate_access.conf eocCBATAdminSNMPCommunityTable
# env MIBS="+NSCRTV-EPONEOC-MOD-EOC-MIB" mib2c -c ../../mib2c.iterate_access.conf eocCBATCardRFTable
    writing to eocCBATCardRFTable.h
    writing to eocCBATCardRFTable_columns.h
    writing to eocCBATCardRFTable_enums.h
    writing to eocCBATCardRFTable.c
    writing to eocCBATCardRFTable_checkfns_local.h
    writing to eocCBATCardRFTable_checkfns_local.c
    writing to eocCBATCardRFTable_checkfns.h
    writing to eocCBATCardRFTable_checkfns.c
    writing to eocCBATCardRFTable_access.h
    writing to eocCBATCardRFTable_access.c
    writing to -

    **********************************************************************
    NOTE:  The only files you MUST modify should be the following:(要修改的地方)
      eocCBATCardRFTable_access.c(業務相關)
      eocCBATCardRFTable_access.h(路徑相關)
      eocCBATCardRFTable_checkfns_local.h()
      eocCBATCardRFTable_checkfns_local.c
    **********************************************************************

    running indent on eocCBATCardRFTable_access.c
    running indent on eocCBATCardRFTable_checkfns.c
    running indent on eocCBATCardRFTable_access.h
    running indent on eocCBATCardRFTable_enums.h
    running indent on eocCBATCardRFTable_columns.h
    running indent on eocCBATCardRFTable_checkfns_local.h
    running indent on eocCBATCardRFTable_checkfns_local.c
    running indent on eocCBATCardRFTable_checkfns.h
    running indent on eocCBATCardRFTable.h
    running indent on eocCBATCardRFTable.c
# 

6.修改程式碼,將修改後的程式碼拷貝至專案net-snmp-5.4.1/agent/mibgroup下面

1.xxxTable_checkfns.h:
修改前:
/** make sure we load the functions that you can modify */
config_require(eocVLANTable_checkfns_local)
修改後:修改後的路徑為實際工程中的編譯路徑
/** make sure we load the functions that you can modify */
config_require(eoc/nscrtv/vlanGroup/eocVLANTable_checkfns_local)
2.xxxTable.h
修改前:
/** other required module components */
config_require(eocVLANTable_access)
config_require(eocVLANTable_checkfns)
修改後:
/** other required module components */
config_require(eoc/nscrtv/vlanGroup/eocVLANTable_access)
config_require(eoc/nscrtv/vlanGroup/eocVLANTable_checkfns)
3.eocVLANTable.c
修改前:
if (!data_context) {
ci->data_context = eocVLANTable_create_data_context(table_info->indexes, COLUMN_):
ci->new_row = 1;
} else {
ci->data_context = data_context;
}
netsnmp_oid_stash_add_data(&commitStorage,
suffix + 1, suffix_len - 1, ci);
修改後:
if (!data_context) {
ci->data_context = eocVLANTable_create_data_context(table_info->indexes, table_info->colnum);
ci->new_row = 1;
} else {
ci->data_context = data_context;
}
netsnmp_oid_stash_add_data(&commitStorage,
suffix + 1, suffix_len - 1, ci);

注意事項:
生成表項的C程式碼的時候執行完命令mib2c ../../mib2c.iterate_access.conf wirelessProdWireless5GTable以後,會提示讓你選擇一些選項:選2 1 2