AWSユーザなら無料で使えるNew Relic導入方法


サーバモニタリングサービス「New Relic」のAWSへの導入についてまとめてみました!

New Relicのアカウント取得

最初は無料だからって焦ってアカウントを取っちゃダメダメ。
もしAWSを使っているならお得な隠れキャンペーンがあります。

隠れキャンペーンページ

このページ経由でSign-UPするとStandard版が無料で使えます。
通常のページからSign-UPすると無料になるのはLITE版です。

はじめてのサーバ監視設定

まずは基本の「き」でサーバの監視から仕込んでみます。
打つコマンドは5つだけです。

# rpm -Uvh http://download.newrelic.com/pub/newrelic/el5/i386/newrelic-repo-5-3.noarch.rpm
# yum install newrelic-sysmond
# nrsysmond-config --set license_key=LICENSE_KEY
# /etc/init.d/newrelic-sysmond start
# chkconfig newrelic-sysmond on

3行目のライセンスキーはNew Relicの管理画面から取得できます

たったこれだけで美麗管理画面からサーバの状況が丸裸です!

なに、この感動プロダクト!!

ときめいた!!!

CloudWatchとの連携

専用エージェントをサーバに入れただけではAWSのEC2以外のサービス(RDSやELB等)のデータは取得できません。
CloudWatchに連携したエージェントを入れることで対応が可能になります。

事前準備

IAMアカウントの作成

  • AWSにNew Relicの監視エージェント専用のIAMを作成しておきます。IAMのポリシールールは下記を使ってください。
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "autoscaling:Describe*",
        "cloudwatch:Describe*",
        "cloudwatch:List*",
        "cloudwatch:Get*",
        "ec2:Describe*",
        "ec2:Get*",
        "ec2:ReportInstanceStatus",
        "elasticache:DescribeCacheClusters",
        "elasticloadbalancing:Describe*",
        "sqs:GetQueueAttributes",
        "sqs:ListQueues",
        "rds:DescribeDBInstances",
        "SNS:ListTopics"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
  • IAMアカウントを作成したらaccess_keyとsecret_keyをメモっておきます。

監視環境の用意

  • EC2 1台(t1.microでエージェント専用インスタンスを立ち上げることが推奨されています)
  • Ruby (1.9.2以上)
  • Rubygems (1.3.7以上)
  • Bundler
  • Git

Ruby、Rubygem、Gitはyumからインストール可能です。Bundlerはgem install bundlerでインストールしましょう。

その際にruby-devellibxml2libxsltが入ってないとBundlerがインストール出来ない場合があるので、yumから追加でインストールしてください。

Bundlerは/usr/local/binにインストールされるのでPATHを通しておくと良いです。

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

  • githubからダウンロードします
  • ダウンロードしたファイルを適当な場所に展開します(展開した場所を/home/newrelic/newrelic_awsとします)
  • 展開したディレクトリのconfigの下にあるtemplate_newrelic_plugin.ymlをnewrelic_plugin.ymlとしてコピーします
  • newrelic_plugin.ymlを編集します(後述)
  • 以下のコマンドでエージェントを起動します
$ cd /home/newrelic/newrelic_aws
$ bundle install
$ bundle exec ./bin/newrelic_aws

しばらくするとNew Relicの管理画面に自動的に項目が追加されます。

newrelic_plugin.ymlの記載方法

license_key,access_key,secret_keyをあなたの環境に応じて書き換えます。

ファイルの下の方にあるenabled: trueとするとそれぞれのAWSサービスでCloudWatchの値が取得できるようになります。

取得したくないサービスはenabled: falseにしましょう

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: 'New Relicのあなたのライセンスキー'
  #
  # 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: '事前準備で作成したAMIのaccess_key'
  secret_key: '事前準備で作成したAMIのsecret_key'
  # Specify AWS regions to query for metrics
  # regions:
  #   us-east-1
#
# Agent configuration.
#
agents:
  #
  # Enable/disable agents with the enabled attribute or by commenting out each agent.
  #
  # Tag filtering is available and is 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: true
  sns:
    enabled: true
  ec:
    enabled: true
  ecr:
    enabled: false

監視エージェントの自動起動

Upstartに登録することでサーバ起動時に監視エージェントが立ち上がるように登録します

  • /etc/init/newrelic-aws.confを作成します
newrelic-aws.conf
# Upstart: /etc/init/newrelic-aws.conf
description 'New Relic AWS Plugin'

start on runlevel [2345]
stop on runlevel [016]

respawn
respawn limit 3 5

chdir /home/newrelic/newrelic_aws

script
        export PATH=$PATH:/usr/local/bin
        bundle install
        bundle exec ./bin/newrelic_aws
end script
  • 監視エージェントを起動します
# initctl start newrelic-aws
# initctl status newlic-aws
newrelic-aws start/running, process 813

様々な使い方

MySQL監視エージェントの導入についてはこちら

有料プランへの切替時の注意

  • NewRelicのAPMなどは大変便利なので、もっと詳細な情報がみたくて課金を検討することもあると思います
  • その際に注意したいのがホスト数です。一度、契約を有料プランに変更すると利用ホスト数だけライセンスがかかってきます
  • このホストは有料、このホストは無料という設定はできず、課金したアカウントにぶらさがっているホスト全部が課金対象になりますので、ご注意ください
  • これを避けるためには別アカウントを取得し、有料プランで使いたいホストと無料プランで使いたいホストをそれぞれのアカウントにひも付けしなおす必要があります

仲間募集

株式会社スピカでは一緒に切磋琢磨できるエンジニアの方を募集しています!ぜひ、こちらをご覧ください!!