読者です 読者をやめる 読者になる 読者になる

部屋を掃除したら漫画が沢山出てきたので書く日記

漫画とか合唱とかUNIXとかLinuxとかについて書く日記です。

Solaris11.1にHRForecastをインストール

perl UNIX

はじめに

kazeburoさん作成のHRForecastというグラフ作成ツールを使おうと思いました。理由としては

  • Perlで書かれている
  • 過去日付のデータも登録できる(同じくkazeburoさん作成のGrowthForecastには無い機能)

だからです。

で、会社のマイSolaris 11.1サーバにインストールしようとしたら苦労したのと、ネットで検索したら私が困った事の解決方法そのものズバリが見つからなかったので書いてみます。

前提条件

OS: Oracle Solaris 11.1 X86 64bit版
Perl: v5.18.1 on plenv 2.1.1
mysql: Ver 14.14 Distrib 5.1.37, for pc-solaris2.11 (i386) using readline 5.1
HRForecast: 2013年11月8日現在の最新版(86a47c399b)
CPANモジュールのインストールにはcpanmを使用する。
その他、gitとかcurlとかcpanmとか、登場するツールは全てインストール済みである事。

インストール

上記のkazeburoさんブログ記事に記載の通りに進めます。
githubから最新版をgit cloneで取得し、適当なディレクトリ以下の「HRForecast」というディレクトリ以下に格納します。

git clone https://github.com/kazeburo/HRForecast.git ./HRForecast

ディレクトリ以下に移動して、関連CPANモジュールをインストールします。

cd ./HRForecast
cpanm -n -lextlib --installdeps .

すると、モジュールのインストールが行われるのですが、
Building DBD-mysql-4.025 ... FAIL
と出力され、DBD::mysqlのビルドに失敗している事がわかります。

$ cpanm -n -lextlib --installdeps .
--> Working on .
Configuring HRForecast-0.01 ... OK
==> Found dependencies: Time::Piece::MySQL, Plack::Middleware::Header, DBD::mysql
--> Working on Time::Piece::MySQL
Fetching http://www.cpan.org/authors/id/K/KA/KASEI/Time-Piece-MySQL-0.06.tar.gz ... OK
Configuring Time-Piece-MySQL-0.06 ... OK
Building Time-Piece-MySQL-0.06 ... OK
Successfully installed Time-Piece-MySQL-0.06
--> Working on Plack::Middleware::Header
Fetching http://www.cpan.org/authors/id/C/CH/CHIBA/Plack-Middleware-Header-0.04.tar.gz ... OK
Configuring Plack-Middleware-Header-0.04 ... OK
Building Plack-Middleware-Header-0.04 ... OK
Successfully installed Plack-Middleware-Header-0.04
--> Working on DBD::mysql
Fetching http://www.cpan.org/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.025.tar.gz ... OK
Configuring DBD-mysql-4.025 ... OK
Building DBD-mysql-4.025 ... FAIL
! Installing DBD::mysql failed. See /export/home/bata64/.cpanm/work/1383915723.15226/build.log for details. Retry with --force to force install it.
! Installing the dependencies failed: Module 'DBD::mysql' is not installed
! Bailing out the installation for HRForecast-0.01.
2 distributions installed

! Installing DBD::mysql failed. See /export/home/bata64/.cpanm/work/1383915723.15226/build.log for details. Retry with --force to force install it.
出力されている通り、.cpanmディレクトリ以下の「.cpanm/work/1383915723.15226/build.log」を参照してみると、以下のように出力されていました。

