课程介绍  
                Nginx日志分析系统  
                Filebeat入门学习  
                Metricbeat入门学习  
                Kibana入门学习  
                Logstash入门学习  
                综合练习  
                1、Nginx日志分析系统  
                1
                .1、项目需求  
                Nginx是一款非常优秀的web服务器,往往nginx服务会作为项目的访问入口,那么,nginx的性能保障就变得非常重  
                要了,如果nginx的运行出现了问题就会对项目有较大的影响,所以,我们需要对nginx的运行有监控措施,实时掌握  
                nginx的运行情况,那就需要收集nginx的运行指标和分析nginx的运行日志了。  
                1
                .2、业务流程  
                说明:  
                通过Beats采集Nginx的指标数据和日志数据  
                Beats采集到数据后发送到Elasticsearch中  
                Kibana读取数据进行分析  
                用户通过Kibana进行查看分析报表  
                2、部署安装Nginx  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                2
                3
                4
                5
                6
                7
                8
                9
                tar -xvf nginx-1.11.6.tar.gz  
                yum -y install pcre-devel zlib-devel  
                ./configure  
                make install  
                #启动  
                cd /usr/local/nginx/sbin/  
                ./nginx  
                #通过浏览器访问页面并且查看日志  
                10 #访问地址:http://192.168.40.133/  
                11 tail -f /usr/local/nginx/logs/access.log  
                3、Beats 简介  
                
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            Beats系列产品:  
                4、Filebeat  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            4
                .1、架构  
                用于监控、收集服务器日志文件.  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            4
                .2、部署与运行  
                
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                mkdir /itcast/beats  
                tar -xvf filebeat-6.5.4-linux-x86_64.tar.gz  
                cd filebeat-6.5.4-linux-x86_64  
                #创建如下配置文件 itcast.yml  
                filebeat.inputs:  
                - type: stdin  
                enabled: true  
                setup.template.settings:  
                index.number_of_shards: 3  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1 output.console:  
                2
                3
                4
                pretty: true  
                enable: true  
                ꢀ
                5 #启动filebeat  
                6 ./filebeat -e -c itcast.yml  
                7
                8 #输入hello运行结果如下:  
                9 hello  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                4
                4
                4
                4
                4
                4
                0 {  
                1
                ꢀ"@timestamp": "2019-01-12T12:50:03.585Z",  
                ꢀ"@metadata": { #元数据信息  
                ꢀ ꢀ"beat": "filebeat",  
                ꢀ ꢀ"type": "doc",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                2
                3
                4
                5
                6
                7
                ꢀ"source": "",  
                8
                ꢀ"offset": 0,  
                9
                ꢀ"message": "hello", ꢀ#输入的内容  
                ꢀ"prospector": { #标准输入勘探器  
                ꢀ ꢀ"type": "stdin"  
                },  
                0
                1
                2
                3
                ꢀ"input": { ꢀ#控制台标准输入  
                ꢀ ꢀ"type": "stdin"  
                },  
                4
                5
                6
                ꢀ"beat": { #beat版本以及主机信息  
                ꢀ ꢀ"name": "itcast01",  
                ꢀ ꢀ"hostname": "itcast01",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                7
                8
                9
                0
                1
                ꢀ"host": {  
                2
                ꢀ ꢀ"name": "itcast01"  
                }
                3
                4 }  
                5
                4
                .3、读取文件  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                #配置读取文件项 itcast-log.yml  
                filebeat.inputs:  
                - type: log  
                enabled: true  
                paths:  
                - /itcast/beats/logs/*.log  
                setup.template.settings:  
                index.number_of_shards: 3  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                0 output.console:  
                1
                2
                3
                pretty: true  
                enable: true  
                ꢀ
                4 #启动filebeat  
                5 ./filebeat -e -c itcast-log.yml  
                6
                7 #/haoke/beats/logs下创建a.log文件,并输入如下内容  
                8 hello  
                9 world  
                0
                1 #观察filebeat输出  
                2 {  
                3
                ꢀ"@timestamp": "2019-01-12T14:16:10.192Z",  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                4
                4
                4
                4
                4
                4
                4
                4
                4
                4
                5
                5
                5
                5
                5
                5
                5
                5
                5
                5
                6
                6
                6
                6
                6
                6
                6
                6
                6
                6
                7
                7
                7
                4
                ꢀ"@metadata": {  
                ꢀ ꢀ"beat": "filebeat",  
                ꢀ ꢀ"type": "doc",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                5
                6
                7
                8
                9
                ꢀ"host": {  
                0
                ꢀ ꢀ"name": "itcast01"  
                },  
                1
                2
                ꢀ"source": "/haoke/beats/logs/a.log",  
                ꢀ"offset": 0,  
                3
                4
                ꢀ"message": "hello",  
                ꢀ"prospector": {  
                ꢀ ꢀ"type": "log"  
                },  
                5
                6
                7
                8
                ꢀ"input": {  
                9
                ꢀ ꢀ"type": "log"  
                },  
                0
                1
                ꢀ"beat": {  
                2
                ꢀ ꢀ"version": "6.5.4",  
                ꢀ ꢀ"name": "itcast01",  
                ꢀ ꢀ"hostname": "itcast01"  
                }
                3
                4
                5
                6 }  
                7 {  
                8
                ꢀ"@timestamp": "2019-01-12T14:16:10.192Z",  
                9
                ꢀ"@metadata": {  
                ꢀ ꢀ"beat": "filebeat",  
                ꢀ ꢀ"type": "doc",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                0
                1
                2
                3
                4
                ꢀ"prospector": {  
                ꢀ ꢀ"type": "log"  
                },  
                5
                6
                7
                ꢀ"input": {  
                8
                ꢀ ꢀ"type": "log"  
                },  
                9
                0
                ꢀ"beat": {  
                1
                ꢀ ꢀ"version": "6.5.4",  
                ꢀ ꢀ"name": "itcast01",  
                ꢀ ꢀ"hostname": "itcast01"  
                },  
                2
                3
                4
                5
                ꢀ"host": {  
                6
                ꢀ ꢀ"name": "itcast01"  
                },  
                7
                8
                ꢀ"source": "/haoke/beats/logs/a.log",  
                ꢀ"offset": 6,  
                9
                0
                ꢀ"message": "world"  
                1 }  
                2
                可以看出,已经检测到日志文件有更新,立刻就会读取到更新的内容,并且输出到控制台。  
                4
                .4、自定义字段  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                #配置读取文件项 itcast-log.yml  
                filebeat.inputs:  
                - type: log  
                enabled: true  
                paths:  
                ꢀ ꢀ- /itcast/beats/logs/*.log  
                tags: ["web"] ꢀ #添加自定义tag,便于后续的处理  
                fields: ꢀ#添加自定义字段  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                4
                4
                4
                4
                4
                4
                4
                4
                4
                4
                5
                5
                5
                5
                ꢀ from: itcast-im  
                fields_under_root: true #true为添加到根节点,false为添加到子节点中  
                2 setup.template.settings:  
                index.number_of_shards: 3  
                4 output.console:  
                3
                5
                6
                7
                pretty: true  
                enable: true  
                ꢀ
                8 #启动filebeat  
                9 ./filebeat -e -c itcast-log.yml  
                0
                1 #/haoke/beats/logs下创建a.log文件,并输入如下内容  
                2 123  
                3
                4 #执行效果  
                5 {  
                6
                ꢀ"@timestamp": "2019-01-12T14:37:19.845Z",  
                7
                ꢀ"@metadata": {  
                ꢀ ꢀ"beat": "filebeat",  
                ꢀ ꢀ"type": "doc",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                8
                9
                0
                1
                2
                ꢀ"offset": 0,  
                ꢀ"tags": [  
                3
                4
                ꢀ ꢀ"haoke-im"  
                ],  
                5
                6
                ꢀ"prospector": {  
                ꢀ ꢀ"type": "log"  
                },  
                7
                8
                9
                ꢀ"beat": {  
                0
                ꢀ ꢀ"name": "itcast01",  
                ꢀ ꢀ"hostname": "itcast01",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                1
                2
                3
                4
                ꢀ"host": {  
                5
                ꢀ ꢀ"name": "itcast01"  
                },  
                6
                7
                ꢀ"source": "/itcast/beats/logs/a.log",  
                ꢀ"message": "123",  
                ꢀ"input": {  
                8
                9
                0
                ꢀ ꢀ"type": "log"  
                },  
                1
                2
                ꢀ"from": "haoke-im"  
                3 }  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            4
                .5、输出到Elasticsearch  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                # itcast-log.yml  
                filebeat.inputs:  
                - type: log  
                enabled: true  
                paths:  
                ꢀ - /itcast/beats/logs/*.log  
                tags: ["haoke-im"]  
                fields:  
                ꢀ from: haoke-im  
                fields_under_root: false  
                1
                1
                1
                1
                1
                1 setup.template.settings:  
                index.number_of_shards: 3 #指定索引的分区数  
                3 output.elasticsearch: #指定ES的配置  
                hosts: ["192.168.1.7:9200","192.168.1.7:9201","192.168.1.7:9202"]  
                2
                4
                在日志文件中输入新的内容进行测试:  
                查看数据:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            4
                .6、Filebeat工作原理  
                Filebeat由两个主要组件组成:prospector 和 harvester。  
                harvester:  
                负责读取单个文件的内容。  
                如果文件在读取时被删除或重命名,Filebeat将继续读取文件。  
                prospector  
                prospector 负责管理harvester并找到所有要读取的文件来源。  
                如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester。  
                Filebeat目前支持两种prospector类型:log和stdin。  
                Filebeat如何保持文件的状态  
                Filebeat 保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中。  
                该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。  
                如果输出(例如Elasticsearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可用  
                时继续读取文件。  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            在Filebeat运行时,每个prospector内存中也会保存的文件状态信息,当重新启动Filebeat时,将使用注册  
                文件的数据来重建文件状态,Filebeat将每个harvester在从保存的最后偏移量继续读取。  
                文件状态记录在data/registry文件中。  
                启动命令:  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
                ./filebeat -e -c itcast.yml  
                ./filebeat -e -c itcast.yml -d "publish"  
                #参数说明  
                -e: 输出到标准输出,默认输出到syslog和logs下  
                -c: 指定配置文件  
                -d: 输出debug信息  
                #测试: ./filebeat -e -c itcast-log.yml -d "publish"  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                4
                4
                DEBUG ꢀ [publish] ꢀ ꢀ ꢀ pipeline/processor.go:308 ꢀ ꢀ ꢀ Publish event: {  
                ꢀ"@timestamp": "2019-01-12T15:03:50.820Z",  
                ꢀ"@metadata": {  
                ꢀ ꢀ"beat": "filebeat",  
                ꢀ ꢀ"type": "doc",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                ꢀ"offset": 0,  
                ꢀ"tags": [  
                ꢀ ꢀ"haoke-im"  
                ],  
                ꢀ"input": {  
                ꢀ ꢀ"type": "log"  
                },  
                ꢀ"prospector": {  
                ꢀ ꢀ"type": "log"  
                },  
                ꢀ"beat": {  
                ꢀ ꢀ"name": "itcast01",  
                ꢀ ꢀ"hostname": "itcast01",  
                ꢀ ꢀ"version": "6.5.4"  
                },  
                ꢀ"source": "/haoke/beats/logs/a.log",  
                ꢀ"fields": {  
                ꢀ ꢀ"from": "haoke-im"  
                },  
                ꢀ"host": {  
                ꢀ ꢀ"name": "itcast01"  
                },  
                ꢀ"message": "456"  
                0 }  
                1
                4
                .7、读取Nginx日志文件  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                2
                3
                4
                5
                6
                7
                8
                9
                # itcast-nginx.yml  
                filebeat.inputs:  
                - type: log  
                enabled: true  
                paths:  
                ꢀ ꢀ- /usr/local/nginx/logs/*.log  
                tags: ["nginx"]  
                setup.template.settings:  
                index.number_of_shards: 3 #指定索引的分区数  
                10 output.elasticsearch: #指定ES的配置  
                11  
                hosts: ["192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]  
                1
                2
                #启动  
                ./filebeat -e -c itcast-nginx.yml  
                启动后,可以在Elasticsearch中看到索引以及查看数据:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            可以看到,在message中已经获取到了nginx的日志,但是,内容并没有经过处理,只是读取到原数据,那么对于我  
                们后期的操作是不利的,有办法解决吗?  
                4
                .7、Module  
                前面要想实现日志数据的读取以及处理都是自己手动配置的,其实,在Filebeat中,有大量的Module,可以简化我  
                们的配置,直接就可以使用,如下:  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                ./filebeat modules list  
                Enabled:  
                Disabled:  
                apache2  
                auditd  
                elasticsearch  
                haproxy  
                1
                1
                1
                0 icinga  
                1 iis  
                2 kafka  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                3 kibana  
                4 logstash  
                5 mongodb  
                6 mysql  
                7 nginx  
                8 osquery  
                9 postgresql  
                0 redis  
                1 suricata  
                2 system  
                3 traefik  
                4
                可以看到,内置了很多的module,但是都没有启用,如果需要启用需要进行enable操作:  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                ./filebeat modules enable nginx #启动  
                ./filebeat modules disable nginx #禁用  
                Enabled:  
                nginx  
                Disabled:  
                apache2  
                auditd  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                0 elasticsearch  
                1 haproxy  
                2 icinga  
                3 iis  
                4 kafka  
                5 kibana  
                6 logstash  
                7 mongodb  
                8 mysql  
                9 redis  
                0
                osquery  
                1 postgresql  
                2 suricata  
                3 system  
                4 traefik  
                可以发现,nginx的module已经被启用。  
                4
                .7.1、nginx module 配置  
                1
                2
                3
                4
                5
                6
                7
                8
                - module: nginx  
                ꢀ# Access logs  
                access:  
                ꢀ enabled: true  
                ꢀ var.paths: ["/usr/local/nginx/logs/access.log*"]  
                ꢀ ꢀ# Set custom paths for the log files. If left empty,  
                ꢀ ꢀ# Filebeat will choose the paths depending on your OS.  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            9
                10  
                11  
                12  
                13  
                14  
                15  
                16  
                17  
                18  
                ꢀ ꢀ#var.paths:  
                ꢀ# Error logs  
                error:  
                ꢀ enabled: true  
                ꢀ var.paths: ["/usr/local/nginx/logs/error.log*"]  
                ꢀ ꢀ# Set custom paths for the log files. If left empty,  
                ꢀ ꢀ# Filebeat will choose the paths depending on your OS.  
                ꢀ ꢀ#var.paths:  
                4
                .7.2、配置filebeat  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                #vim itcast-nginx.yml  
                filebeat.inputs:  
                #- type: log  
                # enabled: true  
                # paths:  
                # ꢀ - /usr/local/nginx/logs/*.log  
                # tags: ["nginx"]  
                setup.template.settings:  
                index.number_of_shards: 3  
                1
                1
                1
                1
                1
                1
                1 output.elasticsearch:  
                hosts: ["192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]  
                3 filebeat.config.modules:  
                2
                4
                path: ${path.config}/modules.d/*.yml  
                reload.enabled: false  
                5
                4
                .7.3、测试  
                1
                2
                3
                4
                ./filebeat -e -c itcast-nginx.yml  
                #启动会出错,如下  
                ERROR ꢀ fileset/factory.go:142 Error loading pipeline: Error loading pipeline for  
                fileset nginx/access: This module requires the following Elasticsearch plugins:  
                ingest-user-agent, ingest-geoip. You can install them by running the following  
                commands on all the Elasticsearch nodes:  
                5
                6
                7
                8
                9
                ꢀ ꢀsudo bin/elasticsearch-plugin install ingest-user-agent  
                ꢀ ꢀsudo bin/elasticsearch-plugin install ingest-geoip  
                ꢀ ꢀ  
                #解决:需要在Elasticsearch中安装ingest-user-agent、ingest-geoip插件  
                #在资料中可以找到,ingest-user-agent.tar、ingest-geoip.tar、ingest-geoip-conf.tar 3个文件  
                10 #其中,ingest-user-agent.tar、ingest-geoip.tar解压到plugins下  
                11 #ingest-geoip-conf.tar解压到config下  
                12 #问题解决。  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            测试发现,数据已经写入到了Elasticsearch中,并且拿到的数据更加明确了:  
                当然了,其他的Module的用法参加官方文档:  
                
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            5、Metricbeat  
                定期收集操作系统或应用服务的指标数据  
                存储到Elasticsearch中,进行实时分析  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            5
                .1、Metricbeat组成  
                Metricbeat有2部分组成,一部分是Module,另一部分为Metricset。  
                Module  
                收集的对象,如:mysql、redis、nginx、操作系统等;  
                Metricset  
                收集指标的集合,如:cpu、memory、network等;  
                以Redis Module为例:  
                5
                .2、部署与收集系统指标  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                tar -xvf metricbeat-6.5.4-linux-x86_64.tar.gz  
                cd metricbeat-6.5.4-linux-x86_64  
                vim metricbeat.yml  
                metricbeat.config.modules:  
                path: ${path.config}/modules.d/*.yml  
                reload.enabled: false  
                setup.template.settings:  
                index.number_of_shards: 1  
                index.codec: best_compression  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1 setup.kibana:  
                2 output.elasticsearch:  
                3
                hosts: ["192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]  
                4 processors:  
                5
                6
                7
                ꢀ- add_host_metadata: ~  
                ꢀ- add_cloud_metadata: ~  
                ꢀ
                8 #启动  
                9 ./metricbeat -e  
                在ELasticsearch中可以看到,系统的一些指标数据已经写入进去了:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            system module配置:  
                1
                2
                3
                root@itcast01:modules.d# cat system.yml  
                # Module: system  
                # Docs: https://www.elastic.co/guide/en/beats/metricbeat/6.5/metricbeat-module-  
                system.html  
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                - module: system  
                period: 10s  
                metricsets:  
                ꢀ - cpu  
                ꢀ - load  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                ꢀ - memory  
                ꢀ - network  
                ꢀ - process  
                ꢀ - process_summary  
                ꢀ ꢀ#- core  
                ꢀ ꢀ#- diskio  
                ꢀ ꢀ#- socket  
                process.include_top_n:  
                ꢀ by_cpu: 5 ꢀ ꢀ ꢀ# include top 5 processes by CPU  
                ꢀ by_memory: 5 ꢀ # include top 5 processes by memory  
                1 - module: system  
                2
                3
                4
                5
                6
                7
                8
                9
                period: 1m  
                metricsets:  
                ꢀ - filesystem  
                ꢀ - fsstat  
                processors:  
                - drop_event.when.regexp:  
                ꢀ ꢀ system.filesystem.mount_point: '^/(sys|cgroup|proc|dev|etc|host|lib)($|/)'  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                0 - module: system  
                1
                2
                3
                4
                period: 15m  
                metricsets:  
                ꢀ - uptime  
                5 #- module: system  
                6 # period: 5m  
                7 # metricsets:  
                8 # ꢀ - raid  
                9 # raid.mount_point: '/'  
                5
                .3、Module  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                ./metricbeat modules list ꢀ#查看列表  
                Enabled:  
                system #默认启用  
                Disabled:  
                aerospike  
                apache  
                ceph  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                0 couchbase  
                1 docker  
                2 dropwizard  
                3 elasticsearch  
                4 envoyproxy  
                5 etcd  
                6 golang  
                7 graphite  
                8 haproxy  
                9 http  
                0 jolokia  
                1 kafka  
                2 kibana  
                3 kubernetes  
                4 kvm  
                5 logstash  
                6 memcached  
                7 mongodb  
                8 munin  
                9 mysql  
                0 nginx  
                1 php_fpm  
                2 postgresql  
                3 prometheus  
                4 rabbitmq  
                5 redis  
                6 traefik  
                7 uwsgi  
                8 vsphere  
                9 windows  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            40 zookeeper  
                5
                .4、Nginx Module  
                5
                .4.1、开启nginx的状态查询  
                在nginx中,需要开启状态查询,才能查询到指标数据。  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                #重新编译nginx  
                ./configure --prefix=/usr/local/nginx --with-http_stub_status_module  
                make  
                make install  
                ./nginx -V ꢀ#查询版本信息  
                nginx version: nginx/1.11.6  
                built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)  
                configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module  
                1
                1
                1
                1
                1
                1
                1
                1 #配置nginx  
                2 vim nginx.conf  
                3 location /nginx-status {  
                4
                stub_status on;  
                access_log off;  
                5
                6 }  
                测试:  
                结果说明:  
                Active connections:正在处理的活动连接数  
                server accepts handled requests  
                第一个 server 表示Nginx启动到现在共处理了9个连接  
                第二个 accepts 表示Nginx启动到现在共成功创建 9 次握手  
                第三个 handled requests 表示总共处理了 21 次请求  
                请求丢失数 = 握手数 - 连接数 ,可以看出目前为止没有丢失请求  
                Reading: 0 Writing: 1 Waiting: 1  
                Reading:Nginx 读取到客户端的 Header 信息数  
                Writing:Nginx 返回给客户端 Header 信息数  
                Waiting:Nginx 已经处理完正在等候下一次请求指令的驻留链接(开启keep-alive的情况下,这个值等于  
                Active - (Reading+Writing))  
                5
                .4.2、配置Nginx Module  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                2
                3
                4
                5
                6
                7
                8
                #启用redis module  
                ./metricbeat modules enable nginx  
                #修改redis module配置  
                vim modules.d/nginx.yml  
                # Module: nginx  
                # Docs: https://www.elastic.co/guide/en/beats/metricbeat/6.5/metricbeat-module-  
                nginx.html  
                9
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                0 - module: nginx  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                ꢀ#metricsets:  
                ꢀ# - stubstatus  
                period: 10s  
                ꢀ# Nginx hosts  
                hosts: ["http://192.168.40.133"]  
                ꢀ# Path to server status. Default server-status  
                server_status_path: "nginx-status"  
                ꢀ#username: "user"  
                ꢀ#password: "secret"  
                5 #启动  
                6 ./metricbeat -e  
                测试:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            可以看到,nginx的指标数据已经写入到了Elasticsearch。  
                更多的Module使用参见官方文档:  
                
                6、Kibana  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作。您可以  
                使用 Kibana 对 Elasticsearch 索引中的数据进行搜索、查看、交互操作。您可以很方便的利用图表、表格及地图对  
                数据进行多元化的分析和呈现。  
                
                6
                .1、配置安装  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                #解压安装包  
                tar -xvf kibana-6.5.4-linux-x86_64.tar.gz  
                #修改配置文件  
                vim config/kibana.yml  
                server.host: "192.168.40.133" ꢀ#对外暴露服务的地址  
                elasticsearch.url: "http://192.168.40.133:9200" ꢀ#配置Elasticsearch  
                1
                1
                1
                1
                1
                0 #启动  
                1 ./bin/kibana  
                2
                3 #通过浏览器进行访问  
                4 http://192.168.40.133:5601/app/kibana  
                可以看到kibana页面,并且可以看到提示,导入数据到Kibana。  
                6
                .2、功能说明  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            6
                .3、数据探索  
                首先先添加索引信息:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            即可查看索引数据:  
                6
                .4、Metricbeat 仪表盘  
                可以将Metricbeat的数据在Kibana中展示。  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                2
                3
                4
                5
                6
                7
                #修改metricbeat配置  
                setup.kibana:  
                host: "192.168.40.133:5601"  
                ꢀ
                #安装仪表盘到Kibana  
                ./metricbeat setup --dashboards  
                即可在Kibana中看到仪表盘数据:  
                查看系统信息:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            6
                .5、Nginx 指标仪表盘  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            6
                .6、Nginx 日志仪表盘  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                #修改配置文件 vim itcast-nginx.yml  
                filebeat.inputs:  
                #- type: log  
                # enabled: true  
                # paths:  
                # ꢀ - /usr/local/nginx/logs/*.log  
                # tags: ["nginx"]  
                setup.template.settings:  
                index.number_of_shards: 3  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                0 output.elasticsearch:  
                hosts: ["192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]  
                2 filebeat.config.modules:  
                1
                3
                4
                path: ${path.config}/modules.d/*.yml  
                reload.enabled: false  
                5 setup.kibana:  
                6
                7
                8
                host: "192.168.40.133:5601"  
                ꢀ
                9 #安装仪表盘到kibana  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            20 ./filebeat -c itcast-nginx.yml setup  
                可以看到nginx的FileBeat的仪表盘了:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            6
                .7、自定义图表  
                在Kibana中,也可以进行自定义图表,如制作柱形图:  
                将图表添加到自定义Dashboard中:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            6
                .8、开发者工具  
                在Kibana中,为开发者的测试提供了便捷的工具使用,如下:  
                7、Logstash  
                7
                .1、简介  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            用途:  
                7
                .2、部署安装  
                1
                2
                3
                4
                5
                6
                7
                8
                #检查jdk环境,要求jdk1.8+  
                java -version  
                #解压安装包  
                tar -xvf logstash-6.5.4.tar.gz  
                #第一个logstash示例  
                bin/logstash -e 'input { stdin { } } output { stdout {} }'  
                执行效果如下:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            7
                .3、配置详解  
                Logstash的配置有三部分,如下:  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                input { #输入  
                stdin { ... } #标准输入  
                }
                filter { #过滤,对数据进行分割、截取等处理  
                ꢀ ...  
                }
                output { #输出  
                1
                1
                stdout { ... } #标准输出  
                1 }  
                7
                .3.1、输入  
                采集各种样式、大小和来源的数据,数据往往以各种各样的形式,或分散或集中地存在于很多系统中。  
                Logstash 支持各种输入选择 ,可以在同一时间从众多常用来源捕捉事件。能够以连续的流式传输方式,轻松地  
                从您的日志、指标、Web 应用、数据存储以及各种 AWS 服务采集数据。  
                7
                .3.2、过滤  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            实时解析和转换数据  
                数据从源传输到存储库的过程中,Logstash 过滤器能够解析各个事件,识别已命名的字段以构建结构,并将它  
                们转换成通用格式,以便更轻松、更快速地分析和实现商业价值。  
                7
                .3.3、输出  
                Logstash 提供众多输出选择,您可以将数据发送到您要指定的地方,并且能够灵活地解锁众多下游用例。  
                7
                .4、读取自定义日志  
                前面我们通过Filebeat读取了nginx的日志,如果是自定义结构的日志,就需要读取处理后才能使用,所以,这个时  
                候就需要使用Logstash了,因为Logstash有着强大的处理能力,可以应对各种各样的场景。  
                7
                .4.1、日志结构  
                1
                2019-03-15 21:21:21|ERROR|读取数据出错|参数:id=1002  
                可以看到,日志中的内容是使用“|”进行分割的,使用,我们在处理的时候,也需要对数据做分割处理。  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            7
                .4.2、编写配置文件  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                #vim itcast-pipeline.conf  
                input {  
                ꢀ file {  
                ꢀ ꢀ path => "/itcast/logstash/logs/app.log"  
                ꢀ ꢀ start_position => "beginning"  
                ꢀ }  
                }
                1
                1
                1
                1
                1
                1
                1
                1
                1
                0 filter {  
                1
                ꢀ mutate {  
                2
                ꢀ ꢀ split => {"message"=>"|"}  
                ꢀ }  
                3
                4 }  
                5
                6 output {  
                7
                ꢀ stdout { codec => rubydebug }  
                8 }  
                7
                .4.3、启动测试  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                #启动  
                ./bin/logstash -f ./itcast-pipeline.conf  
                #写日志到文件  
                echo "2019-03-15 21:21:21|ERROR|读取数据出错|参数:id=1002" >> app.log  
                #输出的结果  
                {
                ꢀ ꢀ"@timestamp" => 2019-03-15T08:44:04.749Z,  
                ꢀ ꢀ ꢀ ꢀ ꢀ"path" => "/itcast/logstash/logs/app.log",  
                ꢀ ꢀ ꢀ"@version" => "1",  
                ꢀ ꢀ ꢀ ꢀ ꢀ"host" => "node01",  
                ꢀ ꢀ ꢀ "message" => [  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                ꢀ ꢀ ꢀ [0] "2019-03-15 21:21:21",  
                ꢀ ꢀ ꢀ [1] "ERROR",  
                ꢀ ꢀ ꢀ [2] "读取数据出错",  
                ꢀ ꢀ ꢀ [3] "参数:id=1002"  
                ꢀ ]  
                9 }  
                可以看到,数据已经被分割了。  
                7
                .4.5、输出到Elasticsearch  
                1
                2
                3
                4
                5
                input {  
                ꢀ file {  
                ꢀ ꢀ path => "/itcast/logstash/logs/app.log"  
                ꢀ ꢀ ꢀ#type => "system"  
                ꢀ ꢀ start_position => "beginning"  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            6
                7
                8
                9
                0
                1
                2
                ꢀ }  
                }
                filter {  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                ꢀ mutate {  
                ꢀ ꢀ split => {"message"=>"|"}  
                ꢀ }  
                3 }  
                4
                5 output {  
                6
                ꢀ elasticsearch {  
                7
                ꢀ ꢀ ꢀ hosts => [ "192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]  
                ꢀ }  
                8
                9 }  
                0
                1
                2 #启动  
                3 ./bin/logstash -f ./itcast-pipeline.conf  
                4
                5 #写入数据  
                6 echo "2019-03-15 21:21:21|ERROR|读取数据出错|参数:id=1003" >> app.log  
                测试:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            8、综合练习  
                下面我们将前面所学习到的Elasticsearch + Logstash + Beats + Kibana整合起来做一个综合性的练习,目的就是让  
                学生们能够更加深刻的理解Elastic Stack的使用。  
                8
                .1、流程说明  
                应用APP生产日志,用来记录用户的操作  
                [
                INFO] 2019-03-15 22:55:20 [cn.itcast.dashboard.Main] - DAU|5206|使用优惠券|2019-03-15  
                3:37:20  
                INFO] 2019-03-15 22:55:21 [cn.itcast.dashboard.Main] - DAU|3880|浏览页面|2019-03-15 07:25:09  
                0
                [
                通过Filebeat读取日志文件中的内容,并且将内容发送给Logstash,原因是需要对内容做处理  
                Logstash接收到内容后,进行处理,如分割操作,然后将内容发送到Elasticsearch中  
                Kibana会读取Elasticsearch中的数据,并且在Kibana中进行设计Dashboard,最后进行展示  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            说明:日志格式、图表、Dashboard都是自定义的。  
                8
                .2、APP介绍  
                APP在生产环境应该是真实的系统,然而,我们现在仅仅的学习,为了简化操作,所以就做数据的模拟生成即可。  
                业务代码如下:  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                package cn.itcast.dashboard;  
                import org.apache.commons.lang3.RandomUtils;  
                import org.joda.time.DateTime;  
                import org.slf4j.Logger;  
                import org.slf4j.LoggerFactory;  
                import org.springframework.boot.autoconfigure.SpringBootApplication;  
                @SpringBootApplication  
                1
                1
                1
                1
                1
                0 public class Main {  
                1
                2
                3
                4
                ꢀ ꢀprivate static final Logger LOGGER = LoggerFactory.getLogger(Main.class);  
                ꢀ ꢀpublic static final String[] VISIT = new String[]{"浏览页面", "评论商品", "加入收藏",  
                加入购物车", "提交订单", "使用优惠券", "领取优惠券", "搜索", "查看订单"};  
                "
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                ꢀ ꢀpublic static void main(String[] args) throws Exception {  
                ꢀ ꢀ ꢀ ꢀwhile(true){  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀLong sleep = RandomUtils.nextLong(200, 1000 * 5);  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀThread.sleep(sleep);  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀLong maxUserId = 9999L;  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀLong userId = RandomUtils.nextLong(1, maxUserId);  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀString visit = VISIT[RandomUtils.nextInt(0, VISIT.length)];  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀDateTime now = new DateTime();  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀint maxHour = now.getHourOfDay();  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀint maxMillis = now.getMinuteOfHour();  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀint maxSeconds = now.getSecondOfMinute();  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀString date = now.plusHours(-(RandomUtils.nextInt(0, maxHour)))  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ .plusMinutes(-(RandomUtils.nextInt(0, maxMillis)))  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ .plusSeconds(-(RandomUtils.nextInt(0, maxSeconds)))  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ ꢀ .toString("yyyy-MM-dd HH:mm:ss");  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀString result = "DAU|" + userId + "|" + visit + "|" + date;  
                ꢀ ꢀ ꢀ ꢀ ꢀ ꢀLOGGER.info(result);  
                ꢀ ꢀ ꢀ }  
                ꢀ }  
                7 }  
                8
                运行结果:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            1
                2
                3
                4
                5
                6
                7
                8
                9
                [INFO] 2019-03-15 22:54:42 [cn.itcast.dashboard.Main] - DAU|4645|领取优惠券|2019-03-15  
                7:40:29  
                [INFO] 2019-03-15 22:54:44 [cn.itcast.dashboard.Main] - DAU|3482|领取优惠券|2019-03-15  
                8:34:04  
                [INFO] 2019-03-15 22:54:48 [cn.itcast.dashboard.Main] - DAU|5607|加入收藏|2019-03-15  
                2:44:09  
                [INFO] 2019-03-15 22:54:50 [cn.itcast.dashboard.Main] - DAU|9619|加入收藏|2019-03-15  
                1:39:47  
                [INFO] 2019-03-15 22:54:53 [cn.itcast.dashboard.Main] - DAU|7666|加入收藏|2019-03-15  
                7:47:18  
                [INFO] 2019-03-15 22:54:54 [cn.itcast.dashboard.Main] - DAU|4871|提交订单|2019-03-15  
                2:36:27  
                [INFO] 2019-03-15 22:54:55 [cn.itcast.dashboard.Main] - DAU|7126|加入收藏|2019-03-15  
                6:11:06  
                [INFO] 2019-03-15 22:55:00 [cn.itcast.dashboard.Main] - DAU|9606|评论商品|2019-03-15  
                2:12:00  
                [INFO] 2019-03-15 22:55:02 [cn.itcast.dashboard.Main] - DAU|7698|查看订单|2019-03-15  
                8:17:02  
                0
                1
                2
                2
                1
                0
                1
                0
                0
                代码在资料中可以找到,itcast-dashboard-generate.zip。  
                部署:  
                1
                2
                3
                #打包成jar包,在linux上运行  
                java -jar itcast-dashboard-generate-1.0-SNAPSHOT.jar  
                #运行之后,就可以将日志写入到/itcast/logs/app.log文件中  
                8
                .3、Filebeat  
                1
                2
                3
                4
                5
                6
                7
                8
                9
                #vim itcast-dashboard.yml  
                filebeat.inputs:  
                - type: log  
                enabled: true  
                paths:  
                ꢀ ꢀ- /itcast/logs/*.log  
                setup.template.settings:  
                index.number_of_shards: 3  
                1
                1
                1
                1
                1
                1
                0 output.logstash:  
                1
                2
                3
                hosts: ["192.168.40.133:5044"]  
                ꢀ
                ꢀ
                4 #启动  
                5 ./filebeat -e -c itcast-dashboard.yml  
                8
                .4、Logstash  
                1
                2
                3
                #vim itcast-dashboard.conf  
                input {  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
                0
                1
                2
                3
                4
                5
                6
                7
                8
                ꢀ beats {  
                ꢀ ꢀ port => "5044"  
                ꢀ }  
                }
                filter {  
                1
                1
                1
                1
                1
                1
                1
                1
                1
                1
                2
                2
                2
                2
                2
                2
                2
                2
                2
                2
                3
                3
                3
                3
                3
                3
                3
                3
                3
                3
                4
                4
                4
                ꢀ mutate { ꢀ  
                ꢀ ꢀ split => {"message"=>"|"}  
                ꢀ }  
                ꢀ mutate {  
                ꢀ ꢀ add_field => {  
                ꢀ ꢀ ꢀ ꢀ"userId" => "%{message[1]}"  
                ꢀ ꢀ ꢀ ꢀ"visit" => "%{message[2]}"  
                ꢀ ꢀ ꢀ ꢀ"date" => "%{message[3]}"  
                ꢀ ꢀ ꢀ }  
                ꢀ }  
                ꢀ mutate {  
                ꢀ ꢀ convert => {  
                ꢀ ꢀ ꢀ ꢀ"userId" => "integer"  
                ꢀ ꢀ ꢀ ꢀ"visit" => "string"  
                ꢀ ꢀ ꢀ ꢀ"date" => "string"  
                ꢀ ꢀ }  
                ꢀ }  
                9 }  
                0
                1 #output {  
                2 # ꢀ stdout { codec => rubydebug }  
                3 #}  
                4
                5 output {  
                6
                ꢀ elasticsearch {  
                7
                ꢀ ꢀ ꢀ hosts => [ "192.168.40.133:9200","192.168.40.134:9200","192.168.40.135:9200"]  
                ꢀ }  
                8
                9 }  
                0
                1 #启动  
                2 ./bin/logstash -f itcast-dashboard.conf  
                8
                .5、Kibana  
                启动Kibana:  
                1
                2
                3
                4
                5
                #启动  
                ./bin/kibana  
                #通过浏览器进行访问  
                http://192.168.40.133:5601/app/kibana  
                添加Logstash索引到Kibana中:  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            8
                .5.1、时间间隔的柱形图  
                说明:x轴是时间,以天为单位,y轴是count数  
                保存:(my-dashboard-时间间隔的柱形图)  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            8
                .5.2、各个操作的饼图分布  
                统计各个操作的数量,形成饼图。  
                保存:(my-dashboard-各个操作的饼图)  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            8
                .5.3、数据表格  
                在数据探索中进行保存,并且保存,将各个操作的数据以表格的形式展现出来。  
                保存:(my-dashboard-表格)  
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090  
            8
                .5.4、制作Dashboard  
                ꢀ
                北京市昌平区建材城西路金燕龙办公楼一层  
                电话:400-618-9090