3 #i'm kinda like freeside-cdr-sftp_and_import... some parts should be libraried
9 use Net::SFTP::Foreign;
10 #use FS::UID qw( adminsuidsetup datasrc );
14 'NASIP' => 'NASIPAddress',
15 'AcctSessionId' => 'AcctSessionId',
16 'Port' => 'NasPortId',
17 #'Status-Type' => 'Acct-Status-Type',
18 'UserID' => 'UserName',
19 'Authentic' => 'AccdtAuthentic',
20 'Service-Type' => 'ServiceType',
21 'FramedProtocol' => 'FramedProtocol',
22 #'FramedCompression' => '', #not handled, needed? unlikely
23 'FramedAddress' => 'FramedIPAddress',
24 'Acct-Delay-Time' => 'AcctStartDelay', #?
25 'Session-Time' => 'AcctSessionTime',
26 #'Input-Gigawords' => '',
27 'Input-Octets' => 'AcctInputOctets',
28 #'Output-Gigawords' => '',
29 'Output-Octets' => 'AcctOutputOctets',
30 'NAS-Port-Type' => 'NASPortType',
31 'Acct-Terminate-Cause' => 'AcctTerminateCause',
38 use vars qw( $opt_m $opt_a $opt_b $opt_r $opt_d $opt_v $opt_P );
39 getopts('m:abr:dP:v:');
43 my $user = shift or die &usage;
44 #adminsuidsetup $user;
46 # %%%FREESIDE_CACHE%%% & hardcoded datasrc
47 #my $cachedir = '%%%FREESIDE_CACHE%%%/cache.'. datasrc. '/cdrs';
48 my $cachedir = '/usr/local/etc/freeside/cache.DBI:Pg:dbname=freeside/cdrs';
49 mkdir $cachedir unless -d $cachedir;
51 my $servername = shift or die &usage;
53 my( $datasrc, $db_user, $db_pass ) = ( shift, shift, shift );
54 my $dbh = DBI->connect( $datasrc, $db_user, $db_pass)
55 or die "can't connect: $DBI::errstr\n";
57 my $csv = Text::CSV_XS->new;
63 warn "Retrieving directory listing\n" if $opt_v;
65 $opt_m = 'sftp' if !defined($opt_m);
71 $options{'Port'} = $opt_P if $opt_P;
72 $options{'Debug'} = $opt_v if $opt_v;
73 $options{'Passive'} = $opt_a if $opt_a;
77 $ls = [ grep { /^.*$/i } $ls_ftp->ls ];
79 elsif($opt_m eq 'sftp') {
80 $options{'port'} = $opt_P if $opt_P;
81 $options{'debug'} = $opt_v if $opt_v;
85 $ls_sftp->setcwd($opt_r) or die "can't chdir to $opt_r\n"
88 $ls = $ls_sftp->ls('.', wanted => qr/^$.*\.$/i,
92 die "Method '$opt_m' not supported; must be ftp or sftp\n";
99 foreach my $filename ( @$ls ) {
101 warn "Downloading $filename\n" if $opt_v;
104 if($opt_m eq 'ftp') {
106 $ftp->get($filename, "$cachedir/$filename")
107 or die "Can't get $filename: ". $ftp->message . "\n";
111 $sftp->get($filename, "$cachedir/$filename")
112 or die "Can't get $filename: ". $sftp->error . "\n";
115 warn "Processing $filename\n" if $opt_v;
117 my $file_timestamp = $filename.'-'.time2str('%Y-%m-%d', time);
119 open my $fh, "$cachedir/$filename" or die "$cachedir/$filename: $!";
120 my $header = $csv->getline($fh);
122 while ( my $row = $csv->getline($fh) ) {
125 my %hash = map { $_ => $row->[$i++] } @$header;
127 my %dbhash = map { $aradial2db{$_} => $hash{$_} }
128 grep $aradial2db{$_},
131 my @keys = keys %dbhash;
132 my @values = map $dbhash{$_}, @keys;
134 if ( $hash{'Acct-Status-Type'} eq 'Start' ) {
136 $dbhash{'AcctStartTime'} = $hash{'Date'};
138 my $sql = 'INSERT INTO radacct ( ', join(',', @keys).
139 ' ) VALUES ( '. map( ' ? ', @values ). ' )';
140 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
141 $sth->execute(@values) or die $sth->errstr;
143 } elsif ( $hash{'Acct-Status-Type'} eq 'Stop' ) {
145 my $AcctSessionId = delete($dbhash{AcctSessionId});
146 $dbhash{'AcctStopTime'} = $hash{'Date'};
148 my $sql = 'UPDATE radacct '. join(' , ', map "SET $_ = ?", @keys ).
149 ' WHERE AcctSessionId = ? ';
150 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
151 $sth->execute(@values, $AcctSessionId) or die $sth->errstr;
153 } elsif ( $hash{'Acct-Status-Type'} eq 'Interim' ) {
154 #not handled, but stop should capture the usage. unless session are
155 # normally super-long, extending across month boundaries, or we need
156 # real-time-ish data usage detail, it isn't a big deal
158 die 'Unknown Acct-Status-Type '. $hash{'Acct-Status-Type'}. "\n";
164 if ( $opt_m eq 'ftp') {
166 $ftp->rename($filename, "$opt_d/$file_timestamp")
168 unlink "$cachedir/$filename";
169 die "Can't move $filename to $opt_d: ".$ftp->message . "\n";
173 $sftp->rename($filename, "$opt_d/$file_timestamp")
175 unlink "$cachedir/$filename";
176 die "can't move $filename to $opt_d: ". $sftp->error . "\n";
181 unlink "$cachedir/$filename";
191 aradial-sftp_and_import [ -m method ] [ -a ] [ -b ]
192 [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
193 user [sftpuser@]servername dbi_datasrc dbi_username dbi_pass
197 use vars qw( $sftp $ftp );
200 return $ftp if $ftp && $ftp->pwd;
202 my ($hostname, $userpass) = reverse split('@', $servername);
203 my ($ftp_user, $ftp_pass) = split(':', $userpass);
205 my $ftp = Net::FTP->new($hostname, %options)
206 or die "FTP connection to '$hostname' failed.";
207 $ftp->login($ftp_user, $ftp_pass) or die "FTP login failed: ".$ftp->message;
208 $ftp->cwd($opt_r) or die "can't chdir to $opt_r\n" if $opt_r;
209 $ftp->binary or die "can't set BINARY mode: ". $ftp->message if $opt_b;
216 return $sftp if $sftp && $sftp->cwd;
218 my %sftp = ( host => $servername );
220 $sftp = Net::SFTP::Foreign->new(%sftp);
221 $sftp->error and die "SFTP connection failed: ". $sftp->error;
228 freeside-aradial-sftp_and_import - Download Aradial "CDR" (really RADIUS detail) files from a remote server via SFTP
232 aradial-sftp_and_import [ -m method ] [ -a ] [ -b ]
233 [ -r remotefolder ] [ -d donefolder ] [ -v level ] [ -P port ]
234 user [sftpuser@]servername dbi_datasrc dbi_username dbi_pass
238 Command line tool to download CDR files from a remote server via SFTP
239 or FTP and then import them into the database.
241 -m: transfer method (sftp or ftp), defaults to sftp
243 -a: use ftp passive mode
245 -b: use ftp binary mode
247 -r: if specified, changes into this remote folder before starting
249 -d: if specified, moves files to the specified folder when done
251 -P: if specified, sets the port to use
253 -v: set verbosity level; this script only has one level, but it will
254 be passed as the 'debug' argument to the transport method
256 user: freeside username
258 [sftpuser@]servername: remote server
259 (or ftpuser:ftppass@servername)