设为首页 收藏本站
查看: 1253|回复: 0

[经验分享] ELK-5.0.1_生产实践杂乱笔记

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-2-10 16:00:04 | 显示全部楼层 |阅读模式
[iyunv@centos7 ~]# cat /etc/redhat-release  (经实践,elk5.0.1 需要内核3.点几以上版本支持)
CentOS Linux release 7.2.1511 (Core)
IP: 本地 192.168.1.73

[iyunv@centos7 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.73 centos7.spring.study

[iyunv@centos7 src]# tar zxf elasticsearch-5.0.1.tar.gz
[iyunv@centos7 src]# mv  elasticsearch-5.0.1 /usr/local/
[iyunv@centos7 local]# ln -s elasticsearch-5.0.1 /usr/local/elasticsearch
            
[iyunv@centos7 config]# vim elasticsearch.yml
cluster.name: ranruichun
node.name: "linux-node1"
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
bootstrap.memory_lock: true

#groupadd  elk
#useradd  elk -g elk

#  su elk /usr/local/services/elk/elasticsearch-5.0.1/bin/elasticsearch

编写启动脚本
[iyunv@centos7 elasticsearch]# cat  /usr/local/elasticsearch/run.sh
su elk -l -c "nohup /usr/local/elasticsearch/bin/elasticsearch >  /usr/local/elasticsearch/log.out &"
su elk -l -c "nohup /usr/local/elasticsearch/bin/elasticsearch >  /usr/local/elasticsearch/log.out &"

[iyunv@centos7 elasticsearch]# vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

[iyunv@iZ25mh87ol1Z elasticsearch-5.0.1]# vim /etc/sysctl.conf
vm.max_map_count=655360
vm.swappiness = 0

[iyunv@centos7 elasticsearch]# curl  http://192.168.1.73:9200
{
  "name" : "linux-node1",
  "cluster_name" : "xxxxxx",
  "cluster_uuid" : "IRmR9sPtSBqIqj5gA7oUiw",
  "version" : {
    "number" : "5.0.1",
    "build_hash" : "080bb47",
    "build_date" : "2016-11-11T22:08:49.812Z",
    "build_snapshot" : false,
    "lucene_version" : "6.2.1"
  },
  "tagline" : "You Know, for Search"
}


# su elk -l -c  "/usr/local/elasticsearch/bin/elasticsearch -d"  后台启动

# cd /usr/local/src/
[iyunv@centos7 src]# git clone https://github.com/elastic/elasticsearch-servicewrapper.g


[iyunv@centos7 src]# mv elasticsearch-servicewrapper/service/ /usr/local/elasticsearch/bin/

[iyunv@centos7 service]# /usr/local/elasticsearch/bin/service/elasticsearch  install
Detected RHEL or Fedora:
Installing the Elasticsearch daemon..

[iyunv@centos7 service]# ls /etc/init.d/elasticsearch
/etc/init.d/elasticsearch


[iyunv@centos7 elasticsearch]# curl -i  -XGET 'http://192.168.1.73:9200/_count?pretty' -d '
{
    "query":{
          "match_all":{}
  }
}
'
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 95

{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  }
}



装集群管理插件  head
[iyunv@centos7 src]# git clone git://github.com/mobz/elasticsearch-head.git

看文章
http://blog.csdn.net/reblue520/article/details/53909409

http://blog.csdn.net/sulei12341/ ... tionNum=4&fps=1
http://hnr520.blog.iyunv.com/4484939/1867033

[iyunv@centos7 node_modules]# /usr/local/elasticsearch-head/node_modules/grunt/bin/grunt server

[iyunv@centos7 elasticsearch-head]# npm install grunt --save
npm WARN package.json elasticsearch-head@0.0.0 license should be a valid SPDX license expression
grunt@1.0.1 node_modules/grunt
├── grunt-known-options@1.1.0
... head 后面还要改很多参数才可以访问es (看上面文章)


Logstash
# cd  /usr/local/src
# wget https://download.elastic.co/logs ... gstash-1.5.4.tar.gz
#  tar  zxf  logstash-1.5.4.tar.gz
#  mv logstash-1.5.4  /usr/local/logstash
[iyunv@rui local]# java -version  验证java环境
java version "1.7.0_09-icedtea"

-- 生产建议是yum 安装 (网上比我牛的人如此说)  这儿就介绍方法  
https://www.elastic.co/guide/en/ ... lling-logstash.html
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
[iyunv@centos7 local]# cat /etc/yum.repos.d/logstash.repo
[logstash-5.x]
name=Elastic repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
[iyunv@centos7 ~]# yum install logstash


