Hina-Mode

とある呑んだくれエンジニアの気が向いた時に書く戯言

AWSにBounceHammerをインストールする

ググったけど有用な記事が見つからなかったので自分で頑張っていれてみた。
結果ちょこっとばかしハマってしまったので記載。
メールサーバをAWSで自作していて、バウンスメールの解析をしたくなったので、ついやってみた。

ちなみにBounceHammerとはオープンソースのバウンスメール集積ソフトです。

bounceHammer | an open source software for handling email bounces

AMIはAmazon Linux, 2014/7月頃に立ち上げた時点で最新でしたがyum updateしてなかったんで色んなモノが古いので厳密にこうだ、という保証はないです。念のため。

手順書

$ su
$ yum install -y perl-core gcc
$ cd /usr/local/bin
$ wget -O- 'http://cpanmin.us/' > ./cpanm
$ chmod +x ./cpanm
$ cd /usr/local/src
$ wget http://dist.bouncehammer.jp/bouncehammer-2.7.12.tar.gz
$ tar xfz bouncehammer-2.7.12.tar.gz
$ export PERL5LIB=/usr/local/bouncehammer/lib/perl5
$ mkdir -p /usr/local/bouncehammer
$ cd bouncehammer-2.7.12
$ perl ./Modules.PL missing | cut -d' ' -f4 | cpanm -l /usr/local/bouncehammer
# テストエラーで入らなかったモジュールを強制追加
$ cpanm -l /usr/local/bouncehammer/ --force DBIx::Skinny
# WARNになっているモジュールのupdate
$ cpanm -l /usr/local/bouncehammer Getopt::Long
$ ./configure --disable-webui --with-perl-extlib=/usr/local/bouncehammer/lib/perl5
$ make
$ make test
$ make install
$ cp /usr/local/bouncehammer/etc/bouncehammer.cf-example /usr/local/bouncehammer/etc/bouncehammer.cf

無駄な事してる部分もあるかもしれませんが、一応これでインストール出来ました。
事前にperl-coreもそうなんですが、gccが入ってないとperl系のライブラリをcpanmでインストールするときにコンパイルできないでインストール中断していまイます。
※そもそもgccが入ってないことに全く気づかなかった自分がアホといえばアホなのですが。。。

あと、BounceHammer公式ではcpanmに渡すのは-Lオプションにしていますが、一応-lオプションに切り替えています。

cpanm - CPANからモジュールを取得、アンパック、インストールする - perldoc.jp

上記でオプションの意味を調べたのですが、-Lだとperl-coreを先にyum installしても、
「コアでない依存が、それらがインストール済みであっても、 インストールされ」るみたいです。
ExtUtilsなんかが該当して、最初何回cpanmにモジュールリストをわたしても依存関係を解決できずにインストールをスキップしていました。


何かの参考になれば幸いです。

AWSのec2インスタンス上にcapistranoとaws-sdk(ruby)をインストールしようとしたらnokogiriに阻まれた話

capistranoのinstallは非常に簡単だったのですが、
aws-sdkを突っ込むときにちょっと引っかかったので備忘録としてエントリーします。

  • 使用するec2インスタンスAmazon Linux AMI 2014.03.2 (HVM)
    ※エントリー記載現在の最新版です
  • VMは立ち上げた初期の状態で特に何もインストールしていません。
  • capistranoで自分が立ち上げたec2インスタンスをデプロイ先として自動認識出来るようにするため、aws-sdkを必要としていました。

先に実行コマンドのまとめ

$ su
$ yum install -y gcc libxml2-devel libxslt-devel ruby-devel
$ gem install capistrano
$ gem install capistrano_colors
$ gem install capistrano-ext
$ gem install railsless-deploy
$ gem install nokogiri -- --use-system-libraries
$ gem install aws-sdk

※railsless-deployは、私がPHPerの為rails使わないので突っ込んでます。

補足

yum-installでgcc, libxml...などをインストールする理由

まずはaws-sdkだけインストールしようとした結果を見てみましょう。

$ gem install aws-sdk
Fetching: mini_portile-0.6.0.gem (100%)
Successfully installed mini_portile-0.6.0
Fetching: nokogiri-1.6.2.1.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing aws-sdk:
        ERROR: Failed to build gem native extension.
    /usr/bin/ruby2.0 extconf.rb
mkmf.rb can't find header files for ruby at /usr/share/ruby/include/ruby.h
 
Gem files will remain installed in /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.2.1 for inspection.
Results logged to /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.2.1/ext/nokogiri/gem_make.out

