ElasticSearch 是一个高可用开源全文检索和分析组件提供存储服务,搜索服务,大数据准实时分析等。一般用于提供一些提供复杂搜索的应。我们为什么要选择 ElasticSearch ?因为它是一个准实时的搜索工具,在一般情况下延时少于一秒,它还支持物理上的水平扩展,并拥有一套分布式协调的管理功能操作比较简单,包括一些 restful 风格的API 等等,接下来我们就来进入今天的正题。

前期准备

1.安装 jdk1.8

  1. yum install java

2.配置源

  1. rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
  2. 配置elasticsearch
  3. cat >/etc/yum.repos.d/elasticsearch.repo <<EOF
  4. [elasticsearch-5.x]
  5. name=Elasticsearch repository for 5.x packages
  6. baseurl=https://artifacts.elastic.co/packages/5.x/yum
  7. gpgcheck=1
  8. gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
  9. enabled=1
  10. autorefresh=1
  11. type=rpm-md
  12. EOF
  13. 配置kibana
  14. cat >/etc/yum.repos.d/kibana.repo << EOF
  15. [kibana-5.x]
  16. name=Kibana repository for 5.x packages
  17. baseurl=https://artifacts.elastic.co/packages/5.x/yum
  18. gpgcheck=1
  19. gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
  20. enabled=1
  21. autorefresh=1
  22. type=rpm-md
  23. EOF

3.安装

  1. yum install elasticsearch -y
  2. yum install kibana -y

4.修改 es 配置文件

  1. mkdir /data/es-data/logs -p
  2. chown -R elasticsearch.elasticsearch /data/es-data/logs
  3. [root@linux-node2 ~]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml |grep -v "^$"
  4. cluster.name: lx
  5. node.name: linux-node2.lx.com
  6. path.data: /data/es-data
  7. path.logs: /data/es-data/logs
  8. http.port: 9200
  9. discovery.zen.ping.unicast.hosts: ["192.168.56.12", "192.168.56.14"]
  10. discovery.zen.minimum_master_nodes: 1

5.修改内核参数

  1. vim /etc/security/limits.conf
  2. * soft nofile 655350
  3. * hard nofile 655350
  4. vim /etc/sysctl.conf
  5. fs.file-max=655350
  6. 调整内存大小
  7. cat /data/elasticsearch/config/jvm.options   (可设置为物理内存的一半)
  8. -Xms8g
  9. -Xmx8g
  10. sysctl -w vm.max_map_count=262144

6.修改 kibana 配置文件

  1. [root@linux-node5 elasticsearch]# grep -v "^#" /etc/kibana/kibana.yml |grep -v "^$"
  2. server.port: 5601
  3. server.host: "0.0.0.0"
  4. server.name: "lx"
  5. elasticsearch.url: "http://192.168.56.14:9200"
  6. elasticsearch.username: "elastic"
  7. elasticsearch.password: "changeme"

7.x-pack 破解

编辑配置文件

  1. cat LicenseVerifier.java
  2. package org.elasticsearch.license;
  3. import java.nio.*;
  4. import java.util.*;
  5. import java.security.*;
  6. import org.elasticsearch.common.xcontent.*;
  7. import org.apache.lucene.util.*;
  8. import org.elasticsearch.common.io.*;
  9. import java.io.*;
  10. public class LicenseVerifier
  11. {
  12. public static boolean verifyLicense(final License license, final byte[] encryptedPublicKeyData) {
  13. return true;
  14. }
  15. public static boolean verifyLicense(final License license) {
  16. return true;
  17. }
  18. }