启动
/usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} }'

[iyunv@rui local]# /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { stdout{codec => rubydebug} }'
Logstash startup completed
hehe
{
       "message" => "hehe",
      "@version" => "1",
    "@timestamp" => "2016-12-13T21:50:51.837Z",
          "host" => "rui.study.com"
}

[iyunv@rui local]# /usr/local/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { host => "192.168.1.104" protocol =>"<a href="http" }"="">http"} }'

配置文件 官方文档
https://www.elastic.co/guide/en/ ... /configuration.html

生产需要
gork规则debug调试  好用!
https://grokdebug.herokuapp.com/
https://grokdebug.herokuapp.com/patterns#
http://www.open-open.com/lib/view/open1453623562651.html
apache日志 --> 标准格式
tomcat 自定义日志样本
[iyunv@iZ2535e0vgsZ filebeat-5.0.1-linux-x86_64]# tail -1  /root/tomcat/tomcat1/logs/fblive-web-www.log.2017-01-22.log
[iZ2535e0vgsZ|10.24.190.246|[fblive-web-www]|2017-01-22 18:44:04.665|[pool-6-thread-1]|WARN |org.hibernate.internal.util.xml.DTDEntityResolver|DTDEntityResolver.java|org.hibernate.internal.util.xml.DTDEntityResolver|resolveEntity|75|1485078461818|HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!||||

修改tomcat匹配规则
# Log Levels
LOGLEVEL ([Aa]lert|ALERT|[Tt]race|TRACE|[Dd]ebug|DEBUG|[Nn]otice|NOTICE|[Ii]nfo |INFO |[Ww]arn?(?:ing)?|WARN?(?:ING)? |[Ee]rr?(?:or)?|ERR?(?:OR)?|[Cc]rit?(?:ical)?|CRIT?(?:ICAL)?|[Ff]atal|FATAL|[Ss]evere|SEVERE|EMERG(?:ENCY)?|[Ee]merg(?:ency)?)

TOMCAT_DATESTAMP 20%{YEAR}-%{MONTHNUM}-%{MONTHDAY} %{HOUR}:?%{MINUTE}(?::?%{SECOND})

TOMCATLOG %{TOMCAT_DATESTAMP:timestamp} \| %{LOGLEVEL:level} \| %{JAVACLASS:class} - %{JAVALOGMESSAGE:logmessage}
TOMCATFBLOG \[%{IPORHOST:hostname}\|%{IP:serverip}\|%{SYSLOG5424SD:application}\|%{TOMCAT_DATESTAMP:timestamp}\|\[%{DATA:thread}\]\|%{LOGLEVEL:level}\|%{JAVACLASS:logger}\|%{JAVACLASS:file}\|%{JAVACLASS:class}\|%{HOSTNAME:method}\|%{NUMBER:line}\|%{NUMBER:lsn}\|%{GREEDYDATA:msg}


logstash配置文件样列(报警设置是linux mail 发送 有错误写进文件,定时任务有错误每5分钟发送,有时间再学习更科学的方法,有错误来直接发)
input {
    beats {
        port => 9500
        #mode => "server"
        ssl => false
    }  
}
filter {
    if [type] == "apache-accesslog" {
            grok {
                patterns_dir => "/usr/local/services/elk/logstash-5.0.1/logstash-patterns-core/patterns"
                       match => { "message" => "%{COMMONAPACHELOG}" }
            }
        geoip {
                   source => "clientip"
                   add_tag => [ "geoip" ]
        }
        if ([clientip] =~ "^100.109") {
           drop {}
        }
        if([request] =~ "server-status"){
           drop {}
        }
        mutate {
                split => ["request", "?"]
            }
        mutate {
                add_field =>   {
                        "requesturl" => "%{[request][0]}"
                        "requestparams" => "%{[request][1]}"
                }
        }
        mutate {
                join => ["request", "?"]
            }
        date {
            match => ["timestamp", "dd/MMM/yyyy:HH:mm:ss Z"]
        }
    }   
   if [type] == "tomcat-accesslog" {
        grok {
                patterns_dir => ["/usr/local/services/elk/logstash-5.0.1/logstash-patterns-core/patterns"]
                match => { "message" => "%{TOMCATFBLOG}" }
        }
         if "fblive-api-web" in [application] {
                mutate { replace => { type => "tomcat-fblive-api-web-accesslog" } }
         }
         else if "fblive-web-www" in [application] {
                mutate { replace => { type => "tomcat-fblive-web-www-accesslog" } }
         }     
      }
}

output {
    if [type] == "apache-accesslog" {
        elasticsearch {
            template_overwrite => "true"
            hosts => ["127.0.0.1:9200"]
            index => "logstash-apache-accesslog-%{+YYYY.MM.dd}"
        }
    }   
  
    if [type] == "tomcat-fblive-api-web-accesslog" {
           elasticsearch {
   hosts => ["127.0.0.1:9200"]

                   index => "tomcat-fblive-api-web-accesslog-%{+YYYY.MM.dd}"               
        }
        if [level] == "ERROR" {
           file {
                        path => "/root/elk/error_mail/%{+yyyyMMdd}/fblive-api-web%{+HH}.log"
                }
        }
    }
    if [type] == "tomcat-fblive-web-www-accesslog" {
      elasticsearch {
      hosts => ["127.0.0.1:9200"]

                   index => "tomcat-fblive-web-www-accesslog-%{+YYYY.MM.dd}"               
        }
        if [level] == "ERROR" {
           file {
                        path => "/root/elk/error_mail/%{+yyyyMMdd}/fblive-web-www%{+HH}.log"
                }
        }
    }
   
}


[iyunv@iZ25mh87ol1Z error_mail]# cat sendmail.sh
#!/bin/sh
#sendmail error log to someone

function sendErrorMail(){
        file=/root/elk/error_mail/$(date -d '-8 hour'  +%Y%m%d/$1%H.log)
#        echo $file

        if [ -f "$file" ]; then
                echo 'send mail'$file
                mail -s '[error]'$1 152xx@qq.com,xx@gamil.com < $file
                mv $file $file.send
        else
                echo 'no file:'$file
        fi
}

#end
sendErrorMail fblive-api-web
sendErrorMail fblive-web-www



[iyunv@iZ25mh87ol1Z ~]# crontab  -l
00 10 * * *  /root/elk/elasticsearch-5.0.1/rm_es_tomcat_7_day_ago.sh delete_tomcat
*/5 * * * * /root/elk/error_mail/sendmail.sh
*/30 * * * * /root/elk/error_mail/stat15m.sh


Kibana
http://kibana.logstash.es/content/index.html   中文指南
https://github.com/chenryn/ELKstack-guide-cn/releases/tag/ELK     中文指南下下来看
[iyunv@centos7 src]# tar zxf kibana-5.0.1-linux-x86_64.tar.gz
[iyunv@centos7 src]# mv kibana-5.0.1-linux-x86_64 /usr/local/kibana

[iyunv@centos7 src]# cd /usr/local/kibana/config/
[iyunv@centos7 config]# ll
总用量 8
-rw-rw-r--. 1 spring spring 4426 12月 30 10:01 kibana.yml
[iyunv@centos7 config]# vim kibana.yml
elasticsearch.url: "<a href="http://192.168.1.73:9200" "="">http://192.168.1.73:9200"

elasticsearch.username: "elastic"
elasticsearch.password: "changeme"

生产环境自己加了个可以加个nignx方向代理做访问认证.

filebeat配置 (tomcat日志有多行配置)
- input_type: log

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /root/apache/logs/access*
  document_type: apache-accesslog

-
  paths:
    - /root/tomcat/tomcat1/logs/fblive*
    - /root/data/../bin/logs/fblive*        
  document_type: tomcat-accesslog
  multiline:
    pattern: ^\[
    negate: true
    match: after


有需求来了, elk立功了!!!  
elasticdump 迁移与导出es数据

导出 搜索匹配 :request":"/redenv_AfterShare.ss 的数据
/root/software/node_modules/elasticdump/bin/elasticdump   --input=http://127.0.0.1:9200/logstash-apache-accesslog-2016.12.13   --output=logstash-apache-accesslog-2016.12.13.json   --searchBody '{"query": {"match":{"request":"/redenv_AfterShare.ss"}}}'   --type=data   --sourceOnly

只导出message                    
/root/software/node_modules/elasticdump/bin/elasticdump   --input=http://127.0.0.1:9200/logstash-apache-accesslog-2016.12.04  \
   --output=/root/software/node_modules/elasticdump/bin/apache_accesslog/logstash-apache-accesslog-2016-12-04.json \
--searchBody='{ "_source": "message", "query": {"match_all": {}} }'   --type=data



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-340329-1-1.html 上篇帖子: 升级redis3.2.7编译报错“ undefined reference to `clock_gettime'” 下篇帖子: Redis Cluster集群搭建
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表