2

我目前正在尝试在 c9.io 开发环境中获取适用于 node.js 库 node-snmpjs 的基本示例代码。

示例代码在这里:

https://github.com/wesolows/node-snmpjs

https://npmjs.org/package/snmpjs

var os = require('os');
var snmp = require('snmpjs');
var logger = require('bunyan');

var log = new logger({
    name: 'snmpd',
    level: 'info'
});

var agent = snmp.createAgent({
    log: log
});

agent.request({ oid: '.1.3.6.1.2.1.1.5', handler: function (prq) {
    console.log("Request received");
    var nodename = os.hostname();
    var val = snmp.data.createData({ type: 'OctetString',
        value: nodename });

    snmp.provider.readOnlyScalar(prq, val);
} });

agent.bind({ family: 'udp4', port: 161 });

我尝试将绑定端口更改为: parseInt(process.env.PORT)

当我尝试对 c9 在运行时吐出的地址和端口执行 snmpget 时,它会失败。我也试过:8080。8080 是 c9 的控制台输出总是说映射到 process.env.PORT。我还尝试了 snmpget 中的端口 80。

作为后备方案,我还尝试使用 c9 进行基本的 telnet、套接字连接,但无法正常工作......似乎根本问题可能是我不知道正在使用什么 IP 和端口.

- - - - - - - - - - - - - 编辑 - - - - - - - - - - - -

我在 agent.bind 行上尝试了以下变体:

agent.bind({ family: 'udp4', port: parseInt(process.env.PORT), addr: process.env.IP});

agent.bind({ family: 'udp4', port: 17000, addr: process.env.IP});

输出:

Your code is running at 'http://node-dev1.thaspius.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
"127.12.254.129:17000"
ex-c9-node47.prod.rhcloud.com
{"name":"snmpd","hostname":"ex-c9-node47.prod.rhcloud.com","pid":32758,"component":"snmp-agent","level":30,"msg":"Bound to 127.12.254.129:17000","time":"2013-07-31T17:59:56.376Z","v":0}

snmpget 结果:

snmpget -v 2c -c any 127.12.254.129:17000 .1.3.6.1.2.1.1.5
Timeout: No Response from 127.12.254.129:17000.

snmpget -v 2c -c any ex-c9-node47.prod.rhcloud.com:17000 .1.3.6.1.2.1.1.5
Timeout: No Response from ex-c9-node47.prod.rhcloud.com:17000.

snmpget -v 2c -c any node-dev1.thaspius.c9.io:17000 .1.3.6.1.2.1.1.5
Timeout: No Response from node-dev1.thaspius.c9.io:17000.
4

2 回答 2

2

You likely need to bind your application to a specific IP as well: process.env.IP. According to the snmpjs docs, you can do that using the addr option (i.e.., agent.bind({ family: 'udp4', port: process.env.PORT, addr: process.env.IP })). Also have a look at https://c9.io/site/blog/2013/05/can-i-use-cloud9-to-do-x/ for more info.

于 2013-07-31T09:59:27.170 回答
0

正如您使用“snmp.provider.readOnlyScalar(prq, val);” 此功能将在您设置的 OID 后自动添加即时 ID“.0”;

所以你必须运行 use ".1.3.6.1.2.1.1.5.0": snmpget -v 2c -c any node-dev1.thaspius.c9.io:17000 .1.3.6.1.2.1.1.5.0

于 2014-11-17T07:41:12.513 回答