【Elastic Engineering】Elastic:使用 Elastic Stack 来监督 Apache 日志及指标

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 在我之前的许多文章中,我基本上都已经讲到了这些方面的内容。在今天的文章中,我想针对一些开发还没有自己的系统,比如 centos 或 Ubuntu OS 来写一篇非常详细的文章


在我之前的许多文章中,我基本上都已经讲到了这些方面的内容。在今天的文章中,我想针对一些开发还没有自己的系统,比如 centos 或 Ubuntu OS 来写一篇非常详细的文章。在这篇文章中,我将详述:


  • 使用 Docker 安装 Elastic Stack:Elasticsearch 及 Kibana
  • 使用 Vagrant 来创建一个 centos 7 的系统,并使用 Filebeat 及 Metricbeat 把 Apache 日志和指标发送至 Elasticsearch
  • 使用 Vagrant 来创建一个 Ubuntu OS 的系统,并使用 Filebeat 及 Metricbeat 把 Apache 日志和指标发送至 Elasticsearch


如果你对 Filebeat 及 Metricbeat 的使用还是不很熟悉的话,你可以阅读我之前的文章:



在今天的文章中,我们的系统架构如下:



如上所示,我们将在左边使用 Docker 来安装 Elasticsearch 及 Kibana。在右边的两个虚拟机中,我们将分别安装 centos 7 及 Ubuntu  20.04。我们分别在这两个系统中安装相应的 Filebeat 及 Metricbeat 来收集 Apache 日志及指标,并发送至  Elasticsearch 进行存储及分析。我们可以使用 Kibana 来对手机的数据进行可视化及分析。


为了细述的方便,我主机使用的是 macOS。它的私有地址如下:


$ ifconfig | grep 192
  inet 192.168.0.3 netmask 0xffffff00 broadcast 192.168.0.255


如上所示,我的 host 机器的私有地址是 192.168.0.3。这个依据你自己的系统的不同而不同。


安装


Docker containers


你可以参考我之前的文章 “Elasticsearch:如何在 Docker 容器中安装 Elastic Stack”。里面有详细的描述。具体来说,我们创建一个目录,并在该目录下创建一个如下的 docker-compose.yml 文件:


docker-compose.yml


version: '2.2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.15.2
    container_name: elasticsearch
    environment:
      - node.name=elasticsearch
      - discovery.seed_hosts=elasticsearch
      - cluster.initial_master_nodes=elasticsearch
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
  kibana:
    image: docker.elastic.co/kibana/kibana:7.15.2
    container_name: kibana
    environment:
      ELASTICSEARCH_URL: "http://elasticsearch:9200"
    ports:
      - 5601:5601
    depends_on:
      - elasticsearch
volumes:
  esdata1:
    driver: local


接下来,我们使用如下的命令来启动 Elasticsearch 及 Kibana。


docker-compose up -d


在上面,我们使用 detach 模式运行,也就是它运行于后台。


$ pwd
/Users/liuxg/data/elk/stack
$ ls
docker-compose.yml
$ docker-compose up -d
Creating network "stack_default" with the default driver
Creating volume "stack_esdata1" with local driver
Creating elasticsearch ... done
Creating kibana        ... done


我们可以通过如下的命令来查看 Elasticsearch 及 Kibana 的日志信息:


docker logs elasticsearch
docker logs kibana



从上面的输出中的输出中,我们可以看出来 Elasticsearch 被绑定于 0.0.0.0:9200,也就是说它可以同时被  localhost:9200 及 privateIP:9200 所访问。我们可以使用如下的命令来查看 Elasticsearch:


