-
-
Save kyletaylored/4a53fc4b2cf626eeeca19ee819cf1770 to your computer and use it in GitHub Desktop.
Datadog Install for Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
####################################### | |
# Datadog Agent Installation Script | |
####################################### | |
# | |
# Description: | |
# This script automates the installation and configuration of the Datadog Agent | |
# on Linux systems. It validates the API key, automatically detects the correct | |
# site, and configures various Datadog features. | |
# | |
# Features: | |
# - API key validation and automatic site detection | |
# - Interactive and non-interactive modes | |
# - Command-line argument support | |
# - Environment variable detection (DD_API_KEY) | |
# - Automatic configuration of Datadog features | |
# | |
# Usage: | |
# Interactive mode: | |
# sudo ./datadog_install.sh | |
# | |
# Non-interactive mode: | |
# sudo ./datadog_install.sh -a YOUR_API_KEY | |
# | |
# Using environment variable: | |
# DD_API_KEY=YOUR_API_KEY sudo ./datadog_install.sh | |
# | |
# Curl execution: | |
# curl -L https://your-gist-url.com | sudo bash | |
# | |
# Options: | |
# -a, --api-key API_KEY Set Datadog API key | |
# -h, --help Display help message | |
# -v, --verbose Enable verbose output | |
# | |
# Environment Variables: | |
# DD_API_KEY Datadog API key (takes precedence over command-line option) | |
# | |
# Notes: | |
# - Requires root/sudo privileges for installation and configuration | |
# - Automatically detects the correct Datadog site based on the API key | |
# - Restarts the Datadog Agent after configuration | |
# - Displays the agent status at the end of installation | |
# | |
# Author: [Your Name] | |
# Last Updated: [Current Date] | |
# Version: 1.4 | |
# | |
####################################### | |
# Enable exit on error | |
set -e | |
# Color codes | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[0;33m' | |
BLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
# Function to print colored output | |
print_color() { | |
local color="$1" | |
local message="$2" | |
echo -e "${color}${message}${NC}" | |
} | |
# Function to print error messages | |
error() { | |
print_color "$RED" "ERROR: $1" | |
} | |
# Function to print success messages | |
success() { | |
print_color "$GREEN" "SUCCESS: $1" | |
} | |
# Function to print warning messages | |
warning() { | |
print_color "$YELLOW" "WARNING: $1" | |
} | |
# Function to print info messages | |
info() { | |
print_color "$BLUE" "INFO: $1" | |
} | |
# Check for root/sudo privileges | |
if [ "$EUID" -ne 0 ]; then | |
error "This script must be run with root/sudo privileges." | |
exit 1 | |
fi | |
# Initialize verbose mode | |
VERBOSE=false | |
# Function to print verbose messages | |
verbose() { | |
if [ "$VERBOSE" = true ]; then | |
info "[VERBOSE] $1" | |
fi | |
} | |
# Function to check if script is being piped | |
is_pipe() { | |
[ -p /dev/stdin ] | |
} | |
# Function to get input (handles both piped and interactive input) | |
get_input() { | |
local prompt="$1" | |
local default="$2" | |
local result | |
if is_pipe; then | |
read -t 1 result | |
if [ -z "$result" ]; then | |
result="$default" | |
fi | |
else | |
read -p "$prompt" result | |
result="${result:-$default}" | |
fi | |
echo "$result" | |
} | |
# Function to display script usage | |
usage() { | |
echo "Usage: $0 [OPTIONS]" | |
echo "Options:" | |
echo " -a, --api-key API_KEY Set Datadog API key" | |
echo " -h, --help Display this help message" | |
echo " -v, --verbose Enable verbose output" | |
exit 1 | |
} | |
# Parse command line arguments | |
while [[ $# -gt 0 ]]; do | |
case "$1" in | |
-a|--api-key) | |
CLI_API_KEY="$2" | |
shift 2 | |
;; | |
-h|--help) | |
usage | |
;; | |
-v|--verbose) | |
VERBOSE=true | |
shift | |
;; | |
*) | |
error "Unknown option: $1" | |
usage | |
;; | |
esac | |
done | |
# Use DD_API_KEY environment variable if set, otherwise use CLI argument | |
DD_API_KEY="${DD_API_KEY:-$CLI_API_KEY}" | |
verbose "Checking for API key..." | |
# Function to prompt for API key | |
prompt_api_key() { | |
while true; do | |
DD_API_KEY=$(get_input "Enter your Datadog API key: " "") | |
if [[ -n "$DD_API_KEY" ]]; then | |
break | |
else | |
error "Please enter a valid Datadog API key." | |
fi | |
done | |
} | |
# Function to validate API key and detect site | |
validate_api_key_and_detect_site() { | |
local api_key="$1" | |
local sites=("datadoghq.com" "us3.datadoghq.com" "us5.datadoghq.com" "datadoghq.eu" "ap1.datadoghq.com") | |
for site in "${sites[@]}"; do | |
verbose "Trying site: $site" | |
local url="https://api.$site/api/v1/validate" | |
local response | |
response=$(curl -s -o /dev/null -w "%{http_code}" -H "DD-API-KEY: $api_key" "$url") | |
if [ "$response" = "200" ]; then | |
local json_response | |
json_response=$(curl -s -H "DD-API-KEY: $api_key" "$url") | |
if [[ $json_response == *'"valid":true'* ]]; then | |
DD_SITE="$site" | |
success "API key is valid. Detected site: $DD_SITE" | |
return 0 | |
fi | |
fi | |
done | |
error "Invalid API key or unable to detect site." | |
return 1 | |
} | |
# Prompt for API key if not provided | |
if [ -z "$DD_API_KEY" ]; then | |
if is_pipe; then | |
error "Datadog API key is required in non-interactive mode. Use -a option or DD_API_KEY environment variable." | |
exit 1 | |
else | |
warning "Datadog API key not provided." | |
prompt_api_key | |
fi | |
fi | |
# Validate API key and detect site | |
if ! validate_api_key_and_detect_site "$DD_API_KEY"; then | |
if is_pipe; then | |
exit 1 | |
else | |
warning "Invalid API key. Please try again." | |
prompt_api_key | |
validate_api_key_and_detect_site "$DD_API_KEY" || exit 1 | |
fi | |
fi | |
verbose "API Key: ${DD_API_KEY}" | |
verbose "Site: ${DD_SITE}" | |
# Export variables | |
export DD_API_KEY DD_SITE | |
# Enable APM Auto Instrumentation | |
export DD_APM_INSTRUMENTATION_ENABLED=host | |
# Start the script | |
info "Datadog agent installation script execution initiated." | |
# Execute the installation command | |
verbose "Running Datadog installation script..." | |
bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script_agent7.sh)" | |
# Enable Remote Configuration | |
verbose "Enabling Remote Configuration..." | |
sudo tee -a /etc/datadog-agent/datadog.yaml <<EOF | |
remote_configuration: | |
enabled: true | |
EOF | |
# Enable Live Processes | |
verbose "Enabling Live Processes..." | |
sudo tee -a /etc/datadog-agent/datadog.yaml <<EOF | |
process_config: | |
process_collection: | |
enabled: true | |
EOF | |
# Enable Logs | |
verbose "Enabling Logs..." | |
sudo tee -a /etc/datadog-agent/datadog.yaml <<EOF | |
logs_enabled: true | |
EOF | |
# Enable I/O Stats | |
verbose "Enabling I/O Stats..." | |
sudo -u dd-agent install -m 0640 /etc/datadog-agent/system-probe.yaml.example /etc/datadog-agent/system-probe.yaml | |
sudo tee -a /etc/datadog-agent/system-probe.yaml <<EOF | |
system_probe_config: | |
process_config: | |
enabled: true | |
EOF | |
# Enable NPM | |
verbose "Enabling Network Performance Monitoring..." | |
sudo tee -a /etc/datadog-agent/system-probe.yaml <<EOF | |
network_config: | |
enabled: true | |
EOF | |
# Enable USM | |
verbose "Enabling Universal Service Monitoring..." | |
sudo tee -a /etc/datadog-agent/system-probe.yaml <<EOF | |
service_monitoring_config: | |
enabled: true | |
process_service_inference: | |
enabled: true | |
EOF | |
# Function to check system probe status | |
check_system_probe() { | |
local status | |
status=$(sudo datadog-agent status | awk '/System Probe/{flag=1; next} /^$/{flag=0} flag && /Status:/{print $2; exit}') | |
if [ "$status" = "Running" ]; then | |
success "System Probe is running." | |
return 0 | |
else | |
warning "System Probe is not running. Current status: $status" | |
return 1 | |
fi | |
} | |
# Function to wait for system probe with timeout | |
wait_for_system_probe() { | |
local timeout=120 | |
local start_time=$(date +%s) | |
local end_time=$((start_time + timeout)) | |
info "Waiting for System Probe to start (timeout: ${timeout}s)..." | |
while [ $(date +%s) -lt $end_time ]; do | |
if check_system_probe; then | |
success "System Probe started successfully." | |
return 0 | |
fi | |
sleep 5 | |
done | |
error "Timeout: System Probe did not start within ${timeout} seconds." | |
return 1 | |
} | |
# Function to check datadog-agent service status | |
check_datadog_agent() { | |
if systemctl is-active --quiet datadog-agent; then | |
success "datadog-agent service is running." | |
return 0 | |
else | |
error "datadog-agent service is not running." | |
return 1 | |
fi | |
} | |
# Restart the agent to apply changes | |
info "Applying changes and restarting Datadog agent..." | |
sudo systemctl restart datadog-agent | |
# Check datadog-agent service | |
info "Checking Datadog agent service status..." | |
if ! check_datadog_agent; then | |
error "datadog-agent service failed to start." | |
exit 1 | |
fi | |
# Wait for System Probe to start | |
if ! wait_for_system_probe; then | |
warning "System Probe did not start properly." | |
fi | |
# Show the Status of the Agent | |
info "Datadog Agent Status:" | |
sudo datadog-agent status | |
# Final status check | |
if check_datadog_agent && check_system_probe; then | |
success "Datadog agent installation and configuration completed successfully!" | |
exit 0 | |
else | |
error "Datadog agent installation encountered issues. Please check the status output above and consult the Datadog documentation for troubleshooting." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment