LoginSignup
12
12

More than 5 years have passed since last update.

0から簡単導入。New RelicによるAWSリソース+PHPアプリケーションの運用監視

Last updated at Posted at 2016-03-30

サーバの運用監視ツールと言えば、Nagiosやzabbixなどが有名ですが、EC2インスタンスでサービスを監視する場合は、New Relicというサービスで無料でサーバを監視することができます。

New Relicを使えば、監視専用にサーバを立てる必要もなく、コマンドをいくつか叩くだけですぐにEC2インスタンスの監視をできるようになります。
また、RDSやELBなどデータベースやロードバランサといったEC2インスタンス以外のAWSリソースの監視も簡単に導入できます。

下記のURLをクリックして、メールアドレスを登録して、ユーザー登録するとStandard版が無料で登録できます。ただし、いくつかの制限はありますが、無料の範囲内でも十分に使用することができます。
https://newrelic.com/aws
スマホ版のアプリもあるので気軽に監視することもできます。

New Relic本体のインストール

EC2インスタンス上で以下のコマンドを実行する。以下の5つのコマンドを叩くだけ。

rpm -Uvh http://yum.newrelic.com/pub/newrelic/el5/x86_64/newrelic-repo-5-3.noarch.rpm
yum -y install newrelic-sysmond
nrsysmond-config --set license_key=【ライセンスキー】
service newrelic-sysmond start
chkconfig newrelic-sysmond on

これだけで、EC2インスタンスの監視が稼働になる。alertメールの設定なども簡単にできるので、適宜カスタマイズすると良い。

PHPを監視可能に

yum -y install newrelic-php5
sed -i "s/^newrelic.license =.*$/newrelic.license = \"【ライセンスキー】\"/" /etc/php.d/newrelic.ini
sed -i "s/^;newrelic.appname =.*$/newrelic.appname = \"【任意のアプリケーション名】\"/" /etc/php.d/newrelic.ini
service httpd restart

監視用エージェントのインストール

sudo yum update

sudo yum install readline-devel git make zlib-devel sqlite-devel.x86_64 gcc g++ svn
sudo yum install gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl openssl-devel make bzip2 autoconf automake libtool bison iconv-devel



curl -sSL https://rvm.io/mpapis.asc | sudo gpg2 --import -
curl -L https://get.rvm.io | bash -s stable --ruby=1.9.3

source /usr/local/rvm/scripts/rvm

bundlerのインストール

gem install bundler --no-rdoc --no-ri


yum install ruby-devel
gem install io-console
gem environtment


echo $PATH
echo 'export PATH="$PATH:/usr/local/bin"' >> ~/.bash_profile
source ~/.bash_profile
bundle -v
cd /home/ec2-user
mkdir newrelic_aws
git clone https://github.com/newrelic-platform/newrelic_aws_cloudwatch_plugin.git newrelic_aws

template_newrelic_plugin.ymlをコピーして、newrelic_plugin.ymlとリネームして、
ファイルを編集する。
AWSアクセスキーとAWSシークレットキーは、IAMでユーザー登録したときに発行されるcsv形式でダウンロードできるデータに記載されているものを使用する。

cp template_newrelic_plugin.yml newrelic_plugin.yml
vim newrelic_plugin.yml
newrelic_plugin.yml
# Please make sure to update the license_key information with the
# license key for your New Relic account.
#
newrelic:
  #
  # Update with your New Relic account license key:
  #
  license_key: '【ライセンスキー】'
  #
  # Set to '1' for verbose output, remove for normal output.
  # All output goes to stdout/stderr.
  #
  # verbose: 1
#
# AWS configuration.
#
aws:
  # Update with you AWS account keys:
  access_key: '【AWS アクセスキー】'
  secret_key: '【AWS シークレットキー】'

  #Use the following if you would like to configure AWS keys with ENV variables
  #access_key: <%= ENV['AWS_ACCESS_KEY'] %>
  #secret_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>

  #Use the following line disable the key checks and enable usage of AWS instance metadata for setting keys
  #use_aws_metadata: true

  # Specify AWS regions to query for metrics
  # regions:
  #  -
  #   us-west-1
  #  -
  #   us-east-2

#
# Agent configuration.
#
agents:
  #
  # Enable/disable agents with the enabled attribute or by commenting out each agent.
  #
  # Tag and instance ID filtering are available and are documented at
  # https://github.com/newrelic-platform/newrelic_aws_cloudwatch_plugin/blob/master/README.md#tag-filtering
  #
  # An optional cloudwatch_delay attribute can be added for each agent
  # which adjusts the time window in seconds for CloudWatch querying.
  # Some of the Cloudwatch metrics are not immediately available and may
  # require a delay before being queried.
  ec2:
    enabled: true
  ebs:
    enabled: true
  elb:
    enabled: true
  rds:
    enabled: true
  sqs:
    enabled: false
  sns:
    enabled: false
  ec:
    enabled: false
  ecr:
    enabled: false

# Uptart: /etc/init/newrelic_aws.conf
description "New Relic AWS Plugin"
author "【名前】<【メールアドレス】>"

start on (local-filesystems and net-device-up and runlevel [2345])
stop on [016]

respawn
respawn limit 3 5

chdir /home/ec2-user/newrelic_aws

script
    export PATH=$PATH:/usr/local/rvm/gems/ruby-1.9.3-p551@global/bin:/usr/local/rvm/rubies/gems/ruby-1.9.3-p551@global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p551/bin:/usr/local/rvm/bin
    bundle install
    bundle exec ./bin/newrelic_aws
end script

bundleのinstallとそれに必要なnokogiriに必要なパッケージのインストール

yum install libxml2-devel libxslt-devel
gem install nokogiri -- --use-system-libraries=true --with-xml2-include=/usr/include/libxml2/

bundle install
cd /home/ec2-user/newrelic_aws
bundle install
bundle exec ./bin/newrelic_aws

以上で、EC2以外のその他のAWSリソースも監視できるようになる。
New Relicのオフィシャルサイト( http://newrelic.com/ )からチャットでの問い合わせにも英語で丁寧に対応してくれました。

参考

http://rikuga.me/category/new-relic/
http://kkurahar.github.io/blog/2013/11/10/newrelic-aws/
http://inaba-serverdesign.jp/blog/20130912/newrelic.html
http://keiwt.hatenablog.com/entry/2015/02/13/123116

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12