Friday, August 15, 2014

erlang init restart issue

Create file reltool.config:
{sys, [{erts, [{mod_cond, derived}, {app_file, strip}]},
   {app_file, strip},
   {rel, "test", "1", []},
   {boot_rel, "test"},
   {profile, embedded},
   {incl_cond, exclude},
   {app, kernel, [{incl_cond, include}]},
   {app, stdlib, [{incl_cond, include}]},
   {app, crypto, [{incl_cond, include}]}
  ]}.
Create an Erlang release named ‘test’ from ‘reltool.conig’ above:
BASH> erl -eval '{ok, [Config]} = file:consult("reltool.config"),{ok, Spec} = reltool:get_target_spec([Config]), _ = file:make_dir("test"), reltool:eval_target_spec(Spec, code:root_dir(), "test").' -s init stop -noshell
Now bring up the node and crash it:
BASH> export BINDIR=$(pwd)/test/erts-6.0.1/bin && $BINDIR/erlexec -boot test/releases/1/test -mode embedded
Erlang/OTP 17 [erts-6.0.1] [source-57e57ad] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.0.1  (abort with ^G)
1> init:restart().
ok
2> {"init terminating in do_boot",{'cannot load',crypto,get_files}}

Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Problematic code in init.erl
load_mod_code(Mod, BinCode, FullName) ->
    case erlang:module_loaded(Mod) of
        false ->
            case erlang:load_module(Mod, BinCode) of
                {module,Mod} -> {ok,FullName};
                {error,on_load} ->
                    ?ON_LOAD_HANDLER ! {loaded,Mod},
                    {ok,FullName};
                Other ->
                    exit({'cannot load',Mod,Other})
            end; 
        _ -> % Already loaded.
            {ok,FullName}
    end. 
?ON_LOAD_HANDLER ! {loaded,Mod}, crashed with ‘badarg’ because ?ON_LOAD_HANDLER was not alive 
?ON_LOAD_HANDLER is not started for init:restart, bug ? 
If boot the node with -init_dbug flag, below log can be observed, but not during init:restart
{running_on_load_handler,crypto}
{on_load_handler_returned_ok,crypto}
In fact, if any of the -on_load(…) modules gets loaded when booting, init:restart won’t work!
bootstrap/lib/asn1/src/asn1rt_nif.erl:-on_load(load_nif/0).
lib/runtime_tools/src/dyntrace.erl:-on_load(on_load/0).
lib/crypto/src/crypto.erl:-on_load(on_load/0).
lib/asn1/src/asn1rt_nif.erl:-on_load(load_nif/0).
erts/example/matrix_nif.erl:-on_load(on_load/0).
Try asn1 for example:
BASH> export BINDIR=$(pwd)/asn1/erts-6.0.1/bin && $BINDIR/erlexec -boot asn1/releases/1/asn1 -mode embedded -init_debug
{progress,preloaded}
{progress,kernel_load_completed}
{progress,modules_loaded}
{start,heart}
{start,error_logger}
{start,application_controller}
{progress,init_kernel_started}
{apply,{application,load,[{application,stdlib,[{description,"ERTS  CXC 138 10"},{vsn,"2.0"},{id,[]},{modules,[array,base64,beam_lib,binary,c,calendar,dets,dets_server,dets_sup,dets_utils,dets_v8,dets_v9,dict,digraph,digraph_utils,edlin,edlin_expand,epp,eval_bits,erl_bits,erl_compile,erl_eval,erl_expand_records,erl_internal,erl_lint,erl_parse,erl_posix_msg,erl_pp,erl_scan,erl_tar,error_logger_file_h,error_logger_tty_h,escript,ets,file_sorter,filelib,filename,gb_trees,gb_sets,gen,gen_event,gen_fsm,gen_server,io,io_lib,io_lib_format,io_lib_fread,io_lib_pretty,lib,lists,log_mf_h,maps,math,ms_transform,orddict,ordsets,otp_internal,pg,pool,proc_lib,proplists,qlc,qlc_pt,queue,random,re,sets,shell,shell_default,slave,sofs,string,supervisor,supervisor_bridge,sys,timer,unicode,win32reg,zip]},{registered,[timer_server,rsh_starter,take_over_monitor,pool_master,dets]},{applications,[kernel]},{included_applications,[]},{env,[]},{maxT,infinity},{maxP,infinity}]}]}}
{apply,{application,load,[{application,asn1,[{description,"The Erlang ASN1 compiler version 3.0"},{vsn,"3.0"},{id,[]},{modules,[asn1rt,asn1rt_nif]},{registered,[asn1_ns,asn1db]},{applications,[kernel,stdlib]},{included_applications,[]},{env,[]},{maxT,infinity},{maxP,infinity}]}]}}
{progress,applications_loaded}
{apply,{application,start_boot,[kernel,permanent]}}
Erlang/OTP 17 [erts-6.0.1] [source-57e57ad] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

{running_on_load_handler,asn1rt_nif}
{on_load_handler_returned_ok,asn1rt_nif}
{apply,{application,start_boot,[stdlib,permanent]}}
{apply,{c,erlangrc,[]}}
{progress,started}
Eshell V6.0.1  (abort with ^G)
1> 
1> 
1> init:restart().
ok
2> {progress,preloaded}
{progress,kernel_load_completed}
{"init terminating in do_boot",{'cannot load',asn1rt_nif,get_files}}

Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Written with StackEdit.

Friday, July 18, 2014

Erlang unixODBC to Oracle


Tested in CentOS 6.5, OTP-R15B03, Oracle 11.2
NOTE examples below assume that:
Oracle server/client are both installed on localhost as instructions here:
http://zmstone.blogspot.se/2014/07/oracle-11g-serverclient-in-cetos-65.html
Test database, username and passwords are created as instructions here:
http://zmstone.blogspot.se/2014/07/create-test-database-and-roles-in-fresh.html
  1. Install some possibly missing clients/libraries [1]
    oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
    oracle-instantclient11.2-odbc-11.2.0.4.0-1.x86_64.rpm
  2. Install unixODBC
    1. download unixODBC-2.3.2.tar.gz from http://www.unixodbc.org/
    2. build unixODBC:
      ./configure --prefix=/usr --sysconfdir=/etc --enable-gui=no --enable-drivers=no --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE
      # enable iconv library to make it work with unicode.
      make
      # if everything goes fine
      sudo make install
  3. Edit unixODBC config files
    1. /etc/odbc.ini
      [TEST]
      Driver          = Oracle-11g
      ServerName      = //localhost:1521
      Database        = test
      
    2. append to /etc/odbcinst.ini
      [Oracle-11g]
      Description     = Oracle ODBC driver for Oracle 11g
      Driver          = /usr/lib/oracle/11.2/client64/lib/libsqora.so.11.1
      Driver64        = /usr/lib/oracle/11.2/client64/lib/libsqora.so.11.1
      Setup           = 
      Setup64         = 
      UsageCount      = 
      CPTimeout       = 
      CPReuse         = 
      FileUsage       = 1 
      Driver Logging  = 7
      
  4. Test unixODBC oracle using isql
    BASH> isql -v test tester1 SECRET
    [01000][unixODBC][Driver Manager]Can't open lib '/usr/lib/oracle/11.2/client64/lib/libsqora.so.11.1' : file not found
    [ISQL]ERROR: Could not SQLConnect
    This does not necessarily mean libsqora.so.11.1 is missing, as it's been investigated in [2], it's probably libodbcinst.so.1 missing.
    To fix it, LD_LIBRARY_PATH should be updated:
    export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib:/usr/lib64
    # and make a soft link like below, since it's unixODBC-2.3.2 we installed,
    # but libsqora.so is looking for libodbcinst.so.1
    sudo ln -s /usr/lib64/libodbcinst.so.2.0.0 /usr/lib64/libodbcinst.so.1
    
    Now it should work (tab0 in the example below is created here: http://zmstone.blogspot.se/2014/07/create-test-database-and-roles-in-fresh.html)
    BASH> isql -v test tester1 SECRET
    +---------------------------------------+
    | Connected!                            |
    |                                       |
    | sql-statement                         |
    | help [tablename]                      |
    | quit                                  |
    |                                       |
    +---------------------------------------+
    SQL> select * from tab0;
    +-------------+---------------------------------+
    | K           | V                               |
    +-------------+---------------------------------+
    | 1           | test value                      |
    +-------------+---------------------------------+
    SQLRowCount returns -1
    1 rows fetched
    
  5. Try connect to oracle from Erlang shell [3]
    NOTE: If Erlang OTP was built from source code, and lib/erlang/lib/odbc-* can not be found in OTP install directory, you'll have to install libodbc first, then redo configure and make install.
    BASH> erl
    Erlang R15B03 ......
    
    Eshell V5.9.3.1  (abort with ^G)
    1> odbc:start().
    ok
    2> {ok, Pid} = odbc:connect("DSN=test;UID=tester1;PWD=SECRET", [{scrollable_cursors, off}]).
    {ok,<0 data-blogger-escaped-.39.0="">}
    3> odbc:sql_query(Pid, "select * from tab0").    
    {selected,["K","V"],[{1.0,"test value"}]}
References
[1] https://community.oracle.com/thread/1115824
[2] http://en.it-usenet.org/thread/876/22511/
[3] http://www.databaseskill.com/331542/

create test database and roles in a fresh oracle install


See here to install oracle and sqlplus.

Connect to oracle as system or sysdba
BASH> rlwrap $ORACLE_HOME/bin/sqlplus -L system/INIT_PASSWORD@//localhost:1521/

Create a test database:
SQL> create bigfile tablespace test datafile 'test.dat' size 32m autoextend on nologging;

Tablespace created.

Create a test user:
SQL> create role tester; 

Role created.

SQL> grant connect, resource, dba to tester;

Grant succeeded.

SQL> create user tester1 identified by SECRET default tablespace test;

User created.

SQL> grant tester to tester1;

Grant succeeded.

SQL> alter user tester1 quota unlimited on test;

User altered.

SQL> quit

...

Now try to connect to oracle using the newly created user
BASH> rlwrap $ORACLE_HOME/bin/sqlplus -L tester1/SECRET@//localhost:1521

Create a test table and insert some random value
SQL> create table tab0(k number(10) not null, v varchar2(32) not null) tablespace test;

Table created

SQL> insert into tab0 (k,v) values (1, 'test value');
1 row created.

SQL> select * from tab0;
         K V
---------- --------------------------------
         1 test value


Wednesday, July 16, 2014

oracle-11g server/client in cetos 6.5

SERVER

1. download
oracle-xe-11.2.0-1.0.x86_64.rpm

2. install
BASH> sudo rpm -ivh oracle-xe-11.2.0-1.0.x86_64.rpm

3. initial configure the database

BASH> sudo /etc/init.d/oracle-xe configure

Oracle Database 11g Express Edition Configuration
-------------------------------------------------
This will configure on-boot properties of Oracle Database 11g Express Edition.
The following questions will determine whether the database should be starting upon system boot, the ports it will use, and the passwords that will be used for database accounts.
Press  to accept the defaults.
Ctrl-C will abort.

Specify the HTTP port that will be used for Oracle Application Express [8080]:
Specify a port that will be used for the database listener [1521]:
Specify a password to be used for database accounts.
Note that the same password will be used for SYS and SYSTEM. Oracle recommends the use of different passwords for each database account. This can be done after
initial configuration: INIT_PASSWORD
Confirm the password: INIT_PASSWORD
Do you want Oracle Database 11g Express Edition to be started on boot (y/n) [y]:y

Starting Oracle Net Listener...
DoneConfiguring database...
DoneStarting Oracle Database 11g Express Edition instance...
DoneInstallation completed successfully.

CLIENT
(although sqlplus comes together with SERVER installation, e.g. /u01/app/oracle/product/11.2.0/xe/sqlplus)

1. download
oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm

2. install
BASH> sudo rpm -ivh oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
...
BASH> sudo rpm -ivh oracle-instantclient11.2-sqlplus-11.2.0.4.0-1.x86_64.rpm
sqlplus executable is to be found here: /usr/lib/oracle/11.2/client64/bin/

3. set environment variables
ORACLE_HOME should be set to /usr/lib/oracle/11.2/client64/ --- one level above the bin directory where sqlplus executable is found etc...
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export ORACLE_HOME_LISTNER=/usr/lib/oracle/11.2/client64/bin
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export SQLPATH=/usr/lib/oracle/11.2/client64/lib
export TNS_ADMIN=/usr/lib/oracle/11.2/client64/bin

4. verify if sqlplus works
BASH> $ORACLE_HOME/bin/sqlplus -V

SQL*Plus: Release 11.2.0.4.0 Production

5. try connect to server
BASH> $ORACLE_HOME/bin/sqlplus -L system/INIT_PASSWORD@//localhost:1521/

SQL*Plus: Release 11.2.0.4.0 Production on Wed Jul 16 15:20:54 2014
Copyright (c) 1982, 2013, Oracle.  All rights reserved

Connected to:

Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> quit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

TIP
install rlwrap to get a more user-friendly SQL> shell, e.g.
BASH> rlwrap $ORACLE_HOME/bin/sqlplus -L system/INIT_PASSWORD@//localhost:1521/










Friday, July 11, 2014

bash print eth0 ip to erlang term format


eth0 ip address to erlang term:
printf "{%s}" $(ip addr show eth0 | grep -o "inet\s[0-9]*.*.*.*/" | cut -d " " -f2 | cut -d "/" -f1 | sed "s/\./,/g")

default gateway for eth0 to erlang term:
printf "{%s}" $(route | grep eth0 | grep default | sed "s/\s/\n/g" | grep "[[1-9][0-9]*\.]*3" | sed "s/\./,/g")

Thursday, July 10, 2014

build rabbtmq-server and rabbitmq-management plugin


1. clone the code
$ hg clone http://hg.rabbitmq.com/rabbitmq-public-umbrella

2. checkout all dependencies:
cd rabbitmq-public-umbrella && make checkout



3. build rabbitmq server:
cd rabbitmq-server && make

4. start rabbitmq server:
$ make run
NB:
a) by default, it requires write access to files in /etc and /var
to change it, edit SYS_PREFIX in rabbtimq-server/scripts/rabbitmq-defaults
b) by default, rabbitmq-server write logs mnesia db to /tmp, to change it, edit TMPDIR in Makefile

5. build rabbitmq-management plugin (in rabbitmq-server directory)
$ mkdir plugins
$ cd ../rabbitmq-management
$ make run
# plugin and its dependencies are build in directory 'dist'
$ cp dist/*.ez ../rabbitmq-server/plugins

6. start rabbitmq-management plugin
$ cd ../rabbitmq-server
$ ./scripts/rabbitmq-plugins enable rabbitmq_management

7. check plugin status:
./scripts/rabbitmq-plugins list
 Configured: E = explicitly enabled; e = implicitly enabled
 | Status:   * = running on rabbit@zmc0
 |/
[e*] amqp_client               0.0.0
[e*] mochiweb                  2.7.0-rmq0.0.0-git680dba8
[  ] rabbit_common             0.0.0
[E*] rabbitmq_management       0.0.0
[e*] rabbitmq_management_agent 0.0.0
[  ] rabbitmq_test             0.0.0
[e*] rabbitmq_web_dispatch     0.0.0
[e*] webmachine                1.10.3-rmq0.0.0-gite9359c7