#!/usr/bin/perl -w # # $Id: queue_log_pgsql.pl,v 1.1 2007/01/15 22:38:51 nugget Exp $ # # Original script by wlloyd at slap.net # The asterisk version indpendant way to get queue stats into Mysql, Postgres # or whatever is supported by Perl DBI # It's all about named pipes # to setup this software # stop asterisk # rm /var/log/asterisk/queue_log # mkfifo /var/log/asterisk/queue_log # make sure permissions are setup # chmod 777 /var/log/asterisk/queue_log # run this program as root or under another user as you see fit. # should start BEFORE asterisk. Add to /etc/rc.d/rc.local or whatever # restart asterisk # requires a DB table like the following.. #CREATE TABLE log_queue ( # qlog_id SERIAL NOT NULL, # ts timestamp with time zone, # qname varchar, # agent varchar, # action varchar, # info1 varchar, # info2 varchar, # info3 varchar, # id varchar, # PRIMARY KEY (qlog_id) #); #GRANT SELECT,INSERT ON TABLE log_queue TO asterisk; #GRANT SELECT,UPDATE ON SEQUENCE log_queue_qlog_id_seq TO asterisk; use DBI; use IO::File; use POSIX qw(strftime); my $opt_debug = 0; # if you want postgres change this to "Pg" my $db_type = "Pg"; my $db_host = "localhost"; my $db_user_name = 'asterisk'; my $db_password = 'v01psux'; my $db_database = 'asterisk'; my $dbh = DBI->connect("DBI:$db_type:dbname=$db_database;host= $db_host;", $db_user_name, $db_password); open(FIFO, "< /var/log/asterisk/queue_log") or die "Can't open queue_log : $!\n"; while (1) { $message = ; next unless defined $message; # interrupted or nothing logged chomp $message; # remove chars that will cause DB problems $message =~ s/\"\'//g; print "raw: [$message]\n" if ($opt_debug); @data = split(/\|/,$message); $ts = strftime("%Y-%m-%d %T-00",gmtime($data[0])); # these messages are almost useless for my purposes next if ($data[4] eq "QUEUESTART" ); next if ($data[4] eq "CONFIGRELOAD" ); if (!defined($data[5])) { $data[5] = ''; } if (!defined($data[6])) { $data[6] = ''; } if (!defined($data[7])) { $data[7] = ''; } my $sql = "INSERT INTO log_queue (ts, id, qname, agent, action, info1, info2, info3) VALUES ('$ts', '$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]')"; print "$sql \n\n" if ($opt_debug); $dbh->do($sql); # if you want an actual logfile you might want to uncomment this # if ( open(LOG, ">> /var/log/asterisk/queue_log_real") ) { # print LOG "$message\n"; # close(LOG); # } else { # warn "Couldn't log to /var/log/asterisk_queue_log: $!\n"; # } # } $dbh->disconnect(); exit 0;