LoginSignup
27
27

More than 5 years have passed since last update.

mackerelをautoscallingで使う(Amazon Linux)

Last updated at Posted at 2015-01-17

AWSでautoscallingを使用しているとインスタンスが不定期で入れ替わるので、ターミネートされるタイミングでmackerel上で退役扱いにしてあげることが必要なんですが、Amazon Linuxだとなんかうまくいかなくてその時のメモ。

やることは一つでmackerelのAPIをコールする起動スクリプトを作成し、chkconfigで登録するだけ。

/etc/init.d/mackerel-retire
#!/bin/bash

### BEGIN INIT INFO
# Provides: mackerel-retire
# Required-Start: $network $local_fs
# Required-Stop: $network $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# description: mackerel retire
### END INIT INFO

API_KEY="hogehoge"
HOST_ID=$(cat /var/lib/mackerel-agent/id)

LOCKFILE=/var/lock/subsys/mackerel-retire

case "$1" in
    start)
        touch ${LOCKFILE}
        ;;
    stop)
        curl -X POST "https://mackerel.io/api/v0/hosts/${HOST_ID}/retire" \
             -d '{}' \
             -H "Content-Type: application/json" \
             -H "X-Api-Key: ${API_KEY}" 
        rm -f ${LOCKFILE} /var/lib/mackerel-agent/id
        ;;
esac

登録する

$ sudo chmod 755 /etc/init.d/mackerel-retire
$ sudo chkconfig --add mackerel-retire
$ sudo chkconfig mackerel-retire on
$ sudo service mackerel-retire start

何故ハマったのか

Amazon Linuxは/var/lock/subsys配下にロックファイルのあるプログラムのみしかシャットダウン時にキルしてくれないらしく、これに気付かなかった。
Ubuntuとかはchkconfigで登録すればOKらしいです。

27
27
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
27
27