0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Logstashを一番シンプルに試す

Last updated at Posted at 2023-12-10

やりたいこと

  • Logstashを設定し、外部のサーバからのログを受け付けるようにする

検証環境

  • VagrantでホスティングしたUbuntu Server 22.04 LTS

インストール

$ sudo apt update
$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic-keyring.gpg
$ sudo apt install apt-transport-https -y
$ echo "deb [signed-by=/usr/share/keyrings/elastic-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list
$ sudo apt update
$ sudo apt install logstash -y

設定する

  • リモートからHTTP(TCPポート8080番)でJSON形式のログを受け取るための設定
  • 受け取ったログを標準出力に出力する設定
$ vim /etc/logstash/conf.d/logstash.conf
input {
  http {
    port => 8080
    codec => json
  }
}

output {
  stdout {}
}
  • UFWを有効化する設定
  • TCPポート8080番への外部からのアクセスを許可する設定
$ sudo ufw status
Status: inactive

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

$ sudo ufw allow 8080/tcp
Rule added
Rule added (v6)

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
8080/tcp                   ALLOW       Anywhere                  
8080/tcp (v6)              ALLOW       Anywhere (v6)

起動する

$ sudo /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf 

動作確認

  • 擬似的にログを送信するコードをPythonで作成する
$ pip install requests
client.py
import requests

logstash_host = "192.168.56.10"
logstash_port = 8080
data = {"message": "hello, world!"}
headers = {"Content-Type": "application/json"}

requests.post(f"http://{logstash_host}:{logstash_port}", json=data, headers=headers)
$ python client.py
  • 上記のコードを実行すると、Fluentdの標準出力にログが流れてくることが確認できる
$ sudo /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf
...
省略
...
{
          "host" => {
        "ip" => "192.168.56.1"
    },
      "@version" => "1",
    "@timestamp" => 2023-12-10T21:20:06.900752387Z,
           "url" => {
          "path" => "/",
        "domain" => "192.168.56.10",
          "port" => 8080
    },
          "http" => {
         "method" => "POST",
        "request" => {
                 "body" => {
                "bytes" => "28"
            },
            "mime_type" => "application/json"
        },
        "version" => "HTTP/1.1"
    },
       "message" => "hello, world!",
         "event" => {
        "original" => "{\"message\": \"hello, world!\"}"
    },
    "user_agent" => {
        "original" => "python-requests/2.31.0"
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?