bash SNMP 扩展为整数而不是字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16365940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:20:58  来源:igfitidea点击:

SNMP extend as an INTEGER and not a STRING

bashnet-snmp

提问by DoRivard

I am trying to extend freeradius statistics to snmp. I want to monitor these values and collect them within rrd files. My problem is that these values should be rrd counter but it appears that counter values needs to retrieve an INTEGER and not a STRING. You can see in this snmpwalk that the value is returned as STRING.

我正在尝试将 freeradius 统计信息扩展到 snmp。我想监视这些值并将它们收集在 rrd 文件中。我的问题是这些值应该是 rrd counter 但似乎计数器值需要检索 INTEGER 而不是 STRING。您可以在此 snmpwalk 中看到该值作为 STRING 返回。

    snmpwalk -v2c -c public 127.0.0.1 .1.3.6.1.3.1983.1.1

    # output
    iso.3.6.1.3.1983.1.1.1.0 = INTEGER: 1
    iso.3.6.1.3.1983.1.1.2.1.2.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = STRING: "/bin/cat"
    iso.3.6.1.3.1983.1.1.2.1.3.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = STRING: "/var/log/freeradius/statistics/total_access_requests"
    iso.3.6.1.3.1983.1.1.2.1.4.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = ""
    iso.3.6.1.3.1983.1.1.2.1.5.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 5
    iso.3.6.1.3.1983.1.1.2.1.6.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 1
    iso.3.6.1.3.1983.1.1.2.1.7.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 1
    iso.3.6.1.3.1983.1.1.2.1.20.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 4
    iso.3.6.1.3.1983.1.1.2.1.21.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 1
    iso.3.6.1.3.1983.1.1.3.1.1.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = STRING: "9566"
    iso.3.6.1.3.1983.1.1.3.1.2.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = STRING: "9566"
    iso.3.6.1.3.1983.1.1.3.1.3.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 1
    iso.3.6.1.3.1983.1.1.3.1.4.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115 = INTEGER: 0
    iso.3.6.1.3.1983.1.1.4.1.2.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115.1 = STRING: "9566"


    # /etc/snmp/snmpd.conf
    extend  .1.3.6.1.3.1983.1.1 access_requests /bin/cat /var/log/freeradius/statistics/total_access_requests

The snmp script is extended by printing the value within the file. The file that are used by /bin/catare generated by the the script /usr/local/sbin/radstat.sh

snmp 脚本通过打印文件中的值进行扩展。/bin/cat使用的文件由脚本/usr/local/sbin/radstat.sh生成

How do I make this output become an INTEGER?

我如何使这个输出变成整数?

    iso.3.6.1.3.1983.1.1.4.1.2.15.97.99.99.101.115.115.95.114.101.113.117.101.115.116.115.1 = STRING: "9566"

Thanks Dominick

谢谢多米尼克

回答by Sir Athos

You probably need to use passinstead of extend, so that your script can specify the type returned. From man snmpd.conf:

您可能需要使用pass而不是extend,以便您的脚本可以指定返回的类型。来自man snmpd.conf

pass [-p priority] MIBOID PROG will pass control of the subtree rooted at MIBOID to the specified PROG command. GET and GETNEXT requests for OIDs within this tree will trigger this command, called as:

PROG -g OID

PROG -n OID respectively, where OID is the requested OID. The PROG command should return the response varbind as three separate lines printed to stdout - the first line should be the OID of the returned value, the second should be its TYPE (one of the text strings integer, gauge, counter, timeticks, ipaddress, objectid, or string), and the third should be the value itself.

If the command cannot return an appropriate varbind - e.g the specified OID did not correspond to a valid instance for a GET request, or there were no following instances for a GETNEXT - then it should exit without producing any output. This will result in an SNMP noSuchName error, or a noSuchInstance exception.

A SET request will result in the command being called as:

PROG -s OID TYPE VALUE where TYPE is one of the tokens listed above, indicating the type of the value passed as the third parameter.

If the assignment is successful, the PROG command should exit without producing any output. Errors should be indicated by writing one of the strings not-writable, or wrong-type to stdout, and the agent will generate the appropriate error response.

pass [-p priority] MIBOID PROG 会将以 MIBOID 为根的子树的控制权传递给指定的 PROG 命令。此树中 OID 的 GET 和 GETNEXT 请求将触发此命令,称为:

PROG -g OID

PROG -n OID 分别,其中 OID 是请求的 OID。PROG 命令应该将响应 varbind 作为打印到 stdout 的三个单独的行返回 - 第一行应该是返回值的 OID,第二行应该是它的 TYPE(文本字符串 integer、gauge、counter、timeticks、ipaddress、 objectid, or string),第三个应该是值本身。

如果该命令无法返回适当的 varbind - 例如,指定的 OID 与 GET 请求的有效实例不对应,或者没有后续的 GETNEXT 实例 - 那么它应该退出而不产生任何输出。这将导致 SNMP noSuchName 错误或 noSuchInstance 异常。

SET 请求将导致命令被调用为:

PROG -s OID TYPE VALUE 其中 TYPE 是上面列出的标记之一,表示作为第三个参数传递的值的类型。

如果分配成功,PROG 命令应该退出而不产生任何输出。应通过将不可写或错误类型的字符串之一写入 stdout 来指示错误,代理将生成适当的错误响应。