ダメでした。
aws-sdkはnokogiriの依存性があるみたいですね。
で、色々調べた結果、最低でもnokogiriをインストールするために

  • libxml2-devel
  • libxslt-devel
  • ruby-devel
  • gcc

の4種が必要なことがわかりました
gccは--use-system-librariesでインストールする際に必要なんでしょうかね。
自分で必要とか言っておきながら、詳しくは分かりませんでした。すいません。

nokogiriを単体でインストールする理由

さて、この状態でもう一度aws-sdkインストールしようとしてみます。

$ gem install aws-sdk
/usr/bin/ruby2.0 extconf.rb
Building nokogiri using packaged libraries.
checking for iconv.h... yes
checking for iconv_open() in iconv.h... yes
Building libxml2-2.8.0 for nokogiri with the following patches applied:
        - 0001-Fix-parser-local-buffers-size-problems.patch
        - 0002-Fix-entities-local-buffers-size-problems.patch
        - 0003-Fix-an-error-in-previous-commit.patch
        - 0004-Fix-potential-out-of-bound-access.patch
        - 0005-Detect-excessive-entities-expansion-upon-replacement.patch
        - 0006-Do-not-fetch-external-parsed-entities.patch
        - 0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch
        - 0008-Improve-handling-of-xmlStopParser.patch
        - 0009-Fix-a-couple-of-return-without-value.patch
        - 0010-Keep-non-significant-blanks-node-in-HTML-parser.patch
        - 0011-Do-not-fetch-external-parameter-entities.patch
************************************************************************
IMPORTANT!  Nokogiri builds and uses a packaged version of libxml2.
If this is a concern for you and you want to use the system library
instead, abort this installation process and reinstall nokogiri as
follows:
    gem install nokogiri -- --use-system-libraries
If you are using Bundler, tell it to use the option:
    bundle config build.nokogiri --use-system-libraries
    bundle install
However, note that nokogiri does not necessarily support all versions
of libxml2.
For example, libxml2-2.9.0 and higher are currently known to be broken
and thus unsupported by nokogiri, due to compatibility problems and
XPath optimization bugs.
************************************************************************
Extracting libxml2-2.8.0.tar.gz into tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.8.0... OK
Running patch with /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.2.1/ports/patches/libxml2/0001-Fix-parser-local-buffers-size-problems.patch...
Running 'patch' for libxml2 2.8.0... ERROR, review 'tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.8.0/patch.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib64
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby2.0
        --help
        --clean
        --use-system-libraries
        --enable-static
        --disable-static
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --enable-cross-build
        --disable-cross-build
/usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.0/lib/mini_portile.rb:279:in `block in execute': Failed to complete patch task (RuntimeError)
        from /usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.0/lib/mini_portile.rb:271:in `chdir'
        from /usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.0/lib/mini_portile.rb:271:in `execute'
        from extconf.rb:282:in `block in patch'
        from extconf.rb:279:in `each'
        from extconf.rb:279:in `patch'
        from /usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.0/lib/mini_portile.rb:108:in `cook'
        from extconf.rb:253:in `block in process_recipe'
        from extconf.rb:154:in `tap'
        from extconf.rb:154:in `process_recipe'
        from extconf.rb:419:in `<main>'
 
Gem files will remain installed in /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.2.1 for inspection.
Results logged to /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.2.1/ext/nokogiri/gem_make.out

aws-sdkが自動でインストールしようとするnokogiriのオプションだとエラーが起きるようだったので、nokogiriだけ個別にインストールします。

$ gem install nokogiri -- --use-system-libraries
Building native extensions with: '--use-system-libraries'
This could take a while...
Building nokogiri using system libraries.
Successfully installed nokogiri-1.6.2.1
Parsing documentation for nokogiri-1.6.2.1
Installing ri documentation for nokogiri-1.6.2.1
Done installing documentation for nokogiri after 4 seconds
1 gem installed

無事、インストール出来たので、改めてaws-sdkをインストール。

$ gem install aws-sdk
Fetching: aws-sdk-1.46.0.gem (100%)
Successfully installed aws-sdk-1.46.0
Parsing documentation for aws-sdk-1.46.0
Installing ri documentation for aws-sdk-1.46.0
Done installing documentation for aws-sdk after 16 seconds
1 gem installed

これでaws-sdkcapistrano環境で利用できるようになりました。めでたしめでたし。



因みに冒頭で記述した

capistranoで自分が立ち上げたec2インスタンスをデプロイ先として自動認識出来るようにする

については、
AWS EC2 capistranoでオートスケーリングインスタンスにデプロイ - cap version2 - Qiita
こちらのエントリーを参考にしました。
ということで、何かの参考になれば幸いです。
ではでは。

FuelPHP用のGoogleMapAPIアクセスパッケージを作成しました

https://github.com/hinashiki/fuelphp-map

こういうライブラリ的なリポジトリ作るのは初めてなのでお作法的に結構間違っている部分も多々あるかと思いますが、許して下さい。
MITライセンス(FuelPHP準拠)なので適当に使ってやってください。

MySQLの自動負荷検知&再起動のスクリプト組んでみた

これも必要にかられて。。。。
週末プログラマだと平日会社に要るときに異常があってDB死んだりしたら即終了(というか実際に起きたんですが)なので、
取り急ぎやばそうだった
LA、メモリ利用量、スレッド数
の3点をチェックし、超えていたら再起動をかけてやむなきを得(れたらいいな)るようにしてみました。
参考までに下に内容を載せておきます。

#!/bin/bash

#setting
mailto="hoge@fuga.com"
sbj="From db load monitoring tool."

# limitation num
thread_limit=150
la_limit=10
mem_limit=16

# common function
send_mail() {
  from=$1
  to=$2
  inputEncoding="utf-8"
  outputEncoding="iso-2022-jp"
  subjectHead="=?${outputEncoding}?B?"
  subjectBody="`echo "$3" | iconv -f ${inputEncoding} -t ${outputEncoding} | base64 | tr -d '\n'`"
  subjectTail="?="
  subject=${subjectHead}${subjectBody}${subjectTail}
  contents="`echo -e $4 | iconv -f ${inputEncoding} -t ${outputEncoding}`"
  echo "$contents" | mail -s "$subject" "$to" -- -f "$from"
  return $?
}


thread=`echo "show global status like 'Threads_connected%';" | ~/bin/mysql_connect.sh | grep connected | cut -f2`
ret=`echo "${thread} > ${thread_limit}" | bc`
if [ ${ret} -eq 1 ]; then
  send_mail "$mailto" "$mailto" "$sbj" "too many connection. mysql will be restart automatically."
  /etc/init.d/mysqld restart
  exit
fi

la=`uptime | sed -e "s/.\+load average: //g" | cut -f1 -d" " | cut -f1 -d","`
ret=`echo "${la} > ${la_limit}" | bc`
if [ ${ret} -eq 1 ]; then
  send_mail "$mailto" "$mailto" "$sbj" "load average is over. mysql will be restart automatically."
  /etc/init.d/mysqld restart
  exit
fi

mem=`free | grep Mem: | sed -e "s/ \+/\t/g" | cut -f3 | xargs -i ksh -c 'echo "scale=2;{}/1024/1024" | bc'`
ret=`echo "${mem} > ${mem_limit}" | bc`
if [ ${ret} -eq 1 ]; then
  send_mail "$mailto" "$mailto" "$sbj" "memory using rate is 50% over. mysql will be restart automatically."
  /etc/init.d/mysqld restart
  exit
fi

DRY的な観点でいうともっと簡素に書けやって感じなんですが
取り急ぎなスクリプトなのでもうこれでいいや。。。

そんな感じです。

正規表現の先読み、後読みを活用して"○○pt"の文字列を抽出する

泥臭かったのですが、これくらいしか思いつきませんでした。
自分で運営しているサイトでポイント表記があったのすが○○ptの部分を、他の文字に変更したかったのでどれ位変更する必要があったのか調査してみた。

$ find -type f -name '*.ctp' | xargs grep 'pt' | wc -l
1300

oh...無理。無理だよパパン。
しかも中身見たら「empty」とか「option」とか「javascript」とか書いてあるし。アカン。こんなん1300行もチェックしてられません。

というわけでうまい具合に
「○○pt」はチェックしたいけど上記のような特定のパターンは除外できないかと思って調べてみたら
Linux上で先読み、後読みを利用して除外していく方法が見つかりました。

$ find -type f -name '*.ctp' | xargs grep -P '(?<![sS]cri|i|javascri|em|acce|div-g)pt' | grep -P 'pt(?!ion)' | wc -l
35

おお、35行まで絞れた。これならイケる。
中身もすごくそれっぽいのとれた。すげー助かる。


今後はこんな事しないでいいように定数に変えておこう。。。死にたい。