net-snmp sample code to parse MIB file and extract trap related information from it

北城余情 提交于 2019-12-18 16:59:42

问题


I am using the net-snmp C library on Windows. I want to parse trap-related information from the MIB files.

I need some sample code to do this. I didn't find anything useful on http://www.net-snmp.org/


回答1:


Here is some sample code to parse MIB files using the net-snmp library. Before you use this code you need to refer or add the Include and Lib directories of net-snmp in your project properties:

#include "stdafx.h"
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/definitions.h>
#include <net-snmp/library/tools.h>
#include <net-snmp/mib_api.h>
#include <net-snmp/library/mib.h>
#include <net-snmp/library/parse.h>

int _tmain(int argc, _TCHAR* argv[])
{
    struct tree *tp;
    struct tree *tree_head = NULL ;
    FILE *fp = NULL;

    char str[] = "c:\\MyMIBFileDir\\My.mib";

    netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_WARNINGS , 2);
    netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SAVE_MIB_DESCRS);

    netsnmp_init_mib();

    add_mibdir("c:\\MyMIBFileDir\\");

    tree_head = read_mib(str);

    if ( tree_head )
    {
        //Successfully parsed the MIB
    }

    // Full traversal of the subtree
    for (tp = tree_head; tp; tp = tp->next_peer)
        // Here you can do custom parsing

    fclose(fp);

    return 0;
}


来源:https://stackoverflow.com/questions/7330168/net-snmp-sample-code-to-parse-mib-file-and-extract-trap-related-information-from

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!