cpanm (App::cpanminus) 1.7001 on perl 5.018001 built for i86pc-solaris
Work directory is /export/home/bata64/.cpanm/work/1383915723.15226
You have make /usr/gnu/bin/make
You have LWP 6.05
You have /usr/gnu/bin/tar, /usr/bin/gzip and /usr/bin/bzip2
You have /usr/bin/unzip
Checking if you have ExtUtils::MakeMaker 6.31 ... Yes (6.66)
Checking if you have ExtUtils::Install 1.46 ... Yes (1.59)
--> Working on .
Entering /export/home/bata64/pl/HRForecast_5_18_1
Checking configure dependencies from META.json
Checking if you have CPAN::Meta::Prereqs 0 ... Yes (2.120921)
Checking if you have Module::Build 0.38 ... Yes (0.4003)
Checking if you have CPAN::Meta 0 ... Yes (2.120921)
Checking if you have Module::Build 0.36 ... Yes (0.4003)
Configuring HRForecast-0.01
-> OK
Checking if you have Plack::Middleware::Scope::Container 0.01 ... Yes (0.04)
Checking if you have Kossy 0.14 ... Yes (0.25)
Checking if you have Time::Piece::MySQL 0.06 ... No
Checking if you have Plack::Middleware::Header 0.04 ... No
Checking if you have List::MoreUtils 0 ... Yes (0.33)
Checking if you have HTTP::Date 0 ... Yes (6.02)
Checking if you have Parallel::Scoreboard 0.03 ... Yes (0.04)
Checking if you have Time::Piece 1.15 ... Yes (1.20_01)
Checking if you have Starlet 0.14 ... Yes (0.20)
Checking if you have DBD::mysql 4.018 ... No
Checking if you have Scope::Container::DBI 0.03 ... Yes (0.09)
Checking if you have Log::Minimal 0.09 ... Yes (0.16)
Checking if you have DBIx::Sunny 0.14 ... Yes (0.21)
Checking if you have Plack::Builder::Conditionals 0.03 ... Yes (0.05)
==> Found dependencies: Time::Piece::MySQL, Plack::Middleware::Header, DBD::mysql
Searching Time::Piece::MySQL on cpanmetadb ...
--> Working on Time::Piece::MySQL
Fetching http://www.cpan.org/authors/id/K/KA/KASEI/Time-Piece-MySQL-0.06.tar.gz
-> OK
Unpacking Time-Piece-MySQL-0.06.tar.gz
Entering Time-Piece-MySQL-0.06
Checking configure dependencies from META.yml
Configuring Time-Piece-MySQL-0.06
Running Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Time::Piece::MySQL
Writing MYMETA.yml and MYMETA.json
-> OK
Checking dependencies from MYMETA.json ...
Checking if you have Time::Piece 1.03 ... Yes (1.20_01)
Checking if you have ExtUtils::MakeMaker 0 ... Yes (6.66)
Checking if you have Test::More 0.47 ... Yes (0.98)
Building Time-Piece-MySQL-0.06
cp lib/Time/Piece/MySQL.pm blib/lib/Time/Piece/MySQL.pm
Manifying blib/man3/Time::Piece::MySQL.3
Manifying blib/man3/Time::Piece::MySQL.3
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/Time/Piece/MySQL.pm
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/man/man3/Time::Piece::MySQL.3
Appending installation info to /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/i86pc-solaris/perllocal.pod
-> OK
Successfully installed Time-Piece-MySQL-0.06
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/i86pc-solaris/.meta/Time-Piece-MySQL-0.06/install.json
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/i86pc-solaris/.meta/Time-Piece-MySQL-0.06/MYMETA.json
Searching Plack::Middleware::Header on cpanmetadb ...
--> Working on Plack::Middleware::Header
Fetching http://www.cpan.org/authors/id/C/CH/CHIBA/Plack-Middleware-Header-0.04.tar.gz
-> OK
Unpacking Plack-Middleware-Header-0.04.tar.gz
Entering Plack-Middleware-Header-0.04
Checking configure dependencies from META.yml
Checking if you have ExtUtils::MakeMaker 6.42 ... Yes (6.66)
Configuring Plack-Middleware-Header-0.04
Running Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Plack::Middleware::Header
Writing MYMETA.yml and MYMETA.json
-> OK
Checking dependencies from MYMETA.json ...
Checking if you have Filter::Util::Call 0 ... Yes (1.45)
Checking if you have Plack::Middleware 0 ... Yes (undef)
Checking if you have Test::More 0 ... Yes (0.98)
Checking if you have ExtUtils::MakeMaker 6.42 ... Yes (6.66)
Checking if you have parent 0 ... Yes (0.225)
Building Plack-Middleware-Header-0.04
cp lib/Plack/Middleware/Header.pm blib/lib/Plack/Middleware/Header.pm
Manifying blib/man3/Plack::Middleware::Header.3
Manifying blib/man3/Plack::Middleware::Header.3
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/Plack/Middleware/Header.pm
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/man/man3/Plack::Middleware::Header.3
Appending installation info to /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/i86pc-solaris/perllocal.pod
-> OK
Successfully installed Plack-Middleware-Header-0.04
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/i86pc-solaris/.meta/Plack-Middleware-Header-0.04/MYMETA.json
Installing /export/home/bata64/pl/HRForecast_5_18_1/extlib/lib/perl5/i86pc-solaris/.meta/Plack-Middleware-Header-0.04/install.json
Searching DBD::mysql on cpanmetadb ...
--> Working on DBD::mysql
Fetching http://www.cpan.org/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.025.tar.gz
-> OK
Unpacking DBD-mysql-4.025.tar.gz
Entering DBD-mysql-4.025
Checking configure dependencies from META.yml
Checking if you have DBI 1.08 ... Yes (1.630)
Configuring DBD-mysql-4.025
Running Makefile.PL
Can't exec "mysql_config": No such file or directory at Makefile.PL line 70.

