Skip to content

Instantly share code, notes, and snippets.

@Arrayly
Last active November 22, 2023 14:14
Show Gist options
  • Save Arrayly/f2ce551bc8751e9974d2ce765d398897 to your computer and use it in GitHub Desktop.
Save Arrayly/f2ce551bc8751e9974d2ce765d398897 to your computer and use it in GitHub Desktop.
Automatic ElasticAgent+APM Install
#!/bin/bash
# =============================================================================
# Author: Eisvidas Jonkus
# Email: eisvidas.jonkus@autorabit.com
# Created: 06.10.2023
# Modified: 22.11.2023
# Version: 1.8
# License: This script is licensed for use
# Description: Installs Elasticsearch agent and APM and restarts tomcat
# =============================================================================
set -e # Exit on error
# Possible tomcat directories
POSSIBLE_DIRS=(
"/opt/newhd/rabitinstall/server-tomcat"
"/opt/installations/server-tomcat"
)
# Initialize base directory as empty
BASE_DIR=""
# Find the base directory
for dir in "${POSSIBLE_DIRS[@]}"; do
if [ -d "$dir" ]; then
BASE_DIR="$dir"
break
fi
done
if [[ "$INSTALL_APM" == "true" ]]; then
# Exit if no valid base directory found
if [ -z "$BASE_DIR" ]; then
echo "[ERROR] No valid base directory found for server-tomcat. Exiting."
exit 1
fi
fi
# Initialize Variables
INSTALL_PATH="/opt/installations/elasticsearch"
APM_JAR_NAME="elastic-apm-agent.jar"
TARGET_FILE="$BASE_DIR/bin/setclasspath.sh"
START_TOMCAT="$BASE_DIR/bin/startup.sh"
SHUTDOWN_TOMCAT="$BASE_DIR/bin/shutdown.sh"
REQUIRE_RESTART=false
ELASTIC_AGENT_HEALTHY=false
ELK_SERVER=${1}
SECRET_TOKEN=${2}
ENROLLMENT_TOKEN=${3}
APM_SERVICE_NAME=${4}
APM_ENVIRONMENT=${5}
APM_APP_PACKAGES=${6}
REMOVE_DATADOG=${7}
INSTALL_APM=${8}
# Check if variables are empty
if [[ -z "$ELK_SERVER" || -z "$SECRET_TOKEN" || -z "$ENROLLMENT_TOKEN" || -z "$APM_SERVICE_NAME" || -z "$APM_ENVIRONMENT" || -z "$APM_APP_PACKAGES" || -z "$INSTALL_APM" ]]; then
echo "One or more variables are empty. Exiting."
exit 1
fi
# Check if essential files exist
if [[ "$INSTALL_APM" == "true" ]]; then
for essential_file in "$TARGET_FILE" "$START_TOMCAT" "$SHUTDOWN_TOMCAT"; do
if [ ! -f "$essential_file" ]; then
echo "[ERROR] Essential file $essential_file does not exist. Exiting."
exit 1
fi
done
fi
# Function to check for required commands
check_dependency() {
if ! command -v "$1" &>/dev/null; then
echo "[ERROR] $1 could not be found. Exiting."
exit 1
fi
}
# Check dependencies
check_dependency "curl"
check_dependency "tar"
check_dependency "awk"
check_dependency "sed"
# Check if at least 10% of disk space is available
percentage_used=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
percentage_available=$((100 - percentage_used))
if [[ "$percentage_available" -ge 10 ]]; then
echo "Sufficient disk space available: $percentage_available%."
else
echo "Insufficient disk space. Only $percentage_available% available."
exit 1
fi
# Create Directory if it doesn't exist
mkdir -p "$INSTALL_PATH"
cd "$INSTALL_PATH"
# Function to get Elastic Agent status
get_elastic_status() {
if command -v "elastic-agent" &>/dev/null; then
ELASTIC_STATUS=$(sudo elastic-agent status 2>&1)
else
ELASTIC_STATUS=""
fi
}
# Check Elastic Agent Status
get_elastic_status
# Install Elastic Agent if needed
if [[ "$ELASTIC_STATUS" == *"State:"* ]]; then
echo "[INFO] Elastic Agent already installed."
else
echo "[INFO] Installing Elastic Agent."
curl -L -O https://artifacts.elastic.co/downloads/beats/elastic-agent/elastic-agent-8.8.0-linux-x86_64.tar.gz
tar xzvf elastic-agent-8.8.0-linux-x86_64.tar.gz
cd elastic-agent-8.8.0-linux-x86_64
sudo ./elastic-agent install -n --url="https://${ELK_SERVER}.fleet.us-west-2.aws.found.io:443" --enrollment-token="$ENROLLMENT_TOKEN"
sleep 45
get_elastic_status
[[ "$ELASTIC_STATUS" == *"HEALTHY"* ]] && ELASTIC_AGENT_HEALTHY=true
fi
# Check for Tomcat running status using pgrep
TOMCAT_PID=$(ps -eaf | grep '[c]atalina.startup.Bootstrap' | awk '{print $2}')
echo "[DEBUG] TOMCAT_PID is: $TOMCAT_PID"
# Only proceed with APM installation if INSTALL_APM is TRUE
if [[ "$INSTALL_APM" == "true" ]]; then
echo "APM Agent install set to True. Installing..."
if [ -n "$TOMCAT_PID" ]; then
# non-zero status may return if values not present. So temporarily disable set -e
set +e
APM_VARS=$(sudo cat /proc/"$TOMCAT_PID"/environ | tr '\0' '\n' | grep 'elastic.apm')
set -e
if [ -z "$APM_VARS" ]; then
echo "[INFO] Installing Elastic APM."
cd "$INSTALL_PATH"
curl -o "$APM_JAR_NAME" -L 'https://oss.sonatype.org/service/local/artifact/maven/redirect?r=releases&g=co.elastic.apm&a=elastic-apm-agent&v=LATEST'
{
echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -javaagent:$INSTALL_PATH/$APM_JAR_NAME\""
echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -Delastic.apm.service_name=$APM_SERVICE_NAME\""
echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -Delastic.apm.environment=$APM_ENVIRONMENT\""
echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -Delastic.apm.application_packages=$APM_APP_PACKAGES\""
echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -Delastic.apm.server_url=https://${ELK_SERVER}.apm.us-west-2.aws.found.io\""
echo "export CATALINA_OPTS=\"\$CATALINA_OPTS -Delastic.apm.secret_token=$SECRET_TOKEN\""
} >> "$TARGET_FILE"
REQUIRE_RESTART=true
else
echo "[INFO] Elastic APM already installed."
fi
else
echo "[WARN] Tomcat is not running. Exiting..."
exit 1
fi
else
echo "[INFO] Skipping Elastic APM installation."
fi
# Restart Tomcat if required
if [ "$REQUIRE_RESTART" = true ]; then
set +e
echo "[INFO] Restarting Tomcat."
bash "$SHUTDOWN_TOMCAT"
sleep 15
bash "$SHUTDOWN_TOMCAT"
sleep 10
bash "$START_TOMCAT"
set -e
fi
# Remove Datadog if Elastic Agent is healthy and REMOVE_DATADOG is true
if [ "$ELASTIC_AGENT_HEALTHY" = "true" ] && [ "$REMOVE_DATADOG" = "true" ]; then
echo "[INFO] Removing Datadog Agent."
sudo apt-get remove --purge datadog-agent -y
else
echo "[INFO] Skipping removing Datadog."
fi
echo "[INFO] Script finished successfully with no errors."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment