|
| 1 | +#!/bin/bash |
| 2 | +# (C) Datadog, Inc. 2010-2016 |
| 3 | +# All rights reserved |
| 4 | +# Licensed under Simplified BSD License (see LICENSE) |
| 5 | +# Datadog Agent installation script: install and set up the Agent on supported Linux distributions |
| 6 | +# using the package manager and Datadog repositories. |
| 7 | + |
| 8 | +set -e |
| 9 | +logfile="ddagent-install.log" |
| 10 | +gist_request=/tmp/agent-gist-request.tmp |
| 11 | +gist_response=/tmp/agent-gist-response.tmp |
| 12 | + |
| 13 | +if [ $(command -v curl) ]; then |
| 14 | + dl_cmd="curl -f" |
| 15 | +else |
| 16 | + dl_cmd="wget --quiet" |
| 17 | +fi |
| 18 | + |
| 19 | +# Set up a named pipe for logging |
| 20 | +npipe=/tmp/$$.tmp |
| 21 | +mknod $npipe p |
| 22 | + |
| 23 | +# Log all output to a log for error checking |
| 24 | +tee <$npipe $logfile & |
| 25 | +exec 1>&- |
| 26 | +exec 1>$npipe 2>&1 |
| 27 | +trap "rm -f $npipe" EXIT |
| 28 | + |
| 29 | + |
| 30 | +function on_error() { |
| 31 | + printf "\033[31m$ERROR_MESSAGE |
| 32 | +It looks like you hit an issue when trying to install the Agent. |
| 33 | +
|
| 34 | +Troubleshooting and basic usage information for the Agent are available at: |
| 35 | +
|
| 36 | + http://docs.datadoghq.com/guides/basic_agent_usage/ |
| 37 | +
|
| 38 | +If you're still having problems, please send an email to support@datadoghq.com |
| 39 | +with the contents of ddagent-install.log and we'll do our very best to help you |
| 40 | +solve your problem.\n\033[0m\n" |
| 41 | +} |
| 42 | +trap on_error ERR |
| 43 | + |
| 44 | +if [ -n "$DD_HOSTNAME" ]; then |
| 45 | + dd_hostname=$DD_HOSTNAME |
| 46 | +fi |
| 47 | + |
| 48 | +if [ -n "$DD_API_KEY" ]; then |
| 49 | + apikey=$DD_API_KEY |
| 50 | +fi |
| 51 | + |
| 52 | +if [ -n "$DD_INSTALL_ONLY" ]; then |
| 53 | + no_start=true |
| 54 | +else |
| 55 | + no_start=false |
| 56 | +fi |
| 57 | + |
| 58 | +if [ ! $apikey ]; then |
| 59 | + printf "\033[31mAPI key not available in DD_API_KEY environment variable.\033[0m\n" |
| 60 | + exit 1; |
| 61 | +fi |
| 62 | + |
| 63 | +# OS/Distro Detection |
| 64 | +# Try lsb_release, fallback with /etc/issue then uname command |
| 65 | +KNOWN_DISTRIBUTION="(Debian|Ubuntu|RedHat|CentOS|openSUSE|Amazon|Arista|SUSE)" |
| 66 | +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) |
| 67 | + |
| 68 | +if [ $DISTRIBUTION = "Darwin" ]; then |
| 69 | + printf "\033[31mThis script does not support installing on the Mac. |
| 70 | +
|
| 71 | +Please use the 1-step script available at https://app.datadoghq.com/account/settings#agent/mac.\033[0m\n" |
| 72 | + exit 1; |
| 73 | + |
| 74 | +elif [ -f /etc/debian_version -o "$DISTRIBUTION" == "Debian" -o "$DISTRIBUTION" == "Ubuntu" ]; then |
| 75 | + OS="Debian" |
| 76 | +elif [ -f /etc/redhat-release -o "$DISTRIBUTION" == "RedHat" -o "$DISTRIBUTION" == "CentOS" -o "$DISTRIBUTION" == "Amazon" ]; then |
| 77 | + OS="RedHat" |
| 78 | +# Some newer distros like Amazon may not have a redhat-release file |
| 79 | +elif [ -f /etc/system-release -o "$DISTRIBUTION" == "Amazon" ]; then |
| 80 | + OS="RedHat" |
| 81 | +# Arista is based off of Fedora14/18 but do not have /etc/redhat-release |
| 82 | +elif [ -f /etc/Eos-release -o "$DISTRIBUTION" == "Arista" ]; then |
| 83 | + OS="RedHat" |
| 84 | +# openSUSE and SUSE use /etc/SuSE-release |
| 85 | +elif [ -f /etc/SuSE-release -o "$DISTRIBUTION" == "SUSE" -o "$DISTRIBUTION" == "openSUSE" ]; then |
| 86 | + OS="SUSE" |
| 87 | +fi |
| 88 | + |
| 89 | +# Root user detection |
| 90 | +if [ $(echo "$UID") = "0" ]; then |
| 91 | + sudo_cmd='' |
| 92 | +else |
| 93 | + sudo_cmd='sudo' |
| 94 | +fi |
| 95 | + |
| 96 | +DDBASE=false |
| 97 | +# Python Detection |
| 98 | +has_python=$(which python || echo "no") |
| 99 | +if [ "$has_python" != "no" ]; then |
| 100 | + PY_VERSION=$(python -c 'import sys; print("{0}.{1}".format(sys.version_info[0], sys.version_info[1]))' 2>/dev/null || echo "TOO OLD") |
| 101 | + if [ "$PY_VERSION" = "TOO OLD" ]; then |
| 102 | + DDBASE=true |
| 103 | + fi |
| 104 | +fi |
| 105 | + |
| 106 | +# Install the necessary package sources |
| 107 | +if [ $OS = "RedHat" ]; then |
| 108 | + echo -e "\033[34m\n* Installing YUM sources for Datadog\n\033[0m" |
| 109 | + |
| 110 | + UNAME_M=$(uname -m) |
| 111 | + if [ "$UNAME_M" == "i686" -o "$UNAME_M" == "i386" -o "$UNAME_M" == "x86" ]; then |
| 112 | + ARCHI="i386" |
| 113 | + else |
| 114 | + ARCHI="x86_64" |
| 115 | + fi |
| 116 | + |
| 117 | + # Versions of yum on RedHat 5 and lower embed M2Crypto with SSL that doesn't support TLS1.2 |
| 118 | + if [ -f /etc/redhat-release ]; then |
| 119 | + REDHAT_MAJOR_VERSION=$(grep -Eo "[0-9].[0-9]{1,2}" /etc/redhat-release | head -c 1) |
| 120 | + fi |
| 121 | + if [ -n "$REDHAT_MAJOR_VERSION" ] && [ "$REDHAT_MAJOR_VERSION" -le "5" ]; then |
| 122 | + PROTOCOL="http" |
| 123 | + else |
| 124 | + PROTOCOL="https" |
| 125 | + fi |
| 126 | + $sudo_cmd sh -c "echo -e '[datadog]\nname = Datadog, Inc.\nbaseurl = $PROTOCOL://yum.datadoghq.com/rpm/$ARCHI/\nenabled=1\ngpgcheck=1\npriority=1\ngpgkey=$PROTOCOL://yum.datadoghq.com/DATADOG_RPM_KEY.public\n $PROTOCOL://yum.datadoghq.com/DATADOG_RPM_KEY_E09422B3.public' > /etc/yum.repos.d/datadog.repo" |
| 127 | + |
| 128 | + printf "\033[34m* Installing the Datadog Agent package\n\033[0m\n" |
| 129 | + |
| 130 | + if $DDBASE; then |
| 131 | + DD_BASE_INSTALLED=$(yum list installed datadog-agent-base > /dev/null 2>&1 || echo "no") |
| 132 | + if [ "$DD_BASE_INSTALLED" != "no" ]; then |
| 133 | + echo -e "\033[34m\n* Uninstall datadog-agent-base\n\033[0m" |
| 134 | + $sudo_cmd yum -y remove datadog-agent-base |
| 135 | + fi |
| 136 | + fi |
| 137 | + $sudo_cmd yum -y --disablerepo='*' --enablerepo='datadog' install datadog-agent || $sudo_cmd yum -y install datadog-agent |
| 138 | +elif [ $OS = "Debian" ]; then |
| 139 | + printf "\033[34m\n* Installing apt-transport-https\n\033[0m\n" |
| 140 | + $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" |
| 141 | + $sudo_cmd apt-get install -y apt-transport-https |
| 142 | + # Only install dirmngr if it's available in the cache |
| 143 | + # it may not be available on Ubuntu <= 14.04 but it's not required there |
| 144 | + cache_output=`apt-cache search dirmngr` |
| 145 | + if [ ! -z "$cache_output" ]; then |
| 146 | + $sudo_cmd apt-get install -y dirmngr |
| 147 | + fi |
| 148 | + printf "\033[34m\n* Installing APT package sources for Datadog\n\033[0m\n" |
| 149 | + $sudo_cmd sh -c "echo 'deb https://apt.datadoghq.com/ stable main' > /etc/apt/sources.list.d/datadog.list" |
| 150 | + $sudo_cmd apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 C7A7DA52 |
| 151 | + $sudo_cmd apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 382E94DE |
| 152 | + |
| 153 | + printf "\033[34m\n* Installing the Datadog Agent package\n\033[0m\n" |
| 154 | + ERROR_MESSAGE="ERROR |
| 155 | +Failed to update the sources after adding the Datadog repository. |
| 156 | +This may be due to any of the configured APT sources failing - |
| 157 | +see the logs above to determine the cause. |
| 158 | +If the failing repository is Datadog, please contact Datadog support. |
| 159 | +***** |
| 160 | +" |
| 161 | + $sudo_cmd apt-get update -o Dir::Etc::sourcelist="sources.list.d/datadog.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0" |
| 162 | + ERROR_MESSAGE="ERROR |
| 163 | +Failed to install the Datadog package, sometimes it may be |
| 164 | +due to another APT source failing. See the logs above to |
| 165 | +determine the cause. |
| 166 | +If the cause is unclear, please contact Datadog support. |
| 167 | +***** |
| 168 | +" |
| 169 | + $sudo_cmd apt-get install -y --force-yes datadog-agent |
| 170 | + ERROR_MESSAGE="" |
| 171 | +elif [ $OS = "SUSE" ]; then |
| 172 | + UNAME_M=$(uname -m) |
| 173 | + if [ "$UNAME_M" == "i686" -o "$UNAME_M" == "i386" -o "$UNAME_M" == "x86" ]; then |
| 174 | + printf "\033[31mThe Datadog Agent installer is only available for 64 bit SUSE Enterprise machines.\033[0m\n" |
| 175 | + exit; |
| 176 | + fi |
| 177 | + |
| 178 | + echo -e "\033[34m\n* Installing YUM Repository for Datadog\n\033[0m" |
| 179 | + $sudo_cmd sh -c "echo -e '[datadog]\nname=datadog\nenabled=1\nbaseurl=https://yum.datadoghq.com/suse/rpm/x86_64\ntype=rpm-md\ngpgcheck=1\nrepo_gpgcheck=0\ngpgkey=https://yum.datadoghq.com/DATADOG_RPM_KEY.public' > /etc/zypp/repos.d/datadog.repo" |
| 180 | + |
| 181 | + echo -e "\033[34m\n* Refreshing repositories\n\033[0m" |
| 182 | + $sudo_cmd zypper --non-interactive --no-gpg-check refresh datadog |
| 183 | + |
| 184 | + echo -e "\033[34m\n* Installing Datadog Agent\n\033[0m" |
| 185 | + $sudo_cmd zypper --non-interactive install datadog-agent |
| 186 | + |
| 187 | +else |
| 188 | + printf "\033[31mYour OS or distribution are not supported by this install script. |
| 189 | +Please follow the instructions on the Agent setup page: |
| 190 | +
|
| 191 | + https://app.datadoghq.com/account/settings#agent\033[0m\n" |
| 192 | + exit; |
| 193 | +fi |
| 194 | + |
| 195 | +# Set the configuration |
| 196 | +if [ -e /etc/dd-agent/datadog.conf ]; then |
| 197 | + printf "\033[34m\n* Keeping old datadog.conf configuration file\n\033[0m\n" |
| 198 | +else |
| 199 | + printf "\033[34m\n* Adding your API key to the Agent configuration: /etc/dd-agent/datadog.conf\n\033[0m\n" |
| 200 | + $sudo_cmd sh -c "sed 's/api_key:.*/api_key: $apikey/' /etc/dd-agent/datadog.conf.example > /etc/dd-agent/datadog.conf" |
| 201 | + if [ $dd_hostname ]; then |
| 202 | + printf "\033[34m\n* Adding your HOSTNAME to the Agent configuration: /etc/dd-agent/datadog.conf\n\033[0m\n" |
| 203 | + $sudo_cmd sh -c "sed -i 's/# hostname:.*/hostname: $dd_hostname/' /etc/dd-agent/datadog.conf" |
| 204 | + fi |
| 205 | + $sudo_cmd chown dd-agent:dd-agent /etc/dd-agent/datadog.conf |
| 206 | + $sudo_cmd chmod 640 /etc/dd-agent/datadog.conf |
| 207 | +fi |
| 208 | + |
| 209 | +restart_cmd="$sudo_cmd /etc/init.d/datadog-agent restart" |
| 210 | +if command -v invoke-rc.d >/dev/null 2>&1; then |
| 211 | + restart_cmd="$sudo_cmd invoke-rc.d datadog-agent restart" |
| 212 | +fi |
| 213 | + |
| 214 | +if $no_start; then |
| 215 | + printf "\033[34m |
| 216 | +* DD_INSTALL_ONLY environment variable set: the newly installed version of the agent |
| 217 | +will not start by itself. You will have to do it manually using the following |
| 218 | +command: |
| 219 | +
|
| 220 | + $restart_cmd |
| 221 | +
|
| 222 | +\033[0m\n" |
| 223 | + exit |
| 224 | +fi |
| 225 | + |
| 226 | +printf "\033[34m* Starting the Agent...\n\033[0m\n" |
| 227 | +eval $restart_cmd |
| 228 | + |
| 229 | +# Wait for metrics to be submitted by the forwarder |
| 230 | +printf "\033[32m |
| 231 | +Your Agent has started up for the first time. We're currently verifying that |
| 232 | +data is being submitted. You should see your Agent show up in Datadog shortly |
| 233 | +at: |
| 234 | +
|
| 235 | + https://app.datadoghq.com/infrastructure\033[0m |
| 236 | +
|
| 237 | +Waiting for metrics..." |
| 238 | + |
| 239 | +c=0 |
| 240 | +while [ "$c" -lt "30" ]; do |
| 241 | + sleep 1 |
| 242 | + echo -n "." |
| 243 | + c=$(($c+1)) |
| 244 | +done |
| 245 | + |
| 246 | +# Reuse the same counter |
| 247 | +c=0 |
| 248 | + |
| 249 | +# The command to check the status of the forwarder might fail at first, this is expected |
| 250 | +# so we remove the trap and we set +e |
| 251 | +set +e |
| 252 | +trap - ERR |
| 253 | + |
| 254 | +$dl_cmd http://127.0.0.1:17123/status?threshold=0 > /dev/null 2>&1 |
| 255 | +success=$? |
| 256 | +while [ "$success" -gt "0" ]; do |
| 257 | + sleep 1 |
| 258 | + echo -n "." |
| 259 | + $dl_cmd http://127.0.0.1:17123/status?threshold=0 > /dev/null 2>&1 |
| 260 | + success=$? |
| 261 | + c=$(($c+1)) |
| 262 | + |
| 263 | + if [ "$c" -gt "15" -o "$success" -eq "0" ]; then |
| 264 | + # After 15 tries, we give up, we restore the trap and set -e |
| 265 | + # Also restore the trap on success |
| 266 | + set -e |
| 267 | + trap on_error ERR |
| 268 | + fi |
| 269 | +done |
| 270 | + |
| 271 | +# Metrics are submitted, echo some instructions and exit |
| 272 | +printf "\033[32m |
| 273 | +
|
| 274 | +Your Agent is running and functioning properly. It will continue to run in the |
| 275 | +background and submit metrics to Datadog. |
| 276 | +
|
| 277 | +If you ever want to stop the Agent, run: |
| 278 | +
|
| 279 | + sudo /etc/init.d/datadog-agent stop |
| 280 | +
|
| 281 | +And to run it again run: |
| 282 | +
|
| 283 | + sudo /etc/init.d/datadog-agent start |
| 284 | +
|
| 285 | +\033[0m" |
0 commit comments