一、ELK搭建篇

官网地址:https://www.elastic.co/cn/

官网权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

安装指南:https://www.elastic.co/guide/en/elasticsearch/reference/5.x/rpm.html

ELK是Elasticsearch、Logstash、Kibana的简称,这三者是核心套件,但并非全部。

Elasticsearch是实时全文搜索和分析引擎,提供搜集、分析、存储数据三大功能;是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。

Logstash是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。它可以从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。

Kibana是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不仅允许用户创建他们自己的数据的定制仪表板视图,还允许他们以特殊的方式查询和过滤数据

环境

  1. Centos6.5 两台
  2. IP192.168.1.202 安装: elasticsearchlogstashKibanaNginxHttpRedis
  3. 192.168.1.201 安装: logstash

安装

  1. 安装elasticsearchyum源的密钥(这个需要在所有服务器上都配置)
  2. # rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
  3. 配置elasticsearchyum
  4. # vim /etc/yum.repos.d/elasticsearch.repo
  5. elasticsearch.repo文件中添加如下内容
  6. [elasticsearch-5.x]
  7. name=Elasticsearch repository for 5.x packages
  8. baseurl=https://artifacts.elastic.co/packages/5.x/yum
  9. gpgcheck=1
  10. gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
  11. enabled=1
  12. autorefresh=1
  13. type=rpm-md

安装elasticsearch的环境

  1. 安装elasticsearch
  2. # yum install -y elasticsearch
  3. 安装java环境(java环境必须是1.8版本以上的)
  4. wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
  5. rpm -ivh jdk-8u131-linux-x64.rpm
  6. 验证java安装成功
  7. java -version
  8. java version "1.8.0_131"
  9. Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
  10. Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

创建elasticsearch data的存放目录,并修改该目录的属主属组

  1. # mkdir -p /data/es-data (自定义用于存放data数据的目录)
  2. # chown -R elasticsearch:elasticsearch /data/es-data

修改elasticsearch的日志属主属组

  1. # chown -R elasticsearch:elasticsearch /var/log/elasticsearch/

修改elasticsearch的配置文件

  1. # vim /etc/elasticsearch/elasticsearch.yml
  2. 找到配置文件中的cluster.name,打开该配置并设置集群名称
  3. cluster.name: demon
  4. 找到配置文件中的node.name,打开该配置并设置节点名称
  5. node.name: elk-1
  6. 修改data存放的路径
  7. path.data: /data/es-data
  8. 修改logs日志的路径
  9. path.logs: /var/log/elasticsearch/
  10. 配置内存使用用交换分区
  11. bootstrap.memory_lock: true
  12. 监听的网络地址
  13. network.host: 0.0.0.0
  14. 开启监听的端口
  15. http.port: 9200
  16. 增加新的参数,这样head插件可以访问es (5.x版本,如果没有可以自己手动加)
  17. http.cors.enabled: true
  18. http.cors.allow-origin: "*"
  19. 启动elasticsearch服务

启动服务

  1. /etc/init.d/elasticsearch start
  2. Starting elasticsearch: Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
  3. #
  4. # There is insufficient memory for the Java Runtime Environment to continue.
  5. # Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
  6. # An error report file with more information is saved as:
  7. # /tmp/hs_err_pid2616.log
  8. [FAILED]
  9. 这个报错是因为默认使用的内存大小为2G,虚拟机没有那么多的空间
  10. 修改参数:
  11. vim /etc/elasticsearch/jvm.options
  12. -Xms512m
  13. -Xmx512m
  14. 再次启动
  15. /etc/init.d/elasticsearch start
  16. 查看服务状态,如果有报错可以去看错误日志 less /var/log/elasticsearch/demon.log(日志的名称是以集群名称命名的)
  17. 创建开机自启动服务
  18. # chkconfig elasticsearch on

注意事项

  1. 需要修改几个参数,不然启动会报错
  2. vim /etc/security/limits.conf
  3. 在末尾追加以下内容(elk为启动用户,当然也可以指定为*)
  4. elk soft nofile 65536
  5. elk hard nofile 65536
  6. elk soft nproc 2048
  7. elk hard nproc 2048
  8. elk soft memlock unlimited
  9. elk hard memlock unlimited
  10. 继续再修改一个参数
  11. vim /etc/security/limits.d/90-nproc.conf
  12. 将里面的1024改为2048ES最少要求为2048
  13. * soft nproc 2048
  14. 另外还需注意一个问题(在日志发现如下内容,这样也会导致启动失败,这一问题困扰了很久)
  15. [2017-06-14T19:19:01,641][INFO ][o.e.b.BootstrapChecks ] [elk-1] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
  16. [2017-06-14T19:19:01,658][ERROR][o.e.b.Bootstrap ] [elk-1] node validation exception
  17. [1] bootstrap checks failed
  18. [1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
  19. 解决:修改配置文件,在配置文件添加一项参数(目前还没明白此参数的作用)
  20. vim /etc/elasticsearch/elasticsearch.yml
  21. bootstrap.system_call_filter: false

通过浏览器请求下9200的端口,看下是否成功

  1. 先检查9200端口是否起来
  2. netstat -antp |grep 9200
  3. tcp 0 0 :::9200 :::* LISTEN 2934/java
  4. 浏览器访问测试是否正常(以下为正常)
  5. # curl http://127.0.0.1:9200/
  6. {
  7. "name" : "linux-node1",
  8. "cluster_name" : "demon",
  9. "cluster_uuid" : "kM0GMFrsQ8K_cl5Fn7BF-g",
  10. "version" : {
  11. "number" : "5.4.0",
  12. "build_hash" : "780f8c4",
  13. "build_date" : "2017-04-28T17:43:27.229Z",
  14. "build_snapshot" : false,
  15. "lucene_version" : "6.5.0"
  16. },
  17. "tagline" : "You Know, for Search"
  18. }

如何和elasticsearch交互

  1. JavaAPI
  2. RESTful API
  3. Javascript,.Net,PHP,Perl,Python
  4. 利用API查看状态
  5. # curl -i -XGET 'localhost:9200/_count?pretty'
  6. HTTP/1.1 200 OK
  7. content-type: application/json; charset=UTF-8
  8. content-length: 95
  9. {
  10. "count" : 0,
  11. "_shards" : {
  12. "total" : 0,
  13. "successful" : 0,
  14. "failed" : 0
  15. }
  16. }

安装插件

  1. 安装elasticsearch-head插件
  2. 安装docker镜像或者通过github下载elasticsearch-head项目都是可以的,1或者2两种方式选择一种安装使用即可
  3. 1. 使用docker的集成好的elasticsearch-head
  4. # docker run -p 9100:9100 mobz/elasticsearch-head:5
  5. docker容器下载成功并启动以后,运行浏览器打开http://localhost:9100/
  6. 2. 使用git安装elasticsearch-head
  7. # yum install -y npm
  8. # git clone git://github.com/mobz/elasticsearch-head.git
  9. # cd elasticsearch-head
  10. # npm install
  11. # npm run start
  12. 检查端口是否起来
  13. netstat -antp |grep 9100
  14. 浏览器访问测试是否正常
  15. http://IP:9100/

LogStash的使用

  1. 安装Logstash环境:
  2. 官方安装手册:
  3. https://www.elastic.co/guide/en/logstash/current/installing-logstash.html
  4. 下载yum源的密钥认证:
  5. # rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
  6. 利用yum安装logstash
  7. # yum install -y logstash
  8. 查看下logstash的安装目录
  9. # rpm -ql logstash
  10. 创建一个软连接,每次执行命令的时候不用在写安装路劲(默认安装在/usr/share下)
  11. ln -s /usr/share/logstash/bin/logstash /bin/
  12. 执行logstash的命令
  13. # logstash -e 'input { stdin { } } output { stdout {} }'
  14. 运行成功以后输入:
  15. nihao
  16. stdout返回的结果:

  1. 注:
  2. -e 执行操作
  3. input 标准输入
  4. { input } 插件
  5. output 标准输出
  6. { stdout } 插件
  7. 通过rubydebug来输出下更详细的信息
  8. # logstash -e 'input { stdin { } } output { stdout {codec => rubydebug} }'
  9. 执行成功输入:
  10. nihao
  11. stdout输出的结果:

  1. 如果标准输出还有elasticsearch中都需要保留应该怎么玩,看下面
  2. # /usr/share/logstash/bin/logstash -e 'input { stdin { } } output { elasticsearch { hosts => ["192.168.1.202:9200"] } stdout { codec => rubydebug }}'
  3. 运行成功以后输入:
  4. I am elk
  5. 返回的结果(标准输出中的结果):

logstash使用配置文件

  1. 官方指南:
  2. https://www.elastic.co/guide/en/logstash/current/configuration.html
  3. 创建配置文件01-logstash.conf
  4. # vim /etc/logstash/conf.d/elk.conf
  5. 文件中添加以下内容
  6. input { stdin { } }
  7. output {
  8. elasticsearch { hosts => ["192.168.1.202:9200"] }
  9. stdout { codec => rubydebug }
  10. }
  11. 使用配置文件运行logstash
  12. # logstash -f ./elk.conf
  13. 运行成功以后输入以及标准输出结果

logstash的数据库类型

  1. 1. Input插件
  2. 权威指南:https://www.elastic.co/guide/en/logstash/current/input-plugins.html
  3. file插件的使用
  4. # vim /etc/logstash/conf.d/elk.conf
  5. 添加如下配置
  6. input {
  7. file {
  8. path => "/var/log/messages"
  9. type => "system"
  10. start_position => "beginning"
  11. }
  12. }
  13. output {
  14. elasticsearch {
  15. hosts => ["192.168.1.202:9200"]
  16. index => "system-%{+YYYY.MM.dd}"
  17. }
  18. }
  19. 运行logstash指定elk.conf配置文件,进行过滤匹配
  20. #logstash -f /etc/logstash/conf.d/elk.conf

来一发配置安全日志的并且把日志的索引按类型做存放,继续编辑elk.conf文件

  1. # vim /etc/logstash/conf.d/elk.conf
  2. 添加secure日志的路径
  3. input {
  4. file {
  5. path => "/var/log/messages"
  6. type => "system"
  7. start_position => "beginning"
  8. }
  9. file {
  10. path => "/var/log/secure"
  11. type => "secure"
  12. start_position => "beginning"
  13. }
  14. }
  15. output {
  16. if [type] == "system" {
  17. elasticsearch {
  18. hosts => ["192.168.1.202:9200"]
  19. index => "nagios-system-%{+YYYY.MM.dd}"
  20. }
  21. }
  22. if [type] == "secure" {
  23. elasticsearch {
  24. hosts => ["192.168.1.202:9200"]
  25. index => "nagios-secure-%{+YYYY.MM.dd}"
  26. }
  27. }
  28. }
  29. 运行logstash指定elk.conf配置文件,进行过滤匹配
  30. # logstash -f ./elk.conf

这些设置都没有问题之后,接下来安装下kibana,可以让在前台展示

Kibana的安装及使用

  1. 安装kibana环境
  2. 官方安装手册:https://www.elastic.co/guide/en/kibana/current/install.html
  3. 下载kibanatar.gz的软件包
  4. # wget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.0-linux-x86_64.tar.gz
  5. 解压kibanatar
  6. # tar -xzf kibana-5.4.0-linux-x86_64.tar.gz
  7. 进入解压好的kibana
  8. # mv kibana-5.4.0-linux-x86_64 /usr/local
  9. 创建kibana的软连接
  10. # ln -s /usr/local/kibana-5.4.0-linux-x86_64/ /usr/local/kibana
  11. 编辑kibana的配置文件
  12. # vim /usr/local/kibana/config/kibana.yml
  13. 修改配置文件如下,开启以下的配置
  14. server.port: 5601
  15. server.host: "0.0.0.0"
  16. elasticsearch.url: "http://192.168.1.202:9200"
  17. kibana.index: ".kibana"
  18. 安装screen,以便于kibana在后台运行(当然也可以不用安装,用其他方式进行后台启动)
  19. # yum -y install screen
  20. # screen
  21. # /usr/local/kibana/bin/kibana
  22. netstat -antp |grep 5601
  23. tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 17007/node
  24. 打开浏览器并设置对应的index
  25. http://IP:5601

二、ELK实战篇

好,现在索引也可以创建了,现在可以来输出nginx、apache、message、secrue的日志到前台展示(Nginx有的话直接修改,没有自行安装)

  1. 编辑nginx配置文件,修改以下内容(在http模块下添加)
  2. log_format json '{"@timestamp":"$time_iso8601",'
  3. '"@version":"1",'
  4. '"client":"$remote_addr",'
  5. '"url":"$uri",'
  6. '"status":"$status",'
  7. '"domian":"$host",'
  8. '"host":"$server_addr",'
  9. '"size":"$body_bytes_sent",'
  10. '"responsetime":"$request_time",'
  11. '"referer":"$http_referer",'
  12. '"ua":"$http_user_agent"'
  13. '}';
  14. 修改access_log的输出格式为刚才定义的json
  15. access_log logs/elk.access.log json;
  16. 继续修改apache的配置文件
  17. LogFormat "{ \
  18. \"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \
  19. \"@version\": \"1\", \
  20. \"tags\":[\"apache\"], \
  21. \"message\": \"%h %l %u %t \\\"%r\\\" %>s %b\", \
  22. \"clientip\": \"%a\", \
  23. \"duration\": %D, \
  24. \"status\": %>s, \
  25. \"request\": \"%U%q\", \
  26. \"urlpath\": \"%U\", \
  27. \"urlquery\": \"%q\", \
  28. \"bytes\": %B, \
  29. \"method\": \"%m\", \
  30. \"site\": \"%{Host}i\", \
  31. \"referer\": \"%{Referer}i\", \
  32. \"useragent\": \"%{User-agent}i\" \
  33. }" ls_apache_json
  34. 一样修改输出格式为上面定义的json格式
  35. CustomLog logs/access_log ls_apache_json
  36. 编辑logstash配置文件,进行日志收集
  37. vim /etc/logstash/conf.d/full.conf
  38. input {
  39. file {
  40. path => "/var/log/messages"
  41. type => "system"
  42. start_position => "beginning"
  43. }
  44. file {
  45. path => "/var/log/secure"
  46. type => "secure"
  47. start_position => "beginning"
  48. }
  49. file {
  50. path => "/var/log/httpd/access_log"
  51. type => "http"
  52. start_position => "beginning"
  53. }
  54. file {
  55. path => "/usr/local/nginx/logs/elk.access.log"
  56. type => "nginx"
  57. start_position => "beginning"
  58. }
  59. }
  60. output {
  61. if [type] == "system" {
  62. elasticsearch {
  63. hosts => ["192.168.1.202:9200"]
  64. index => "nagios-system-%{+YYYY.MM.dd}"
  65. }
  66. }
  67. if [type] == "secure" {
  68. elasticsearch {
  69. hosts => ["192.168.1.202:9200"]
  70. index => "nagios-secure-%{+YYYY.MM.dd}"
  71. }
  72. }
  73. if [type] == "http" {
  74. elasticsearch {
  75. hosts => ["192.168.1.202:9200"]
  76. index => "nagios-http-%{+YYYY.MM.dd}"
  77. }
  78. }
  79. if [type] == "nginx" {
  80. elasticsearch {
  81. hosts => ["192.168.1.202:9200"]
  82. index => "nagios-nginx-%{+YYYY.MM.dd}"
  83. }
  84. }
  85. }
  86. 运行看看效果如何
  87. logstash -f /etc/logstash/conf.d/full.conf

可以发现所有创建日志的索引都已存在,接下来就去Kibana创建日志索引,进行展示(按照上面的方法进行创建索引即可),看下展示的效果

接下来再来一发MySQL慢日志的展示

  1. 由于MySQL的慢日志查询格式比较特殊,所以需要用正则进行匹配,并使用multiline能够进行多行匹配(看具体配置)
  2. input {
  3. file {
  4. path => "/var/log/messages"
  5. type => "system"
  6. start_position => "beginning"
  7. }
  8. file {
  9. path => "/var/log/secure"
  10. type => "secure"
  11. start_position => "beginning"
  12. }
  13. file {
  14. path => "/var/log/httpd/access_log"
  15. type => "http"
  16. start_position => "beginning"
  17. }
  18. file {
  19. path => "/usr/local/nginx/logs/elk.access.log"
  20. type => "nginx"
  21. start_position => "beginning"
  22. }
  23. file {
  24. path => "/var/log/mysql/mysql.slow.log"
  25. type => "mysql"
  26. start_position => "beginning"
  27. codec => multiline {
  28. pattern => "^# User@Host:"
  29. negate => true
  30. what => "previous"
  31. }
  32. }
  33. }
  34. filter {
  35. grok {
  36. match => { "message" => "SELECT SLEEP" }
  37. add_tag => [ "sleep_drop" ]
  38. tag_on_failure => []
  39. }
  40. if "sleep_drop" in [tags] {
  41. drop {}
  42. }
  43. grok {
  44. match => { "message" => "(?m)^# User@Host: %{USER:User}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:Client_IP})?\]\s.*# Query_time: %{NUMBER:Query_Time:float}\s+Lock_time: %{NUMBER:Lock_Time:float}\s+Rows_sent: %{NUMBER:Rows_Sent:int}\s+Rows_examined: %{NUMBER:Rows_Examined:int}\s*(?:use %{DATA:Database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<Query>(?<Action>\w+)\s+.*)\n# Time:.*$" }
  45. }
  46. date {
  47. match => [ "timestamp", "UNIX" ]
  48. remove_field => [ "timestamp" ]
  49. }
  50. }
  51. output {
  52. if [type] == "system" {
  53. elasticsearch {
  54. hosts => ["192.168.1.202:9200"]
  55. index => "nagios-system-%{+YYYY.MM.dd}"
  56. }
  57. }
  58. if [type] == "secure" {
  59. elasticsearch {
  60. hosts => ["192.168.1.202:9200"]
  61. index => "nagios-secure-%{+YYYY.MM.dd}"
  62. }
  63. }
  64. if [type] == "http" {
  65. elasticsearch {
  66. hosts => ["192.168.1.202:9200"]
  67. index => "nagios-http-%{+YYYY.MM.dd}"
  68. }
  69. }
  70. if [type] == "nginx" {
  71. elasticsearch {
  72. hosts => ["192.168.1.202:9200"]
  73. index => "nagios-nginx-%{+YYYY.MM.dd}"
  74. }
  75. }
  76. if [type] == "mysql" {
  77. elasticsearch {
  78. hosts => ["192.168.1.202:9200"]
  79. index => "nagios-mysql-slow-%{+YYYY.MM.dd}"
  80. }
  81. }
  82. }

查看效果(一条慢日志查询会显示一条,如果不进行正则匹配,那么一行就会显示一条)

具体的日志输出需求,进行具体的分析

三:ELK终极篇

  1. 安装reids
  2. # yum install -y redis
  3. 修改redis的配置文件
  4. # vim /etc/redis.conf
  5. 修改内容如下
  6. daemonize yes
  7. bind 192.168.1.202
  8. 启动redis服务
  9. # /etc/init.d/redis restart
  10. 测试redis的是否启用成功
  11. # redis-cli -h 192.168.1.202
  12. 输入info如果有不报错即可
  13. redis 192.168.1.202:6379> info
  14. redis_version:2.4.10
  15. ....
  16. 编辑配置redis-out.conf配置文件,把标准输入的数据存储到redis
  17. # vim /etc/logstash/conf.d/redis-out.conf
  18. 添加如下内容
  19. input {
  20. stdin {}
  21. }
  22. output {
  23. redis {
  24. host => "192.168.1.202"
  25. port => "6379"
  26. password => 'test'
  27. db => '1'
  28. data_type => "list"
  29. key => 'elk-test'
  30. }
  31. }
  32. 运行logstash指定redis-out.conf的配置文件
  33. # /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf

运行成功以后,在logstash中输入内容(查看下效果)

  1. 编辑配置redis-in.conf配置文件,把reids的存储的数据输出到elasticsearch
  2. # vim /etc/logstash/conf.d/redis-out.conf
  3. 添加如下内容
  4. input{
  5. redis {
  6. host => "192.168.1.202"
  7. port => "6379"
  8. password => 'test'
  9. db => '1'
  10. data_type => "list"
  11. key => 'elk-test'
  12. batch_count => 1 #这个值是指从队列中读取数据时,一次性取出多少条,默认125条(如果redis中没有125条,就会报错,所以在测试期间加上这个值)
  13. }
  14. }
  15. output {
  16. elasticsearch {
  17. hosts => ['192.168.1.202:9200']
  18. index => 'redis-test-%{+YYYY.MM.dd}'
  19. }
  20. }
  21. 运行logstash指定redis-in.conf的配置文件
  22. # /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf

  1. 把之前的配置文件修改一下,变成所有的日志监控的来源文件都存放到redis中,然后通过redis在输出到elasticsearch
  2. 更改为如下,编辑full.conf
  3. input {
  4. file {
  5. path => "/var/log/httpd/access_log"
  6. type => "http"
  7. start_position => "beginning"
  8. }
  9. file {
  10. path => "/usr/local/nginx/logs/elk.access.log"
  11. type => "nginx"
  12. start_position => "beginning"
  13. }
  14. file {
  15. path => "/var/log/secure"
  16. type => "secure"
  17. start_position => "beginning"
  18. }
  19. file {
  20. path => "/var/log/messages"
  21. type => "system"
  22. start_position => "beginning"
  23. }
  24. }
  25. output {
  26. if [type] == "http" {
  27. redis {
  28. host => "192.168.1.202"
  29. password => 'test'
  30. port => "6379"
  31. db => "6"
  32. data_type => "list"
  33. key => 'nagios_http'
  34. }
  35. }
  36. if [type] == "nginx" {
  37. redis {
  38. host => "192.168.1.202"
  39. password => 'test'
  40. port => "6379"
  41. db => "6"
  42. data_type => "list"
  43. key => 'nagios_nginx'
  44. }
  45. }
  46. if [type] == "secure" {
  47. redis {
  48. host => "192.168.1.202"
  49. password => 'test'
  50. port => "6379"
  51. db => "6"
  52. data_type => "list"
  53. key => 'nagios_secure'
  54. }
  55. }
  56. if [type] == "system" {
  57. redis {
  58. host => "192.168.1.202"
  59. password => 'test'
  60. port => "6379"
  61. db => "6"
  62. data_type => "list"
  63. key => 'nagios_system'
  64. }
  65. }
  66. }
  67. 运行logstash指定shipper.conf的配置文件
  68. # /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/full.conf
  69. redis中查看是否已经将数据写到里面(有时候输入的日志文件不产生日志,会导致redis里面也没有写入日志)

  1. redis中的数据读取出来,写入到elasticsearch中(需要另外一台主机做实验)
  2. 编辑配置文件
  3. # vim /etc/logstash/conf.d/redis-out.conf
  4. 添加如下内容
  5. input {
  6. redis {
  7. type => "system"
  8. host => "192.168.1.202"
  9. password => 'test'
  10. port => "6379"
  11. db => "6"
  12. data_type => "list"
  13. key => 'nagios_system'
  14. batch_count => 1
  15. }
  16. redis {
  17. type => "http"
  18. host => "192.168.1.202"
  19. password => 'test'
  20. port => "6379"
  21. db => "6"
  22. data_type => "list"
  23. key => 'nagios_http'
  24. batch_count => 1
  25. }
  26. redis {
  27. type => "nginx"
  28. host => "192.168.1.202"
  29. password => 'test'
  30. port => "6379"
  31. db => "6"
  32. data_type => "list"
  33. key => 'nagios_nginx'
  34. batch_count => 1
  35. }
  36. redis {
  37. type => "secure"
  38. host => "192.168.1.202"
  39. password => 'test'
  40. port => "6379"
  41. db => "6"
  42. data_type => "list"
  43. key => 'nagios_secure'
  44. batch_count => 1
  45. }
  46. }
  47. output {
  48. if [type] == "system" {
  49. elasticsearch {
  50. hosts => ["192.168.1.202:9200"]
  51. index => "nagios-system-%{+YYYY.MM.dd}"
  52. }
  53. }
  54. if [type] == "http" {
  55. elasticsearch {
  56. hosts => ["192.168.1.202:9200"]
  57. index => "nagios-http-%{+YYYY.MM.dd}"
  58. }
  59. }
  60. if [type] == "nginx" {
  61. elasticsearch {
  62. hosts => ["192.168.1.202:9200"]
  63. index => "nagios-nginx-%{+YYYY.MM.dd}"
  64. }
  65. }
  66. if [type] == "secure" {
  67. elasticsearch {
  68. hosts => ["192.168.1.202:9200"]
  69. index => "nagios-secure-%{+YYYY.MM.dd}"
  70. }
  71. }
  72. }
  73. 注意:
  74. input是从客户端收集的
  75. output是同样也保存到192.168.1.202中的elasticsearch中,如果要保存到当前的主机上,可以把output中的hosts修改成localhost,如果还需要在kibana中显示,需要在本机上部署kabana,为何要这样做,起到一个松耦合的目的
  76. 说白了,就是在客户端收集日志,写到服务端的redis里或是本地的redis里面,输出的时候对接ES服务器即可
  77. 运行命令看看效果
  78. # /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/redis-out.conf

效果是和直接往ES服务器输出一样的(这样是先将日志存到redis数据库,然后再从redis数据库里取出日志)

上线ELK

  1. 1. 日志分类
  2. 系统日志 rsyslog logstash syslog插件
  3. 访问日志 nginx logstash codec json
  4. 错误日志 file logstash mulitline
  5. 运行日志 file logstash codec json
  6. 设备日志 syslog logstash syslog插件
  7. Debug日志 file logstash json 或者 mulitline
  8. 2. 日志标准化
  9. 路径 固定
  10. 格式 尽量json
  11. 3. 系统个日志开始-->错误日志-->运行日志-->访问日志

因为ES保存日志是永久保存,所以需要定期删除一下日志,下面命令为删除指定时间前的日志

  1. curl -X DELETE http://xx.xx.com:9200/logstash-*-`date +%Y-%m-%d -d "-$n days"`

快速搭建ELK日志分析系统的更多相关文章

  1. 搭建ELK日志分析系统

    看了辣么多博客,就数这个最详细最容易理解了:https://blog.csdn.net/qq_22211217/article/details/80764568 >>>>> ...

  2. 十分钟搭建和使用ELK日志分析系统

    前言 为满足研发可视化查看测试环境日志的目的,准备采用EK+filebeat实现日志可视化(ElasticSearch+Kibana+Filebeat).题目为“十分钟搭建和使用ELK日志分析系统”听 ...

  3. Docker笔记(十):使用Docker来搭建一套ELK日志分析系统

    一段时间没关注ELK(elasticsearch —— 搜索引擎,可用于存储.索引日志, logstash —— 可用于日志传输.转换,kibana —— WebUI,将日志可视化),发现最新版已到7 ...

  4. centos7搭建EFK日志分析系统

    前言 EFK可能都不熟悉,实际上EFK是大名鼎鼎的日志系统ELK的一个变种 在没有分布式日志的时候,每次出问题了需要查询日志的时候,需要登录到Linux服务器,使用命令cat -n xxxx|grep ...

  5. 快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana)

    快速搭建应用服务日志收集系统(Filebeat + ElasticSearch + kibana) 概要说明 需求场景,系统环境是CentOS,多个应用部署在多台服务器上,平时查看应用日志及排查问题十 ...

  6. Rsyslog+ELK日志分析系统

    转自:https://www.cnblogs.com/itworks/p/7272740.html Rsyslog+ELK日志分析系统搭建总结1.0(测试环境) 因为工作需求,最近在搭建日志分析系统, ...

  7. ELK日志分析系统-Logstack

    ELK日志分析系统 作者:Danbo 2016-*-* 本文是学习笔记,参考ELK Stack中文指南,链接:https://www.gitbook.com/book/chenryn/kibana-g ...

  8. 搭建ELK日志分析平台(上)—— ELK介绍及搭建 Elasticsearch 分布式集群

    笔记内容:搭建ELK日志分析平台(上)-- ELK介绍及搭建 Elasticsearch 分布式集群笔记日期:2018-03-02 27.1 ELK介绍 27.2 ELK安装准备工作 27.3 安装e ...

  9. ELK 日志分析系统的部署

    一.ELK简介 ElasticSearch介绍Elasticsearch是一个基于Lucene的搜索服务器. 它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口. Elasti ...

随机推荐

  1. day-17(基础加强)

    回顾: listener(了解) 监听器,监听javaweb中三个域对象 监听对象的创建和销毁 ServletContextListener 在项目启动的时候加载配置文件 ServletRequest ...

  2. 洛谷P2770 双路DP // 网络流

    https://www.luogu.org/problemnew/show/P2770 第一眼看过去,觉得这不是一个经典的双路DP模型吗,将一条过去一条回来互不相交的路径看作是起点出发了两条路径一起走 ...

  3. 如何修改Tomcat的默认项目发布路径

    tomcat默认的项目发布目录是/webapp/ROOT,如果想自定义发布目录,应该怎么办呢? 修改配置文件 首先,修改$tomcat/conf/server.xml文件. 在server.xml文件 ...

  4. MySQL 8.0.14 新的密码认证方式和客户端链接

    MySQL 8.0.14 新的密码认证方式和客户端链接 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   MySQL8.0在密码认证方式发生了改变,这也是有点小伙伴在MySQL创建 ...

  5. mysql 遍历所有的库并根据表和sql语句备份

    建库.用户语句 create database test_hb; create user ' test_hb'@'%' identified by '123456'; grant all privil ...

  6. twitter分布式主键id生成器

    pom <!--生成id--> <dependency> <groupId>com.github.bingoohuang</groupId> <a ...

  7. Sqlserver中的视图

    一.视图的基本知识 什么是视图:视图是从一个或多个表导出的虚拟的表,具有普通表的结构,物理上是不存在的.视图是动态的数据的集合,数据是随着基表的更新而更新. 视图的优点: ①在多表查询时,查询方便. ...

  8. C#中的特性(Attributes)

    约定: 1.”attribute”和”attributes”均不翻译 2.”property”译为“属性” 3.msdn中的原句不翻译 4.”program entity”译为”语言元素” Attri ...

  9. idea上使用maven模块开发

    使用maven模块开发: 使用Maven构建多模块项目 在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为common(域模型层).dao(数据库访问层).s ...

  10. java.io.OutputStream & java.io.FileOutputStream

    java.io.OutputStream & java.io.FileOutputStream 1.Java.io.OutputStream(字节输出流) 字节输出流,这是一个抽象类,是表示输 ...