$ curl http://localhost:9200
{
  "name" : "elasticsearch",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "8QFFNfXFQcuMRl93Q-pt9A",
  "version" : {
    "number" : "7.15.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "93d5a7f6192e8a1a12e154a2b81bf6fa7309da0c",
    "build_date" : "2021-11-04T14:04:42.515624022Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}


$ curl 192.168.0.3:9200
{
  "name" : "elasticsearch",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "8QFFNfXFQcuMRl93Q-pt9A",
  "version" : {
    "number" : "7.15.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "93d5a7f6192e8a1a12e154a2b81bf6fa7309da0c",
    "build_date" : "2021-11-04T14:04:42.515624022Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}


从上面的输出中,我们可以看出来它可以同时被两个地址所访问。我们也可以使用浏览器来进行查看:



同样地,我们可以通过 Kibana 的日志输出:



我们可以查看到 Kibana 也被绑定于当前 host 的所有的 IP 网路接口上。我们可以通过地址 localhost:5601 来进行访问:



我们可以使用如下的命令来查看 docker 的运行:


$ docker ps
CONTAINER ID   IMAGE                                                  COMMAND                  CREATED          STATUS          PORTS                              NAMES
1b16df46b8a0   docker.elastic.co/kibana/kibana:7.15.2                 "/bin/tini -- /usr/l…"   24 minutes ago   Up 24 minutes   0.0.0.0:5601->5601/tcp             kibana
ead212dcc7ad   docker.elastic.co/elasticsearch/elasticsearch:7.15.2   "/bin/tini -- /usr/l…"   24 minutes ago   Up 24 minutes   0.0.0.0:9200->9200/tcp, 9300/tcp   elasticsearch


到目前为止,我们的 Docker containers 部分安装完毕。


CentOS 7


对于一些还没有自己的 centos 的开发者来说,我们可以使用 Vagrant 来创建一个自己的 centos 系统。关于 Vagrant 的一些用法,请参考我之前的文章 “Vagrant 入门教程” 。具体来说,我们在自己的电脑上创建一个目录,并在该目录下创建一个如下的

Vagrantfile 文件:


Vagrantfile


# -*- mode: ruby -*-
# vi: set ft=ruby :
ENV['VAGRANT_NO_PARALLEL'] = 'yes'
Vagrant.configure(2) do |config|
  NodeCount = 1
  (1..NodeCount).each do |i|
    config.vm.define "centosvm0#{i}" do |node|
      node.vm.box = "centos/7"
      node.vm.hostname = "centosvm0#{i}.example.com"
      node.vm.network "private_network", ip: "172.42.42.10#{i}"
      node.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
        v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]         
        v.name = "centosvm0#{i}"
        v.memory = 2048
        v.cpus = 1
      end
    end
  end
end


如上所示,我们设置 NodeCount 为 1,它表明我们只创建一个 centos 的实例。这个虚拟机的 IP 地址为 172.42.42.101。


我们在 Vagrantfile 所在的目录里打入如下的命令:


vagrant up



等虚拟机起来过后,我们可以使用如下的命令来进入大虚拟机中:


vagrant ssh


$ vagrant ssh
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[vagrant@localhost ~]$ yum update -y


在 centos 中,我们可以通过打入上面的 update 命令来更新系统,直至完成:



我们可以通过如下的命令来查看操作系统的版本:


[vagrant@localhost ~]$ cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)


或者我们需要安装如下的包,然后再执行命令来查看 OS 的版本:


[vagrant@localhost ~]$ sudo yum install redhat-lsb-core -y
[vagrant@localhost ~]$ lsb_release -dirc
Distributor ID: CentOS
Description:  CentOS Linux release 7.9.2009 (Core)
Release:  7.9.2009
Codename: Core


我们通过如下的命令来确保 firewall 是处于禁止的状态:


[vagrant@localhost ~]$ sudo service firewalld status
Redirecting to /bin/systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)


Apache


我们使用如下的命令来安装 Apache 服务器:


sudo yum install httpd


我们使用如下的命令来检查当前 centos 的 IP 地址:


[vagrant@centosvm01 ~]$ ip a 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:4d:77:d3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 84547sec preferred_lft 84547sec
    inet6 fe80::5054:ff:fe4d:77d3/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:a5:25:0a brd ff:ff:ff:ff:ff:ff
    inet 172.42.42.101/24 brd 172.42.42.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fea5:250a/64 scope link 
       valid_lft forever preferred_lft forever


上面显示我们的 IP 地址是 172.42.42.101。我们首先来创建一个简单的网页:


[vagrant@centosvm01 ~]$ sudo su -
Last failed login: Wed Dec  8 01:37:45 UTC 2021 on pts/0
There were 2 failed login attempts since the last successful login.
[root@centosvm01 ~]# echo "<h1>This is liuxg's homepage</h1>" > /var/www/html/index.html 
[root@centosvm01 ~]# cat /var/www/html/index.html
<h1>This is liuxg's homepage</h1>


我们通过如下的命令来启动 httpd 并运行它:


[root@centosvm01 ~]# systemctl start httpd
[root@centosvm01 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-12-08 01:44:42 UTC; 11s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 27497 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─27497 /usr/sbin/httpd -DFOREGROUND
           ├─27498 /usr/sbin/httpd -DFOREGROUND
           ├─27499 /usr/sbin/httpd -DFOREGROUND
           ├─27500 /usr/sbin/httpd -DFOREGROUND
           ├─27501 /usr/sbin/httpd -DFOREGROUND
           └─27502 /usr/sbin/httpd -DFOREGROUND
Dec 08 01:44:42 centosvm01.example.com systemd[1]: Starting The Apache HTTP S...
Dec 08 01:44:42 centosvm01.example.com systemd[1]: Started The Apache HTTP Se...
Hint: Some lines were ellipsized, use -l to show in full.


上面显示我们的网络服务器已经运行起来了。我们可以在 host 机器的浏览器中进行查看:



显示上面是我们已经创建的简单网页。我们可以在如下的地址查看 apache 的日志信息:


[root@centosvm01 ~]# ls /var/log/httpd/
access_log  error_log
[root@centosvm01 ~]# cat /var/log/httpd/access_log 
172.42.42.1 - - [08/Dec/2021:01:45:46 +0000] "GET / HTTP/1.1" 200 34 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36"
172.42.42.1 - - [08/Dec/2021:01:45:47 +0000] "GET /favicon.ico HTTP/1.1" 404 209 "http://172.42.42.101/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36"
172.42.42.1 - - [08/Dec/2021:01:46:38 +0000] "-" 408 - "-" "-"
[root@centosvm01 ~]# cat /var/log/httpd/error_log 
[Wed Dec 08 01:44:42.286667 2021] [core:notice] [pid 27497] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Wed Dec 08 01:44:42.287365 2021] [suexec:notice] [pid 27497] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Dec 08 01:44:42.295296 2021] [lbmethod_heartbeat:notice] [pid 27497] AH02282: No slotmem from mod_heartmonitor
[Wed Dec 08 01:44:42.302397 2021] [mpm_prefork:notice] [pid 27497] AH00163: Apache/2.4.6 (CentOS) configured -- resuming normal operations
[Wed Dec 08 01:44:42.302419 2021] [core:notice] [pid 27497] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'


