Ubuntu 18.04的Elasticsearch、Kibana、Logstash(文件+HTTP)和Apache的日志监控

0. 概述

使用Kibana + Elasticsearch + Logstash来监控Apache日志。

从将日志附加到常规文件中并吸取日志的方法,到通过网络接收日志的方法。

1. 安装

$ sudo apt-get install openjdk-8-jdk
$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
$ sudo apt-get install apt-transport-https
$ echo "deb https://artifacts.elastic.co/packages/6.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-6.x.list
$ sudo apt-get update && sudo apt-get install elasticsearch
$ sudo vi /etc/elasticsearch/elasticsearch.yml
# add configuration
network.host:0.0.0.0
$ sudo vi /etc/elasticsearch/jvm.options
# modify configuration
-Xmx64m
-Xms64m
$ sudo systemctl start elasticsearch
$ systemctl status elasticsearch
$ sudo systemctl enable elasticsearch
$ sudo apt-get install kibana
$ sudo vi /etc/kibana/kibana.yml
server.host: "0.0.0.0"
$ sudo systemctl start kibana
$ systemctl status kibana
$ sudo systemctl enable kibana
$ sudo apt-get install logstash
$ sudo systemctl start logstash
$ systemctl status logstash
$ sudo systemctl enable logstash

2. 设定

$ sudo vim /etc/logstash/conf.d/apache.conf

请提供以下内容

input {
  file {
    path=> "/var/log/apache2/access.log"
  }
}

filter {
  grok {
    match => { "message" => "%{COMBINEDAPACHELOG}" }
  }
}

output {
  elasticsearch {
    hosts => ["localhost:9200"]
    index => "apache"
  }

}

重新启动

$ sudo systemctl restart logstash.service

在Kibana中打开,进入Management -> Index Patterns -> Create Index Pattern,然后添加apache。
这样就可以监视日志了。

3. 检查并删除

请检查。

返回数据库的列表

$ curl http://localhost:9200/_cat/indices?v

这样的感觉回来

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   index   YZfpJsZAStOXOSynXNvxpQ   5   1          0            0       868b           868b
yellow open   apache  m5JbbZVITx6q-LxNHKW-TA   5   1        199            0    534.7kb        534.7kb
yellow open   .kibana bngpvzOfRJ2UYLan0WvfYA   1   1          3            0     15.3kb         15.3kb

使用这个方法来确认索引,并按以下步骤删除不必要的数据库。

删除

$ curl -XDELETE localhost:9200/index/type/[INDEX NAME]

申请

为了能够可视化外部的 JSON 格式日志并进行保存,如果继续保持现状,则只能可视化内部文件。要实现这一点,可以使用预装的 HTTP 插件,但如果以常规方式编写配置文件,则会发生冲突。

可以通过编写如下的Config文件来避免冲突。

首先,Apache

input {
  file {
    tags => "apache"
    path=> "/var/log/apache2/access.log"
  }
}

filter {
  if "apache" in [tags] {
    grok {
      match => { "message" => "%{COMBINEDAPACHELOG}" }
    }
  }
}

output {
  if "apache" in [tags]{
    elasticsearch {
      hosts => ["localhost:9200"]
      index => "apache"
    }
  }
}

下一步是外部Json。

input {
  http {
    tags => "external"
    host=> "0.0.0.0"
    port=> 31000
  }
}

output {
  if "external" in [tags] {
    elasticsearch {
      hosts => ["localhost:9200"]
      index => "external"
    }
  }
}

如果只接收来自内部环回的日志,则将127.0.0.1作为主机指定。

然后重新启动Logstash并在下面发送日志

curl -H "content-type: application/json" -XPUT 'http://127.0.0.1:31000/' -d '{
    "user" : "log",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "hello"
}'
image.png

顺便说一下,您可以在URL后面添加斜杠并指定标头。

'http://127.0.0.1:31000/[Request_URI]'

因此,如果要将网络页面的爬取结果等放入其中,可能可以直接将网络页面的URL连续输入。

广告
将在 10 秒后关闭
bannerAds