Last active
December 17, 2017 23:40
-
-
Save josebet1/cf359c372d2f8ec5db799e111e22531c to your computer and use it in GitHub Desktop.
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 | |
# (C) Datadog, Inc. 2010-2016 | |
# All rights reserved | |
# Licensed under Simplified BSD License (see LICENSE) | |
# Datadog Agent installation script: install and set up the Agent on supported Linux distributions | |
# using the package manager and Datadog repositories. | |
set -e | |
logfile="ddagent-install.log" | |
gist_request=/tmp/agent-gist-request.tmp | |
gist_response=/tmp/agent-gist-response.tmp | |
LEGACY_ETCDIR="/etc/dd-agent" | |
LEGACY_CONF="$LEGACY_ETCDIR/datadog.conf" | |
ETCDIR="/etc/datadog-agent" | |
CONF="$ETCDIR/datadog.yaml" | |
if [ $(command -v curl) ]; then | |
dl_cmd="curl -f" | |
else | |
dl_cmd="wget --quiet" | |
fi | |
# Set up a named pipe for logging | |
npipe=/tmp/$$.tmp | |
mknod $npipe p | |
# Log all output to a log for error checking | |
tee <$npipe $logfile & | |
exec 1>&- | |
exec 1>$npipe 2>&1 | |
trap "rm -f $npipe" EXIT | |
function on_error() { | |
printf "\033[31m$ERROR_MESSAGE | |
It looks like you hit an issue when trying to install the Agent. | |
Troubleshooting and basic usage information for the Agent are available at: | |
http://docs.datadoghq.com/guides/basic_agent_usage/ | |
If you're still having problems, please send an email to support@datadoghq.com | |
with the contents of ddagent-install.log and we'll do our very best to help you | |
solve your problem.\n\033[0m\n" | |
} | |
trap on_error ERR | |
if [ -n "$DD_HOSTNAME" ]; then | |
dd_hostname=$DD_HOSTNAME | |
fi | |
if [ -n "$DD_API_KEY" ]; then | |
apikey=$DD_API_KEY | |
fi | |
no_start= | |
if [ -n "$DD_INSTALL_ONLY" ]; then | |
no_start=true | |
fi | |
if [ -n "$DD_URL" ]; then | |
dd_url=$DD_URL | |
else | |
dd_url="datadoghq.com" | |
fi | |
dd_upgrade= | |
if [ -n "$DD_UPGRADE" ]; then | |
dd_upgrade=$DD_UPGRADE | |
fi | |
if [ ! $apikey ]; then | |
# if it's an upgrade, then we will use the transition script | |
if [ ! $dd_upgrade ]; then | |
printf "\033[31mAPI key not available in DD_API_KEY environment variable.\033[0m\n" | |
exit 1; | |
fi | |
fi | |
# OS/Distro Detection | |
# Try lsb_release, fallback with /etc/issue then uname command | |
KNOWN_DISTRIBUTION="(Debian|Ubuntu|RedHat|CentOS|openSUSE|Amazon|Arista|SUSE)" | |
DISTRIBUTION=$(lsb_release -d 2>/dev/null | grep -Eo $KNOWN_DISTRIBUTION || grep -Eo $KNOWN_DISTRIBUTION /etc/issue 2>/dev/null || grep -Eo $KNOWN_DISTRIBUTION /etc/Eos-release 2>/dev/null || uname -s) | |
if [ $DISTRIBUTION = "Darwin" ]; then | |
printf "\033[31mThis script does not support installing on the Mac. | |
Please use the 1-step script available at https://app.datadoghq.com/account/settings#agent/mac.\033[0m\n" | |
exit 1; | |
elif [ -f /etc/debian_version -o "$DISTRIBUTION" == "Debian" -o "$DISTRIBUTION" == "Ubuntu" ]; then | |
OS="Debian" | |
elif [ -f /etc/redhat-release -o "$DISTRIBUTION" == "RedHat" -o "$DISTRIBUTION" == "CentOS" -o "$DISTRIBUTION" == "Amazon" ]; then | |
OS="RedHat" | |
# Some newer distros like Amazon may not have a redhat-release file | |
elif [ -f /etc/system-release -o "$DISTRIBUTION" == "Amazon" ]; then | |
OS="RedHat" | |
# Arista is based off of Fedora14/18 but do not have /etc/redhat-release | |
elif [ -f /etc/Eos-release -o "$DISTRIBUTION" == "Arista" ]; then | |
OS="RedHat" | |
# openSUSE and SUSE use /etc/SuSE-release | |
elif [ -f /etc/SuSE-release -o "$DISTRIBUTION" == "SUSE" -o "$DISTRIBUTION" == "openSUSE" ]; then | |
OS="SUSE" | |
fi | |
# Root user detection | |
if [ $(echo "$UID") = "0" ]; then | |
sudo_cmd='' | |
else | |
sudo_cmd='sudo' | |
fi | |
# Install the necessary package sources | |
if [ $OS = "RedHat" ]; then | |
echo -e "\033[34m\n* Installing YUM sources for Datadog\n\033[0m" | |
UNAME_M=$(uname -m) | |
if [ "$UNAME_M" == "i686" -o "$UNAME_M" == "i386" -o "$UNAME_M" == "x86" ]; then | |
ARCHI="i386" | |
else | |
ARCHI="x86_64" | |
fi | |
$sudo_cmd sh -c "echo -e '[datadog]\nname = Datadog, Inc.\nbaseurl = https://yum.${dd_url}/beta/$ARCHI/\nenabled=1\ngpgcheck=1\npriority=1\ngpgkey=https://yum.${dd_url}/DATADOG_RPM_KEY.public\n https://yum.${dd_url}/DATADOG_RPM_KEY_E09422B3.public' > /etc/yum.repos.d/datadog-beta.repo" | |
printf "\033[34m* Installing the Datadog Agent package\n\033[0m\n" | |
$sudo_cmd yum -y --disablerepo='*' --enablerepo='datadog' install datadog-agent || $sudo_cmd yum -y install datadog-agent | |
elif [ $OS = "Debian" ]; then | |
printf "\033[34m\n* Installing apt-transport-https\n\033[0m\n" | |
$sudo_cmd apt-get update || printf "\033[31m'apt-get update' failed, the script will not install the latest version of apt-transport-https.\033[0m\n" | |
$sudo_cmd apt-get install -y apt-transport-https | |
# Only install dirmngr if it's available in the cache | |
# it may not be available on Ubuntu <= 14.04 but it's not required there | |
cache_output=`apt-cache search dirmngr` | |
if [ ! -z "$cache_output" ]; then | |
$sudo_cmd apt-get install -y dirmngr | |
fi | |
printf "\033[34m\n* Installing APT package sources for Datadog\n\033[0m\n" | |
$sudo_cmd sh -c "echo 'deb https://apt.${dd_url}/ beta main' > /etc/apt/sources.list.d/datadog-beta.list" | |
$sudo_cmd apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 C7A7DA52 | |
$sudo_cmd apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 382E94DE | |
printf "\033[34m\n* Installing the Datadog Agent package\n\033[0m\n" | |
ERROR_MESSAGE="ERROR | |
Failed to update the sources after adding the Datadog repository. | |
This may be due to any of the configured APT sources failing - | |
see the logs above to determine the cause. | |
If the failing repository is Datadog, please contact Datadog support. | |
***** | |
" | |
$sudo_cmd apt-get update -o Dir::Etc::sourcelist="sources.list.d/datadog-beta.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" | |
ERROR_MESSAGE="ERROR | |
Failed to install the Datadog package, sometimes it may be | |
due to another APT source failing. See the logs above to | |
determine the cause. | |
If the cause is unclear, please contact Datadog support. | |
***** | |
" | |
$sudo_cmd apt-get install -y --force-yes datadog-agent | |
ERROR_MESSAGE="" | |
elif [ $OS = "SUSE" ]; then | |
UNAME_M=$(uname -m) | |
if [ "$UNAME_M" == "i686" -o "$UNAME_M" == "i386" -o "$UNAME_M" == "x86" ]; then | |
printf "\033[31mThe Datadog Agent installer is only available for 64 bit SUSE Enterprise machines.\033[0m\n" | |
exit; | |
fi | |
echo -e "\033[34m\n* Installing YUM Repository for Datadog\n\033[0m" | |
$sudo_cmd sh -c "echo -e '[datadog]\nname=datadog\nenabled=1\nbaseurl=https://yum.${dd_url}/suse/beta/x86_64\ntype=rpm-md\ngpgcheck=1\nrepo_gpgcheck=0\ngpgkey=https://yum.${dd_url}/DATADOG_RPM_KEY.public' > /etc/zypp/repos.d/datadog.repo" | |
echo -e "\033[34m\n* Importing the Datadog GPG Key\n\033[0m" | |
$sudo_cmd rpm --import https://yum.${dd_url}/DATADOG_RPM_KEY.public | |
echo -e "\033[34m\n* Refreshing repositories\n\033[0m" | |
$sudo_cmd zypper --non-interactive --no-gpg-check refresh datadog | |
echo -e "\033[34m\n* Installing Datadog Agent\n\033[0m" | |
$sudo_cmd zypper --non-interactive install datadog-agent | |
else | |
printf "\033[31mYour OS or distribution are not supported by this install script. | |
Please follow the instructions on the Agent setup page: | |
https://app.datadoghq.com/account/settings#agent\033[0m\n" | |
exit; | |
fi | |
if [ $dd_upgrade ]; then | |
if [ -e $LEGACY_CONF ]; then | |
# try to import the config file from the previous version | |
icmd="datadog-agent import $LEGACY_ETCDIR $ETCDIR" | |
$sudo_cmd $icmd || printf "\033[31mAutomatic import failed, you can still try to manually run: $icmd\n\033[0m\n" | |
# fix file owner and permissions since the script moves around some files | |
$sudo_cmd chown -R dd-agent:dd-agent $ETCDIR | |
$sudo_cmd find $ETCDIR/ -type f -exec chmod 640 {} \; | |
else | |
printf "\033[31mYou don't have a datadog.conf file to convert.\n\033[0m\n" | |
fi | |
fi | |
# Set the configuration | |
if [ -e $CONF -a -z "$dd_upgrade" ]; then | |
printf "\033[34m\n* Keeping old datadog.yaml configuration file\n\033[0m\n" | |
else | |
if [ ! -e $CONF ]; then | |
$sudo_cmd cp $CONF.example $CONF | |
fi | |
if [ $apikey ]; then | |
printf "\033[34m\n* Adding your API key to the Agent configuration: $CONF\n\033[0m\n" | |
$sudo_cmd sh -c "sed -i 's/api_key:.*/api_key: $apikey/' $CONF" | |
else | |
# If the import script failed for any reason, we might end here also in case | |
# of upgrade, let's not start the agent or it would fail because the api key | |
# is missing | |
if ! $sudo_cmd -u dd-agent -- grep -q -E '^api_key: .+' $CONF; then | |
printf "\033[31mThe Agent won't start automatically at the end of the script because the Api key is missing, please add one in datadog.yaml and start the agent manually.\n\033[0m\n" | |
no_start=true | |
fi | |
fi | |
if [ $dd_hostname ]; then | |
printf "\033[34m\n* Adding your HOSTNAME to the Agent configuration: $CONF\n\033[0m\n" | |
$sudo_cmd sh -c "sed -i 's/# hostname:.*/hostname: $dd_hostname/' $CONF" | |
fi | |
$sudo_cmd chown dd-agent:dd-agent $CONF | |
$sudo_cmd chmod 640 $CONF | |
fi | |
# Force Sudo | |
sudo_cmd='sudo' | |
# Use systemd by default | |
restart_cmd="$sudo_cmd systemctl restart datadog-agent.service" | |
stop_instructions="$sudo_cmd systemctl stop datadog-agent" | |
start_instructions="$sudo_cmd systemctl start datadog-agent" | |
# Try to detect Upstart, this works most of the times but still a best effort | |
if /sbin/init --version 2>&1 | grep -q upstart; then | |
restart_cmd="$sudo_cmd start datadog-agent" | |
stop_instructions="$sudo_cmd stop datadog-agent" | |
start_instructions="$sudo_cmd start datadog-agent" | |
fi | |
if [ $no_start ]; then | |
printf "\033[34m | |
* DD_INSTALL_ONLY environment variable set: the newly installed version of the agent | |
will not be started. You will have to do it manually using the following | |
command: | |
$restart_cmd | |
\033[0m\n" | |
exit | |
fi | |
printf "\033[34m* Starting the Agent...\n\033[0m\n" | |
echo $restart_cmd | |
eval $restart_cmd | |
# Metrics are submitted, echo some instructions and exit | |
printf "\033[32m | |
Your Agent is running and functioning properly. It will continue to run in the | |
background and submit metrics to Datadog. | |
If you ever want to stop the Agent, run: | |
$stop_instructions | |
And to run it again run: | |
$start_instructions | |
\033[0m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment