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

简介: 在我之前的许多文章中,我基本上都已经讲到了这些方面的内容。在今天的文章中,我想针对一些开发还没有自己的系统,比如 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模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
6月前
|
存储 消息中间件 Java
Apache Flink 实践问题之原生TM UI日志问题如何解决
Apache Flink 实践问题之原生TM UI日志问题如何解决
64 1
|
1月前
|
存储 监控 安全
实时记录和查看Apache 日志
Apache 是一个开源、跨平台的 Web 服务器,保护其平台需监控活动和事件。Apache 日志分为访问日志和错误日志,分别记录用户请求和服务器错误信息。EventLog Analyzer 是一款强大的日志查看工具,提供集中收集、分析、实时警报和安全监控功能,帮助管理员识别趋势、检测威胁并确保合规性。通过直观的仪表板和自动化响应,它简化了大规模日志管理,增强了 Apache 服务器的安全性和性能。
|
2月前
|
监控 安全 Apache
什么是Apache日志?为什么Apache日志分析很重要?
Apache是全球广泛使用的Web服务器软件,支持超过30%的活跃网站。它通过接收和处理HTTP请求,与后端服务器通信,返回响应并记录日志,确保网页请求的快速准确处理。Apache日志分为访问日志和错误日志,对提升用户体验、保障安全及优化性能至关重要。EventLog Analyzer等工具可有效管理和分析这些日志,增强Web服务的安全性和可靠性。
|
1月前
|
存储 运维 监控
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
中信银行信用卡中心每日新增日志数据 140 亿条(80TB),全量归档日志量超 40PB,早期基于 Elasticsearch 构建的日志云平台,面临存储成本高、实时写入性能差、文本检索慢以及日志分析能力不足等问题。因此使用 Apache Doris 替换 Elasticsearch,实现资源投入降低 50%、查询速度提升 2~4 倍,同时显著提高了运维效率。
金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
|
2月前
|
运维 监控 Cloud Native
一行代码都不改,Golang 应用链路指标日志全知道
本文将通过阿里云开源的 Golang Agent,帮助用户实现“一行代码都不改”就能获取到应用产生的各种观测数据,同时提升运维团队和研发团队的幸福感。
223 13
|
2月前
|
存储 监控 安全
实时记录和查看Apache 日志
Apache 是一个开源、跨平台的Web服务器,保护其安全依赖于监控活动和分析访问日志。日志分为访问日志和错误日志,前者记录用户请求及响应情况,后者记录服务器错误信息。EventLog Analyzer等工具可集中收集、分析日志,提供直观的仪表板和实时警报,帮助识别趋势、异常和威胁,确保服务器稳定性和安全性,并支持合规管理。
|
6月前
|
存储 消息中间件 人工智能
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
早期 MiniMax 基于 Grafana Loki 构建了日志系统,在资源消耗、写入性能及系统稳定性上都面临巨大的挑战。为此 MiniMax 开始寻找全新的日志系统方案,并基于阿里云数据库 SelectDB 版内核 Apache Doris 升级了日志系统,新系统已接入 MiniMax 内部所有业务线日志数据,数据规模为 PB 级, 整体可用性达到 99.9% 以上,10 亿级日志数据的检索速度可实现秒级响应。
AI大模型独角兽 MiniMax 基于阿里云数据库 SelectDB 版内核 Apache Doris 升级日志系统,PB 数据秒级查询响应
|
6月前
|
Ubuntu Linux 测试技术
在Linux中,已知 apache 服务的访问日志按天记录在服务器本地目录/app/logs 下,由于磁盘空间紧张现在要求只能保留最近7天的访问日志,请问如何解决?
在Linux中,已知 apache 服务的访问日志按天记录在服务器本地目录/app/logs 下,由于磁盘空间紧张现在要求只能保留最近7天的访问日志,请问如何解决?
|
6月前
|
存储 Ubuntu Apache
如何在 Ubuntu VPS 上配置 Apache 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Apache 的日志记录和日志轮转
70 6
|
6月前
|
消息中间件 Java Kafka
【Azure 事件中心】开启 Apache Flink 制造者 Producer 示例代码中的日志输出 (连接 Azure Event Hub Kafka 终结点)
【Azure 事件中心】开启 Apache Flink 制造者 Producer 示例代码中的日志输出 (连接 Azure Event Hub Kafka 终结点)

热门文章

最新文章

推荐镜像

更多