在上面的两个文件 access_log 及 error_log 里,我们可以看到 httpd 运行的日志信息。我们记下如下的两个日志文件以便如下的 apache 模块使用:


  • /var/log/httpd/access_log
  • /var/log/httpd/error_log


我们接下来安装 Filebeat 及 Metricbeat。对于不同的平台有不同的安装方法。那么它们的安装指令是咋样的呢?


Filebeat


我们首先打开 Kibana:




由于一些原因,在最新的 7.15.2 的发布中,我在上面的界面中并没有看到 System logs 的选项。我不能确定这是否一个  bug。也许推荐的办法是使用 Elastic Agent 来对 System logs 进行采集。尽管如此,我们可以选择任何 Apache  logs 来查看安装说明,这是因为对于它们来说安装 Filebeat 的指令是一样的,只是启动的模块不同而已。我们点击上面的 Apache  logs:



我们选择 RPM 选项,这是因为在 centos 上的安装包是 RPM 格式的。我们按照上面的指令来对 centos 进行安装:


curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.15.2-x86_64.rpm
sudo rpm -vi filebeat-7.15.2-x86_64.rpm


我们发现通过上面的方法,我们很容易下载到和 Elasticsearch 匹配的版本,比如上面指出的 7.15.2 版本。我们可以通过如下的命令来查看 filebeat 的版本:


[vagrant@centosvm01 ~]$ sudo filebeat version
filebeat version 7.15.2 (amd64), libbeat 7.15.2 [fd322dad6ceafec40c84df4d2a0694ea357d16cc built 2021-11-04 14:22:49 +0000 UTC]



上面显示我们的安装是成功的。我们可以使用如下的命令来查看安装文件的位置:


