logstash json发布输出 [英] logstash json post output

查看:290
本文介绍了logstash json发布输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用tcp输入在JavaScript上发布日志到Logstash.

I am current trying to do a JavaScript post to Logstash by using a tcp input.

JavaScript帖子

 xhr = new XMLHttpRequest();
 var url = "http://localhost:5043";
 xhr.open("POST", url, true);
 xhr.setRequestHeader("Content-type", "application/json");
 var data = JSON.stringify({"test" : hello});
 xhr.send(data);

Logstash配置文件

input {
    tcp {
        port => 5043
    }
}

filter{
}

output {
  stdout  { 
        codec => rubydebug
    }
}

控制台中的输出

{
       "message" => "OPTIONS / HTTP/1.1\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.611Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Host: localhost:5043\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.620Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Connection: keep-alive\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.621Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Access-Control-Request-Method: POST\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.622Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Origin: http://atgdev11\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.623Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.626Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Access-Control-Request-Headers: content-type\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.634Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Accept: */*\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.651Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Referer: http://test/Welcome.jsp\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.653Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Accept-Encoding: gzip, deflate, sdch, br\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.719Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}
{
       "message" => "Accept-Language: en-US,en;q=0.8\r",
      "@version" => "1",
    "@timestamp" => "2016-12-15T09:58:54.720Z",
          "host" => "0:0:0:0:0:0:0:1",
          "port" => 55867,

}

我似乎看不到我的json数据 {"test":hello} 传递到logstash中,logstash.config文件是否可能有问题?请帮助

I cant seem to see my json data {"test" : hello} passing into logstash could there be something wrong with my logstash.config file ? Please help

推荐答案

实际上,它们是以下@@ strong> JavaScript Post 部分中的javascript错误

Actually their is javascript error found in following line @JavaScript Post section

var data = JSON.stringify( {"test":hello} );

var data = JSON.stringify({"test" : hello});

替换为

var data = JSON.stringify({"test":"hello" });

var data = JSON.stringify({"test" : "hello"});

我,双引号被遗漏了

以上更改将为您提供所需的结果

Above changes will give u the required result

我已经按照以下方式进行了测试

I have tested in following way

JavaScript帖子

<!DOCTYPE html>
<html>
<title>Web Page Design</title>

<script>
function sayHello() {
 var  xhr = new XMLHttpRequest();
 var url = "http://localhost:5043";
 xhr.open("POST", url, true);
 xhr.setRequestHeader("Content-type", "application/json");
 var data = JSON.stringify({"test" : "hello"});
 xhr.send(data);
}
sayHello();
</script>

<body>
</body>
</html>

Logstash配置文件

input {
  http {
    port => 5043
    response_headers => {
        "Access-Control-Allow-Origin" => "*"
        "Content-Type" => "text/plain"
        "Access-Control-Allow-Headers" => "Origin, X-Requested-With, Content-Type, 
        Accept"
    }   
    }
}

filter {
}

output {
  stdout  { 
        codec => rubydebug
    }
}

控制台中的输出

{
          "host" => "0:0:0:0:0:0:0:1",
    "@timestamp" => 2018-10-08T11:01:34.395Z,
       "headers" => {
             "http_user_agent" => "Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl
eWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
              "content_length" => "17",
                "request_path" => "/",
              "request_method" => "POST",
                 "http_origin" => "null",
                "content_type" => "application/json",
        "http_accept_encoding" => "gzip, deflate, br",
                   "http_host" => "localhost:5043",
                 "request_uri" => "/",
        "http_accept_language" => "en-US,en;q=0.9",
                 "http_accept" => "*/*",
             "http_connection" => "keep-alive",
                "http_version" => "HTTP/1.1"
    },
          "test" => "hello",#### here is you input data in json format #####
      "@version" => "1"
}

这篇关于logstash json发布输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