3 #i'm kinda like freeside-cdr-sftp_and_import... some parts should be libraried
10 use DBI qw( :sql_types );
11 use Net::SFTP::Foreign;
12 #use FS::UID qw( adminsuidsetup datasrc );
14 #adjusted these for what we're actually seeing in the real log files
17 #'NASIP' => 'NASIPAddress',
18 'NASID' => 'NASIPAddress',
19 'AcctSessionId' => 'AcctSessionId',
20 'Port' => 'NasPortId',
21 #'Status-Type' => 'Acct-Status-Type',
22 #'UserID' => 'UserName',
23 'User ID' => 'UserName',
24 'Authentic' => 'AcctAuthentic',
25 'Service-Type' => 'ServiceType',
26 'FramedProtocol' => 'FramedProtocol',
27 #'FramedCompression' => '', #not handled, needed? unlikely
28 'FramedAddress' => 'FramedIPAddress',
29 'Acct-Delay-Time' => 'AcctStartDelay', #?
30 'Session-Time' => 'AcctSessionTime',
31 #'Input-Gigawords' => '', #XXX handle lots of data
32 'Input-Octets' => 'AcctInputOctets',
33 #'Output-Gigawords' => '', #XXX handle lots of data
34 'Output-Octets' => 'AcctOutputOctets',
35 'NAS-Port-Type' => 'NASPortType',
36 'Acct-Terminate-Cause' => 'AcctTerminateCause',
40 'AcctInputOctets' => SQL_INTEGER,
41 'AcctOutputOctets' => SQL_INTEGER,
42 'AcctSessionTime' => SQL_INTEGER,
43 'AcctStartDelay' => SQL_INTEGER,
44 'AcctStopDelay' => SQL_INTEGER,
47 #http://www.iana.org/assignments/radius-types/radius-types.xhtml#radius-types-10
51 3 => 'Interim-Update',
54 8 => 'Accounting-Off',
57 11 => 'Tunnel-Reject',
58 12 => 'Tunnel-Link-Start',
59 13 => 'Tunnel-Link-Stop',
60 14 => 'Tunnel-Link-Reject',
68 use vars qw( $opt_m $opt_a $opt_b $opt_r $opt_d $opt_v $opt_P );
69 getopts('m:abr:d:P:v:');
73 my $user = shift or die &usage;
74 #adminsuidsetup $user;
76 # %%%FREESIDE_CACHE%%% & hardcoded datasrc
77 #my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
78 my $cachedir = '/usr/local/etc/freeside/cache.DBI:Pg:dbname=freeside/cdrs';
79 mkdir $cachedir unless -d $cachedir;
81 my $servername = shift or die &usage;
83 my( $datasrc, $db_user, $db_pass ) = ( shift, shift, shift );
84 my $dbh = DBI->connect( $datasrc, $db_user, $db_pass)
85 or die "can't connect: $DBI::errstr\n";
87 my $csv = Text::CSV_XS->new;
93 warn "Retrieving directory listing\n" if $opt_v;
95 $opt_m = 'sftp' if !defined($opt_m);
100 if($opt_m eq 'ftp') {
101 $options{'Port'} = $opt_P if $opt_P;
102 $options{'Debug'} = $opt_v if $opt_v;
103 $options{'Passive'} = $opt_a if $opt_a;
107 $ls = [ grep { /^.*$/i } $ls_ftp->ls ];
109 elsif($opt_m eq 'sftp') {
110 $options{'port'} = $opt_P if $opt_P;
111 $options{'debug'} = $opt_v if $opt_v;
113 my $ls_sftp = sftp();
115 $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
118 $ls = $ls_sftp->ls('.', no_wanted => qr/^\.+$/,
122 die "Method '$opt_m' not supported; must be ftp or sftp\n";
129 foreach my $filename ( @$ls ) {
131 next if $opt_d && $filename eq $opt_d;
133 warn "Downloading $filename\n" if $opt_v;
136 if($opt_m eq 'ftp') {
138 $ftp->get($filename, "$cachedir/$filename")
139 or die "Can't get $filename: ". $ftp->message . "\n";
143 $sftp->get($filename, "$cachedir/$filename")
144 or die "Can't get $filename: ". $sftp->error . "\n";
147 warn "Processing $filename\n" if $opt_v;
149 open my $fh, "$cachedir/$filename" or die "$cachedir/$filename: $!";
150 my $header = $csv->getline($fh);
152 while ( my $row = $csv->getline($fh) ) {
155 my %hash = map { $_ => $row->[$i++] } @$header;
157 my %dbhash = map { $aradial2db{$_} => $hash{$_} }
158 grep $aradial2db{$_},
161 my @keys = keys %dbhash;
164 next unless grep defined($_), values %dbhash;
166 my $date = time2str( '%Y-%m-%d %X', str2time( $hash{'Date'} ) );
168 $hash{'Status-Type'} = $status_type{ $hash{'Status-Type'} }
169 if exists $status_type{ $hash{'Status-Type'} };
172 my @extra_values = ();
173 if ( $hash{'Status-Type'} eq 'Start' ) {
175 push @keys, 'AcctStartTime';
176 $dbhash{'AcctStartTime'} = $date;
178 $sql = 'INSERT INTO radacct ( '. join(',', @keys).
179 ' ) VALUES ( '. join(',', map ' ? ', @keys ). ' )';
181 } elsif ( $hash{'Status-Type'} eq 'Stop' ) {
183 my $AcctSessionId = delete($dbhash{AcctSessionId});
185 push @keys, 'AcctStopTime';
186 $dbhash{'AcctStopTime'} = $date;
188 push @extra_values, $AcctSessionId;
190 $sql = 'UPDATE radacct SET '. join(',', map "$_ = ?", @keys ).
191 ' WHERE AcctSessionId = ? ';
193 } elsif ( $hash{'Status-Type'} eq 'Interim' ) {
194 #not handled, but stop should capture the usage. unless session are
195 # normally super-long, extending across month boundaries, or we need
196 # real-time-ish data usage detail, it isn't a big deal
198 warn 'Unknown Status-Type '. $hash{'Status-Type'}. "; skipping\n";
202 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
205 foreach my $value ( map $dbhash{$_}, @keys ) {
206 my $key = shift @keys;
207 my $type = exists($bind_type{$key}) ? $bind_type{$key} : SQL_VARCHAR;
208 $value ||= 0 if $type == SQL_INTEGER;
209 $sth->bind_param($p_num++, $value, $type);
211 foreach my $value ( @extra_values ) {
212 $sth->bind_param($p_num++, $value);
215 $sth->execute or die $sth->errstr;
220 my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
221 if ( $opt_m eq 'ftp') {
223 $ftp->rename($filename, "$opt_d/$file_timestamp")
225 unlink "$cachedir/$filename";
226 die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
230 $sftp->rename($filename, "$opt_d/$file_timestamp")
232 unlink "$cachedir/$filename";
233 die "can't move $filename to $opt_d: ". $sftp->error . "\n";
238 unlink "$cachedir/$filename";
248 aradial-sftp_and_import [ -m method ] [ -a ] [ -b ]
249 [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
250 user [sftpuser@]servername dbi_datasrc dbi_username dbi_pass
254 use vars qw( $sftp $ftp );
257 return $ftp if $ftp && $ftp->pwd;
259 my ($hostname, $userpass) = reverse split('@', $servername);
260 my ($ftp_user, $ftp_pass) = split(':', $userpass);
262 my $ftp = Net::FTP->new($hostname, %options)
263 or die "FTP connection to '$hostname' failed.";
264 $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
265 $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
266 $ftp->binary or die "can't set BINARY mode: ". $ftp->message if $opt_b;
273 return $sftp if $sftp && $sftp->cwd;
275 my %sftp = ( host => $servername );
277 $sftp = Net::SFTP::Foreign->new(%sftp);
278 $sftp->error and die "SFTP connection failed: ". $sftp->error;
285 freeside-aradial-sftp_and_import - Download Aradial "CDR" (really RADIUS detail) files from a remote server via SFTP
289 aradial-sftp_and_import [ -m method ] [ -a ] [ -b ]
290 [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
291 user [sftpuser@]servername dbi_datasrc dbi_username dbi_pass
295 Command line tool to download CDR files from a remote server via SFTP
296 or FTP and then import them into the database.
298 -m: transfer method (sftp or ftp), defaults to sftp
300 -a: use ftp passive mode
302 -b: use ftp binary mode
304 -r: if specified, changes into this remote folder before starting
306 -d: if specified, moves files to the specified folder when done
308 -P: if specified, sets the port to use
310 -v: set verbosity level; this script only has one level, but it will
311 be passed as the 'debug' argument to the transport method
313 user: freeside username
315 [sftpuser@]servername: remote server
316 (or ftpuser:ftppass@servername)