orenoblog

エンジニアになりたいExcel方眼紙erの物語

sensu-clientに独自attributeを持たせたい(additional parameter)

sensu-pluginをテストしてる私ですこんばんは。

自分で見つけたわけではないのですが、忘れてしまうのでadditional parameterについてメモします

sensu-clientをserverへ登録するときに与えられるパラメータは固定されているように見えます。

parameter 意味
address ホスト名やIPアドレス(node["hostname"])
subscriptions sensuに登録するホストのロール(chefのnode["role"])とか
keepalive client - serverの通信チェック(秒)
handlers 通知に利用するイベントハンドラ

が、

sensuのLWRPを見てみるとadditonalというHash attributeがありました。

providers/client.rb

resources/client.rb

これを利用するとsensu-clientがsensu-serverへ情報を登録するとき、独自のattributeを渡すことができそうです。

例えばsensu-client登録時にEC2のメタデータを渡す時はこのように書くとうまく渡せます。

公式sensuのrecipeを直接書き換えるのではなく、include_recipeで取り込みましょう。

  • sensu-client用のadditional attributeを登録
default.ec2_instance_id         = %x(curl -s http://169.254.169.254/latest/meta-data/instance-id)
default.ec2_availability_zone   = %x(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
default.ec2_instance_type       = %x(curl -s http://169.254.169.254/latest/meta-data/instance-type)
  • sensu-clientのrecipe

additionalパラメータとして、EC2のmetadataとどのプロジェクトで利用しているのかといった情報を書いてみます。

include_recipe "sensu::client"

sensu_client node.sensu.client_name do
  address node.sensu.client_ipaddress
  subscriptions node.roles
  keepalive(
    thresholds: {
      warning: 10,
      critical: 180,
    },
    handlers: node.sensu.default_handlers,
    refresh: 1800,
  )
  additional(
    ec2_instance_id:        node.ec2_instance_id,
    ec2_availability_zone:  node.ec2_availability_zone,
    ec2_instance_type:      node.ec2_instance_type,
    "customer_name": "test",
    "customer_description": "test",
    "project_name": "testpj",
    "project_description": "test project"
  )
end 
"run_list" : [ "recipe[sensu-run]"
  • 上記recipeの適用

適用後/etc/sensu/client.jsonにEC2のmetadataが反映されています。

{
  "client": {
    "name": "********_i-15c7290c",
    "address": "192.168.XXX.XXX",
    "subscriptions": [
      "all"
    ],
    "keepalive": {
      "thresholds": {
        "warning": 10,
        "critical": 180
      },
      "handlers": [
        "mailer-ses"
      ],
      "refresh": 1800
    },
    "ec2_instance_id": "i-15c7290c",
    "ec2_availability_zone": "ap-northeast-1c",
    "ec2_instance_type": "c3.large",
    "customer_name": "test",
    "customer_description": "test",
    "project_name": "testpj",
    "project_description": "test project"
  }
}

sensu-clientを起動してsensu-adminで確認すると いい感じではないのですがadditional attributeも表示してくれます。

f:id:buta9999:20140514213155p:plain

f:id:buta9999:20140514214941p:plain

AWS-SDKを組み込むとタグ情報をひもづけることができるかもしれませんね(chef-server + knife-ec2でもいいのかな?)

ちょっと行間読めという文章になってしまいましたが取り急ぎ。

これでattributeに悩むことがなくなりました。