LoginSignup
22
10

More than 5 years have passed since last update.

AWS CloudWatchの汎用監視プラグインMetrinの紹介(Mackerelも対応)

Last updated at Posted at 2016-12-24

Metrin(メットリン)

AWSのCloudWatchの値を取得できるMackerelのプラグインはいくつかあるのですが、
対象リソースが未対応のものが多く、また新しいサービスにはすぐに対応しない作りだっため困ってました。

そこで、汎用で利用できるプラグインがほしいなーとy13iにアピールしていましたら、
なんと作って頂けたので紹介になります!!

Metrinはy13iが作成した汎用CloudWatchプラグインです。
1つのプラグインでチェック機能とメトリックの取得の両方を兼ね備えています。

Zabbix/Sensu/Mackerel監視システムを選ばず、言語はGoで書かれているため、
OSはWindows/Linux/Mac/BSDどれでも使用できます。

また、MetrinはデフォルトでMackerelのカスタムメトリックのフォーマットに対応してくれています!

準備

対象のOS(Windows/Linux/Mac/BSD)のものをダウンロードして、お好きなディレクトリに配置します。
https://github.com/y13i/metrin/releases

また、AWSのインスタンスにCloudWatchが参照できるIAMRoleを付与しておいてます。

$ cd /usr/local/bin/
$ wget https://github.com/y13i/metrin/releases/download/v0.0.3/metrin-linux_amd64.zip
$ unzip metrin-linux_amd64.zip

コマンド

metrinはグローバルオプションと、サブコマンドの2つに分かれています。
グローバルオプションでは、対象のCloudWatchの値に関してのオプションを設定します。
サブコマンドでは、チェックなのかプリントなのかを決めることができます。

例えば、CloudWatchのCPUの使用率がある一定以上の値になったらアラートを上げる場合はcheckを利用。
Mackerel側にCPU使用率をカスタムメトリックとして送信したいのであればprintを利用します。

NAME:
   metrin - Very simple CloudWatch CLI for Zabbix/Nagios/Sensu/Mackerel/etc.

USAGE:
   metrin [global options] command [command options] [arguments...]

VERSION:
   0.0.3

COMMANDS:
     check    perform check and exit with status codes (0: OK, 1: WARNING, 2: CRITICAL, 3: UNKNOWN)
     print    Prints GetMetricStatistics response with given format template
     debug    Prints GetMetricStatistics params and response
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --namespace value, -n value                          CloudWatch namespace. e.g. 'AWS/EC2'
   --metric-name value, -m value                        CloudWatch metric name. e.g. 'CPUUtilization'
   --start-time value, -S value                         start time as unix timestamp, relative from now if 0 or negative value given (default: -300)
   --end-time value, -E value                           end time as unix timestamp, relative from now if 0 or negative value given (default: 0)
   --period value, -p value                             CloudWatch metric statistic period. (default: 60)
   --unit value, -u value                               CloudWatch metric statistic unit. e.g. 'Percent'
   --statistic value, -s value                          CloudWatch metrics statistic. e.g. 'Average'
   --extended-statistic value, -e value                 CloudWatch extended metrics statistic. e.g. 'p99.5'
   --dimension DIM_KEY:DIM_VALUE, -d DIM_KEY:DIM_VALUE  CloudWatch dimension. DIM_KEY:DIM_VALUE e.g. 'InstanceId:i-12345678'
   --region value                                       AWS region. e.g. 'us-west-2'
   --profile value                                      AWS profile name. e.g. 'myprofile'
   --access-key-id value                                AWS access key id. e.g. 'AKIAIOSFODNN7EXAMPLE'
   --secret-access-key value                            AWS secret access key. e.g. 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
   --help, -h                                           show help
   --version, -v                                        print the version

Print

試しにインスタンスのCPU使用率を取得してみます。

$ metrin --namespace AWS/EC2 --metric-name CPUUtilization --statistic Average --dimension InstanceId:i-xxxxxxxxx --start-time -600 print --last-value-only
CloudWatch.InstanceId.i-xxxxxxxxx.CPUUtilization.Average    1.468   1482596160

問題なく表示できることが確認しできました。
後は確認できたコマンドをそのままMackerelのconfに入れれば終わりです。

[plugin.metrics.cloudwatch-i-xxxxxxxxx-cpu]
command = "/usr/local/bin/metrin--namespace AWS/EC2 --metric-name CPUUtilization --statistic Average --dimension InstanceId:i-xxxxxxxxx --start-time -600 print --last-value-only"

後はCloudWatchの好きな値を選ぶだけで、Mackerelのコンソール側で表示したりすることが可能です。

デフォルトのprint表示はMackerelに対応しているフォーマットになりますが、
--template オプションを利用すればお好みで変更が可能です。
ZabbixやSensuでも利用可能です。

Check

Printとグローバルオプションは同じで、checkの部分のみ変更すればチェックプラグインとして利用できます。
リターンコードはsensuと同様になりますので、Mackerelでも問題なく利用できます。

例えば、値が1以上であればアラートを上げる場合はこちら。

# metrin --namespace AWS/EC2 --metric-name CPUUtilization --statistic Average --dimension InstanceId:i-xxxxxxx --start-time -600 check --critical-gt 1
CloudWatch CRITICAL, got `1.628000` (Percent), greater than `1.000000`
Params: {
  Dimensions: [{
      Name: "InstanceId",
      Value: "i-xxxxxxxxxx"
    }],
  EndTime: 2016-12-24 xx:xx:xx +0000 UTC,
  MetricName: "CPUUtilization",
  Namespace: "AWS/EC2",
  Period: 60,
  StartTime: 2016-12-24 xx:xx:xx +0000 UTC,
  Statistics: ["Average"]
}

確認ができたら同様にMackerelのconfに記載すればよい。

[plugin.checks.cloudwatch-i-xxxxxxxxx-cpu]
command = "/usr/local/bin/metrin --namespace AWS/EC2 --metric-name CPUUtilization --statistic Average --dimension InstanceId:i-xxxxxxxxx --start-time -600 check --critical-gt 1"
notification_interval = 60
max_check_attempts = 1
check_interval = 1

これで監視設定も可能。

おまけ

複数dimensionがある場合は[--dimension]を複数記載すればよい

テンプレートの書き方はGoのPackage templateを参照
https://golang.org/pkg/text/template/

etc

使いまわせる汎用監視プラグインMetrinの紹介となりました。
一つ一つの値を取得しなければいけないですが、confの反映はchefなどを利用するのであまり気になりませんでした。

それでは皆さん、楽しいMackrelライフを!

\アッカリ~ン/
にプラグイン名が似てるなって思ったけど、俺だけだった模様。
12月24日に記事を上げると言ったが、JSTとは言っていない。(ごめんなさい

22
10
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
22
10