|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# |
| 4 | +# This script is used to test the logstash pipeline configuration. |
| 5 | +# |
| 6 | +# It sets up a logstash pipeline with the postfix configuration, |
| 7 | +# sends a test logline through the pipeline and checks the results. |
| 8 | +# |
| 9 | + |
| 10 | +set -eux |
| 11 | + |
| 12 | +INPUT=$(mktemp tmp.logstash.in.XXXXX) |
| 13 | +OUTPUT=$(mktemp tmp.logstash.out.XXXXX) |
| 14 | +PIPELINE=$(mktemp tmp.logstash.pipeline.XXXXX) |
| 15 | + |
| 16 | +echo Preparing input data |
| 17 | +echo "postfix/smtp[123]: 7EE668039: to=<[email protected]>, relay=127.0.0.1[127.0.0.1]:2525, delay=3.6, delays=0.2/0.02/0.04/3.3, dsn=2.0.0, status=sent (250 2.0.0 Ok: queued as 153053D)" > "$INPUT" |
| 18 | + |
| 19 | +echo Preparing pipeline config |
| 20 | +cat > "$PIPELINE" << EOF |
| 21 | +input { |
| 22 | + file { |
| 23 | + path => "/tmp/logstash.in" |
| 24 | + start_position => beginning |
| 25 | + } |
| 26 | +} |
| 27 | +filter { |
| 28 | + dissect { |
| 29 | + mapping => { |
| 30 | + "message" => "%{program}[%{pid}]: %{message}" |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +EOF |
| 35 | + |
| 36 | +cat 50-filter-postfix.conf >> "$PIPELINE" |
| 37 | + |
| 38 | +cat >> "$PIPELINE" << EOF |
| 39 | +output { |
| 40 | + file { |
| 41 | + path => "/tmp/logstash.out" |
| 42 | + } |
| 43 | +} |
| 44 | +EOF |
| 45 | + |
| 46 | +echo Starting logstash docker container |
| 47 | +CONTAINER_ID=$(docker run --rm --detach \ |
| 48 | + --volume ./"${INPUT}":/tmp/logstash.in \ |
| 49 | + --volume ./"${OUTPUT}":/tmp/logstash.out \ |
| 50 | + --volume ./postfix.grok:/etc/logstash/patterns.d/postfix.grok \ |
| 51 | + --volume ./"${PIPELINE}":/usr/share/logstash/pipeline/pipeline.conf \ |
| 52 | + logstash:8.12.0 \ |
| 53 | + logstash -f /usr/share/logstash/pipeline/pipeline.conf) |
| 54 | + |
| 55 | +printf "Waiting for output from logstash " |
| 56 | +until test -s "$OUTPUT"; do |
| 57 | + printf "." |
| 58 | + sleep 2 |
| 59 | +done |
| 60 | +echo |
| 61 | + |
| 62 | +docker stop --time 1 "$CONTAINER_ID" > /dev/null |
| 63 | + |
| 64 | +if test "$(jq .tags[0] "$OUTPUT")" = '"_grok_postfix_success"'; then |
| 65 | + echo Grok processing successful! |
| 66 | + jq . "$OUTPUT" |
| 67 | +else |
| 68 | + echo "Grok processing failed :<" |
| 69 | + jq . "$OUTPUT" |
| 70 | + exit 1 |
| 71 | +fi |
| 72 | + |
| 73 | +echo Cleaning up |
| 74 | +rm -f "$INPUT" "$OUTPUT" "$PIPELINE" |
| 75 | + |
| 76 | +echo Done |
0 commit comments