安装 java-devel

  1. yum install java-devel -y
  2. 编译生成LicenseVerifier.class
  3. javac -cp "/usr/share/elasticsearch/lib/elasticsearch-5.6.4.jar:/usr/share/elasticsearch/lib/lucene-core-6.6.1.jar:/usr/share/elasticsearch/plugins/x-pack/x-pack-5.6.4.jar" LicenseVerifier.java
  4. 查看生成的LicenseVerifier.class类文件
  5. ll LicenseVerifier.class
  6. 替换class文件
  7. cp /usr/share/elasticsearch/plugins/x-pack/x-pack-5.6.4.jar /tmp/test
  8. cd /tmp/test
  9. jar xvf x-pack-5.6.4.jar
  10. cd /tmp/test/org/elasticsearch/license
  11. rm LicenseVerifier.class -f
  12. cp /root/LicenseVerifier.class /tmp/test/org/elasticsearch/license/  #拷贝上文编译生成的java类文件
  13. jar cvf x-pack-5.6.4.jar /tmp/test/*                             #压缩
  14. \cp /tmp/test/x-pack-5.6.4.jar /usr/share/elasticsearch/plugins/x-pack/    #拷贝到原来的x-pack目录

注意x-pack-5.6.4.jar 应该拷贝到两个 elasticsearch 集群插件,即目录 /usr/share/elasticsearch/plugins/x-pack/ 

重启 es 集群

  1. systemct restart elasticsearch

获取 license 证书 

https://license.elastic.co/registration

通过填写的邮件获取官方发来的邮件获取证书

  1. cat li-xiang-d28260d9-6c96-4dd2-92dc-2f14a9787903-v5.json
  2. {"license":{"uid":"d28260d9-6c96-4dd2-92dc-2f14a9787903","type":"platinum","issue_date_in_millis":1511740800000,"expiry_date_in_millis":1827359999000,"max_nodes":100,"issued_to":"li xiang (ceshi)","issuer":"Web Form","signature":"AAAAAwAAAA2lsE14rcZQLw3V/JuUAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSmkxaktJRVl5MUYvUWh3bHZVUTllbXNPbzBUemtnbWpBbmlWRmRZb25KNFlBR2x0TXc2K2p1Y1VtMG1UQU9TRGZVSGRwaEJGUjE3bXd3LzRqZ05iLzRteWFNekdxRGpIYlFwYkJiNUs0U1hTVlJKNVlXekMrSlVUdFIvV0FNeWdOYnlESDc3MWhlY3hSQmdKSjJ2ZTcvYlBFOHhPQlV3ZHdDQ0tHcG5uOElCaDJ4K1hob29xSG85N0kvTWV3THhlQk9NL01VMFRjNDZpZEVXeUtUMXIyMlIveFpJUkk2WUdveEZaME9XWitGUi9WNTZVQW1FMG1DenhZU0ZmeXlZakVEMjZFT2NvOWxpZGlqVmlHNC8rWVVUYzMwRGVySHpIdURzKzFiRDl4TmM1TUp2VTBOUlJZUlAyV0ZVL2kvVk10L0NsbXNFYVZwT3NSU082dFNNa2prQ0ZsclZ4NTltbU1CVE5lR09Bck93V2J1Y3c9PQAAAQBE870HCp9jTj22SVdEP2uAFLF6ikLdDJFtSlbHuYOki6rPtWxIcw8y+WWpPUT5e8lYZw0GkB8CYT5hFLXZTrBqTsNbYRZ3ABdHS1BnKBlkfE4PPcvnTTt4HtBCawNHaW0BNWQ2BA2fFj6zX3HyYJ8q5OaQk/il0t5f/TkIuf0yo3Y/F3rzDIXOHieBFnVvfG3EpNB4lo+G+e6vPMeOW86PsF9eKvQ24nucGDK3S4WSpwxbP1gZFuTdmE9zDguJhRHrtJ6k//A/Q0Fbo8gFntWgHNp+1OJEklH+VBBZUWo17UMGnjjxDrGlLTZcsz2BPmk7mC7e8gBQY4z7zJ/SgnsU","start_date_in_millis":1511740800000}}

此证书的时间为1年使用时间,你可以通过下面网站进行换算http://tool.chinaz.com/Tools/unixtime.aspx,目前我申请了一个10 年的时间

  1. "type":"basic" 替换为 "type":"platinum"    # 基础班变更为铂金版
  2. "expiry_date_in_millis":1543363199999 替换为 "expiry_date_in_millis":1827359999000 # 1年变为10年

查看当前的 license

  1. curl -XGET -u elastic:changeme 'http://127.0.0.1:9200/_license'
  2. {
  3.  "license" : {
  4.    "status" : "active",
  5.    "uid" : "21389992-4010-4d2c-917b-94b4e3d5a1dc",
  6.    "type" : "trial",
  7.    "issue_date" : "2017-11-27T05:12:27.999Z",
  8.    "issue_date_in_millis" : 1511759547999,
  9.    "expiry_date" : "2017-12-27T05:12:27.999Z",
  10.    "expiry_date_in_millis" : 1514351547999,
  11.    "max_nodes" : 1000,
  12.    "issued_to" : "lx",
  13.    "issuer" : "elasticsearch",
  14.    "start_date_in_millis" : -1
  15.  }
  16. }

替换 license

  1. curl -XPUT -u elastic:changeme 'http://127.0.0.1:9200/_xpack/license?acknowledge=true' -d @li-xiang-d28260d9-6c96-4dd2-92dc-2f14a9787903-v5.json

重启 es 集群

  1. systemctl restart elasticsearch

查看 license

  1. [root@linux-node5 license]# curl -XGET -u elastic:changeme 'http://127.0.0.1:9200/_license'
  2. {
  3.  "license" : {
  4.    "status" : "active",
  5.    "uid" : "d28260d9-6c96-4dd2-92dc-2f14a9787903",
  6.    "type" : "platinum",
  7.    "issue_date" : "2017-11-27T00:00:00.000Z",
  8.    "issue_date_in_millis" : 1511740800000,
  9.    "expiry_date" : "2027-11-27T23:59:59.000Z",
  10.    "expiry_date_in_millis" : 1827359999000,
  11.    "max_nodes" : 100,
  12.    "issued_to" : "li xiang (ceshi)",
  13.    "issuer" : "Web Form",
  14.    "start_date_in_millis" : 1511740800000
  15.  }
  16. }

配置 X- Pack 告警

本文配置 X- Pack 告警是通过 filebeat 收集 Nginx 的日志来做的模拟

配置邮件报警

1.安装 Nginx

2.给 Nginx 配置 json 格式的数据

  1.    log_format json '{"@timestamp":"$time_iso8601",'
  2.               '"@version":"1",'
  3.               '"client":"$remote_addr",'
  4.               '"url":"$uri",'
  5.               '"status":"$status",'
  6.               '"domain":"$host",'
  7.               '"host":"$server_addr",'
  8.               '"size":$body_bytes_sent,'
  9.               '"responsetime":$request_time,'
  10.               '"referer": "$http_referer",'
  11.               '"ua": "$http_user_agent"'
  12.               '}';
  13.    access_log  logs/access.log  json;

3.安装 filebeta

  1. 安装
  2. yum install -y filebeta
  3. 配置filebeta
  4. [root@linux-node4 filebeat]# grep -v "^  #" filebeat.yml|grep -v "^$"|grep -v "^#"
  5. filebeat.prospectors:
  6. - input_type: log
  7.  paths:
  8.    - /usr/local/nginx/logs/access.log
  9.  json.keys_under_root: true
  10.  json.overwrite_keys: true
  11. output.elasticsearch:
  12.  hosts: ["localhost:9200"]
  13.  username: "elastic"
  14.  password: "changeme"
  15. 参考网址:https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html#config-json
  16. http://www.iyunw.cn/archives/filebeat-shou-ji-json-ge-shi-de-nginx-ri-zhi-fa-song-gei-elasticsearch/
  17. 启动filebeta服务
  18. systemctl status filebeat.service

这里默认进入 es 的索引是 filebeat-{%}

4.登陆 kibana 添加索引

5.模拟一些 404  的页面,让其展示

for i in {1..1000};do curl http://192.168.56.14:/lx-0$i;sleep 1;done

6.添加报警触发器(收集每分钟内超过访问状态大于 400 且个数超过 20 报警)

Watch JSON

  1. {
  2.  "trigger": {
  3.    "schedule": {
  4.      "interval": "1m"
  5.    }
  6.  },
  7.  "input": {
  8.    "search": {
  9.      "request": {
  10.        "search_type": "query_then_fetch",
  11.        "indices": [
  12.          "filebeat-2017*"
  13.        ],
  14.        "types": [],
  15.        "body": {
  16.          "size": 0,
  17.          "query": {
  18.            "bool": {
  19.              "must": [
  20.                {
  21.                  "query_string": {
  22.                    "query": "status:>=400"
  23.                  }
  24.                },
  25.                {
  26.                  "range": {
  27.                    "@timestamp": {
  28.                      "gte": "now-1m"
  29.                    }
  30.                  }
  31.                }
  32.              ]
  33.            }
  34.          },
  35.          "sort": [
  36.            {
  37.              "@timestamp": {
  38.                "order": "desc"
  39.              }
  40.            }
  41.          ]
  42.        }
  43.      }
  44.    }
  45.  },
  46.  "condition": {
  47.    "compare": {
  48.      "ctx.payload.hits.total": {
  49.        "gt": 20
  50.      }
  51.    }
  52.  },
  53.  "actions": {
  54.    "elasticsearch": {
  55.      "throttle_period_in_millis": 60000,
  56.      "email": {
  57.        "profile": "outlook",
  58.        "attachments": {
  59.          "attached_data": {
  60.            "data": {
  61.              "format": "json"
  62.            }
  63.          }
  64.        },
  65.        "priority": "high",
  66.        "to": [
  67.          "lixiang@xxxxx.com"
  68.        ],
  69.        "subject": "Nginx {{ctx.payload.hits.total}} errors",
  70.        "body": {
  71.          "text": "nginx 404 错误过多,请注意查看"
  72.        }
  73.      }
  74.    }
  75.  }
  76. }

7.这一步特别重要需要在 ES 配置文件设置否则无法报警(因为我公司使用的是  Exchange 客户端)

  1. xpack.notification.email.account:
  2.    exchange_account:
  3.        profile: outlook
  4.        email_defaults:
  5.            from: warning@xxxxx.com
  6.        smtp:
  7.            auth: true
  8.            starttls.enable: true
  9.            host: smtp.partner.outlook.cn
  10.            port: 587
  11.            user: 123@xxxxxx.com
  12.            password: 233g@123
  13. 参考网址:https://www.elastic.co/guide/en/x-pack/5.6/actions-email.html #邮件设置

8. 查看邮件报警状态以及 kibana 中的状态

对于上面的内容大家可以参看下面的 url 来了解其中的含义

参考网址: 

https://www.elastic.co/guide/en/x-pack/5.6/xpack-alerting.html 对集群和索引事件的警告 Xpack document

配置 Webhook 报警

对于 webhook 的报警,我们需要前期做一下工作需要了解一个东西 web.py,具体  URL 链接大家可以访问这个地方 http://webpy.org/tutorial3.zh-cn#starting

1.用 web.py 启动一个 webhook 的监听

  1. [root@linux-node5 ~]# cat webhooks.py
  2. #!/usr/bin/evn python
  3. # -*- coding:utf-8 -*-
  4. import web
  5. import os
  6. import demjson
  7. import sys
  8. reload(sys)
  9. sys.setdefaultencoding( "utf-8" )
  10. urls = (
  11.    '/log_event_watch','abc',
  12. )
  13. class abc:
  14.    def POST(self):
  15.        data = web.data()
  16.        print data
  17.        cmd = '''curl -G -v "http://abc.com/abc/" --data "user=lixiang" --data "media=all" --data-urlencode "subject=test" --data-urlencode "message=%s"''' %(data)
  18.        os.system(cmd)
  19. if __name__ == "__main__":
  20.    app = web.application(urls, globals())
  21.    app.run()
  22. 运行 python webhooks.py 9000

注意 :urls,因为我的 Watcher ID 为 "log_event_watch"

2.再次回到我们的 kibana 界面, "Management"->"Edit"

查看 Watches

查看 Wathch Json 内容

  1. {
  2.  "trigger": {
  3.    "schedule": {
  4.      "interval": "1m"
  5.    }
  6.  },
  7.  "input": {
  8.    "search": {
  9.      "request": {
  10.        "search_type": "query_then_fetch",
  11.        "indices": [
  12.          "nginx_access*"
  13.        ],
  14.        "types": [],
  15.        "body": {
  16.          "size": 0,
  17.          "query": {
  18.            "bool": {
  19.              "must": [
  20.                {
  21.                  "query_string": {
  22.                    "query": "status:>=400"
  23.                  }
  24.                },
  25.                {
  26.                  "range": {
  27.                    "@timestamp": {
  28.                      "gte": "now-1m"
  29.                    }
  30.                  }
  31.                }
  32.              ]
  33.            }
  34.          },
  35.          "sort": [
  36.            {
  37.              "@timestamp": {
  38.                "order": "desc"
  39.              }
  40.            }
  41.          ]
  42.        }
  43.      }
  44.    }
  45.  },
  46.  "condition": {
  47.    "compare": {
  48.      "ctx.payload.hits.total": {
  49.        "gt": 5
  50.      }
  51.    }
  52.  },
  53.  "actions": {
  54.    "email": {
  55.      "throttle_period_in_millis": 60000,
  56.      "email": {
  57.        "profile": "outlook",
  58.        "attachments": {
  59.          "attached_data": {
  60.            "data": {
  61.              "format": "json"
  62.            }
  63.          }
  64.        },
  65.        "priority": "high",
  66.        "to": [
  67.          "lixiang@xxxxx.com"
  68.        ],
  69.        "subject": "Nginx {{ctx.payload.hits.total}} errors",
  70.        "body": {
  71.          "text": "nginx 404 错误过多,请注意查看"
  72.        }
  73.      }
  74.    },
  75.    "webhook": {
  76.      "condition": {
  77.        "compare": {
  78.          "ctx.payload.hits.total": {
  79.            "gt": 5
  80.          }
  81.        }
  82.      },
  83.      "webhook": {
  84.        "scheme": "http",
  85.        "host": "192.168.56.15",
  86.        "port": 9000,
  87.        "method": "post",
  88.        "path": "/{{watch_id}}",
  89.        "params": {},
  90.        "headers": {},
  91.        "body": "Encountered {{ctx.payload.hits.total}} errors"
  92.      }
  93.    }
  94.  }
  95. }

3.模拟触发报警

  1. for i in {1..200};do curl http://192.168.56.14:/lx-0$i;sleep 2;done

4.查看结果,这里就不贴出来了(微信,短信,邮件都能收到报警)

5.这就是实现报警消息附带 URL 地址

官方参考网址:

https://www.elastic.co/guide/en/x-pack/5.6/watcher-getting-started.html

阅读原文

ElasticSearch + xpack 使用的更多相关文章

  1. ElasticSearch + xpack 使用.md

    ElasticSearch 是一个高可用开源全文检索和分析组件.提供存储服务,搜索服务,大数据准实时分析等.一般用于提供一些提供复杂搜索的应.我们为什么要选择 ElasticSearch ?因为它是一 ...

  2. Elasticsearch+x-pack认证license过期处理

    介绍: x-pack是Elasticsearch中的一个plugin,主要功能是实现认证功能 报错: 类似下面这样的报错,是因为licese过期导致的 解决: 1.首先去Elasticsearch的官 ...

  3. elasticsearch x-pack

    elasticsearch-plugin.bat install x-pack D:\elasticsearch-5.5.3\bin>elasticsearch-plugin.bat insta ...

  4. elasticsearch x-pack license过期

    1.注册一个新的license,每一项都要填写,每次可以使用一年,一年到期后再来注册一个新的 2.更新license (官方文档:https://www.elastic.co/guide/en/x-p ...

  5. Spring Data Elasticsearch 和 x-pack 用户名/密码验证连接

    Elasticsearch Java API 客户端连接 一个是TransportClient,一个是NodeClient,还有一个XPackTransportClient TransportClie ...

  6. 申请elasticsearch中x-pack插件许可证及授权

    前提:         ES主机中elasticsearch x-pack插件许可证申请使用期限为1年,到期后x-pack插件将不再可用,重启elasticsearch服务后日志会提示一下警告,如图所 ...

  7. ElasticSearch 5学习(3)——单台服务器部署多个节点

    一般情况下单台服务器只会部署一个ElasticSearch node,但是在学习过程中,很多情况下会需要实现ElasticSearch的分布式效果,所以需要启动多个节点,但是学习开发环境(不想开多个虚 ...

  8. X-pack 6.4.0 破解

    X-package 6.4.0 破解 获取x-pack-core-6.4.0.jar 下载 elasticsearch下载页面:https://www.elastic.co/downloads/ela ...

  9. Elasticsearch6.5.2 X-pack破解及安装教程

    先正常安装 elasticSearch, kibana. 1. 如果是6.5.2版本,可以直接下载jar文件:https://download.csdn.net/download/bigben0123 ...

随机推荐

  1. ipython是python的交互式shell工具

    ipython: 是python的交互式shell工具,比默认的python shell工具要好用.支持变了自动补全,自动缩进,内置了很多的功能和函数 启动:可以通过cmd来启动该工具 自动补全: I ...

  2. java第六节 字符串/集合

    /* *String类和StringBuffer类 * 位于java.lang包中 * String类对象中的内容一旦被初始化就不能再改变 * StringBuffer类中用于封装内容可以改变的字符串 ...

  3. CentOS 7 修改网卡名

    假设原网卡名为eth1, 那么在 /etc/sysconfig/network-scripts/ 目录下,必定会存在一个文件名为ifcfg-eth1,和网卡名对应, 这里假设要修改为eth0. 方法一 ...

  4. Jenkins执行批处理文件失败

    今天搭建Jenkins持续集成环境,编译环境是.net,在.net下没有比较好的代码覆盖率测试插件,所以用了开源的OpenCover,计算代码覆盖率,然后用ReportGenerator导出hmtl格 ...

  5. >/dev/null 2>&1 这句话的含义及使用的意义

    一般在shell包括dos下都可能会这样使用. null是一个名叫null小桶的东西,将输出重定向到它的好处是不会因为输出的内容过多而导致文件大小不断的增加.其实,你就认为null就是什么都没有,也就 ...

  6. Delphi 7调用C语言编写的DLL

    DLL一定是要C语言导出的符号,也就是extern “C”. 当然,我们都知道DLL调用分为动态调用和静态调用. 动态调用的一般思想为,先LoadLibrary那个你想加载的DLL,然后通过GetPr ...

  7. Map的深浅拷贝的探究

    1. 复制map示例 首先看一个例子,当我使用不同方法将一个源map拷贝到另一个map后,改变源map,复制后的map理应不受影响 import java.math.BigDecimal; impor ...

  8. centos中执行apt-get命令提示apt-get command not found

    先说结论: 在centos下用yum install xxx yum和apt-get的区别: 一般来说著名的linux系统基本上分两大类:  RedHat系列:Redhat.Centos.Fedora ...

  9. 程序员,不要让自己做兔子(updated) 网上最近流传的一个笑话,关于兔子,狼还有一只老虎的,故事 我就是想打你了,还需要什么理由吗?谁让你是兔子 项目经理是这样当的

    程序员,不要让自己做兔子(updated) 前段时间和一个朋友聊天,酒席间向我抱怨他那段时间的郁闷:项目经理从客户那里拿来一个需求,实际上就是一个ppt描述,我这个朋友拿过来看后刚开始不觉得什么,一个 ...

  10. 细说PHP中strlen和mb_strlen的区别(转)

    在PHP中,strlen与mb_strlen是求字符串长度的函数,但是对于一些初学者来说,如果不看手册,也许不太清楚其中的区别.下面通过例子,讲解这两者之间的区别. 先看例子: <?php // ...