Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Last active March 24, 2019 10:14
Show Gist options
  • Save jtpaasch/8105854 to your computer and use it in GitHub Desktop.
Save jtpaasch/8105854 to your computer and use it in GitHub Desktop.
Installs the nginx and logwatcher plugins for newrelic on a CentOS system.
#!/bin/bash
# ----------------------------------------------------------------------
# Prelimenary checks
# ----------------------------------------------------------------------
# Make sure there's a license key file.
KEYFILE="LICENSEKEY"
if [ ! -e $KEYFILE ]; then
echo "No LICENSEKEY found."
echo "Please copy your new relic license key"
echo "into a file called LICENSEKEY in this directory."
exit 1
else
# Read the license key from the file.
KEY=`cat $KEYFILE`
# Make sure it's not empty.
if [ -z $KEY ]; then
echo "No key found in $KEYFILE."
echo "Please copy your new relic license key into this file."
exit 1
fi
fi
# ----------------------------------------------------------------------
# Install Ruby crap
# ----------------------------------------------------------------------
# Make sure ruby-dev and ruby gems is on the system.
sudo yum install -y ruby-devel
sudo yum install -y rubygems
# Download the ruby bundler.
sudo gem install bundler
# ----------------------------------------------------------------------
# Downloads folder and other tools
# ----------------------------------------------------------------------
# Where should we download new relic foo?
DOWNLOADS='/newrelic'
# Does a newrelic folder already exist?
if [ ! -e $DOWNLOADS ]; then
sudo mkdir $DOWNLOADS
fi
# We'll need unzip
if ! hash unzip 2>/dev/null; then
sudo yum install -y unzip
fi
# ------------------------------------------------------------------------
# Install NGINX plugin
# ------------------------------------------------------------------------
# Make sure the nginx_stub_status has a server block in the nginx confs.
sudo echo '' > /etc/nginx/conf.d/stub_status.conf
sudo echo 'server {' >> /etc/nginx/conf.d/stub_status.conf
sudo echo ' listen 127.0.0.1;' >> /etc/nginx/conf.d/stub_status.conf
sudo echo ' location /nginx_stub_status {' >> /etc/nginx/conf.d/stub_statu\
s.conf
sudo echo ' stub_status on;' >> /etc/nginx/conf.d/stub_status.conf
sudo echo ' access_log off;' >> /etc/nginx/conf.d/stub_status.conf
sudo echo ' allow 127.0.0.1;' >> /etc/nginx/conf.d/stub_status.conf
sudo echo ' deny all;' >> /etc/nginx/conf.d/stub_status.conf
sudo echo ' }' >> /etc/nginx/conf.d/stub_status.conf
sudo echo '}' >> /etc/nginx/conf.d/stub_status.conf
sudo echo '' >> /etc/nginx/conf.d/stub_status.conf
# Restart nginx.
sudo service nginx restart
# Download the new relic nginx plugin and unpack it.
curl -O http://nginx.com/download/newrelic/newrelic_nginx_agent.tar.gz
tar -xzf newrelic_nginx_agent.tar.gz
rm -rf newrelic_nginx_agent.tar.gz
# Bundle it.
cd newrelic_nginx_agent
sudo bundle install
# Copy the config file.
cp config/newrelic_plugin.yml.in config/newrelic_plugin.yml
# Add your license key:
sed -i "s/license_key: 'YOUR_LICENSE_KEY_HERE'/license_key: '$KEY'/" config/n\
ewrelic_plugin.yml
# Start the daemon.
sudo ./newrelic_nginx_agent.daemon start
# ------------------------------------------------------------------------
# Install Logwatcher
# ------------------------------------------------------------------------
# Make sure we're in the downloads folder.
cd $DOWNLOADS
# What's the name of the folder we'll keep logwatcher stuff in?
LOGWATCHER='logwatcher'
# Skip all this installation if the logwatcher folder already exists.
if [ ! -e $LOGWATCHER ]; then
# Create the directory.
sudo mkdir $LOGWATCHER && cd $LOGWATCHER
# Download the logwatcher package.
sudo curl -O -L https://github.com/railsware/newrelic_platform_plugins/ar\
chive/master.zip
sudo unzip master.zip
sudo rm -rf master.zip
# Copy the logwatcher package and delete the rest.
sudo cp -r newrelic_platform_plugins-master/newrelic_logwatcher_agent .
sudo rm -rf newrelic_platform_plugins-master
# Change into the logwatche directory and bundle it.
cd newrelic_logwatcher_agent
sudo bundle install
# Copy the config file.
sudo cp config/newrelic_plugin.yml.example config/newrelic_plugin.yml
# Add our key.
sudo sed -i "s/license_key: 'YOUR_LICENSE_KEY_HERE'/license_key: '$KEY'/"\
config/newrelic_plugin.yml
# Add the paths to our log file.
LOG1='\/var\/log\/nginx\/nara.access.log'
sudo sed -i "s/log_path: tmp.log/log_path: $LOG1/" config/newrelic_plugin\
.yml
LOG2='\/var\/log\/nginx\/nara.error.log'
sudo sed -i "s/log_path: tmp2.log/log_path: $LOG2/" config/newrelic_plugi\
n.yml
# Add errors to watch for in our logs.
# ERROR1='HTTP\\\/1\\\.1\\\" 40[0-9]|50[0-9] '
ERROR1='404'
sudo sed -i "0,/term: \"\[Ee\]rror \"/{s/term: \"\[Ee\]rror \"/term: \
\"$ERROR1\"/}" config/newrelic_plugin.yml
# Start up the logwatcher.
sudo ruby newrelic_logwatcher_agent.rb &
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment