Skip to content
Sep 3 10

Lua script a day – get_country_code.lua

by ldr

I really want to dive a bit deeper into Lua, so I thought it would be nice to write a Lua script every day. They’ll probably be FreeSWITCH related as that’s where I plan to use the language mostly, but we’ll see. If anyone has ideas, please let me know in the comments.

The first one is simple: pass a telephone number in international format to the script and get the country-code returned (or get “INVALID” otherwise).

read more…

Sep 3 10

FreeSWITCH and SWIG – Part 1

by ldr

Since FreeSWITCH uses SWIG to wrap its core functions into mod_lua (amongst others), I’ll try to use that method for wrapping switch_core_sqldb functions and document how it’s done here. Maybe other languages will then profit from it as well.

I’m aiming for the following syntax:

local dbh = freeswitch.Dbh("dsn","user","pass")
 
local res = {}
 
dbh:query(
  "select 1 as foo, 2 as bar",
  function(cbt, row) cbt.insert(row); return 1 end, -- callback function
   -- row is a table as assoc array like { ["foo"] = "1"; ["bar"] = "2"; };
  res -- will be passed to each call of the callback function as 1st argument
)
 
-- now res will contain { [1] = { ["foo"] = "1"; ["bar"] = "2"; }; };
 
dbh:query(
  "select 1 as foo, 2 as bar",
  function(cbt, row) stream:write("foo[" .. row.foo .. "]\n"); return 1 end
)
 
-- yes this will be cool :)
 
dbh:query(
  "update first_name = \"foo\" where id = 7"
)
 
dbh:release() -- optional

read more…

Aug 29 10

FreeSWITCH mod_lua_luasql

by ldr

The early dissatisfaction with mod_odbc_query paid off. The module still has its use as an app in the dialplan, and perhaps for doing a quick query when in the FreeSWITCH cli, but passing a Lua table serialized as a string to Lua itself was not the best way to achieve my goal. So I created a driver for LuaSQL that makes use of core ODBC and with that got a huge increase in speed.

read more…

Aug 26 10

Code Smell

by ldr

How soon do you get code smell ? My code apparently already stinks the same day I finish it. Or perhaps it’s bad planning :-) I’m now writing a LuaSQL driver for FreeSWITCH Core DB. So the plan is people will be able to use their current scripts while gaining the advantage of cached connections (connection pooling) !

I already have the basics running, can get the environment from luasql.scdb.

I will definitely post here when it’s done :-)

Aug 25 10

Testing mod_odbc_query

by ldr

Well, mod_odbc_query works nicely. I did some tests to see how it performs compared to LuaSQL (these tests can also be found here) :

The speed of both implementations to access a database can be compared in two ways:

* Run an API n times and do one SQL query per iteration of the API
* Run an API one time and do n SQL queries

read more…

Aug 22 10

FreeSWITCH dynamic XML through ODBC

by ldr

It all started a bit over a year ago. I wanted to dynamically pull user profiles from a database and there was this really neat module called mod_xml_curl that allows you to post some variables to a webserver for requesting an XML document (FreeSWITCH configuration is one big XML document, but you can bind certain searches in that XML to modules for dynamic generation).
So I wrote a Ruby on Rails application that generated these user XML’s and went even further so that it created all FreeSWITCH configuration. But soon it felt like a swamp, I was doing too much in the Rails application and the whole system felt very fragile. Also, the purpose of the dynamic configuration was to handle *lots* of queries which it would probably not handle.

read more…

Aug 12 10

Install Trinity in FreeBSD 8.1

by ldr

Yesterday evening I’ve been up all night with a friend to get a World of Warcraft server named Trinity running on FreeBSD. I’m not a wow player myself, but am interested in how these things work :) So before I forget how we got it running, here’s some documentation on it.

I got most of the installation instructions from here, but they aren’t FreeBSD specific, so those details are below.

read more…

Mar 31 10

CentOS Firewall

by ldr

Since recently I’ve started using CentOS 5.4. Main reason is that most of the FreeSWITCH devs use it and getting support from them is definitely easiest when running the same distro.

read more…

Feb 24 10

FreeBSD & ZFS RAID1 Root ++

by ldr

I’ve been using ZFS for a while now on OpenSolaris, the filesystem really rocks ! Unfortunately the OpenSolaris is not going to be so Open anymore now that Oracle made its plans clear. But fear not, there’s FreeBSD ! A great OS that we use at work for a lot of our servers which now has a stable implementation of ZFS as well !

I tried installing FreeBSD 8.0 on a ZFS filesystem, which went pretty well actually. I first installed the os on the third disk (/dev/ad14) in the system (regular sysinstall). When that was done, I continued installing the final system on the first two disks.

Below is a transcript of everything I did to get it running.

read more…

Feb 11 10

Dovecot statistics through SNMP

by ldr

I couldn’t find a simple script to get some statistics out of Dovecot so it can be queried through Net-SNMP, so I wrote a little Perl thing myself:

#!/usr/local/bin/perl -w
use strict;
use Getopt::Long;
use File::ReadBackwards;
use Time::Local;
 
my $help;
my $log;
my $sec = 0;
my $now = time;
my %stats = ( 'pop3-logins' => 0,
              'failed-pop3-logins' => 0,
              'imap-logins' => 0,
              'failed-imap-logins' => 0 );
 
 
# Get options
&GetOptions ( 'help'  => \$help,
              'log=s' => \$log,
              'sec=i' => \$sec );
 
# Print some help
if ($help or !$log) {
print <<EOF
 
Dovecot-Stats Log Analyzer
 
Usage: dovecot-stats.pl [arguments]
 
-h    --help     Show this help
-l    --log      Log file to analyze (mandatory)
-s    --sec      Seconds to look backwards in the log file
                   (default 0 - analyze entire file)
 
EOF
;
 
$help && exit(0) || exit(1);
}
 
# Open log file
my $bw = File::ReadBackwards->new($log) or
    die "Error: Can't read '$log' $!" ;
 
# Loop through log file (backwards)
while (defined(my $nl = $bw->readline)) {
    $nl =~ /^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d) (.*)$/; # YYYY-MM-DD HH:MM:SS
    my $time = timelocal($6, $5, $4, $3, $2-1, $1); # Note: MM is 0-11, not 1-12
    my $ll = $7; # Log line without leading date / time
 
    last if ($sec > 0 && $time + $sec < $now);
 
    $stats{'pop3-logins'}++ if $ll =~ /^pop3-login/;
    $stats{'failed-pop3-logins'}++ if $ll =~ /^pop3-login.*\(auth failed/;
    $stats{'imap-logins'}++ if $ll =~ /^imap-login/;
    $stats{'failed-imap-logins'}++ if $ll =~ /^imap-login.*\(auth failed/;
}
 
foreach my $key (keys %stats) {
    print "$key:$stats{$key} ";
}
print "\n";

The script may need some finishing touch (more statistics), but for now it counts (failed) pop3- and imap logins. I put it in snmpd.conf like this:

exec /path/to/dovecot-stats.pl -l /var/log/dovecot.log -s 60

You can test it by walking your tree from the following OID:

snmpwalk -v 2c -c topsecret localhost .1.3.6.1.4.1.2021.8