背景:在 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客戶端發送的請求結果
2. 查看收集到的指標監控日誌
七、查看作爲服務端的結果
1. 發送請求
curl --header "Content-Type:application/json" -X POST --data '{"logLevel":"ERROR", "msg":"hello world"}' http://localhost:8002/api
1.1 下圖爲通過容器啓動metricbeat的操作結果
1.2 下圖爲直接在主機啓動metricbeat的操作結果
注: 從以上操作結果可以看出,如果配置的host是ip地址,那麼不能使用localhost替代,同理配置的是localhost,也不能使用ip地址替換
2. 查看收集到的指標監控日誌
2.1 下圖爲通過容器啓動metricbeat的操作結果
2.2 下圖爲直接在主機啓動metricbeat的操作結果
到此http指標收集成功