#!/bin/bash # CiAgent install script. set -e logfile="CiAgent-install.log" HOSTNAME=`hostname -f` HOSTNAME_DENY="localhost localhost.localdomain localhost6.localdomain6 ip6-localhost" gist_request=/tmp/agent-gist-request.tmp gist_response=/tmp/agent-gist-response.tmp if [ $(command -v curl) ]; then cl_cmd="curl -f" else cl_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://support.aiops.com/hc/ If you're still having problems, please contact to support@aiops.com and we'll try our best to help you solve your problem.\n\033[0m\n" } trap on_error ERR if [ -n "$CI_LICENSE_KEY" ]; then license_key=$CI_LICENSE_KEY fi if [ -n "$CI_INSTALL_ONLY" ]; then no_start=true else no_start=false fi if [ -n "$CI_HOSTNAME" ]; then for j in `echo ${HOSTNAME_DENY}`; do if [[ $CI_HOSTNAME == "$j" ]]; then printf "\033[31mCI_HOSTNAME is using in local, please use CI_HOSTNAME and ensure that it isn't localhost, or just modify your hostname. \033[0m\n" exit 1 fi done ci_hostname=$CI_HOSTNAME elif [[ $HOSTNAME =~ " " ]]; then printf "\033[31mHostname contains spaces, which will cause the data transfer to fail. Please use CI_HOSTNAME and ensure that it doesn't contain space or just modify your hostname. \033[0m\n" exit 1 else for i in `echo ${HOSTNAME_DENY}`; do if [[ $HOSTNAME == "$i" ]]; then printf "\033[31mYour hostname is using in local, please use CI_HOSTNAME and ensure that it isn't localhost, or just modify your hostname. \033[0m\n" exit 1 fi done fi if [ -n "$CI_HOST_TAG" ]; then ci_host_tag=$CI_HOST_TAG fi if [ ! $license_key ]; then printf "\033[31mLicense key not available in CI_LICENSE_KEY environment variable.\033[0m\n" exit 1; fi # OS/Distro Detection # Try lsb_release, fallback with /etc/issue then uname command KNOWN_DISTRIBUTION="(Debian|Ubuntu|RedHat|CentOS|Amazon)" DISTRIBUTION=$(lsb_release -d 2>/dev/null | grep -Eo $KNOWN_DISTRIBUTION || grep -Eo $KNOWN_DISTRIBUTION /etc/issue 2>/dev/null || uname -s) if [ $DISTRIBUTION = "Darwin" ]; then printf "\033[31mThis script does not support installing on the 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" elif [ -f /etc/system-release -o "$DISTRIBUTION" == "Amazon" ]; then OS="RedHat" fi # Root user detection if [ $(echo "$UID") = "0" ]; then sudo_cmd='' else sudo_cmd='sudo' fi function backup_old_configuration { if [[ ! -d /etc/CiAgent_bak ]]; then # Backup configuration does not exist, move legacy configuration to backup location echo -e "\033[34m\n* CiAgent's configuration is now located at '/etc/CiAgent'\033[0m" $sudo_cmd mkdir /etc/CiAgent_bak $sudo_cmd cp -r /etc/CiAgent/conf.d /etc/CiAgent_bak/ echo "\033[34m\n* A backup of your current configuration can be found at: /etc/CiAgent_bak/CiAgent.conf.bak\033[0m" $sudo_cmd cp -a /etc/CiAgent/CiAgent.conf /etc/CiAgent_bak/CiAgent.conf.bak fi } # Install the necessary package sources if [ $OS = "RedHat" ]; then UNAME_M=$(uname -m) if [ "$UNAME_M" == "i686" -o "$UNAME_M" == "i386" -o "$UNAME_M" == "x86" ]; then ARCHI="i386" else ARCHI="x86_64" fi printf "\033[34m* Installing the CiAgent package\n\033[0m\n" CI_OLD_INSTALLED=$($sudo_cmd rpm -qa | grep CiAgent > /dev/null 2>&1 || echo "no") if [ "$CI_OLD_INSTALLED" != "no" ]; then echo -e "\033[34m\n* Please note, You have installed CiAgent.\n\033[0m" backup_old_configuration echo -e "\033[34m\n* Uninstall CiAgent\n\033[0m" $sudo_cmd rpm -e CiAgent fi $sudo_cmd python2 -V > /dev/null 2>&1 if [ $? -ne 0 ];then $sudo_cmd yum install -y python echo -e "\033[34m\n* Installing Python for CiAgent\n\033[0m" fi CI_SYSSTAT=$($sudo_cmd rpm -qa | grep sysstat > /dev/null 2>&1 || echo "no") if [ "$CI_SYSSTAT" == "no" ]; then echo -e "\033[34m\n* Installing sysstat for CiAgent\n\033[0m" $sudo_cmd yum install -y sysstat fi echo -e "\033[34m\n* Installing RPM for CiAgent\n\033[0m" PY_V=`python --version 2>&1 |awk '{print $2}' | awk -F '.' '{print $2;}'` if [ "$PY_V" == "7" ]; then $sudo_cmd rpm -ivh https://download.aiops.com/ci_agent/CiAgent-4.7.7-1-py27.x86_64.rpm elif [ "$PY_V" == "6" ]; then $sudo_cmd rpm -ivh https://download.aiops.com/ci_agent/CiAgent-4.7.7-1-py26.x86_64.rpm fi elif [ $OS = "Debian" ]; then echo -e "\033[34m\n* Installing RPM for CiAgent\n\033[0m" printf "\033[34m\n* Installing the CiAgent package\n\033[0m\n" ERROR_MESSAGE="ERROR Failed to update the package after adding the CiAgent repository. This may be due to any of the configured DEB package failing - see the logs above to determine the cause. If the failing repository is CiAgent, please contact Cloudinsight support. ***** " ERROR_MESSAGE="ERROR Failed to install the CiAgent package, sometimes it may be due to another DEB source failing. See the logs above to determine the cause. If the cause is unclear, please contact CiAgent support. ***** " CI_OLD_INSTALLED=$($sudo_cmd dpkg -l | grep ciagent > /dev/null 2>&1 || echo "no") if [ "$CI_OLD_INSTALLED" != "no" ]; then echo -e "\033[34m\n* Please note, You have installed CiAgent.\n\033[0m" backup_old_configuration echo -e "\033[34m\n* Uninstall CiAgent\n\033[0m" $sudo_cmd apt-get purge -y ciagent fi CI_SYSSTAT=$($sudo_cmd dpkg -l | grep sysstat > /dev/null 2>&1 || echo "no") if [ "$CI_SYSSTAT" == "no" ]; then echo -e "\033[34m\n* Installing sysstat for CiAgent.\n\033[0m" $sudo_cmd apt-get install -y sysstat fi echo -e "\033[34m\n* Installing Python for CiAgent\n\033[0m" $sudo_cmd apt-get install -y python echo -e "\033[34m\n* Installing DEB for CiAgent\n\033[0m" $sudo_cmd curl -LO https://download.aiops.com/ci_agent/CiAgent_4.7.7_amd64.deb $sudo_cmd dpkg -i ./CiAgent_4.7.7_amd64.deb $sudo_cmd rm -rf ./CiAgent_4.7.7_amd64.deb ERROR_MESSAGE="" else printf "\033[31mYour OS or distribution are not supported by this install script. Please follow the instructions on the Agent setup page.\033[0m\n" exit; fi # Set the configuration if [ -e /etc/CiAgent/CiAgent.conf ]; then printf "\033[34m\n* Adding your LICENSE KEY to the Agent configuration: /etc/CiAgent/CiAgent.conf\n\033[0m\n" $sudo_cmd sed -i "s/license_key:.*/license_key: $license_key/" /etc/CiAgent/CiAgent.conf if [ $ci_hostname ]; then printf "\033[34m\n* Adding your HOSTNAME to the Agent configuration: /etc/CiAgent/CiAgent.conf\n\033[0m\n" $sudo_cmd sed -i "s/# hostname:.*/hostname: $ci_hostname/" /etc/CiAgent/CiAgent.conf fi if [ $ci_host_tag ]; then printf "\033[34m\n* Adding your HOST TAGS to the Agent configuration: /etc/CiAgent/CiAgent.conf\n\033[0m\n" $sudo_cmd sed -i "s/# tags:.*/tags: $ci_host_tag/" /etc/CiAgent/CiAgent.conf fi fi $sudo_cmd chmod -Rf 777 /etc/init.d/CiAgent restart_cmd="$sudo_cmd /etc/init.d/CiAgent restart" #if command -v invoke-rc.d >/dev/null 2>&1; then # restart_cmd="$sudo_cmd invoke-rc.d CiAgent restart" #fi if $no_start; then printf "\033[34m * CI_INSTALL_ONLY environment variable set: the newly installed version of the agent will not start by itself. 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" eval $restart_cmd # Wait for metrics to be submitted by the forwarder printf "\033[32m CiAgent has started up for the first time. We're currently verifying that data is being submitted.\033[0m Waiting for metrics..." c=0 while [ "$c" -lt "15" ]; do sleep 1 echo -n "." c=$(($c+1)) done # Reuse the same counter c=0 # The command to check the status of the forwarder might fail at first, this is expected # so we remove the trap and we set +e set +e trap - ERR $cl_cmd http://127.0.0.1:40012/status?threshold=0 > /dev/null 2>&1 success=$? while [ "$success" -gt "0" ]; do sleep 1 echo -n "." $cl_cmd http://127.0.0.1:40012/status?threshold=0 > /dev/null 2>&1 success=$? c=$(($c+1)) if [ "$c" -gt "30" -o "$success" -eq "0" ]; then # After 30 tries, we give up, we restore the trap and set -e # Also restore the trap on success set -e trap on_error ERR fi done # Metrics are submitted, echo some instructions and exit printf "\033[32m CiAgent is running and functioning properly. It will continue to run in the background and submit metrics to Cloudinsight. If you ever want to stop the Agent, run: sudo /etc/init.d/CiAgent stop And to run it again run: sudo /etc/init.d/CiAgent start \033[0m"