[vagrant@localhost ~]$ rpm -qc filebeat
/etc/filebeat/filebeat.yml
/etc/filebeat/modules.d/activemq.yml.disabled
/etc/filebeat/modules.d/apache.yml.disabled
/etc/filebeat/modules.d/auditd.yml.disabled
/etc/filebeat/modules.d/aws.yml.disabled
/etc/filebeat/modules.d/awsfargate.yml.disabled
/etc/filebeat/modules.d/azure.yml.disabled
/etc/filebeat/modules.d/barracuda.yml.disabled
/etc/filebeat/modules.d/bluecoat.yml.disabled
/etc/filebeat/modules.d/cef.yml.disabled
/etc/filebeat/modules.d/checkpoint.yml.disabled
/etc/filebeat/modules.d/cisco.yml.disabled
/etc/filebeat/modules.d/coredns.yml.disabled
/etc/filebeat/modules.d/crowdstrike.yml.disabled
/etc/filebeat/modules.d/cyberark.yml.disabled
/etc/filebeat/modules.d/cyberarkpas.yml.disabled
/etc/filebeat/modules.d/cylance.yml.disabled
/etc/filebeat/modules.d/elasticsearch.yml.disabled
/etc/filebeat/modules.d/envoyproxy.yml.disabled
/etc/filebeat/modules.d/f5.yml.disabled
/etc/filebeat/modules.d/fortinet.yml.disabled
/etc/filebeat/modules.d/gcp.yml.disabled
/etc/filebeat/modules.d/google_workspace.yml.disabled
/etc/filebeat/modules.d/googlecloud.yml.disabled
/etc/filebeat/modules.d/gsuite.yml.disabled
/etc/filebeat/modules.d/haproxy.yml.disabled
/etc/filebeat/modules.d/ibmmq.yml.disabled
/etc/filebeat/modules.d/icinga.yml.disabled
/etc/filebeat/modules.d/iis.yml.disabled
/etc/filebeat/modules.d/imperva.yml.disabled
/etc/filebeat/modules.d/infoblox.yml.disabled
/etc/filebeat/modules.d/iptables.yml.disabled
/etc/filebeat/modules.d/juniper.yml.disabled
/etc/filebeat/modules.d/kafka.yml.disabled
/etc/filebeat/modules.d/kibana.yml.disabled
/etc/filebeat/modules.d/logstash.yml.disabled
/etc/filebeat/modules.d/microsoft.yml.disabled
/etc/filebeat/modules.d/misp.yml.disabled
/etc/filebeat/modules.d/mongodb.yml.disabled
/etc/filebeat/modules.d/mssql.yml.disabled
/etc/filebeat/modules.d/mysql.yml.disabled
/etc/filebeat/modules.d/mysqlenterprise.yml.disabled
/etc/filebeat/modules.d/nats.yml.disabled
/etc/filebeat/modules.d/netflow.yml.disabled
/etc/filebeat/modules.d/netscout.yml.disabled
/etc/filebeat/modules.d/nginx.yml.disabled
/etc/filebeat/modules.d/o365.yml.disabled
/etc/filebeat/modules.d/okta.yml.disabled
/etc/filebeat/modules.d/oracle.yml.disabled
/etc/filebeat/modules.d/osquery.yml.disabled
/etc/filebeat/modules.d/panw.yml.disabled
/etc/filebeat/modules.d/pensando.yml.disabled
/etc/filebeat/modules.d/postgresql.yml.disabled
/etc/filebeat/modules.d/proofpoint.yml.disabled
/etc/filebeat/modules.d/rabbitmq.yml.disabled
/etc/filebeat/modules.d/radware.yml.disabled
/etc/filebeat/modules.d/redis.yml.disabled
/etc/filebeat/modules.d/santa.yml.disabled
/etc/filebeat/modules.d/snort.yml.disabled
/etc/filebeat/modules.d/snyk.yml.disabled
/etc/filebeat/modules.d/sonicwall.yml.disabled
/etc/filebeat/modules.d/sophos.yml.disabled
/etc/filebeat/modules.d/squid.yml.disabled
/etc/filebeat/modules.d/suricata.yml.disabled
/etc/filebeat/modules.d/system.yml.disabled
/etc/filebeat/modules.d/threatintel.yml.disabled
/etc/filebeat/modules.d/tomcat.yml.disabled
/etc/filebeat/modules.d/traefik.yml.disabled
/etc/filebeat/modules.d/zeek.yml.disabled
/etc/filebeat/modules.d/zookeeper.yml.disabled
/etc/filebeat/modules.d/zoom.yml.disabled
/etc/filebeat/modules.d/zscaler.yml.disabled


在上面,我们需要注意的一个文件就是 /etc/filebeat/filebeat.yml。这个文件就是我们 Filebeat 的配置文件。首先,我们查看一下已经启动的模块:


sudo filebeat modules list



从上面的输出中,我们可以看出来没有任何的模块被启动。我们可以使用如下的命令来启动 apache 模块:


sudo filebeat modules enable apache


[vagrant@centosvm01 ~]$ sudo filebeat modules enable apache
Enabled apache


我们可以看到 apache 模块已经被成功地启动了。我们可以使用如下的命令来进行查看:



我们也可以使用如下的命令来禁止一个已经启动的模块,比如:


sudo filebeat modules disable nginx

在 nginx 模块已经启动的情况下,上面的命令将禁止 nginx 模块。


我们看到的另外一个变化是进入到如下的目录:


[vagrant@localhost ~]$ cd /etc/filebeat/modules.d
[vagrant@localhost modules.d]$ ls


我们发现只有 apache.yml 文件是没有 disabled 的:


ls /etc/filebeat/modules.d



其它所有的模块都是被禁止的。事实上,apache.yml 文件就是针对 Apache log 进行的配置。通常它会自动根据系统的不同获取当前系统的日志的位置及文件:


vi /etc/filebeat/apache.yml


