metricbeat收集http指标

背景:在 metricbeat收集redis日志以及系统日志、python接收请求参数 基础上进行,部分简单内容省略

一、查看容器ip

docker inspect metricbeat | egrep IPAddress

二、编辑metricbeat.yml配置文件

1. 新增http模块的配置

  #--------------------------------- HTTP Module ---------------------------------
  - module: http
    metricsets:
      - json  # As client
    period: 100s
    hosts: ["192.168.1.110:8001"]
    namespace: "json_namespace"
    path: "/api"
    #body: ""
    #method: "GET"
    #username: "user"
    #password: "secret"
    #request.enabled: false
    #response.enabled: false
    #json.is_array: false
    #dedot.enabled: false
    processors:
      - add_fields:
          target: ''
          fields:
            from: http

  - module: http
    metricsets: ["server"]  # As a server,only accept post requests
    host: "172.18.0.2" # If using metricbeat container, here is the container ip
    port: "8002"
    server.paths:
      - path: "/api"
        namespace: "server_namespace"
        fields: # added to the the response in root. overwrites existing fields
          env: "test"
    processors:
      - add_fields:
          target: ''
          fields:
            from: http

三、启动python接收请求参数中的脚本

E:\Python\Python38\python3.exe F:/IdeaProjects/my-jooq/my-python/receive_data.py
 * Serving Flask app "receive_data" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 936-026-506
 * Running on http://0.0.0.0:8001/ (Press CTRL+C to quit)

四、添加容器对外暴露端口

services:
  metricbeat:
    image: elastic/metricbeat:7.7.0
    user: root
    container_name: metricbeat
    command: ["--strict.perms=false"]
    volumes:
      - ${PWD}/config/metricbeat.yml:/usr/share/metricbeat/metricbeat.yml
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /sys/fs/cgroup:/hostfs/sys/fs/cgroup:ro
      - /proc:/hostfs/proc:ro
      - /:/hostfs:ro
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "8002:8002"
    networks:
      - shared

 五、重新加载容器

docker-compose up -d

六、查看作为客户端的结果

1. 查看作为http客户端发送的请求结果

metricbeat收集http指标_第1张图片

2. 查看收集到的指标监控日志 

metricbeat收集http指标_第2张图片

七、查看作为服务端的结果 

1. 发送请求

curl --header "Content-Type:application/json" -X POST --data '{"logLevel":"ERROR", "msg":"hello world"}' http://localhost:8002/api

1.1 下图为通过容器启动metricbeat的操作结果 

metricbeat收集http指标_第3张图片

1.2 下图为直接在主机启动metricbeat的操作结果 

metricbeat收集http指标_第4张图片

注: 从以上操作结果可以看出,如果配置的host是ip地址,那么不能使用localhost替代,同理配置的是localhost,也不能使用ip地址替换

2. 查看收集到的指标监控日志

2.1 下图为通过容器启动metricbeat的操作结果 

metricbeat收集http指标_第5张图片

2.2 下图为直接在主机启动metricbeat的操作结果 

metricbeat收集http指标_第6张图片

到此http指标收集成功

你可能感兴趣的:(elastic,stack,metricbeat)