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

简介: 在我之前的许多文章中,我基本上都已经讲到了这些方面的内容。在今天的文章中,我想针对一些开发还没有自己的系统,比如 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 并进行分析的开发者有所帮助。


相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
WGLOG日志管理系统是怎么收集日志的
WGLOG通过部署Agent客户端采集日志,Agent持续收集指定日志文件并上报Server,Server负责展示与分析。Agent与Server需保持相同版本。官网下载地址:www.wgstart.com
|
8月前
|
Prometheus 监控 Cloud Native
基于docker搭建监控系统&日志收集
Prometheus 是一款由 SoundCloud 开发的开源监控报警系统及时序数据库(TSDB),支持多维数据模型和灵活查询语言,适用于大规模集群监控。它通过 HTTP 拉取数据,支持服务发现、多种图表展示(如 Grafana),并可结合 Loki 实现日志聚合。本文介绍其架构、部署及与 Docker 集成的监控方案。
732 122
基于docker搭建监控系统&日志收集
|
11月前
|
监控 API 开发工具
HarmonyOS Next的HiLog日志系统完全指南:从入门到精通
本文深入解析HarmonyOS Next的HiLog日志系统,涵盖日志级别、核心API、隐私保护与高级回调功能,助你从入门到精通掌握这一重要开发工具。
|
8月前
|
Ubuntu
在Ubuntu系统上设置syslog日志轮替与大小限制
请注意,在修改任何系统级别配置之前,请务必备份相应得原始档案并理解每项变更可能带来得影响。
943 2
|
存储 前端开发 数据可视化
Grafana Loki,轻量级日志系统
本文介绍了基于Grafana、Loki和Alloy构建的轻量级日志系统。Loki是一个由Grafana Labs开发的日志聚合系统,具备高可用性和多租户支持,专注于日志而非指标,通过标签索引而非内容索引实现高效存储。Alloy则是用于收集和转发日志至Loki的强大工具。文章详细描述了系统的架构、组件及其工作流程,并提供了快速搭建指南,包括准备步骤、部署命令及验证方法。此外,还展示了如何使用Grafana查看日志,以及一些基本的LogQL查询示例。最后,作者探讨了Loki架构的独特之处,提出了“巨型单体模块化”的概念,即一个应用既可单体部署也可分布式部署,整体协同实现全部功能。
5394 70
Grafana Loki,轻量级日志系统
|
运维 监控 Cloud Native
一行代码都不改,Golang 应用链路指标日志全知道
本文将通过阿里云开源的 Golang Agent,帮助用户实现“一行代码都不改”就能获取到应用产生的各种观测数据,同时提升运维团队和研发团队的幸福感。
779 138
|
10月前
|
存储
WGLOG日志管理系统可以采集网络设备的日志吗
WGLOG日志审计系统提供开放接口,支持外部获取日志内容后发送至该接口,实现日志的存储与分析。详情请访问:https://www.wgstart.com/wglog/docs9.html
|
存储 消息中间件 缓存
MiniMax GenAI 可观测性分析 :基于阿里云 SelectDB 构建 PB 级别日志系统
基于阿里云SelectDB,MiniMax构建了覆盖国内及海外业务的日志可观测中台,总体数据规模超过数PB,日均新增日志写入量达数百TB。系统在P95分位查询场景下的响应时间小于3秒,峰值时刻实现了超过10GB/s的读写吞吐。通过存算分离、高压缩比算法和单副本热缓存等技术手段,MiniMax在优化性能的同时显著降低了建设成本,计算资源用量降低40%,热数据存储用量降低50%,为未来业务的高速发展和技术演进奠定了坚实基础。
612 1
MiniMax GenAI 可观测性分析 :基于阿里云 SelectDB 构建 PB 级别日志系统
|
存储 JSON Go
PHP 日志系统的最佳搭档:一个 Go 写的远程日志收集服务
为了不再 SSH 上去翻日志,我写了个 Go 小脚本,用来接收远程日志。PHP 负责记录日志,Go 负责存储和展示,按天存储、支持 API 访问、可远程管理,终于能第一时间知道项目炸了。
318 10

热门文章

最新文章