|
|
@ -1,6 +1,8 @@
|
|
|
|
#!/usr/bin/perl
|
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
|
|
|
|
use English;
|
|
|
|
use English;
|
|
|
|
use Getopt::Long;
|
|
|
|
use Getopt::Long;
|
|
|
|
use Pod::Usage;
|
|
|
|
use Pod::Usage;
|
|
|
@ -22,7 +24,7 @@ my $PROGRAM_BASE = 'ngcp-fraud-notifier';
|
|
|
|
my $retcode = 0;
|
|
|
|
my $retcode = 0;
|
|
|
|
my $piddir = '/run/ngcp-fraud-notifier';
|
|
|
|
my $piddir = '/run/ngcp-fraud-notifier';
|
|
|
|
my $pidfile = "$piddir/ngcp-fraud-notifier.pid";
|
|
|
|
my $pidfile = "$piddir/ngcp-fraud-notifier.pid";
|
|
|
|
my $pf = File::Pid->new({ file => $pidfile });
|
|
|
|
my $pf = File::Pid->new({ file => $pidfile });
|
|
|
|
|
|
|
|
|
|
|
|
local $PROGRAM_NAME = $PROGRAM_BASE;
|
|
|
|
local $PROGRAM_NAME = $PROGRAM_BASE;
|
|
|
|
local $OUTPUT_AUTOFLUSH = 1;
|
|
|
|
local $OUTPUT_AUTOFLUSH = 1;
|
|
|
@ -37,7 +39,7 @@ my $opts = {
|
|
|
|
|
|
|
|
|
|
|
|
my $config;
|
|
|
|
my $config;
|
|
|
|
|
|
|
|
|
|
|
|
GetOptions( $opts,
|
|
|
|
GetOptions($opts,
|
|
|
|
'help|h' => sub { usage() },
|
|
|
|
'help|h' => sub { usage() },
|
|
|
|
'verbose',
|
|
|
|
'verbose',
|
|
|
|
) or usage();
|
|
|
|
) or usage();
|
|
|
@ -48,19 +50,24 @@ sub DESTROY {
|
|
|
|
|
|
|
|
|
|
|
|
sub check_params {
|
|
|
|
sub check_params {
|
|
|
|
my @missing;
|
|
|
|
my @missing;
|
|
|
|
|
|
|
|
|
|
|
|
foreach my $param (@required) {
|
|
|
|
foreach my $param (@required) {
|
|
|
|
push @missing, $param unless $opts->{$param};
|
|
|
|
push @missing, $param unless $opts->{$param};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usage(join(' ', @missing)) if scalar @missing;
|
|
|
|
usage(join(' ', @missing)) if scalar @missing;
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub usage {
|
|
|
|
sub usage {
|
|
|
|
my $missing = shift;
|
|
|
|
my $missing = shift;
|
|
|
|
pod2usage(-exitval => $missing ? 1 : 0,
|
|
|
|
|
|
|
|
-verbose => 1,
|
|
|
|
pod2usage(
|
|
|
|
-message => $missing ? "Missing parameters: $missing" : '',
|
|
|
|
-exitval => $missing ? 1 : 0,
|
|
|
|
);
|
|
|
|
-verbose => 1,
|
|
|
|
|
|
|
|
-message => $missing ? "Missing parameters: $missing" : '',
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -83,11 +90,11 @@ sub get_data {
|
|
|
|
while (my $res = $client->next_page($uri)) {
|
|
|
|
while (my $res = $client->next_page($uri)) {
|
|
|
|
die "$res->result\n" unless $res->is_success;
|
|
|
|
die "$res->result\n" unless $res->is_success;
|
|
|
|
my $res_hash = $res->as_hash;
|
|
|
|
my $res_hash = $res->as_hash;
|
|
|
|
my $data = $res_hash->{_embedded}{'ngcp:'.$link};
|
|
|
|
my $data = $res_hash->{_embedded}{'ngcp:' . $link};
|
|
|
|
if ('ARRAY' eq ref $data) {
|
|
|
|
if ('ARRAY' eq ref $data) {
|
|
|
|
push(@result,@$data);
|
|
|
|
push @result, @{$data};
|
|
|
|
} elsif ($data) {
|
|
|
|
} elsif ($data) {
|
|
|
|
push(@result,$data);
|
|
|
|
push @result, $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (defined $code and 'CODE' eq ref $code) {
|
|
|
|
if (defined $code and 'CODE' eq ref $code) {
|
|
|
|
$code->(\@result);
|
|
|
|
$code->(\@result);
|
|
|
@ -99,6 +106,7 @@ sub get_data {
|
|
|
|
|
|
|
|
|
|
|
|
sub get_email_template {
|
|
|
|
sub get_email_template {
|
|
|
|
my $event = shift;
|
|
|
|
my $event = shift;
|
|
|
|
|
|
|
|
|
|
|
|
my $lock_type = $event->{interval_lock} ? 'lock' : 'warning';
|
|
|
|
my $lock_type = $event->{interval_lock} ? 'lock' : 'warning';
|
|
|
|
my $reseller_id = $event->{reseller_id};
|
|
|
|
my $reseller_id = $event->{reseller_id};
|
|
|
|
my @templates_data = ();
|
|
|
|
my @templates_data = ();
|
|
|
@ -110,8 +118,8 @@ sub get_email_template {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my $selected_template;
|
|
|
|
my $selected_template;
|
|
|
|
foreach my $template (@templates_data) {
|
|
|
|
foreach my $template (@templates_data) {
|
|
|
|
next if $template->{name} ne 'customer_fraud_'.$lock_type.'_default_email'
|
|
|
|
next if $template->{name} ne 'customer_fraud_' . $lock_type . '_default_email'
|
|
|
|
&& $template->{name} ne 'customer_fraud_'.$lock_type.'_email';
|
|
|
|
&& $template->{name} ne 'customer_fraud_' . $lock_type . '_email';
|
|
|
|
next if $template->{reseller_id} && $template->{reseller_id} != $reseller_id;
|
|
|
|
next if $template->{reseller_id} && $template->{reseller_id} != $reseller_id;
|
|
|
|
$selected_template = $template;
|
|
|
|
$selected_template = $template;
|
|
|
|
last if $template->{reseller_id};
|
|
|
|
last if $template->{reseller_id};
|
|
|
@ -130,20 +138,22 @@ sub send_email {
|
|
|
|
|
|
|
|
|
|
|
|
my $template = get_email_template($event);
|
|
|
|
my $template = get_email_template($event);
|
|
|
|
|
|
|
|
|
|
|
|
my $vars = { adminmail => $config->{adminmail},
|
|
|
|
my $vars = {
|
|
|
|
customer_id => $event->{contract_id},
|
|
|
|
adminmail => $config->{adminmail},
|
|
|
|
interval => $event->{interval},
|
|
|
|
customer_id => $event->{contract_id},
|
|
|
|
interval_cost => sprintf('%.2f', $event->{interval_cost}/100),
|
|
|
|
interval => $event->{interval},
|
|
|
|
interval_limit => sprintf('%.2f', $event->{interval_limit}/100),
|
|
|
|
interval_cost => sprintf('%.2f', $event->{interval_cost} / 100),
|
|
|
|
type => $event->{type} eq 'profile_limit'
|
|
|
|
interval_limit => sprintf('%.2f', $event->{interval_limit} / 100),
|
|
|
|
? 'billing profile' : 'customer',
|
|
|
|
type => $event->{type} eq 'profile_limit'
|
|
|
|
};
|
|
|
|
? 'billing profile' : 'customer',
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
foreach my $subscriber (@{$subscribers}) {
|
|
|
|
foreach my $subscriber (@{$subscribers}) {
|
|
|
|
$vars->{subscribers} .= sprintf "%s\@%s %s\n",
|
|
|
|
$vars->{subscribers} .= sprintf "%s\@%s %s\n",
|
|
|
|
@{$subscriber}{qw(username domain)},
|
|
|
|
@{$subscriber}{qw(username domain)},
|
|
|
|
$subscriber->{external_id}
|
|
|
|
$subscriber->{external_id}
|
|
|
|
? '('.$subscriber->{external_id}.')' : '';
|
|
|
|
? '(' . $subscriber->{external_id} . ')'
|
|
|
|
|
|
|
|
: '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my $tt = Template->new();
|
|
|
|
my $tt = Template->new();
|
|
|
@ -183,20 +193,26 @@ sub update_notify_status {
|
|
|
|
my $client = NGCP::API::Client->new(verbose => $opts->{verbose});
|
|
|
|
my $client = NGCP::API::Client->new(verbose => $opts->{verbose});
|
|
|
|
|
|
|
|
|
|
|
|
my $now = strftime('%Y-%m-%d %H:%M:%S', localtime);
|
|
|
|
my $now = strftime('%Y-%m-%d %H:%M:%S', localtime);
|
|
|
|
my $uri = '/api/customerfraudevents/'.$event->{id};
|
|
|
|
my $uri = '/api/customerfraudevents/' . $event->{id};
|
|
|
|
my $data = [ { op => 'replace',
|
|
|
|
my $data = [
|
|
|
|
path => '/notify_status',
|
|
|
|
{
|
|
|
|
value => 'notified' },
|
|
|
|
op => 'replace',
|
|
|
|
{ op => 'replace',
|
|
|
|
path => '/notify_status',
|
|
|
|
path => '/notified_at',
|
|
|
|
value => 'notified',
|
|
|
|
value => $now } ];
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
op => 'replace',
|
|
|
|
|
|
|
|
path => '/notified_at',
|
|
|
|
|
|
|
|
value => $now,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
my $res = $client->request('PATCH', $uri, $data);
|
|
|
|
my $res = $client->request('PATCH', $uri, $data);
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub main {
|
|
|
|
sub main {
|
|
|
|
|
|
|
|
|
|
|
|
if (my $num = $pf->running) {
|
|
|
|
if (my $num = $pf->running) {
|
|
|
|
print "$PROGRAM_BASE is already running.\n";
|
|
|
|
print "$PROGRAM_BASE is already running.\n";
|
|
|
|
$retcode = 1;
|
|
|
|
$retcode = 1;
|
|
|
@ -207,22 +223,26 @@ sub main {
|
|
|
|
|
|
|
|
|
|
|
|
check_params();
|
|
|
|
check_params();
|
|
|
|
load_config();
|
|
|
|
load_config();
|
|
|
|
|
|
|
|
|
|
|
|
get_data(
|
|
|
|
get_data(
|
|
|
|
sprintf('/api/customerfraudevents/?no_count=true¬ify_status=%s','new'),'customerfraudevents', sub {
|
|
|
|
sprintf('/api/customerfraudevents/?no_count=true¬ify_status=%s', 'new'),
|
|
|
|
my $events = shift;
|
|
|
|
'customerfraudevents',
|
|
|
|
foreach my $event (@{$events}) {
|
|
|
|
sub {
|
|
|
|
if ($event->{interval_notify}) {
|
|
|
|
my $events = shift;
|
|
|
|
my $subscribers = get_data(sprintf('/api/subscribers/?customer_id=%d',
|
|
|
|
foreach my $event (@{$events}) {
|
|
|
|
$event->{contract_id}),
|
|
|
|
if ($event->{interval_notify}) {
|
|
|
|
'subscribers');
|
|
|
|
my $subscribers = get_data(sprintf('/api/subscribers/?customer_id=%d',
|
|
|
|
next unless scalar @$subscribers;
|
|
|
|
$event->{contract_id}),
|
|
|
|
eval {
|
|
|
|
'subscribers');
|
|
|
|
send_email($event, $subscribers);
|
|
|
|
next unless scalar @$subscribers;
|
|
|
|
};
|
|
|
|
eval {
|
|
|
|
print $EVAL_ERROR if $EVAL_ERROR;
|
|
|
|
send_email($event, $subscribers);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
print $EVAL_ERROR if $EVAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|