ITお絵かき修行

3歩歩いても忘れないために

RHELにFnordmetricインストール

公式に書かれているインストール手順(というかコマンド)「yum install fnordmetric」を実行してもインストールできなかったので、手順をまとめた。

基本的には下のチュートリアルに沿ってインストール・設定する。
FnordMetric | Getting Started | Documentation

It assumes that you have ruby and a redis server on port 6379 running on your machine.

とあるので、先にRubyとRedis、その他必要なライブラリをインストールし、Fnordmetricをインストールする。


【前提】
・動作環境としてRed Hat Enterprise Linux 6.5 (PV) が用意済。


【インストール手順】

0.準備
(1)SELinuxの無効化

vi /etc/selinux/config

SELINUX=disabled


(2)yumコマンドのアップデート

yum update


(3)zsh、make、gccvim、tcl、wgetをインストールする。
RHELにはmakeはインストールされていたので、vimzshgcc、tcl、wgetをインストールする。

yum install vim -y
yum install zsh -y
yum install gcc -y
yum install tcl -y
yum install wget -y



1.Redisをインストールする。 
[参考]
Redis
Redis使おうとしていろいろやったメモ - Qiita

(1)パッケージをwgetしてmakeコマンドでインストールする。

wget http://download.redis.io/releases/redis-2.8.9.tar.gz
tar xzf redis-2.8.9.tar.gz
cd redis-2.8.9
make install

※以下が表示され、redis-cliコンパイルされていない場合はSELinuxがdisabledにされていない。
f:id:hhhhhskw:20140601194542p:plain

インストールに成功すると以下の表示がされ、redis-serverコマンド、redis-cliコマンド等が使えるようになる。
f:id:hhhhhskw:20140601194638p:plain

(2)redis-serverコマンドを実行後、redis-cliコマンドで動作確認する。

[root@ip-xxx-xxx-xxx-xxx src]# ./redis-server
[12888] 03 Jun 09:26:04.406 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
[12888] 03 Jun 09:26:04.407 * Increased maximum number of open files to 10032 (it was originally set to 1024).


[root@ip-xxx-xxx-xxx-xxx src]# ./redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379>
127.0.0.1:6379>
[root@ip-xxx-xxx-xxx-xxx src]#


2.RubyRubyGemsをインストールする。
ruby.gemコマンドが正常に動くようになればOK

[root@ip-xxx-xxx-xxx-xxx ~]# ruby -v
ruby 1.9.3p547 (2014-05-14 revision 45962) [x86_64-linux]
[root@ip-xxx-xxx-xxx-xxx ~]# gem -v
1.8.23.2
[root@ip-xxx-xxx-xxx-xxx ~]#

yumコマンドでインストールできるRubyは1.8.7であるため、1.9.3をwgetしてインストールする。
 Ruby1.8.7にてFnordmetricをインストールすると以下のエラーが出力される。

ERROR:  Error installing fnordmetric:
        activesupport requires Ruby version >= 1.9.3.

手順は下記サイト参照。
さくらのレンタルサーバに Ruby 1.9.3 と gem をインストール - TMD45INC!!!
Install Ruby 1.9.3 with libyaml on CentOS // Collective Idea | Crafting web and mobile software based in Holland, Michigan

Linux 環境変数を設定する方法(PATHの設定)を、わかりやすく解説 - web新参
Ruby1.9.3をインストールしたときのいろいろなエラー - yokochiの開発メモ

※上記ページ以外ではrpmbuildコマンドを使った方法を推奨しているサイトもあるが、時間がかかるうえに面倒なのでオススメしない。
※「libyaml-devel」のインストール忘れに注意する。


3.Fnordmetricのインストールに必要なライブラリをインストールする。
(1)ruby-develをインストールする。

yum install ruby-devel -y

ruby-develをいれずにFnordmetricをインストールすると以下のエラーが出力される。
[参考]
「ruby extconf.rb」で「can't find header files for ruby」エラー - Life with IT

[root@ip-xxx-xxx-xxx-xxx ~]# tail -f nohup.out
ERROR:  Error installing fnordmetric:
        ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/eventmachine-1.0.3 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/eventmachine-1.0.3/ext/gem_make.out
Building native extensions.  This could take a while...

(2)g++をインストールする。

yum install gcc-c++

※g++をインストールしないとFnordmetricインストール時に以下のエラーが出力される。

make: g++: Command not found
make: *** [cmain.o] Error 127


(3)gemコマンドが遅いので/etc/resolv.confに以下のオプションを追記する。
※参考:
Vagrant+VirtualBox(CentOS6)で「gem install rails」がすっごい遅い時の対処法 | IDEA*IDEA
RHEL6/CentOS6では、single-request-reopen を必須にしたい…

vim /etc/resolv.conf

下の1文を追記する。

options single-request-reopen


(4)zlibをインストールする。手順は下記サイト参照。
[参考]zlibのインストール - [Linux [Red Hatなど]/サーバー] ぺんたん info