sudo vi /etc/filebeat/modules.d/apache.yml


这个就是 apache 模块的配置文件。通常我们不需要配置任何东西,因为它在默认的情况下会自动配置 Apache 的日志所在的路径。为了说明问题,我们可以把我们在上面得到的日志文件路径写入到这个配置文件中并保存:



接下来,我们需要来配置 filebeat.yml 文件。我们使用编辑器来对它进行编辑:


sudo vi /etc/filebeat/filebeat.yml




我们按照上面的部分来进行修改,并保存文件。修改完毕后,我们来检查一下我们的配置是否成功:


sudo filebeat test config


[vagrant@localhost modules.d]$ sudo filebeat test config
Config OK


上面表明我们的配置是没有问题的。如果有语法错误,上面肯定是不成功的。


我们使用如下的命令来测试一下我们的 Filebeat 和 Elasticsearch 是否成功:


sudo filebeat test output


[vagrant@localhost modules.d]$ sudo filebeat test output
elasticsearch: http://192.168.0.3:9200...
  parse url... OK
  connection...
    parse host... OK
    dns lookup... OK
    addresses: 192.168.0.3
    dial up... OK
  TLS... WARN secure connection disabled
  talk to server... OK
  version: 7.15.2


上面显示我们的连接是成功的。


接下来,我们可以使用 setup 命令来完成配置。关于这个命令的说明,请参阅我之前的文章 “Beats:解密 Filebeat 中的 setup 命令”。这里就不赘述了。我们接着执行如下的命令:


sudo filebeat setup


对所有的 Filebeat 模块来说,我们只需要执行一次即可。它可以帮我们生成相应的 pipeline,dashboard 及 index patterns。


[vagrant@localhost ~]$ sudo filebeat setup
Overwriting ILM policy is disabled. Set `setup.ilm.overwrite: true` for enabling.
Index setup finished.
Loading dashboards (Kibana must be running and reachable)
Loaded dashboards
Setting up ML using setup --machine-learning is going to be removed in 8.0.0. Please use the ML app instead.
See more: https://www.elastic.co/guide/en/machine-learning/current/index.html
Loaded machine learning job configurations
Loaded Ingest pipelines


执行完这个指令后,我们可以在 Kibana 的 Dashboard 里看到已经生成的 Dashboard:



我们虽然已经配置好了 Filebeat,但是我们还没有运行它。我们可以使用如下的命令来检查 Filebeat 的运行状态:


[vagrant@localhost ~]$ service filebeat status
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://www.elastic.co/beats/filebeat


显然 Filebeat 是处于非运行状态。我们可以使用如下的命令来启动 Filebeat 服务:


sudo service filebeat start


[vagrant@localhost ~]$ sudo service filebeat start
Starting filebeat (via systemctl):                         [  OK  ]


我们再次检查 Filebeat 的运行状态:


