summaryrefslogtreecommitdiff
path: root/bin/cdr-transnexus.import
blob: b9fe41ab14f99bdfafb0e7fb162a4b5db48dae49 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl

use strict;
use Getopt::Std;
use Net::SFTP::Foreign;
use FS::UID qw(adminsuidsetup datasrc);
use FS::cdr;

###
# parse command line
###

use vars qw( $opt_p $opt_d $opt_v );
getopts('v');

$opt_p = 'last';
$opt_d = 'done';

my $user = shift or die &usage;
adminsuidsetup $user;

# %%%FREESIDE_CACHE%%%
my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
mkdir $cachedir unless -d $cachedir;

#my $format = shift or die &usage;
my $format = 'transnexus';

use vars qw( $servername );
$servername = shift or die &usage;

my $DIR = '/home/ossadmin/OSS/nexoss/CDR_ARCHIVE_BY_ACCOUNT';

###
# get the file list
###

warn "Retreiving directory listing\n" if $opt_v;

my $ls_sftp = sftp();

my $lsdir = $ls_sftp->ls($DIR);

###
# import each file in each dir
###

foreach my $dir ( @$lsdir ) {

  my $dirname = $dir->{filename};
  warn "Scanning dir $dirname\n" if $opt_v;

  #my $ls = $ls_sftp->ls("$DIR/$dirname", wanted => qr/^$opt_p.*-CDRs$/i );
  my $ls = $ls_sftp->ls("$DIR/$dirname", wanted => qr/^$opt_p.*Customer-CDRs$/i );

  foreach my $file ( @$ls ) {

    my $filename = $file->{filename};
    warn "Downloading $filename\n" if $opt_v;

    #get the file
    my $get_sftp = sftp();
    $get_sftp->get("$DIR/$dirname/$filename", "$cachedir/$filename")
      or die "Can't get $filename: ". $get_sftp->error;

    warn "Processing $filename\n" if $opt_v;

    my $error = FS::cdr::batch_import( {
      'file'            => "$cachedir/$filename",
      'format'          => $format,
      'batch_namevalue' => $filename,
      'empty_ok'        => 1,
    } );
    die $error if $error;

    if ( $opt_d ) {
      my $mv_sftp = sftp();
      $mv_sftp->mkdir("$DIR/$dirname/$opt_d");
      $mv_sftp->rename( "$DIR/$dirname/$filename",
                        "$DIR/$dirname/$opt_d/$filename" )
        or die "can't move $filename to $opt_d: ". $mv_sftp->error;
    }

    unlink "$cachedir/$filename";

  }

}

###
# subs
###

sub usage {
  "Usage: \n  cdr-transnexus.import [ -v ] user [sftpuser@]servername\n";
}

use vars qw( $sftp );

sub sftp {

  #reuse connections
  return $sftp if $sftp && $sftp->cwd;

  my %sftp = ( host => $servername );

  $sftp = Net::SFTP::Foreign->new(%sftp);
  $sftp->error and die "SFTP connection failed: ". $sftp->error;

  $sftp;
}

=head1 NAME

cdr.sftp_and_import - Download CDR files from a remote server via SFTP

=head1 SYNOPSIS

  cdr-transnexus.import [ -v ] user [sftpuser@]servername

=head1 DESCRIPTION

Command line tool to download CDR files from a remote server via SFTP and then
import them into the database.

-v: verbose

user: freeside username

[sftpuser@]servername: remote server

=head1 BUGS

Hacked up copy of freeside-cdr-sftp_and_import

=head1 SEE ALSO

L<FS::cdr>

=cut

1;