WUT_Computer_Science/Programming/PSD/zin3/python/run_temperature_system.sh
Krzysztof kuhy Rudnicki 0928dfdd13
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (push) Waiting to run
CI: pin shellcheck, and enable Actions on the repo
Two things kept CI from being green, and neither was the code.

First, GitHub Actions was DISABLED at the repository level. The workflows
registered fine and simply never ran -- `gh api .../actions/permissions`
returned {"enabled": false}. Now enabled.

Second, once it ran, shellcheck failed on a file that is clean locally:
SC2236 in PSD/zin3/python/run_temperature_system.sh. `language: system` hooks
use whatever is on PATH, and Ubuntu's packaged shellcheck is not the 0.11.0
this machine has, so the build went red for a difference in tool version
rather than in the code. That is the failure mode testsAndMisc pins tools to
avoid, so the workflow now installs shellcheck v0.11.0 and verifies its
SHA256 -- checked here first: the digest matches and the archive really does
unpack to shellcheck-v0.11.0/shellcheck.

The `! -z` -> `-n` fix is applied too. Pinning stops the next version drift;
fixing the code means this instance is right under either version.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0172dj34FDpao6wsix8ZuCzE
2026-08-28 18:31:11 +02:00

133 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
# Directory where the code is located
CODE_DIR="$(dirname "$(readlink -f "$0")")/code"
KAFKA_DIR=${KAFKA_HOME:-"/opt/kafka"}
KAFKA_BIN="$KAFKA_DIR/bin"
# Colors for console output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check if Kafka is running
kafka_running() {
if command_exists netstat; then
netstat -tuln | grep -q 9092
else
echo -e "${YELLOW}Warning: netstat not available, assuming Kafka is running${NC}"
return 0
fi
}
# Cleanup function to kill all processes on exit
cleanup() {
echo -e "${YELLOW}Shutting down the temperature monitoring system...${NC}"
# Kill all background processes
if [[ -n "${GENERATOR_PID}" ]]; then
echo "Stopping temperature generator..."
kill $GENERATOR_PID 2>/dev/null || true
fi
if [[ -n "${DETECTOR_PID}" ]]; then
echo "Stopping anomaly detector..."
kill $DETECTOR_PID 2>/dev/null || true
fi
if [[ -n "${VISUALIZER_PID}" ]]; then
echo "Stopping alarm visualizer..."
kill $VISUALIZER_PID 2>/dev/null || true
fi
echo -e "${GREEN}All components stopped successfully.${NC}"
exit 0
}
# Register the cleanup function for script termination
trap cleanup EXIT INT TERM
# Check Python environment
if ! command_exists python3; then
echo -e "${RED}Error: Python3 is not installed. Please install Python3 and try again.${NC}"
exit 1
fi
KAFKA_BIN="/home/psd/Downloads/kafka_2.13-3.4.0/bin"
# Check if Kafka is running
echo "Checking Kafka status..."
if ! kafka_running; then
echo -e "${YELLOW}Kafka is not running. Attempting to start Kafka...${NC}"
# Check Zookeeper first
if ! netstat -tuln | grep -q 2181; then
echo "Starting Zookeeper..."
if [[ -f "$KAFKA_BIN/zookeeper-server-start.sh" ]]; then
"$KAFKA_BIN/zookeeper-server-start.sh" "$KAFKA_DIR/config/zookeeper.properties" > /dev/null 2>&1 &
sleep 5
else
echo -e "${RED}Error: Zookeeper startup script not found.${NC}"
exit 1
fi
fi
# Start Kafka
if [[ -f "$KAFKA_BIN/kafka-server-start.sh" ]]; then
echo "Starting Kafka server..."
"$KAFKA_BIN/kafka-server-start.sh" "$KAFKA_DIR/config/server.properties" > /dev/null 2>&1 &
sleep 10
# Check if Kafka started successfully
if ! kafka_running; then
echo -e "${RED}Error: Failed to start Kafka. Please check Kafka installation.${NC}"
exit 1
fi
else
echo -e "${RED}Error: Kafka startup script not found.${NC}"
exit 1
fi
fi
echo -e "${GREEN}Kafka is running.${NC}"
# Create Kafka topics if they don't exist
echo "Creating Kafka topics if they don't exist..."
if [[ -f "$KAFKA_BIN/kafka-topics.sh" ]]; then
"$KAFKA_BIN/kafka-topics.sh" --create --bootstrap-server localhost:9092 --topic Temperatura --partitions 3 --replication-factor 1 --if-not-exists
"$KAFKA_BIN/kafka-topics.sh" --create --bootstrap-server localhost:9092 --topic Alarm --partitions 3 --replication-factor 1 --if-not-exists
else
echo -e "${YELLOW}Warning: Kafka topics script not found. Assuming topics already exist.${NC}"
fi
# Start components in the correct order
echo -e "${GREEN}Starting temperature monitoring system...${NC}"
# 1. Start the alarm visualizer
echo "Starting alarm visualizer..."
python3 "$CODE_DIR/visualizer/alarm_visualizer.py" &
VISUALIZER_PID=$!
sleep 2
# 2. Start the anomaly detector
echo "Starting temperature anomaly detector..."
python3 "$CODE_DIR/processor/temperature_anomaly_detector.py" &
DETECTOR_PID=$!
sleep 2
# 3. Start the temperature generator
echo "Starting temperature generator..."
python3 "$CODE_DIR/generator/temperature_generator.py" &
GENERATOR_PID=$!
echo -e "${GREEN}All components started successfully!${NC}"
echo "Press Ctrl+C to stop the system."
# Wait for any process to exit
wait