※zlibをインストールしないと以下のエ(ry

It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::InstallCommand


4.Fnordmetricをインストール・実行する。
(1)インストールする。
→コマンドのオプションで不要なファイルを出力しないよう指定する。地味に時間がかかったので念のためバックグランドプロセスとして実行する。
[参考]
CodeZine(コードジン)

nohup gem install fnordmetric --no-ri --no-rdoc &

Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...

HEADS UP! Haml 4.0 has many improvements, but also has changes that may break
your application:

* Support for Ruby 1.8.6 dropped
* Support for Rails 2 dropped
* Sass filter now always outputs <style> tags
* Data attributes are now hyphenated, not underscored
* html2haml utility moved to the html2haml gem
* Textile and Maruku filters moved to the haml-contrib gem

For more info see:

http://rubydoc.info/github/haml/haml/file/CHANGELOG.md

Building native extensions.  This could take a while...
Successfully installed rack-1.5.2
Successfully installed tilt-1.4.1
Successfully installed rack-protection-1.5.3
Successfully installed sinatra-1.4.5
Successfully installed redis-3.0.7
Successfully installed eventmachine-1.0.3
Successfully installed addressable-2.3.6
Successfully installed em-websocket-0.3.8
Successfully installed daemons-1.1.9
Successfully installed thin-1.6.2
Successfully installed websocket-rack-0.4.0
Successfully installed hiredis-0.4.5
Successfully installed em-hiredis-0.2.1
Successfully installed json-1.8.1
Successfully installed i18n-0.6.9
Successfully installed haml-4.0.5
Successfully installed rack-test-0.6.2
Successfully installed yajl-ruby-1.2.0
Successfully installed thread_safe-0.3.4
Successfully installed tzinfo-1.2.1
Successfully installed minitest-5.3.4
Successfully installed activesupport-4.1.1
Successfully installed fnordmetric-1.2.9
23 gems installed
[root@ip-xxx-xxx-xxx-xxx ~]# cd /usr/lib/ruby/



(2)実行ファイル(my_fnordmetric.rb)を用意する。

[root@ip-xxx-xxx-xxx-xxx fnordmetric]# vim my_fnordmetric.rb

require "fnordmetric"

FnordMetric.namespace :myapp do

  # render a timeseries graph
  widget 'Sales',
    :title => "Sales per Minute",
    :gauges => [:sales_per_minute],
    :type => :timeline,
    :width => 100,
    :autoupdate => 1

end

FnordMetric.standalone


(3)実行(Fnordmetricのプロセス起動)する。

ruby my_fnordmetric.rb

実行直後のメッセージ

[root@ip-xxx-xxx-xxx-xxx fnordmetric]# ruby my_fnordmetric.rb
DEPRECATION WARNING - FIXPAUL
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on 0.0.0.0:4242, CTRL+C to stop
[14-06-02 11:31:04] listening on http://0.0.0.0:4242
[14-06-02 11:31:04] worker started


Redisが起動していないと以下のエラーが出力される。

Redis::CannotConnectError - Error connecting to Redis on localhost:6379 (ECONNREFUSED):
        /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.7/lib/redis/client.rb:290:in `rescue in establish_connection'
        /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.7/lib/redis/client.rb:285:in `establish_connection'
        /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.7/lib/redis/client.rb:79:in `block in connect'
        /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.7/lib/redis/client.rb:257:in `with_reconnect'
        /usr/local/lib/ruby/gems/1.9.1/gems/redis-3.0.7/lib/redis/client.rb:78:in `connect'


(4)FnordmetricにJSONデータをPOSTする。

curl -X POST -d '{ "_type": "_incr", "value": 1, "gauge": "sales_per_minute" }' http://localhost:4242/events

何回かアクセスした結果が以下の通り。

http://(Fnordmetricが入ったサーバのIPアドレスorホスト名):4242

f:id:hhhhhskw:20140608145144p:plain
※「Select a Gauge」の箇所に「sales_per_minute」と入力する。

Fnordmetric側のログ

127.0.0.1 - - [08/Jun/2014 01:38:36] "POST /events HTTP/1.1" 200 - 0.0019
127.0.0.1 - - [08/Jun/2014 01:38:37] "POST /events HTTP/1.1" 200 - 0.0020
127.0.0.1 - - [08/Jun/2014 01:38:37] "POST /events HTTP/1.1" 200 - 0.0019
127.0.0.1 - - [08/Jun/2014 01:38:43] "POST /events HTTP/1.1" 200 - 0.0020

Redis側のログ

[9769] 08 Jun 01:39:33.456 * 100 changes in 300 seconds. Saving...
[9769] 08 Jun 01:39:33.457 * Background saving started by pid 9952
[9952] 08 Jun 01:39:33.481 * DB saved on disk
[9952] 08 Jun 01:39:33.482 * RDB: 0 MB of memory used by copy-on-write
[9769] 08 Jun 01:39:33.557 * Background saving terminated with success
[9769] 08 Jun 01:45:25.422 * 100 changes in 300 seconds. Saving...
[9769] 08 Jun 01:45:25.423 * Background saving started by pid 10013
[10013] 08 Jun 01:45:25.433 * DB saved on disk
[10013] 08 Jun 01:45:25.434 * RDB: 0 MB of memory used by copy-on-write
[9769] 08 Jun 01:45:25.523 * Background saving terminated with success


【感想】
・すっぴんLinux(RHEL)から「yum install fnordmetric」までが遠すぎた(そもそもyumリポジトリにFnordmetricがないんですがそれは)



WEB+DB PRESS Vol.76

WEB+DB PRESS Vol.76

  • 作者: 五十嵐啓人,伊野亘輝,近藤宇智朗,渡邊恵太,須藤耕平,中島聡,A-Listers,はまちや2,川添貴生,片山育美,池田拓司,濱崎健吾,佐藤太一,曾川景介,久保渓,門脇恒平,登尾徳誠,伊藤直也,mala,後藤秀宣,若原祥正,奥野幹也,大林源,WEB+DB PRESS編集部
  • 出版社/メーカー: 技術評論社
  • 発売日: 2013/08/24
  • メディア: 大型本
  • この商品を含むブログを見る