Cannot find the file 'mysql_config'! Your execution PATH doesn't seem
not contain the path to mysql_config. Resorting to guessed values!
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located


PLEASE NOTE:

For 'make test' to run properly, you must ensure that the
database user 'bata64' can connect to your MySQL server
and has the proper privileges that these tests require such
as 'drop table', 'create table', 'drop procedure', 'create procedure'
as well as others.

mysql> grant all privileges on test.* to 'bata64'@'localhost' identified by 's3kr1t';

You can also optionally set the user to run 'make test' with:

perl Makefile.PL --testuser=username

Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
Can't exec "mysql_config": No such file or directory at Makefile.PL line 473.
Can't find mysql_config. Use --mysql_config option to specify where mysql_config is located
I will use the following settings for compiling and testing:

  cflags        (guessed) = -I/usr/local/mysql/include
  embedded      (guessed) =
  libs          (guessed) = -L/usr/local/mysql/lib -lmysqlclient -lz -lm -lcrypt -lnsl
  mysql_config  (guessed) = mysql_config
  nocatchstderr (default) = 0
  nofoundrows   (default) = 0
  ssl           (guessed) = 0
  testdb        (default) = test
  testhost      (default) =
  testpassword  (default) =
  testport      (default) =
  testsocket    (default) =
  testuser      (guessed) = bata64

To change these settings, see 'perl Makefile.PL --help' and
'perldoc DBD::mysql::INSTALL'.

Checking if your kit is complete...
Warning: the following files are missing in your kit:
        META.json
        t/mem_leak.pl
