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

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


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


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

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


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



如上所示,我们将在左边使用 Docker 来安装 Elasticsearch 及 Kibana。在右边的两个虚拟机中,我们将分别安装 centos 7 及 Ubuntu  20.04。我们分别在这两个系统中安装相应的 Filebeat 及 Metricbeat 来收集系统日志及指标,并发送至  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 的日志信息:


1. docker logs elasticsearch
2. 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)


我们接下来安装 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... 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,但是我们还没有运行它。我们可以使用如下的命令来检查它的运行状态:


[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-* 以及合适的时间范围,否则我们有可能看不到任何的数据。


我们打开 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  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



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


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... 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


我们打开 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  = false
      node.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  = 1024
        v.cpus    = 1
      end
      node.vm.provider :libvirt do |v|
        v.nested  = true
        v.memory  = 1024
        v.cpus    = 1
      end
    end
  end
end


在上面,我们设置 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 并进行分析的开发者有所帮助。


相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
Shell Linux C语言
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
【Shell 命令集合 网络通讯 】Linux 查看系统中的UUCP日志文件 uulog命令 使用指南
29 0
|
2月前
电子书阅读分享《Elasticsearch全观测技术解析与应用(构建日志、指标、APM统一观测平台)》
电子书阅读分享《Elasticsearch全观测技术解析与应用(构建日志、指标、APM统一观测平台)》
235 1
|
4月前
|
存储 监控 数据可视化
小白带你学习linux的ELK日志收集系统
小白带你学习linux的ELK日志收集系统
159 0
|
5月前
|
Web App开发 监控 NoSQL
ELK日志分析系统部署文档 1
ELK日志分析系统部署文档
113 0
|
4月前
|
存储 监控 安全
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(1)
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(1)
|
4月前
|
SQL 存储 安全
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(2)
带你读《Apache Doris 案例集》——07查询平均提速700% ,奇安信基于 Apache Doris 升级日志安全分析系统(2)
110 0
|
6天前
|
JavaScript Java 测试技术
基于Java的公司员工工作日志办公系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的公司员工工作日志办公系统的设计与实现(源码+lw+部署文档+讲解等)
30 3
|
23天前
|
C++
QT实现一个简单的日志打印系统
QT实现一个简单的日志打印系统
|
2月前
|
调度 数据库 数据安全/隐私保护
ABAP 系统里使用事务码 SM21 查看系统日志的技巧介绍
ABAP 系统里使用事务码 SM21 查看系统日志的技巧介绍
44 0
|
3月前
|
存储 JSON 运维
【运维】Powershell 服务器系统管理信息总结(进程、线程、磁盘、内存、网络、CPU、持续运行时间、系统账户、日志事件)
【运维】Powershell 服务器系统管理信息总结(进程、线程、磁盘、内存、网络、CPU、持续运行时间、系统账户、日志事件)
49 0