-
Notifications
You must be signed in to change notification settings - Fork 1
/
vmsmsnotify
executable file
·115 lines (90 loc) · 2.69 KB
/
vmsmsnotify
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
#!/usr/bin/perl -w
use strict;
use warnings;
use English;
use Readonly;
use NGCP::API::Client;
use Config::Any;
use Log::Log4perl;
Readonly my $CONF_FILE => '/etc/ngcp-vmnotify/vmnotify.conf';
my %CONFIG = %{
Config::Any->load_files({
files => [ $CONF_FILE ],
use_ext => 1
})->[0]->{$CONF_FILE}
or do {
log_syslog(
"$PROGRAM_NAME error: Cannot load config $CONF_FILE: $ERRNO");
exit 1;
};
};
my $debug = $CONFIG{DEBUG} ? "DEBUG" : "INFO";
Log::Log4perl->init(\<<EOF);
log4perl.category.vmnotify=$debug, SYSLOG, SCREEN
log4perl.appender.SYSLOG=Log::Dispatch::Syslog
log4perl.appender.SYSLOG.facility=local0
log4perl.appender.SYSLOG.ident=vmnotify
log4perl.appender.SYSLOG.layout=PatternLayout
log4perl.appender.SYSLOG.layout.ConversionPattern=%-5p %m%n
log4perl.appender.SCREEN=Log::Log4perl::Appender::Screen
log4perl.appender.SCREEN.mode=append
log4perl.appender.SCREEN.layout=PatternLayout
log4perl.appender.SCREEN.layout.ConversionPattern=%-5p %m%n
EOF
#----------------------------------------------------------------------
my $log = Log::Log4perl->get_logger("vmnotify");
sub send_notify {
my $from = $ARGV[0];
my $dest = $ARGV[1];
my $mailbox = $ARGV[2];
my $caller = $ARGV[3];
my $duration = $ARGV[4];
my $date = $ARGV[5];
my $body = $ARGV[6];
$from =~ s/\s+//g;
my $log_str = sprintf <<EOF,
vmsmsnotify from=%s dest=%s mailbox=%s caller=%s duration=%s date=%s body=%s
EOF
$from, $dest, $mailbox, $caller, $duration, $date, $body;
eval {
my $client = NGCP::API::Client->new();
my $res = $client->request('GET', "/api/subscribers/?alias=$mailbox");
unless($res->is_success) {
die "Failed to fetch subscriber for alias $mailbox, aborting!";
}
my $subs = $res->as_hash->{_embedded}->{'ngcp:subscribers'};
unless(ref $subs eq 'HASH') {
die "Failed to fetch single subscriber for alias $mailbox, aborting!";
}
my $content = {
subscriber_id => $subs->{id},
caller => $from,
callee => $dest,
text => $body,
};
$res = $client->request(
'POST',
'/api/sms/?skip_checks=true&skip_journal=false',
$content
);
die $res->status_line,"\n" unless $res->is_success;
};
if ($EVAL_ERROR) {
die sprintf "Cannot send %s error=%s", $log_str, $EVAL_ERROR;
} else {
$log->debug($log_str);
}
return;
}
sub main {
eval {
send_notify();
};
if ($EVAL_ERROR) {
$log->error($EVAL_ERROR);
exit 1;
}
return;
}
main();
exit 0;