Please inform the author.
Using DBI 1.63 (for perl 5.018001 on i86pc-solaris) installed in /export/home/bata64/.plenv/versions/5.18.1/lib/perl5/site_perl/5.18.1/i86pc-solaris/auto/DBI/
Writing Makefile for DBD::mysql
Writing MYMETA.yml and MYMETA.json
-> OK
Checking dependencies from MYMETA.json ...
Checking if you have Test::More 0 ... Yes (0.98)
Checking if you have Data::Dumper 0 ... Yes (2.145)
Checking if you have Test::Deep 0 ... Yes (0.110)
Checking if you have DBI 1.08 ... Yes (1.630)
Building DBD-mysql-4.025
cp lib/DBD/mysql/INSTALL.pod blib/lib/DBD/mysql/INSTALL.pod
cp README.pod blib/lib/DBD/README.pod
cp lib/DBD/mysql.pm blib/lib/DBD/mysql.pm
cp lib/DBD/mysql/GetInfo.pm blib/lib/DBD/mysql/GetInfo.pm
cp lib/Bundle/DBD/mysql.pm blib/lib/Bundle/DBD/mysql.pm
cc -c  -I/export/home/bata64/.plenv/versions/5.18.1/lib/perl5/site_perl/5.18.1/i86pc-solaris/auto/DBI -I/usr/local/mysql/include -DDBD_MYSQL_INSERT_ID_IS_GOOD -g  -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DPERL_USE_SAFE_PUTENV -O   -DVERSION=\"4.025\" -DXS_VERSION=\"4.025\" -fPIC "-I/export/home/bata64/.plenv/versions/5.18.1/lib/perl5/5.18.1/i86pc-solaris/CORE"   dbdimp.c
dbdimp.c: In function ‘mysql_describe’:
dbdimp.c:3661:22: warning: assignment from incompatible pointer type
/export/home/bata64/.plenv/versions/5.18.1/bin/perl5.18.1 -p -e "s/~DRIVER~/mysql/g" /export/home/bata64/.plenv/versions/5.18.1/lib/perl5/site_perl/5.18.1/i86pc-solaris/auto/DBI/Driver.xst > mysql.xsi
/export/home/bata64/.plenv/versions/5.18.1/bin/perl5.18.1 /export/home/bata64/.plenv/versions/5.18.1/lib/perl5/5.18.1/ExtUtils/xsubpp  -typemap /export/home/bata64/.plenv/versions/5.18.1/lib/perl5/5.18.1/ExtUtils/typemap  mysql.xs > mysql.xsc && mv mysql.xsc mysql.c
Warning: duplicate function definition 'do' detected in mysql.xs, line 242
Warning: duplicate function definition 'rows' detected in mysql.xs, line 754
cc -c  -I/export/home/bata64/.plenv/versions/5.18.1/lib/perl5/site_perl/5.18.1/i86pc-solaris/auto/DBI -I/usr/local/mysql/include -DDBD_MYSQL_INSERT_ID_IS_GOOD -g  -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DPERL_USE_SAFE_PUTENV -O   -DVERSION=\"4.025\" -DXS_VERSION=\"4.025\" -fPIC "-I/export/home/bata64/.plenv/versions/5.18.1/lib/perl5/5.18.1/i86pc-solaris/CORE"   mysql.c
Running Mkbootstrap for DBD::mysql ()
chmod 644 mysql.bs
rm -f blib/arch/auto/DBD/mysql/mysql.so
LD_RUN_PATH="/usr/local/mysql/lib:/usr/local/lib:/usr/lib" /export/home/bata64/.plenv/versions/5.18.1/bin/perl5.18.1 myld cc  -shared -L/usr/local/lib -L/usr/gnu/lib -fstack-protector dbdimp.o mysql.o  -o blib/arch/auto/DBD/mysql/mysql.so        \
   -L/usr/local/mysql/lib -lmysqlclient -lz -lm -lcrypt -lnsl   \

ld: fatal: file /usr/local/mysql/lib/libmysqlclient.so: wrong ELF class: ELFCLASS64
ld: fatal: file processing errors. No output written to blib/arch/auto/DBD/mysql/mysql.so
collect2: ld returned 1 exit status
make: *** [blib/arch/auto/DBD/mysql/mysql.so] エラー 1
-> FAIL Installing DBD::mysql failed. See /export/home/bata64/.cpanm/work/1383915723.15226/build.log for details. Retry with --force to force install it.
-> FAIL Installing the dependencies failed: Module 'DBD::mysql' is not installed
-> FAIL Bailing out the installation for HRForecast-0.01.
2 distributions installed

とりあえず
ld: fatal: file /usr/local/mysql/lib/libmysqlclient.so: wrong ELF class: ELFCLASS64
あたりが問題と思ったのでいろいろ調べました。ここら辺は長くなるので別の記事
Solaris11.1にDBD::mysqlをインストール - 部屋を掃除したら漫画が沢山出てきたので書く日記
で説明しますが、結論だけ述べると以下のようにしてDBD::mysqlの再インストールを行いました。

cpanm DBD::mysql -L extlib -n --configure-args="CC=gcc --mysql_config=/usr/mysql/bin/mysql_config --with-mysql=/usr/mysql/5.1 --cflags=\"-I/usr/mysql/include/mysql -m32 -DHAVE_RWLOCK_T -DUNIV_SOLARIS\""

すると以下のように出力され、インストールに成功しました。

