This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: run filebeat to grab the Docker logs
- Loading branch information
1 parent
219bdaa
commit a24c481
Showing
6 changed files
with
125 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
OUTPUT_DIR=${OUTPUT_DIR:-'/tmp/filebeat'} | ||
OUTPUT_FILE=${OUTPUT_FILE:-'docker'} | ||
CONFIG_PATH=${CONFIG_PATH:-'/tmp/filebeat.yml'} | ||
DOCKER_IMAGE=${DOCKER_IMAGE:-'docker.elastic.co/beats/filebeat:8.5.3'} | ||
|
||
echo "OUTPUT_DIR=${OUTPUT_DIR}" | ||
echo "OUTPUT_FILE=${OUTPUT_FILE}" | ||
echo "CONFIG_PATH=${CONFIG_PATH}" | ||
echo "DOCKER_IMAGE=${DOCKER_IMAGE}" | ||
|
||
docker ps --filter label="name=filebeat" -q | xargs docker kill || true | ||
|
||
mkdir -p "${OUTPUT_DIR}" | ||
|
||
cat <<EOF > "${CONFIG_PATH}" | ||
--- | ||
filebeat.autodiscover: | ||
providers: | ||
- type: docker | ||
templates: | ||
- config: | ||
- type: container | ||
paths: | ||
- /var/lib/docker/containers/\${data.docker.container.id}/*.log | ||
processors: | ||
- add_host_metadata: ~ | ||
- add_cloud_metadata: ~ | ||
- add_docker_metadata: ~ | ||
- add_kubernetes_metadata: ~ | ||
output.file: | ||
path: "/output" | ||
filename: ${OUTPUT_FILE} | ||
permissions: 0644 | ||
codec.format: | ||
string: '[%{[container.name]}][%{[container.image.name]}][%{[container.id]}][%{[@timestamp]}] %{[message]}' | ||
EOF | ||
|
||
echo "INFO: Run filebeat" | ||
docker run \ | ||
--detach \ | ||
-v "${OUTPUT_DIR}:/output" \ | ||
-v "${CONFIG_PATH}:/usr/share/filebeat/filebeat.yml" \ | ||
-u 0:0 \ | ||
-v /var/lib/docker/containers:/var/lib/docker/containers \ | ||
-v /var/run/docker.sock:/var/run/docker.sock \ | ||
-e OUTPUT_FILE="${OUTPUT_FILE}" \ | ||
"${DOCKER_IMAGE}" \ | ||
--strict.perms=false \ | ||
-environment container \ | ||
-E http.enabled=true > filebeat_docker_id | ||
|
||
ID=$(docker ps --filter label="name=filebeat" -q) | ||
URL=${2:-"http://localhost:5066/stats?pretty"} | ||
|
||
echo "INFO: print existing docker context" | ||
docker ps -a || true | ||
|
||
sleep 10 | ||
|
||
echo "INFO: wait for the docker container to be available" | ||
N=0 | ||
until docker exec "${ID}" curl -sSfI --retry 10 --retry-delay 5 --max-time 5 "${URL}" | ||
do | ||
sleep 5 | ||
if [ "${N}" -gt 6 ]; then | ||
echo "ERROR: print docker inspect" | ||
docker inspect "${ID}" | ||
echo "ERROR: docker container is not available" | ||
docker logs "${ID}" | ||
break; | ||
fi | ||
N=$((N + 1)) | ||
done |