[vagrant@localhost ~]$ service filebeat status
● filebeat.service - Filebeat sends log files to Logstash or directly to Elasticsearch.
   Loaded: loaded (/usr/lib/systemd/system/filebeat.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-12-07 03:25:55 UTC; 44s ago
     Docs: https://www.elastic.co/beats/filebeat
 Main PID: 32279 (filebeat)
   CGroup: /system.slice/filebeat.service
           └─32279 /usr/share/filebeat/bin/filebeat --environment systemd -c ...


上面的 active 状态表示 Filebeat 已经成功运行。我们可以通过如下的命令来查看 Filebeat 的运行日志:


sudo journalctl -u filebeat


我们在 Kibana 的 Discover 中来查看收集上来的 Filebeat 日志:




在上面请注意,我们必须选择好索引模式 filebeat-* 以及合适的时间范围,否则我们有可能看不到任何的数据。从上面我们可以看出来我们已经收集到 Apache 的相关日志。

 

我们打开 Dashboard:





我们可以看到相关的数据分析可视化图。细心的开发者可能发现在地图上显示的是美国的一个地址。这是因为我们在 centos 中设置的 IP 是一个美国的地址。我们可以在浏览器中反复刷新页面 http://172.42.42.101/,这样可以生产更多的日志。我们可以在上面的 Dashboard 里查看更多的日志信息。 我们甚至可以使用不同的浏览器来进行访问,以生成各种不同的日志:





Metricbeat


我们首先来安装 Metricbeat。它的安装步骤和 Filebeat 基本相似。打开 Kibana:





如上所示,我们选择 RPM,并按照上面的指令来安装 Metricbeat:


如上所示,我们选择 RPM,并按照上面的指令来安装 Metricbeat:


[vagrant@localhost ~]$ curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.15.2-x86_64.rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 40.8M    0 76405    0     0   176k      0  0:03:57 --:--:--  0:03:57  176k
100 40.8M  100 40.8M    0     0  32.3M      0  0:00:01  0:00:01 --:--:-- 32.3M
[vagrant@localhost ~]$ sudo rpm -vi metricbeat-7.15.2-x86_64.rpm
warning: metricbeat-7.15.2-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing packages...
metricbeat-7.15.2-1.x86_64


上面显示我们的安装是成功的。我们可以使用如下的命令来查看安装文件的位置:


我们使用如下的命令来查看按照包安装的文件的位置:


rpm -qc metricbeat


[vagrant@localhost ~]$ rpm -qc metricbeat
/etc/metricbeat/metricbeat.yml
/etc/metricbeat/modules.d/activemq.yml.disabled
/etc/metricbeat/modules.d/aerospike.yml.disabled
/etc/metricbeat/modules.d/airflow.yml.disabled
/etc/metricbeat/modules.d/apache.yml.disabled
/etc/metricbeat/modules.d/appsearch.yml.disabled
/etc/metricbeat/modules.d/aws.yml.disabled
/etc/metricbeat/modules.d/awsfargate.yml.disabled
/etc/metricbeat/modules.d/azure.yml.disabled
/etc/metricbeat/modules.d/beat-xpack.yml.disabled
/etc/metricbeat/modules.d/beat.yml.disabled
/etc/metricbeat/modules.d/ceph-mgr.yml.disabled
/etc/metricbeat/modules.d/ceph.yml.disabled
/etc/metricbeat/modules.d/cloudfoundry.yml.disabled
/etc/metricbeat/modules.d/cockroachdb.yml.disabled
/etc/metricbeat/modules.d/consul.yml.disabled
/etc/metricbeat/modules.d/coredns.yml.disabled
/etc/metricbeat/modules.d/couchbase.yml.disabled
/etc/metricbeat/modules.d/couchdb.yml.disabled
/etc/metricbeat/modules.d/docker.yml.disabled
/etc/metricbeat/modules.d/dropwizard.yml.disabled
/etc/metricbeat/modules.d/elasticsearch-xpack.yml.disabled
/etc/metricbeat/modules.d/elasticsearch.yml.disabled
/etc/metricbeat/modules.d/envoyproxy.yml.disabled
/etc/metricbeat/modules.d/etcd.yml.disabled
/etc/metricbeat/modules.d/gcp.yml.disabled
/etc/metricbeat/modules.d/golang.yml.disabled
/etc/metricbeat/modules.d/graphite.yml.disabled
/etc/metricbeat/modules.d/haproxy.yml.disabled
/etc/metricbeat/modules.d/http.yml.disabled
/etc/metricbeat/modules.d/ibmmq.yml.disabled
/etc/metricbeat/modules.d/iis.yml.disabled
/etc/metricbeat/modules.d/istio.yml.disabled
/etc/metricbeat/modules.d/jolokia.yml.disabled
/etc/metricbeat/modules.d/kafka.yml.disabled
/etc/metricbeat/modules.d/kibana-xpack.yml.disabled
/etc/metricbeat/modules.d/kibana.yml.disabled
/etc/metricbeat/modules.d/kubernetes.yml.disabled
/etc/metricbeat/modules.d/kvm.yml.disabled
/etc/metricbeat/modules.d/linux.yml.disabled
/etc/metricbeat/modules.d/logstash-xpack.yml.disabled
/etc/metricbeat/modules.d/logstash.yml.disabled
/etc/metricbeat/modules.d/memcached.yml.disabled
/etc/metricbeat/modules.d/mongodb.yml.disabled
/etc/metricbeat/modules.d/mssql.yml.disabled
/etc/metricbeat/modules.d/munin.yml.disabled
/etc/metricbeat/modules.d/mysql.yml.disabled
/etc/metricbeat/modules.d/nats.yml.disabled
/etc/metricbeat/modules.d/nginx.yml.disabled
/etc/metricbeat/modules.d/openmetrics.yml.disabled
/etc/metricbeat/modules.d/oracle.yml.disabled
/etc/metricbeat/modules.d/php_fpm.yml.disabled
/etc/metricbeat/modules.d/postgresql.yml.disabled
/etc/metricbeat/modules.d/prometheus.yml.disabled
/etc/metricbeat/modules.d/rabbitmq.yml.disabled
/etc/metricbeat/modules.d/redis.yml.disabled
/etc/metricbeat/modules.d/redisenterprise.yml.disabled
/etc/metricbeat/modules.d/sql.yml.disabled
/etc/metricbeat/modules.d/stan.yml.disabled
/etc/metricbeat/modules.d/statsd.yml.disabled
/etc/metricbeat/modules.d/syncgateway.yml.disabled
/etc/metricbeat/modules.d/system.yml
/etc/metricbeat/modules.d/tomcat.yml.disabled
/etc/metricbeat/modules.d/traefik.yml.disabled
/etc/metricbeat/modules.d/uwsgi.yml.disabled
/etc/metricbeat/modules.d/vsphere.yml.disabled
/etc/metricbeat/modules.d/windows.yml.disabled
/etc/metricbeat/modules.d/zookeeper.yml.disabled


如上所示,/etc/metricbeat/metricbeat.yml 是 Metricbeat 的配置文件。我们还可以注意到  system.yml 文件是唯一一个后缀不是 disabled 的文件。它表明 system  模块是已经启动的。我们可以可以通过如下的命令来进行查看:


sudo metricbeat modules list



在上面我们可以看到 system 模块在默认的情况下已经被启动。为了说明问题的方便,我们首先来禁止该模块:


sudo metricbeat modules disable system


[vagrant@centosvm01 ~]$ sudo metricbeat modules disable system
Disabled system


我们可以使用如下的命令来启动 apache 模块:


sudo metricbeat modules enable apache


[vagrant@centosvm01 ~]$ sudo metricbeat modules enable apache
Enabled apache


我们可以在如下的地址查看到 apache 模块的配置信息:


sudo vi /etc/metricbeat/modules.d/apache.yml



我们保存好修改后的 metricbeat.yml 文件。


由于我们已经修改了 mericbeat.yml 文件,有可能我们也修改了自己的模块配置文件。我们使用如下的命令来检查我们所修改的是否正确:


sudo metricbeat test config


[vagrant@localhost ~]$ sudo metricbeat test config
Config OK


我们来测试输出:


sudo metricbeat test output


[vagrant@localhost ~]$ sudo metricbeat test output
elasticsearch: http://192.168.0.3:9200...
  parse url... OK
  connection...
    parse host... OK
    dns lookup... OK
    addresses: 192.168.0.3
    dial up... OK
  TLS... WARN secure connection disabled
  talk to server... OK
  version: 7.15.2


它表明,我们可以正确地连接到 Elasticsearch。我们接着测试模块:


sudo metricbeat test modules system | grep OK


[vagrant@localhost ~]$ sudo metricbeat test modules system | grep OK
  cpu...OK
  load...OK
  memory...OK
  network...OK
  process...OK
  process_summary...OK
  socket_summary...OK
  filesystem...OK
  fsstat...OK
  uptime...OK


它表明我们的模块配置是没有问题的。


接下来,我们设置 Metricbeat:


sudo metricbeat setup


如果我们在 metricbeat.yml 里的配置不是很成功的话,上面的命令将不能正确运行。上述命令将生成相应的 pipeline,dashboard 及 index pattern。


[vagrant@localhost ~]$ sudo metricbeat setup
Overwriting ILM policy is disabled. Set `setup.ilm.overwrite: true` for enabling.
Index setup finished.
Loading dashboards (Kibana must be running and reachable)
Loaded dashboards


上面的输出表明我们的配置是成功的。


我们可以查看 metricbeat 的运行状态:


service metricbeat status


[vagrant@localhost ~]$ service metricbeat status
● metricbeat.service - Metricbeat is a lightweight shipper for metrics.
   Loaded: loaded (/usr/lib/systemd/system/metricbeat.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://www.elastic.co/beats/metricbeat


上面表明,我们的 Metricbeat 没有运行起来。我们使用如下的命令来运行:


sudo service metricbeat start


[vagrant@localhost ~]$ sudo service metricbeat start
Starting metricbeat (via systemctl):                       [  OK  ]


我们再次来查看一下 metricbeat 服务的运行状况:


[vagrant@localhost ~]$ service metricbeat status
● metricbeat.service - Metricbeat is a lightweight shipper for metrics.
   Loaded: loaded (/usr/lib/systemd/system/metricbeat.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-12-07 04:19:56 UTC; 4min 56s ago
     Docs: https://www.elastic.co/beats/metricbeat
 Main PID: 390 (metricbeat)
   CGroup: /system.slice/metricbeat.service
           └─390 /usr/share/metricbeat/bin/metricbeat --environment systemd -..


从上面我们可以看出来,metricbeat 服务已经在运行。


我们可以通过如下的命令来查看 metricbeat 服务的日志信息:


sudo journalctl -u metricbeat


到目前为止,我们还没有启动 Apache 的 status 页面:



我们需要针对 Apache 做一些配置:


[vagrant@centosvm01 ~]$ sudo su -
Last login: Wed Dec  8 01:41:49 UTC 2021 on pts/0
[root@centosvm01 ~]# vi /etc/httpd/conf.modules.d/00-base.conf



在默认的情况下,它是启动的。我们接下来修改 httpd 的配置文件:


[root@centosvm01 ~]# vi /etc/httpd/conf/httpd.conf



在配置文件的最后添加上面的三行,并保存好 httpd.conf 文件。我们需要重新启动 httpd 服务以让它起作用:


[root@centosvm01 ~]# service httpd restart


我们在浏览器中重新访问 server status 页面:



这次我们可以看到 server status 页面显示正常。


我们现在回到  Kibana 中通过 Discover 来查看 Metricbeat 所收集到的数据:




从上面,我们可以看出来许多已经收集上来的 Apache 的指标信息。


我们打开 Dashboard:




从上面我们可以看出  Apache 整个的运行状态。


Ubuntu OS


关于 Ubuntu OS 的安装,我们可以参考我的另外一篇文章 “Elastic:使用 Elastic Stack 来监督系统日志及指标”。在这里就不再赘述了。关于 Apache 模块的使用和上面的 centos 的非常相近。就留给开发者你们自己探讨了。



相关实践学习
日志服务之数据清洗与入湖
本教程介绍如何使用日志服务接入NGINX模拟数据,通过数据加工对数据进行清洗并归档至OSS中进行存储。
相关文章
|
2月前
|
存储 算法 数据挖掘
带你读《Apache Doris 案例集》——06 Apache Doris 助力中国联通万亿日志数据分析提速10倍(2)
带你读《Apache Doris 案例集》——06 Apache Doris 助力中国联通万亿日志数据分析提速10倍(2)
164 1
|
2月前
|
存储 安全 数据挖掘
带你读《Apache Doris 案例集》——06 Apache Doris 助力中国联通万亿日志数据分析提速10倍(1)
带你读《Apache Doris 案例集》——06 Apache Doris 助力中国联通万亿日志数据分析提速10倍(1)
143 1
|
2月前
|
SQL 缓存 监控
带你读《Apache Doris 案例集》——03 Apache Doris 在金融壹账通指标中台的应用实践(2)
带你读《Apache Doris 案例集》——03 Apache Doris 在金融壹账通指标中台的应用实践(2)
带你读《Apache Doris 案例集》——03  Apache   Doris  在金融壹账通指标中台的应用实践(2)
|
2月前
|
SQL 运维 数据挖掘
带你读《Apache Doris 案例集》——03 Apache Doris 在金融壹账通指标中台的应用实践(1)
带你读《Apache Doris 案例集》——03 Apache Doris 在金融壹账通指标中台的应用实践(1)
|
2月前
|
存储 监控 Apache
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
网易的灵犀办公和云信利用 Apache Doris 改进了大规模日志和时序数据处理,取代了 Elasticsearch 和 InfluxDB。Doris 实现了更低的服务器资源消耗和更高的查询性能,相比 Elasticsearch,查询速度提升至少 11 倍,存储资源节省达 70%。Doris 的列式存储、高压缩比和倒排索引等功能,优化了日志和时序数据的存储与分析,降低了存储成本并提高了查询效率。在灵犀办公和云信的实际应用中,Doris 显示出显著的性能优势,成功应对了数据增长带来的挑战。
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
|
2月前
|
Apache
web服务器(Apache)访问日志(access_log)详细解释
web服务器(Apache)访问日志(access_log)详细解释
|
2月前
|
存储 消息中间件 监控
Zoom 基于Apache Hudi 的流式日志处理实践
Zoom 基于Apache Hudi 的流式日志处理实践
56 1
|
18天前
|
存储 关系型数据库 MySQL
|
3天前
|
监控
查看服务器/IIS日志、log、访问信息基本方法
除了手动查看,你也可以使用日志分析工具,如Log Parser、AWStats等,这些工具可以帮助你更方便地分析日志数据。
4 1
|
11天前
|
Java 测试技术 Apache
《手把手教你》系列基础篇(八十六)-java+ selenium自动化测试-框架设计基础-Log4j实现日志输出(详解教程)
【7月更文挑战第4天】Apache Log4j 是一个广泛使用的 Java 日志框架,它允许开发者控制日志信息的输出目的地、格式和级别。Log4j 包含三个主要组件:Loggers(记录器)负责生成日志信息,Appenders(输出源)确定日志输出的位置(如控制台、文件、数据库等),而 Layouts(布局)则控制日志信息的格式。通过配置 Log4j,可以灵活地定制日志记录行为。
27 4

热门文章

最新文章

推荐镜像

更多