$ cpanm DBD::mysql -L extlib -n --configure-args="CC=gcc --mysql_config=/usr/mysql/bin/mysql_config --with-mysql=/usr/mysql/5.1 --cflags=\"-I/usr/mysql/include/mysql -m32 -DHAVE_RWLOCK_T -DUNIV_SOLARIS\""
--> Working on DBD::mysql
Fetching http://www.cpan.org/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.025.tar.gz ... OK
==> Found dependencies: DBI
--> Working on DBI
Fetching http://www.cpan.org/authors/id/T/TI/TIMB/DBI-1.630.tar.gz ... OK
Configuring DBI-1.630 ... OK
Building DBI-1.630 ... OK
Successfully installed DBI-1.630
Configuring DBD-mysql-4.025 ... OK
==> Found dependencies: Test::Deep
--> Working on Test::Deep
Fetching http://www.cpan.org/authors/id/R/RJ/RJBS/Test-Deep-0.110.tar.gz ... OK
Configuring Test-Deep-0.110 ... OK
==> Found dependencies: Test::Tester, Test::NoWarnings
--> Working on Test::Tester
Fetching http://www.cpan.org/authors/id/F/FD/FDALY/Test-Tester-0.109.tar.gz ... OK
Configuring Test-Tester-0.109 ... OK
Building Test-Tester-0.109 ... OK
Successfully installed Test-Tester-0.109
--> Working on Test::NoWarnings
Fetching http://www.cpan.org/authors/id/A/AD/ADAMK/Test-NoWarnings-1.04.tar.gz ... OK
Configuring Test-NoWarnings-1.04 ... OK
Building Test-NoWarnings-1.04 ... OK
Successfully installed Test-NoWarnings-1.04
Building Test-Deep-0.110 ... OK
Successfully installed Test-Deep-0.110
Building DBD-mysql-4.025 ... OK
Successfully installed DBD-mysql-4.025
5 distributions installed

この呪文のような長さのconfigureオプションを編み出すのに苦労しましたが、ともあれこれで必要なCPANモジュールのインストールに成功しました。

mysqlの設定

次はmysqlの設定です。これも作者ブログ記事の通りです。
まず、MySQLにHRForecast用のテーブルを作成し、用意されているschema.sqlファイルの内容通りに設定を行います。

sudo mysql
mysql> create database hrforecast;
mysql> quit
cat schema.sql | sudo mysql hrforecast

次に、作成されたテーブルに関する権限を、mysqlのbata64ユーザに対して付与します。

sudo mysql
mysql> grant all on hrforecast.* to 'bata64';
mysql> quit

設定ファイルの編集

HRForecastの設定ファイル「config.pl」に各種設定を記述します。

vi config.pl

以下の通りに編集します。

{
    dsn => 'dbi:mysql:hrforecast;hostname=127.0.0.1',
    username => 'bata64', #dbのユーザ名
    password => 'password', #dbのパスワード
    port => '5127', #bindするport
    host => '0.0.0.0', #bindするIP
    front_proxy => [],
    allow_from => [],
};

これで準備は完了です。

起動してみる

以下のコマンドを実行します。

perl hrforecast.pl --config config.pl

ブラウザで

http://bata64.com:5127

にアクセスしてみると、以下のようにHRForecastの画面が表示されました。やったね。
f:id:bata64:20131108224057p:plain

データを登録してみる

データは以下のURIに対してPOSTリクエストを行う事で登録されます。

http://bata64.com:5127/api/:service_name/:section_name/:graph_name

service_name、section_nameおよびgraph_nameは、グラフの種類を考慮して分かりやすいものを任意でつけられます。
が、ここでは試しに、

http://bata64.com:5127/api/テスト1/テスト2/テスト3

としてみます。
以下のPerlスクリプトを作成し、実行します。全角文字も、UTF-8であれば登録できるようです。

$ cat HRForecast_POST.pl
use strict;
use warnings;

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->post('http://bata64.com:5127/api/テスト1/テスト2/テスト3', {
            number      => 1_000_000,
            datetime    => '2013-11-08 20:00:00'
            });

で、実行します。

perl HRForecast_POST.pl

グラフが分かりやすくなるように、もう一つプロットします。今度は

                    number      => 100,
                    datetime    => '2013-10-08 19:00:00'

と変更して、再度実行します。
左上の「グラフ2」というリンクをクリックし、「1ヶ月」という時間幅にすると、以下の通りのグラフが表示されました。
f:id:bata64:20131108225619p:plain

これからやりたい事

以前書いた、朝測定した体重をはてなグラフにメール投稿するプログラムの応用で、Perlスクリプトからグラフにデータを登録できそうに思えます。
グラフ化したいデータが入っているCSVファイルがたくさんあるので、CSVファイルからデータ読みとって登録するプログラムをこれから作ってみようと思います。

ではー。