Mirror of https://github.com/roostorg/osprey
github.com/roostorg/osprey
1#!/bin/bash
2
3# Configuration variables
4KAFKA_BROKER=${KAFKA_BROKER:-"localhost:9092"}
5KAFKA_TOPIC=${KAFKA_TOPIC:-"test-data"}
6KAFKA_CONFIG_FILE=${KAFKA_CONFIG_FILE:-""}
7
8# Function to generate random user ID
9generate_random_user_id() {
10 echo "user_$(shuf -i 100-9999 -n 1)"
11}
12
13# Function to generate current timestamp in ISO 8601 format with nanoseconds
14generate_timestamp() {
15 date -u +"%Y-%m-%dT%H:%M:%S.%NZ"
16}
17
18# Initialize action_id counter
19action_id=1
20
21# words in post
22words=(hello the quick brown fox jumps over lazy dog and cat runs fast);
23SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
24
25
26# Function to generate a single JSON action
27generate_action() {
28 local text="${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]} ${words[RANDOM % ${#words[@]}]}."
29 local timestamp=$(generate_timestamp)
30 local user_id=$(generate_random_user_id)
31 local ip_address="192.168.1.$(shuf -i 1-254 -n 1)"
32
33 local sed_commands=()
34 sed_commands+=("s/\$text/$text/g")
35 sed_commands+=("s/\$timestamp/$timestamp/g")
36 sed_commands+=("s/\$user_id/$user_id/g")
37 sed_commands+=("s/\$ip_address/$ip_address/g")
38 sed_commands+=("s/\$action_id/$action_id/g")
39
40 # Apply all sed commands
41 local cmd="sed"
42 for sed_cmd in "${sed_commands[@]}"; do
43 cmd="$cmd -e '$sed_cmd'"
44 done
45 eval "$cmd" "$SCRIPT_DIR/template.json"
46}
47
48# Function to build kafka-console-producer command
49build_kafka_command() {
50 local cmd="kafka-console-producer --broker-list $KAFKA_BROKER --topic $KAFKA_TOPIC"
51
52 if [ -n "$KAFKA_CONFIG_FILE" ]; then
53 cmd="$cmd --producer.config $KAFKA_CONFIG_FILE"
54 fi
55
56 echo "$cmd"
57}
58
59# Function to handle cleanup on script termination
60cleanup() {
61 echo
62 echo "Stopping data generation..."
63 exit 0
64}
65
66# Set up signal handlers for graceful shutdown
67trap cleanup SIGINT SIGTERM
68
69# Main execution
70echo "Generating actions every second to Kafka topic '$KAFKA_TOPIC'..."
71echo "Kafka broker: $KAFKA_BROKER"
72if [ -n "$KAFKA_CONFIG_FILE" ]; then
73 echo "Using config file: $KAFKA_CONFIG_FILE"
74fi
75echo "Press Ctrl+C to stop..."
76echo
77
78# Build the kafka command
79kafka_cmd=$(build_kafka_command)
80
81# Infinite loop to generate and send actions
82while true; do
83 action=$(generate_action)
84 echo -e "Sending $action"
85 echo -e "$action" | $kafka_cmd
86
87 # Increment action_id in the main shell
88 ((action_id++))
89 sleep 1
90done