【问题标题】:How to get status from Monit from a NodeJS program?如何从 NodeJS 程序中获取 Monit 的状态?
【发布时间】:2012-12-04 16:58:58
【问题描述】:

我正在尝试从 NodeJS 程序中获取 Monit 的状态。在我的监视器中,我将其设置为使用端口 2812,但我不确定在我的节点程序中该做什么。任何建议将不胜感激。

我会补充一点,我目前一无所知,但我已经尝试过:

var net = require('net');

var client = net.connect({port: 2812},
    function() { //'connect' listener
  console.log('client connected');
  client.write('monit status');
});
client.on('data', function(data) {
  console.log(data.toString());
  client.end();
});
client.on('end', function() {
  console.log('client disconnected');
});

哪些输出:

client connected
HTTP/1.0 400 Bad Request
Date: Tue, 04 Dec 2012 17:03:15 GMT
Server: monit 5.3.2
Content-Type: text/html
Connection: close

<html><head><title>Bad Request</title></head><body bgcolor=#FFFFFF><h2>Bad Request</h2>Cannot parse request<p><hr><a href='http://mmonit.com/monit/'><font size=-1>monit 5.3.2</font></a></body></html>

client disconnected

这不仅仅是没有,因为它实际上将 monit 列为服务器,但我不知道如何使它工作。

【问题讨论】:

    标签: node.js monit


    【解决方案1】:

    我想通了。原来我想做的是:

    var http = require('http');
    
    var options = {
      hostname: '127.0.0.1',
      port: 2812,
      method: 'GET'
    };
    
    var req = http.request(options, function(res) {
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));
      res.setEncoding('utf8');
      res.on('data', function (data) {
        console.log('BODY: ' + data);
      });
    });
    
    req.on('error', function(e) {
      console.log('problem with request: ' + e.message);
    });
    
    req.end();
    

    【讨论】:

    • 另外它并没有真正做我想做的事,我不太明白通过端口连接到monit可以做什么。但至少这是可行的。
    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    相关资源
    最近更新 更多