-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmdr-logger
99 lines (88 loc) · 3.12 KB
/
smdr-logger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/perl
#
# Connect to IP500 SMDR connector then log the
# calls to text files and SQL database.
#
use DBI;
use strict;
use warnings;
use IO::Socket;
use POSIX;
my $host = shift || $ARGV[0];
my $port = shift || $ARGV[1];
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
for(;;){
my $sock = new IO::Socket::INET(PeerAddr => $host, PeerPort => $port,Proto => "tcp",)
or die "Cannot connect to PBX at address: $host port: $port: $!";
while (<$sock>) {
s/^\0+//; # Remove leading null characters
print $_;
chomp ($_);
#$_ =~ s/^[^ ]+//;
if ($_ =~m"/") {
&TXTout; #send data to CSV TXT subroutine
# &DBconnect; #send data to the database subroutine
}
}
sub TXTout {
my $dir = "/var/log/smdr-logger/";
my $filename = strftime("%Y%m%d",localtime(time));
my $fileexpression = $dir.$filename.".csv";
open (my $fh, '>>:crlf', $fileexpression) or die "Cannot create file in $dir";
print $fh $_;
close $fh;
}
sub DBconnect {
my $driver = "mysql";
my $database = "smdr";
my $dsn = "DBI:$driver:database=$database";
my $userid = "smdr";
my $password = "myp4ss!";
my $date = ""; # First split of first column using space delimiter
my $time = ""; # Second split of first column using space delimiter
my $cstart = ""; # Call Start
my $cdur = ""; # Call Duration
my $wtime = ""; # Wait Time
my $caller = ""; # Caller
my $dir = ""; # Direction
my $called = ""; # Called
my $dialled = ""; # Dialled
my $acccode = ""; # Account Code
my $int = ""; # Internal
my $callid = ""; # Call ID
my $cont = ""; # Continuation
my $p1dev = ""; # Party 1 Device
my $p1name = ""; # Party 1 Name
my $p2dev = ""; # Party 2 Device
my $p2name = ""; # Party 2 Name
my $htime = ""; # Hold Time (Total)
my $ptime = ""; # Park Time (Total)
my $avalid = ""; # Auth Valid
my $acode = ""; # Auth Code
my $ucharged = ""; # User Charged
my $callcharge = ""; # Call Charge
my $cur = ""; # Currency
my $aocam = ""; # AOC Amount
my $calluni = ""; # Call Units
my $aocuni = ""; # AOC Units
my $cpuni = ""; # Cost per Unit
my $markup = ""; # Mark Up
my $extarc = ""; # Ex Targeting Cause
my $extari = ""; # Ex Targeting ID
my $extarn = ""; # Ex Targeting Number
my $dbh = DBI->connect($dsn, $userid, $password) or die $DBI::errstr;
my $sth = $dbh->prepare("INSERT into calls
(,,,,,,,,,,,)
VALUES
(?,?,?,?,?,?,?,?,?)");
#$sth->execute($date,$time,$cstart,$cdur,$wtime,$caller,$dir,$called,$dialled,$acccode,$int,$callid,$cont,$p1dev,$p1name,$p2dev,$p2name,$htime,$ptime,$avalid,$acode,$ucharged,$callcharge,$cur,$aocam,$calluni,$aocuni,$cpuni,$markup,$extarc,$extari,$extarn) or die $DBI::errstr;
$sth->finish();
$dbh->commit or die $DBI::errstr;
$dbh->disconnect or warn $dbh->errstr;
}
close $sock or die "close: $!";
print "socket closed";
print "<br />";
}