-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-dashboard-event-to-kafka.sh
executable file
·65 lines (49 loc) · 1.9 KB
/
send-dashboard-event-to-kafka.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
# debugging switches
# set -o errexit # abort on nonzero exitstatus; same as set -e
# set -o nounset # abort on unbound variable; same as set -u
# set -o pipefail # don't hide errors within pipes
# set -o xtrace # show commands being executed; same as set -x
# set -o verbose # verbose mode; same as set -v
# exit immediately if $ONCE_FLAG exists and has a non-empty value
# so we avoid spurious logging during "ONCE" runs of loop-thnr.sh
if [ -n "$ONCE_FLAG" ]; then
exit 0
fi
if [ $(($# % 2)) -ne 0 ]; then
echo "Please provide an even number of arguments."
exit 1
fi
source /srv/timbos-hn-reader/functions.sh
source /srv/timbos-hn-reader/thnr-common-functions.sh
# retrieve secrets
kafka_dashboard_topic="$(get-secret 'kafka_dashboard_topic')"
# kafka_message_version="$(get-secret 'kafka_message_version')"
kafka_message_version="0.1.0"
kafka_server_host_ip="$(get-secret 'kafka_server_host_ip')"
kafka_server_port="$(get-secret 'kafka_server_port')"
kafka_server_username="$(get-secret 'kafka_server_username')"
if [ -z "${kafka_server_host_ip}" ] || [ -z "${kafka_server_username}" ]; then
echo "Failed to retrieve Kafka server host IP or username from secrets."
exit 1
fi
kv_pairs=""
while [ $# -gt 0 ]; do
key="$1"
value="$2"
pair="\"$key\":\"$value\""
if [ -z "$kv_pairs" ]; then
kv_pairs=$pair
else
kv_pairs="$kv_pairs, $pair"
fi
shift 2
done
timestamp_unix="$(get-time-in-unix-seconds)"
message="{\"topic\":\"${kafka_dashboard_topic}\", ${kv_pairs}, \"timestamp_unix\":${timestamp_unix}, \"kafka_message_version\":\"${kafka_message_version}\", \"username\":\"${kafka_server_username}\"}"
# printf "${message}\n"
echo "${message}" | kafkacat -P -b "${kafka_server_host_ip}:${kafka_server_port}" -t "${kafka_dashboard_topic}"
res=$?
if [[ "${res}" != 0 ]]; then
printf "Failed to send message to Kafka: ${message}\n"
fi