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

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

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

  • Beats:Beats 入门教程 (一)
  • Beats:Beats 入门教程 (二)

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

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

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

$ ifconfig | grep 192inet 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.2container_name: elasticsearchenvironment:- 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: -1hard: -1volumes:- esdata1:/usr/share/elasticsearch/dataports:- 9200:9200kibana:image: docker.elastic.co/kibana/kibana:7.15.2container_name: kibanaenvironment:ELASTICSEARCH_URL: "http://elasticsearch:9200"ports:- 5601:5601depends_on:- elasticsearchvolumes: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 = 2048v.cpus = 1endendendend

如上所示,我们设置 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 daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)

我们接下来安装 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 版本。

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

[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

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

sudo filebeat modules enable system
[vagrant@localhost ~]$ sudo filebeat modules enable system
Enabled system

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

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

sudo filebeat modules disable nginx

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

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

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

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

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

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

system.yml

一般来说,我们并不需要针对这个文件做任何的配置,除非你的系统的日志路径确实和标准的不同。这个时候,我们需要来重新配置。

接下来,我们需要来配置 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... OKconnection...parse host... OKdns lookup... OKaddresses: 192.168.0.3dial up... OKTLS... WARN secure connection disabledtalk to server... OKversion: 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,但是我们还没有运行它。我们可以使用如下的命令来检查它的运行状态:

[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 agoDocs: https://www.elastic.co/beats/filebeatMain 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-* 以及合适的时间范围,否则我们有可能看不到任何的数据。

我们打开 Dashboard:

我们选择上面的 [Filebeat System] Syslog dashboard ECS:

我们可以看到 Syslog 的 Dashboard。在这里,我们可以对数据进行分析。

接下来,我们来安装 Metricbeat。

Metricbeat

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

我们向下滚动直至看到 System metrics:

如上所示,我们选择 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  CurrentDload  Upload   Total   Spent    Left  Speed0 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

当然,我们也可以使用如下的命令来启动一个模块:

sudo metricbeat modules enable nginx

上面的命令将启动 nginx 模块。我们也可以使用如下的命令来禁止一个模块:

sudo metricbeat modules disable nginx

上面的命令将禁止 nginx 模块。针对我们的练习,我们禁止 nginx 模块,因为我们目前只针对 system 模块感兴趣。

我们接下来查看文件 /etc/metricbeat/modules.d/system.yml 来了解该模块所收集的信息:

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

通常的情况下,我们并不需要来修改这个文件,但是如果我们对默认的配置并不是很满意,我们可以修改这个文件,并保存。

接下来,我们来对 Metricbeat 的配置文件 /etc/metricbeat/metricbeat.yml 进行修改:

sudo vi /etc/metricbeat/metricbeat.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... OKconnection...parse host... OKdns lookup... OKaddresses: 192.168.0.3dial up... OKTLS... WARN secure connection disabledtalk to server... OKversion: 7.15.2

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

sudo metricbeat test modules system | grep OK
[vagrant@localhost ~]$ sudo metricbeat test modules system | grep OKcpu...OKload...OKmemory...OKnetwork...OKprocess...OKprocess_summary...OKsocket_summary...OKfilesystem...OKfsstat...OKuptime...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 agoDocs: https://www.elastic.co/beats/metricbeatMain PID: 390 (metricbeat)CGroup: /system.slice/metricbeat.service└─390 /usr/share/metricbeat/bin/metricbeat --environment systemd -..

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

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

sudo journalctl -u metricbeat

我们打开 Kibana 中的 Discover:

这次我们选择 metricbeat-* 索引模式。同时我们也选择相应的时间范围。

在练习中,我发现我的 centos 的时间设置和本地的时间是有差距的。我们可以使用如下的命令来设置 centos 的时间:

sudo timedatectl set-ntp 0

上面的命令是禁止 “Automatic time synchronization”。我们还需要使用如下的命令来设置时间:

sudo timedatectl set-time '2021-12-07 11:20:45'

请注意上面设置的是 UTC 时间。我们可以把时钟设置为当前 host 电脑的时间(我们需要把本地时间转换为 UTC 时间 )。

我们可以通过如下的命令来查看当前 centos 的时间:

[vagrant@localhost ~]$ date
date
Tue Dec  7 11:20:46 UTC 2021

我们可以打开 Metricbeat 的 Dashboard 来查看 Metricbeat 所收集上来的指标:

 

从上面,我们可以看出来 centos 的所有指标信息。

至此,我们完成了 centos 上的系统日志及指标的采集及分析。我们接下来针对 Ubuntu OS 来进行采集。

Ubuntu OS 20.04

我们按照同样的步骤来安装 Ubuntu OS。这次我们采用如下的 Vagrantfile:

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :ENV['VAGRANT_NO_PARALLEL'] = 'yes'Vagrant.configure(2) do |config|config.vm.provision "shell", path: "bootstrap.sh"NodeCount = 1(1..NodeCount).each do |i|config.vm.define "ubuntuvm#{i}" do |node|node.vm.box               = "generic/ubuntu2004"node.vm.box_check_update  = falsenode.vm.box_version       = "3.3.0"node.vm.hostname          = "ubuntuvm#{i}.example.com"node.vm.network "private_network", ip: "172.16.16.10#{i}"node.vm.provider :virtualbox do |v|v.name    = "ubuntuvm#{i}"v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]  v.memory  = 1024v.cpus    = 1endnode.vm.provider :libvirt do |v|v.nested  = truev.memory  = 1024v.cpus    = 1endendendend

在上面,我们设置 NodeCount 为1,也就是创建一个 Ubuntu OS 系统。它的 IP 地址为 172.16.16.101

我们在 Vagrantfile 文件所在的目录里也同时创建一个叫做 boostrap.sh 的文件:

bootstrap.sh

#!/bin/bash# Enable ssh password authentication
echo "Enable ssh password authentication"
sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
systemctl reload sshd# Set Root password
echo "Set root password"
echo -e "admin\nadmin" | passwd root >/dev/null 2>&1

我们使用如下的命令来创建 Ubuntu 20.04 系统:

vagrant up

等系统完全安装好过后,我们使用如下的命令来进入到 Ubuntu OS:

vagrant ssh

一旦进入到 Ubuntu OS,我们可以按照之前在 centos 安装 Filebeat 和 Metricbeat 的方法来安装它们。在按照过程中,我们需要选择 DEB 格式:

这是唯一的区别。其它的请参照上面的步骤来进行配置。

我们仿照上面 centos 里 Filebeat 的方式来配置及运行  Filebeat。在 Ubuntu OS 上,我们可以同如下的命令来查看安装的文件:

 dpkg -L filebeat

我们可以通过如下的命令来找到 filebeat.yml 文件的安装位置:

vagrant@ubuntuvm1:~$ dpkg -L filebeat | grep filebeat.yml
/etc/filebeat/filebeat.yml

当我们配置好,并运行我们的 Filebeat。我们再次查看 Filebeat 的 Dashboard:

这次,我们可以看到两个 host: localhost 及 ubuntu2004。

按照同样的方法,我们安装及配置 Metricbeat。我们在 Dashboard 里查看 System Metricbeat 的可视化:

我也同样在 Dashboard 里看到两个 host 的指标信息。

好了我今天的文章写到这里希望对一些想学习如何把日志及指标数据写入 Elastic Stack 并进行分析的开发者有所帮助。

Elastic:使用 Elastic Stack 来监督系统日志及指标相关推荐

  1. Elastic:Elastic Stack 7.6.0 重磅发布

    我们非常兴奋地宣布 Elastic Stack 7.6 正式发布了.此版本借助新推出的 SIEM 检测引擎和与 MITRE ATT&CK™ 知识库相相匹配的精选检测规则集,简化了自动威胁检测过 ...

  2. Elastic:Elastic Stack 8.0.0-alpha1 发布

    虽然 7.x 次要版本继续提供一个又一个功能,但我们很高兴地宣布 8.0.0 的第一个公开 alpha. 在我们继续这个令人兴奋的消息之前,我们想提醒你这是一个 alpha 版本. 我们建议你与生产保 ...

  3. Observability:使用 Elastic Agent 提取应用程序跟踪 - Elastic Stack 8.0

    在 Elastic Stack 8.0 之前,我们使用 APM server 来采集 Application Monitoring (APM) 数据.随着 Elastic Agent 在 8.0 的推 ...

  4. The Elastic Stack (ELK)7.14:简单配置到SSL配置教程(带Beats部署)

    文章目录 一.简介 二.环境 1.安装Docker.Docker Compose 三.部署Elastic Stack(ELK) 1.部署ElasticSearch(Distributed, RESTf ...

  5. Elasticsearch:如何在 Elastic Agents 中配置 Beats 来采集定制日志

    在我之前的文章 "Observability:使用 Elastic Agent 来摄入日志及指标 - Elastic Stack 8.0",我详细地描述了如何安装 Elastics ...

  6. Elastic 7.10 发布了可搜索快照的公测版和 Kibana Lens 的正式版

    我们非常高兴地宣布 Elastic 7.10 版正式发布.这一新版本为基于 Elastic Stack(包括 Elasticsearch.Kibana.Beats 和 Logstash)构建而成的 E ...

  7. Elastic:运用 Elastic Maps 实时跟踪,可视化资产分布及地理围栏告警(一)

    你对资产跟踪感兴趣吗? 好消息! 使用Elastic 地图应用可以轻松可视化和分析移动的数据. 你可以跟踪 IoT 设备的位置并监控运输途中的包裹或车辆. 在本教程中,你将查看来自俄勒冈州波特兰市的实 ...

  8. Elastic 7.11 重磅发布:可搜索快照和新冷层的正式版以及读时模式的公测版

    我们非常高兴地宣布 Elastic 7.11 版正式发布.这一新版本为基于 Elastic Stack(包括 Elasticsearch 和 Kibana)构建的 Elastic 企业搜索.可观测性和 ...

  9. 如何看懂Elastic解决方案与Gartner的魔力象限

    最近Elastic的解决方案频繁进入Gartner的魔力象限,或许社区里的小伙伴有听说过Gartner,也听说过魔力象限,但相信对此特别了解的人不多,而对入选的意义,以及分属不同的象限所说明的问题不甚 ...

最新文章

  1. [大数据之Spark]——Actions算子操作入门实例
  2. 前端技术选型的遗憾和经验教训
  3. form 表单 + HTML5(FileReader) +iframe 实现无刷新图片上传+图片预览效果
  4. Android Studio 单刷《第一行代码》系列 01 —— 第一战 HelloWorld
  5. 时频分析:短时傅立叶变换实现(5)
  6. Workaround for Search for my account in MyAppointment
  7. zabbix3.2监控
  8. Linux下静态IP地址的设置及TFTP服务的搭建
  9. c语言黄建灯第七章答案,c语言实训大纲.doc
  10. Android fillViewPort属性用法
  11. java62e62e,【报Bug】云端打包错误 apk
  12. Linux 文件夹压缩命令总结
  13. 从零开始学USB(三、基础知识3)
  14. WORD排版视频教程
  15. 施耐德PLC Unity Pro xl 软件使用四
  16. 第六季 流放之路教程
  17. 美国主要经济指标(2)
  18. 服务器和交换机物理连接_二层、三层及四层交换机的区别 | 小知识
  19. 特征值比对代码/计算相似度代码
  20. 虚拟机Ubuntu18.04中文输入法设置(同时保留系统英文语言)

热门文章

  1. iOS设备分辨率 UI规范 以及适配
  2. UI设计规范-全文篇
  3. leetcode 183. Customers Who Never Order
  4. One Billion Customers
  5. CPU内核和逻辑处理器的区别
  6. 慎用create table as select,一定要注意默认值的问题
  7. 第三方支付如何玩转大数据
  8. 会议及作用篇--项目管理(十六)
  9. 奥鹏计算机基础计算机病毒是指,奥鹏教育《计算机应用基础》在线考核A卷.doc...
  10. 三菱fx5u modbus tcp fb块用法_FX5U强势来袭