SNMP - C - 从MIB实现子树

时间:2022-09-17 09:46:19

I've been working on my own SNMP agent using the example found here : http://www.net-snmp.org/dev/agent/example_8c_source.html

我一直在使用我在这里找到的示例开发自己的SNMP代理:http://www.net-snmp.org/dev/agent/example_8c_source.html

I am wanting to better organize my tree structure to make more sense which in turn makes using client commands easier.

我想更好地组织我的树结构以使其更有意义,这反过来使得使用客户端命令更容易。

I am using the traditional old C API to achieve this and is what is used in the example link.

我使用传统的旧C API来实现这一点,并在示例链接中使用。

I have a tree I want to implement

我有一棵树想要实现

SNMP  -  C  - 从MIB实现子树

My MIB :

我的MIB:

    MIB-NAME-HERE DEFINITIONS ::= BEGIN

    IMPORTS
        MODULE-IDENTITY, OBJECT-TYPE, Integer32, enterprises,
        NOTIFICATION-TYPE                       FROM SNMPv2-SMI
        OBJECT-GROUP, NOTIFICATION-GROUP        FROM SNMPv2-CONF
    ;

    myProduct MODULE-IDENTITY
        LAST-UPDATED "201505200000Z"
        ORGANIZATION "www.example.com"
        CONTACT-INFO
             "email: support@example.com"
        DESCRIPTION
            "MIB Example."
        REVISION     "201505200000Z"
        DESCRIPTION
            "version 1.0"
        ::= { enterprises 54321 }

    --
    -- top level structure
    --
       IPConfig OBJECT IDENTIFIER ::= { myProduct 1 }
       Services OBJECT IDENTIFIER ::= { myProduct 3 }

    IPConfigValuesGroup OBJECT-GROUP
        OBJECTS { ObjectA,
                  ObjectB,
                  ObjectC
                }

        STATUS current
        DESCRIPTION
               "Group of all blahblah variables."

        ::= { myProduct 4 } <----**How would this affect a client request?**

    --
    -- Values
    --

    ObjectA OBJECT-TYPE
        SYNTAX      OCTET STRING (SIZE(1..4096))
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
            "Test Example"
        ::= { IPConfig 1 }

    ObjectB OBJECT-TYPE
        SYNTAX      OCTET STRING (SIZE(1..4096))
        MAX-ACCESS  read-write
        STATUS      current
        DESCRIPTION
            "Test Example"
        ::= { IPConfig 2 } 

--MORE STUFF...
    .... END 

I want to be able to group relative objects together so the user could do an

我希望能够将相关对象组合在一起,以便用户可以执行此操作

snmpset -v 2c -c communityNameHere -m MIB-NAME-HERE.txt 10.20.30.40 1.3.6.1.4.1.54321.x.1.3 s "I am a string"

snmpset -v 2c -c communityNameHere -m MIB-NAME-HERE.txt 10.20.30.40 1.3.6.1.4.1.54321.x.1.3 s“我是一个字符串”

to access ObjectC under the IPConfig group.

访问IPConfig组下的ObjectC。

Question : How do I implement subtrees into my 'subagent'?

问题:如何在我的“子代理”中实现子树?

This is an excerpt from the link shown above.
    /*
     * This array defines the OID of the top of the mib tree that we're
     *  registering underneath.
     * Note that this needs to be the correct size for the OID being 
     *  registered, so that the length of the OID can be calculated.
     *  The format given here is the simplest way to achieve this.
     */
oid             example_variables_oid[] = { 1, 3, 6, 1, 4, 1, 54321, x};

Do I have to declare another array to include, per say, the Services OID?

我是否必须声明另一个数组,包括服务OID?

oid             example_variables_oid[] = { 1, 3, 6, 1, 4, 1, 54321, x, 3};

Or for each subtree do they need to have an example.c*-type* file?

或者对于每个子树,他们需要有一个example.c * -type *文件吗?

Question : Would this MIB achieve what I want? What would need to be done different? I've read up on OBJECT-GROUPS, SEQUENCE, O'Reily's book as well as the RFCs. I'm still trying to grasp everything.

问题:这个MIB会实现我想要的吗?需要做些什么不同?我已经阅读了OBJECT-GROUPS,SEQUENCE,O'Reily的书以及RFC。我还在努力抓住一切。

1 个解决方案

#1


2  

There is a lot of questions here, and honestly it would take a super long post to answer them all. So, I'll answer them at a high level and then provide you a bunch of links to go read longer and more in depth articles on.

这里有很多问题,老实说,需要一个超长的帖子来回答它们。因此,我会在较高的层次上回答它们,然后为您提供一系列链接,以便阅读更长时间和更深入的文章。

First, writing mibs is not exactly straight forward. The most referenced book on the subject is probably Understand SNMP MIBs and is quite good (I have a copy). All your mib writing questions are well answered in there, but a couple of quick points about what you have above:

首先,编写mibs并不是一件容易的事。关于这个主题的最多参考书可能是理解SNMP MIB并且非常好(我有一份副本)。所有的mib写作问题都在那里得到了很好的解答,但关于你的上述内容有几点快速解答:

1) Almost all MIB objects should start with a lower case letter (except table sequences which you're not to yet).

1)几乎所有的MIB对象都应该以小写字母开头(除了你还没有的表序列)。

2) There is no 'x' in your oids above. You've created a tree structure in the definitions that derive straight to 1.3.6.1.4.1.54321.1.3, for example.

