提交 d9a0018a authored 作者: Raymond Chandler's avatar Raymond Chandler

adding config file to hold configurables and logging to syslog now

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/scripts/contrib@12530 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 e3345e2e
...@@ -2,32 +2,37 @@ ...@@ -2,32 +2,37 @@
use DBI; use DBI;
use Data::Dumper; use Data::Dumper;
use Sys::Syslog;
$config->{connect_string} = 'DBI:mysql:freeswitch'; my $config={};
$config->{username} = 'root'; my $db;
$config->{password} = ''; my $identifier = 'cdr-csv-importer';
$config->{table} = "cdrs";
$config->{fields} = [ sub parse_config {
"caller_id_name", open(CONFIG, "<cdr-csv-importer.ini") or die("Can't open config file (cdr-csv-importer.ini)\n");
"caller_id_number", while (<CONFIG>) {
"destination_number", #print $_;
"context", chomp; #kill whitespaces
"start_stamp",
"answer_stamp", next if !(m/^[[:alnum:]]/); #skip lines that don't start right
"end_stamp", next if m/^\s*$/; #skip lines with only whitespace
"duration", next if m/^;/; #skip ini comments
"billsec", next if m(/^\[.*\]/); # skip the start of a section
"hangup_cause",
"uuid", my ($key, $val) = split(/\s+=\s+/, $_, 2);
"bleg_uuid", #print "'$key' = '$val'\n";
"accountcode", if($key eq "fields") {
"read_codec", @{$config->{fields}} = split(/,/, $val);
"write_codec", } else {
]; $config->{$key} = $val;
}
}
close(CONFIG);
}
sub create_table { sub create_table {
my $db = shift; # my $db = shift;
my $sql = "CREATE TABLE IF NOT EXISTS`cdrs` ( my $sql = "CREATE TABLE IF NOT EXISTS `$config->{table}` (
`id` int(11) NOT NULL auto_increment, `id` int(11) NOT NULL auto_increment,
`caller_id_name` varchar(255) NOT NULL default '', `caller_id_name` varchar(255) NOT NULL default '',
`caller_id_number` varchar(255) NOT NULL default '', `caller_id_number` varchar(255) NOT NULL default '',
...@@ -62,7 +67,14 @@ sub is_db_connected { ...@@ -62,7 +67,14 @@ sub is_db_connected {
$query->execute(); $query->execute();
} }
my $db = &connect_db; openlog($identifier, 'ndelay,pid', LOG_USER);
open(STDERR,"|logger -t \"${identifier}[$$]: \"") or die "Error: Unable to redirect STDERR to logger!";
open(STDOUT,"|logger -t \"${identifier}[$$]: \"") or die "Error: Unable to redirect STDOUT to logger!";
&parse_config;
#print Dumper $config;
open(PID, ">$config->{pid_file}");
print PID $$;
$db = &connect_db;
&create_table($db); &create_table($db);
$field_count = @{$config->{fields}}; $field_count = @{$config->{fields}};
my $values = '?'; my $values = '?';
...@@ -71,18 +83,23 @@ for ($i=1; $i<$field_count; $i++) { ...@@ -71,18 +83,23 @@ for ($i=1; $i<$field_count; $i++) {
$fields = sprintf('%s, %s', $fields, $config->{fields}[$i]); $fields = sprintf('%s, %s', $fields, $config->{fields}[$i]);
$values = sprintf('%s, ?', $values); $values = sprintf('%s, ?', $values);
} }
#print "$field_count - $fields $values\n";
my $query = sprintf( my $query = sprintf(
"INSERT INTO %s (%s) VALUES (%s);", "INSERT INTO %s (%s) VALUES (%s);",
$config->{table}, join(',', @{$config->{fields}}), $values $config->{table}, join(',', @{$config->{fields}}), $values
); );
#print "$query\n"; syslog(LOG_INFO, "$query");
sub mylog {
$log = shift;
syslog(LOG_INFO, "$log");
}
print Dumper @ARGV;
my $query_handle = $db->prepare($query); my $query_handle = $db->prepare($query);
while (<STDIN>) { while (<STDIN>) {
print $_; syslog(LOG_INFO, $_);
chomp; chomp;
@values = split(/,/); @values = split(/,/);
#print Dumper @values; #print Dumper @values;
......
[default]
table = cdrs
username = freeswitch
password = Fr33Sw1tch
connect_string = DBI:mysql:freeswitch
pid_file = /var/run/cdr-csv-importer.pid
fields = caller_id_name,caller_id_number,destination_number,context,start_stamp,answer_stamp,end_stamp,duration,billsec,hangup_cause,uuid,bleg_uuid,accountcode,read_codec,write_codec
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论