LoginSignup
9
0

More than 3 years have passed since last update.

ApacheのログをELK環境で可視化した

Last updated at Posted at 2019-12-07

お前は誰だ?

IPFactory所属の1年です
honeypotに関する事やってたりします

やったこと

コンソール上でログ見るのつらすぎ。泣いちゃう:sob:

というわけでログを可視化してみることにした

ELKとは

Elastic search, Kibana, Logstashの略
それぞれの役割は以下の通り

  • Elastic search : データ検索
  • Kibana : 可視化
  • Logstash : データ取り込み

環境

Ubuntu18.04
Apache2.4.29

導入

Elasticsearch

apt install -y apt-transport-https
wget https://artifacts.elastic.co/GPG-KEY-elasticsearch
apt-key add GPG-KEY-elasticsearch
echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
apt update
apt install -y elasticsearch
#elasticsearch起動
systemctl start elasticsearch

起動後 http://localhost:9200 にアクセスし、このようなJSONが返ってくればうまく動作している

{
  "name" : "ubuntu",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "4lR3_HlKTa--IPJr8xLTxA",
  "version" : {
    "number" : "7.5.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "e9ccaed468e2fac2275a3761849cbee64b39519f",
    "build_date" : "2019-11-26T01:06:52.518245Z",
    "build_snapshot" : false,
    "lucene_version" : "8.3.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Logstash

apt install -y logstash

ルールを作成する

/etc/logstash/conf.d/apache2.conf
input {
   file {
      mode => "tail"
      path => ["/var/log/apache2/access.log"]
   }
}
filter {
   grok {
      match => { "message" => "%{COMBINEDAPACHELOG}" }
   }
   date {
      match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
      locale => "en"
   }
}
output {
   elasticsearch {
      hosts => ["localhost:9200"]
      index => "apache2"
   }
}

elasticsearchを起動する
なぜかsystemctlで動かすとうまく動作しなかったのでこうした

/usr/share/logstash -f /etc/logstash/conf.d/apache2.conf

起動後、以下のコマンドを実行し動作確認

curl http://localhost:9200/_cat/health?v
#indexにapache2があればうまく動作している
health status index                    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   apache2                   L59zRp1nQqWGdrJJEZS5Gw   1   1         12            0     56.5kb         56.5kb
green  open   .kibana_task_manager_1   CAup6rtVTsi6tD2tFxdy0A   1   0          2            0       57kb           57kb
green  open   .apm-agent-configuration GvocK76nQlaOEsViuM0o2Q   1   0          0            0       283b           283b
green  open   .kibana_2                wPziMyNSSBuIPmQ6wqMpSg   1   0          6            0     29.1kb         29.1kb
green  open   .kibana_1                uRrfym5VS4qC_x70142Emg   1   0          1            0        4kb            4kb
green  open   .tasks                   hQ70Y5N7TsGdOBw-SO8p5g   1   0          1            0      6.3kb          6.3kb


Kibana


apt install -y kibana
#kinaba起動
systemctl start kibana

起動後 http://localhost:5601 にアクセスし、それっぽい画面が表示されればうまく動作している
続いてKibanaの左メニューから Management > Index Patterns > Create Index Pattern の順に進む
Index patternの部分にapache2と入力し、Next Stepを押す
Time Filer field nameのドロップダウンリストで @timestamp を選択後、Create index patternと書かれたボタンを押す
左メニューのDiscoverをクリックすることでログの表、グラフが見れる

最後に

  • logstashのオプションが多く、全て理解していない
  • kibanaのグラフがアクセス数だけしかないので実用に程遠い

など問題点がいくつかあるので、今後解決していきたい

9
0
1

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
9
0