2)上面的oid中没有'x'。例如,您在定义中创建了一个直接导出到1.3.6.1.4.1.54321.1.3的树结构。

3) Just throw out the object group clause for now. It'll only confuse you at first and isn't needed. It's only there to really write a standards-definition for listing objects you must implement in order to conform to the mib (using a conformance statement). For you right now, this is not at all needed. Just kill the whole thing.

3)暂时抛出对象组子句。它一开始只会让你困惑而且不需要。只有真正为列出对象编写标准定义才能符合mib(使用一致性声明)。对你来说,这根本不需要。只是杀死整个事情。

As for writing code to support the object you're trying to define, you'll need to do that in C code within the agent or subagent that you're writing. There is already a lot of documentation on the Net-SNMP project site about that, so you should really go look there. The links that will help you get started are:

至于编写代码以支持您尝试定义的对象,您需要在您编写的代理或子代理中的C代码中执行此操作。关于Net-SNMP项目网站已经有很多文档,所以你应该去那里看看。有助于您入门的链接是:

Generally how to write mib code for a net-smnp based agent:

一般来说,如何为基于net-smnp的代理编写mib代码:

http://www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module

How to use the mib2c translator to produce some template code to start with:

如何使用mib2c转换器生成一些模板代码以开始:

http://www.net-snmp.org/wiki/index.php/TUT:mib2c_General_Overview

And more generically, all the coding tutorials for Net-SNMP can be found here:

更一般地说,Net-SNMP的所有编码教程都可以在这里找到:

http://www.net-snmp.org/wiki/index.php/Tutorials#Coding_Tutorials

And one final comment: the objects you're defining above are called "scalars". IE, there is only a single instance of them in the tree. So when you're reading the tutorials or the mib2c questions it asks you, the above are "scalars". Tables will likely come next in your project, as everyone seems to end up with tables! Good luck!

最后一条评论:您在上面定义的对象称为“标量”。 IE,树中只有一个实例。因此,当您阅读教程或mib2c问题时,它会问您,以上是“标量”。表格可能会出现在您的项目中,因为每个人似乎最终都会有桌子!祝你好运!

#1


2  

There is a lot of questions here, and honestly it would take a super long post to answer them all. So, I'll answer them at a high level and then provide you a bunch of links to go read longer and more in depth articles on.

这里有很多问题,老实说,需要一个超长的帖子来回答它们。因此,我会在较高的层次上回答它们,然后为您提供一系列链接,以便阅读更长时间和更深入的文章。

First, writing mibs is not exactly straight forward. The most referenced book on the subject is probably Understand SNMP MIBs and is quite good (I have a copy). All your mib writing questions are well answered in there, but a couple of quick points about what you have above:

首先,编写mibs并不是一件容易的事。关于这个主题的最多参考书可能是理解SNMP MIB并且非常好(我有一份副本)。所有的mib写作问题都在那里得到了很好的解答,但关于你的上述内容有几点快速解答:

1) Almost all MIB objects should start with a lower case letter (except table sequences which you're not to yet).

1)几乎所有的MIB对象都应该以小写字母开头(除了你还没有的表序列)。

2) There is no 'x' in your oids above. You've created a tree structure in the definitions that derive straight to 1.3.6.1.4.1.54321.1.3, for example.

2)上面的oid中没有'x'。例如,您在定义中创建了一个直接导出到1.3.6.1.4.1.54321.1.3的树结构。

3) Just throw out the object group clause for now. It'll only confuse you at first and isn't needed. It's only there to really write a standards-definition for listing objects you must implement in order to conform to the mib (using a conformance statement). For you right now, this is not at all needed. Just kill the whole thing.

3)暂时抛出对象组子句。它一开始只会让你困惑而且不需要。只有真正为列出对象编写标准定义才能符合mib(使用一致性声明)。对你来说,这根本不需要。只是杀死整个事情。

As for writing code to support the object you're trying to define, you'll need to do that in C code within the agent or subagent that you're writing. There is already a lot of documentation on the Net-SNMP project site about that, so you should really go look there. The links that will help you get started are:

至于编写代码以支持您尝试定义的对象,您需要在您编写的代理或子代理中的C代码中执行此操作。关于Net-SNMP项目网站已经有很多文档,所以你应该去那里看看。有助于您入门的链接是:

Generally how to write mib code for a net-smnp based agent:

一般来说,如何为基于net-smnp的代理编写mib代码:

http://www.net-snmp.org/wiki/index.php/TUT:Writing_a_MIB_Module

How to use the mib2c translator to produce some template code to start with:

如何使用mib2c转换器生成一些模板代码以开始:

http://www.net-snmp.org/wiki/index.php/TUT:mib2c_General_Overview

And more generically, all the coding tutorials for Net-SNMP can be found here:

更一般地说,Net-SNMP的所有编码教程都可以在这里找到:

http://www.net-snmp.org/wiki/index.php/Tutorials#Coding_Tutorials

And one final comment: the objects you're defining above are called "scalars". IE, there is only a single instance of them in the tree. So when you're reading the tutorials or the mib2c questions it asks you, the above are "scalars". Tables will likely come next in your project, as everyone seems to end up with tables! Good luck!

最后一条评论:您在上面定义的对象称为“标量”。 IE,树中只有一个实例。因此,当您阅读教程或mib2c问题时,它会问您,以上是“标量”。表格可能会出现在您的项目中,因为每个人似乎最终都会有桌子!祝你好运!