Skip to content

Instantly share code, notes, and snippets.

@theletterf
Created Apr 13, 2020
Embed
What would you like to do?
#!/usr/bin/env bash
set -e
echo "
+--------------------------------------------+
| |
| New Relic Infrastructure Agent Installer |
| |
| Version 0.1 |
| |
+--------------------------------------------+
"
# Request the New Relic license key
while true; do
read -p "Supply your New Relic license key: " license_key
# Check for a 40-character hexadecimal string
if ! [[ $license_key =~ ^[a-zA-Z0-9]{40}$ ]]
then
printf "\nKey format is not valid. Please enter your license key.\n\n"
else
break
fi
done
# Let the user select the installation mode
while true; do
read -p "Select the installation mode: [R]oot (Default), [P]rivileged, [U]nprivileged: " install_mode
if ! [[ $install_mode =~ ^([pPuUrR]{1})$ ]]
then
printf "\nInvalid install option.\n\n"
else
case $install_mode in
[pP])
dependencies="sudo apt-get install libcap2-bin"
mode="NRIA_MODE=\"PRIVILEGED\""
break
;;
[uU])
dependencies="sudo apt-get install libcap2-bin"
mode="NRIA_MODE=\"UNPRIVILEGED\""
break
;;
[rR])
dependencies="echo Installing in Root mode..."
mode=""
break
;;
esac
fi
done
# Load in OS file and prepare vars for later usage
if [ -f /etc/os-release ]
then
. /etc/os-release
elif [ -f /etc/lsb-release ]
then
. /etc/lsb-release
else
printf "ERROR: I need the file /etc/os-release to determine what my distribution is..."
exit
fi
# Check if license key is set in New Relic config file
if [ ! -z $(grep license_key "/etc/newrelic-infra.yml") ]; then
echo "New Relic license key found.";
else
echo "Adding license key to /etc/newrelic-infra.yml..."
echo "license_key: $license_key" | sudo tee -a /etc/newrelic-infra.yml
fi
# Check if GnuPG is installed - install it if not present
if which gpg >/dev/null; then
echo "GnuPG is installed."
else
case $ID in
[ubuntu]*)
sudo apt-get install gnupg -y
;;
[debian]*)
sudo apt-get install gnupg -y
;;
[centos]*)
sudo yum install gnupg -y
;;
[rhel]*)
sudo yum install gnupg -y
;;
[sles]*)
sudo zypper -n install gnupg
;;
[amzn]*)
sudo yum install gnupg -y
;;
esac
fi
# Check if newrelic-infra is installed - install it if not present, update it if present
if which newrelic-infra >/dev/null; then
echo "New Relic Infrastructure agent is installed. Updating..."
case $ID in
[ubuntu]*)
sudo apt-get update && sudo apt-get install --only-upgrade newrelic-infra -y
;;
[debian]*)
sudo apt-get update && sudo apt-get install --only-upgrade newrelic-infra -y
;;
[amzn]*)
sudo yum update newrelic-infra -y
;;
[centos]*)
sudo yum update newrelic-infra -y
;;
[rhel]*)
sudo yum update newrelic-infra -y
;;
[sles]*)
sudo zypper -n update newrelic-infra
;;
esac
else
case $ID in
[ubuntu]*)
curl -s https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg | sudo apt-key add -
printf "deb [arch=amd64] https://download.newrelic.com/infrastructure_agent/linux/apt $DISTRIB_CODENAME main" | sudo tee -a /etc/apt/sources.list.d/newrelic-infra.list
sudo apt-get update
eval $dependencies
sudo $mode apt-get install newrelic-infra -y
;;
[debian]*)
curl -s https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg | sudo apt-key add -
printf "deb [arch=amd64] https://download.newrelic.com/infrastructure_agent/linux/apt $DISTRIB_CODENAME main" | sudo tee -a /etc/apt/sources.list.d/newrelic-infra.list
sudo apt-get update
eval $dependencies
sudo $mode apt-get install newrelic-infra -y
;;
[amzn]*)
sudo curl -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/$(rpm -E %{rhel})/x86_64/newrelic-infra.repo
sudo yum -q makecache -y --disablerepo='*' --enablerepo='newrelic-infra'
eval $dependencies
sudo $mode yum install newrelic-infra -y
;;
[centos]*)
sudo curl -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/$(rpm -E %{rhel})/x86_64/newrelic-infra.repo
sudo yum -q makecache -y --disablerepo='*' --enablerepo='newrelic-infra'
eval $dependencies
sudo $mode yum install newrelic-infra -y
;;
[rhel]*)
sudo curl -o /etc/yum.repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/yum/el/$(rpm -E %{rhel})/x86_64/newrelic-infra.repo
sudo yum -q makecache -y --disablerepo='*' --enablerepo='newrelic-infra'
eval $dependencies
sudo $mode yum install newrelic-infra -y
;;
[sles]*)
curl https://download.newrelic.com/infrastructure_agent/gpg/newrelic-infra.gpg -s | sudo gpg --import
sudo curl -o /etc/zypp/repos.d/newrelic-infra.repo https://download.newrelic.com/infrastructure_agent/linux/zypp/sles/$VERSION_ID/x86_64/newrelic-infra.repo
sudo zypper -n ref -r newrelic-infra
eval $dependencies
sudo $mode zypper -n install newrelic-infra
;;
esac
fi
printf "\n\nNew Relic Infrastructure agent installed.\n\nTo start, stop, restart, or check the agent status, see https://docs.newrelic.com/docs/infrastructure/new-relic-infrastructure/configuration/start-stop-restart-check-infrastructure-agent-status\n\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment