Monday, September 3, 2012

Easy ejabberd clustering procedure

I was fed up with lack of easy ways to connect multiple ejabberd together. The documentation contains what you need but because of not so friendly command lines, I was never satisfied by the procedure.

So I've added a simple command "attach" to "ejabberdctl" that speed up the procedure, basically it uses already defined vars from the script:


    $EXEC_CMD "$ERL \
      $NAME $ERLANG_NODE \
      -mnesia dir \"\\\"$SPOOLDIR\\\"\" \
        -mnesia extra_db_nodes \"[$MASTER]\" \
      -s mnesia"

}
Apply this patch, and enjoy the "attach" command:
=== modified file 'src/ejabberdctl.template'
--- src/ejabberdctl.template    2012-08-02 14:39:16 +0000
+++ src/ejabberdctl.template    2012-08-31 09:06:56 +0000
@@ -364,13 +364,63 @@
         status=0
     }
     return $status
+}
+
+attach ()
+{
+
+RUNAS=${INSTALLUSER-root}
+
+cat <<END
+- Attaching Procedure - (running as '$RUNAS')
+
+You're about to initialise this local mnesia with another running instance.
+Data will be written to '$SPOOLDIR'
+
+First, determine which node do you want to use as master, and answer the question.
+
+Now, once you're in the erlang shell, type:
+mnesia:info().
+
+Look at the line that starts with: "running db nodes", and observe that your node
+is listed.
+
+Then type the following:
+mnesia:change_table_copy_type(schema, node(), disc_copies).
+
+You'll normally get: "{atomic,ok}"
+
+You can now close the shell using:
+q().
+
+You can start ejabberd now, and observe this new node in the webadmin...
+END
+
+echo -n "Specify which master do you want to use (ex: ejabberd@node02): "
+read MASTER
+
+        if [[ -n $MASTER ]]
+        then
+
+    $EXEC_CMD "$ERL \
+      $NAME $ERLANG_NODE \
+      -mnesia dir \"\\\"$SPOOLDIR\\\"\" \
+        -mnesia extra_db_nodes \"[$MASTER]\" \
+      -s mnesia"
+
+        else
+                echo
+
+        fi
 }

+
 case $ARGS in
     ' start') start;;
     ' debug') debug;;
     ' live') live;;
     ' started') wait_for_status 0 30 2;; # wait 30x2s before timeout
     ' stopped') wait_for_status 3 15 2; stop_epmd;; # wait 15x2s before timeout
+    ' attach') attach;;
     *) ctl $ARGS;;
 esac



Ejabberd cluster connection problem using mnesia ?

If you try to connect two ejabberd servers and still can't get the job done because mnesia seems to be blind, check out the content of the file "ejabberdctl.cfg". By default ejabberd vm don't accept network connections ...
#.
#' INET_DIST_INTERFACE: IP address where this Erlang node listens other nodes
#
# This communication is used by ejabberdctl command line tool,
# and in a cluster of several ejabberd nodes.
# Notice that the IP address must be specified in the Erlang syntax.
#
# Default: {127,0,0,1}

INET_DIST_INTERFACE={0,0,0,0}
Fix your file like the code above and retry...

Configuring ejabberd for BOSH using a specific url

Put this inside the "listen" part:
  {5280, ejabberd_http, [
                         {request_handlers,
                                [
                                        {["boshurl"], mod_http_bind}
                                ]
                        },
                         http_bind,
                         web_admin
                        ]}

Reload ejabberd, and connect to "yourdomain:5280/boshurl" and enjoy :)

Wednesday, October 12, 2011

"Pseudo Randomly" retrieve data...

When you want to crawl some websites and you want to hide yourself a little, here's a simple trick to change randomly your user-agent string.
There's a very convenient function in erlang called 'uniform' from the module 'random', you can use it by calling 'random:uniform(MaxValue)' where MaxValue is the high limit.

So if you want to generate a random value from 1 to 5 you can simple use, 'random:uniform(5)'...

Now that you know how to generate random values, here's some code that do just what's the title say:

Thursday, July 29, 2010

erlang: Simple Debug macro

When testing things I need some quick way to debug "a la" printf() style. Old habits :)

Here's is the simple debug macro I use:
-ifdef(debug).
-define(DEBUG(Format, Args),
  io:format("~s.~w: DEBUG: " ++ Format, [ ?MODULE, ?LINE | Args])).
-else.
-define(DEBUG(Format, Args), true).
-endif.
Write it down in a "debug.hrl" file, then you only need to add this line in any file header:
-include("debug.hrl").
This simple macro gives you the module name and the line number. This saves me a lot of time.

Then you need to define the "debug" atom to let your macro do what you want. The compile:file/2 handles options for this, the syntax is {d, debug}

Sticky