Skip to content

Commit 244a807

Browse files
authored
Added perl scripts for GET and POST notifications APIs.
1 parent e868a2c commit 244a807

5 files changed

Lines changed: 344 additions & 1 deletion

File tree

snippets/perl/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,13 @@ API key Details:
126126

127127
- Use the following command to use API key instead of JWT to trigger a NetBackup REST API on your NetBackup Master server:
128128
- `perl apikey_usage.pl -nbmaster <master_server> -apikey <apikey> [--verbose]`
129+
130+
#### Scripts for NetBackup 8.3 or higher
131+
132+
NetBackup Event Log Notifications:
133+
134+
- Use the following command to obtain the top 10 NetBackup notifications in descending order of their creation time.
135+
- `perl get_notifications.pl -nbmaster <master_server> -username <username> -password <password> [-domainname <domain_name>] [-domaintype <domain_type>] [-filter <filter>] [--verbose]`
136+
137+
- Use the following command to post notifications to the event log database.
138+
- `perl post_notifications.pl -nbmaster <master_server> -username <username> -password <password> [-domainname <domain_name>] [-domaintype <domain_type>] [-payload <payload file name>] [--verbose]`
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
#!/usr/bin/env perl
2+
3+
package eventlogs;
4+
5+
use JSON;
6+
use warnings;
7+
use Text::Table;
8+
use LWP::UserAgent;
9+
use LWP::Protocol::https;
10+
11+
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
12+
$CONTENT_TYPE_V4 = "application/vnd.netbackup+json; version=4.0";
13+
$NB_PORT = 1556;
14+
15+
#
16+
# This function displays data in a tabular form. It takes table title array and
17+
# table data (2-d matrix) as inputs and renders it in a tabular form with border
18+
#
19+
sub displayDataInTable {
20+
21+
my $arguments_count = scalar(@_);
22+
if ($arguments_count != 2) {
23+
print "ERROR :: Incorrect number of arguments passed to displayDataInTable()\n";
24+
print "Usage : displayDataInTable( <Array of Table Column Titles>, <Matrix of table data> ) \n";
25+
return;
26+
}
27+
28+
my @titletext = @{$_[0]};
29+
my @data = @{$_[1]};
30+
31+
my @tabletitle;
32+
my $val;
33+
foreach $val (@titletext) {
34+
push @tabletitle, {is_sep => 1, title => '| ', body => '| '};
35+
push @tabletitle, $val;
36+
}
37+
push @tabletitle, {is_sep => 1, title => '| ', body => '| '};
38+
39+
my $tb = Text::Table->new( @tabletitle );
40+
$tb->load(@data);
41+
42+
print $tb->rule('-', '+');
43+
for (0 .. @data) {
44+
print $tb->table($_);
45+
print $tb->rule('-', '+');
46+
}
47+
48+
}
49+
50+
51+
#
52+
# This function returns a list of notifications based on
53+
# a filter parameter
54+
#
55+
56+
sub getNotificationsByFilter {
57+
58+
my $arguments_count = scalar(@_);
59+
my $fqdn_hostname;
60+
my $token;
61+
my $filter;
62+
63+
if ($arguments_count == 2) {
64+
$fqdn_hostname = $_[0];
65+
$token = $_[1];
66+
} elsif ($arguments_count == 3) {
67+
$fqdn_hostname = $_[0];
68+
$token = $_[1];
69+
$filter = $_[2];
70+
} else {
71+
print "ERROR :: Incorrect number of arguments passed to getNotificationsByFilter()\n";
72+
print "Usage : getNotificationsByFilter( <FQDN Hostname>, <Token>. (optional) <filter> ) \n";
73+
return;
74+
}
75+
76+
my $url;
77+
if ($filter) {
78+
$url = "https://$fqdn_hostname:$NB_PORT/netbackup/eventlog/notifications?filter=$filter";
79+
} else {
80+
$url = "https://$fqdn_hostname:$NB_PORT/netbackup/eventlog/notifications";
81+
}
82+
83+
my $notifications_req = HTTP::Request->new(GET => $url);
84+
$notifications_req->header('Authorization' => $token);
85+
86+
my $ua = LWP::UserAgent->new(
87+
timeout => 1000,
88+
ssl_opts => { verify_hostname => 0, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE },
89+
);
90+
91+
print "Performing Get Notifications Request on $url\n";
92+
my $response = $ua->request($notifications_req);
93+
if ($response->is_success) {
94+
print "Successfully completed Get Notifications Request.\n";
95+
96+
$data = decode_json($response->content);
97+
my $pretty = JSON->new->pretty->encode($data);
98+
return $pretty;
99+
} else {
100+
print "ERROR :: Get Notifications Request Failed!\n";
101+
print "HTTP GET error code: ", $response->code, "\n";
102+
print "HTTP GET error message: ", $response->message, "\n";
103+
}
104+
105+
}
106+
107+
#
108+
## This function displays the JSON content returned from GET Notifications API
109+
## using query filter in a tabular format
110+
##
111+
sub displayNotifications {
112+
113+
my $arguments_count = scalar(@_);
114+
if ($arguments_count != 1) {
115+
print "ERROR :: Incorrect number of arguments passed to displayNotifications()\n";
116+
print "Usage : displayNotifications( <JSON content returned from GET Notifications API> ) \n";
117+
return;
118+
}
119+
120+
my $jsonstring = $_[0];
121+
my $json = decode_json($jsonstring);
122+
my @notifications = @{$json->{'data'}};
123+
124+
my @tablerows;
125+
126+
foreach (@notifications) {
127+
my $notification = $_;
128+
129+
my $type = $notification->{'type'};
130+
my $id = $notification->{'id'};
131+
my $version = $notification->{'attributes'}->{'version'};
132+
my $priority = $notification->{'attributes'}->{'priority'};
133+
my $severity = $notification->{'attributes'}->{'severity'};
134+
my $createdDateTime = $notification->{'attributes'}->{'createdDateTime'};
135+
my $insertionDateTime = $notification->{'attributes'}->{'insertionDateTime'};
136+
my $displayString = $notification->{'attributes'}->{'displayString'};
137+
my $notificationType = $notification->{'attributes'}->{'notificationType'};
138+
my $producerName = $notification->{'attributes'}->{'producerName'};
139+
my $producerId = $notification->{'attributes'}->{'producerId'};
140+
my $producerType = $notification->{'attributes'}->{'producerType'};
141+
my $producerSubType = $notification->{'attributes'}->{'producerSubType'};
142+
my $namespace = $notification->{'attributes'}->{'namespace'};
143+
144+
my @tablerow = ($type, $id, $version, $priority, $severity, $createdDateTime, $insertionDateTime, $displayString,
145+
$notificationType, $producerName, $producerId, $producerType,
146+
$producerSubType, $namespace);
147+
push @tablerows, \@tablerow;
148+
}
149+
150+
my @title = ("Type", "ID", "Version", "Priority", "Severity", "Created Date Time", "Insertion Date Time",
151+
"Display String", "Notification Type", "Producer Name", "Producer ID", "Producer Type",
152+
"Producer Sub Type", "Namespace");
153+
print "\n";
154+
displayDataInTable(\@title, \@tablerows);
155+
print "\n";
156+
157+
}
158+
159+
# Post notifications
160+
sub postNotifications {
161+
my $arguments_count = scalar(@_);
162+
if ($arguments_count != 3) {
163+
print "ERROR :: Incorrect number of arguments passed to postNotifications()\n";
164+
print "Usage : postNotifications( <Master Server Hostname>, <Token>, <Payload>) \n";
165+
return;
166+
}
167+
168+
my $master_server = $_[0];
169+
my $token = $_[1];
170+
my $filename = $_[2];
171+
my $url = "https://$master_server:$NB_PORT/netbackup/eventlog/notifications";
172+
open(my $fh, '<:encoding(UTF-8)', $filename)
173+
or die "Could not open file '$filename' $!";
174+
175+
my $payload = "";
176+
while (my $row = <$fh>) {
177+
chomp $row;
178+
$payload .= $row;
179+
}
180+
print "payload: $payload\n";
181+
182+
my $json = common::send_http_request($url, "POST", $token, $payload, undef, $CONTENT_TYPE_V4);
183+
184+
if (defined $json) {
185+
print "Successfully completed POST Notifications.\n";
186+
187+
my $pretty = JSON->new->pretty->encode($json);
188+
return $pretty;
189+
}
190+
else {
191+
print "ERROR :: POST Notifications Request Failed!\n";
192+
}
193+
}
194+
195+
1;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#Load module netbackup.pm
2+
use lib"../.";
3+
4+
use netbackup;
5+
use strict;
6+
use eventlogs::eventlogs;
7+
use warnings;
8+
use Getopt::Long qw(GetOptions);
9+
10+
sub printUsage {
11+
print "\nUsage : perl get_notifications.pl -nbmaster <master_server> -username <username> -password <password> [-domainname <domain_name>] [-domaintype <domain_type>] [-filter <filter>] [--verbose]\n\n";
12+
die;
13+
}
14+
15+
my $fqdn_hostname;
16+
my $username;
17+
my $password;
18+
my $domainname;
19+
my $domaintype;
20+
my $filter;
21+
my $verbose;
22+
23+
GetOptions(
24+
'nbmaster=s' => \$fqdn_hostname,
25+
'username=s' => \$username,
26+
'password=s' => \$password,
27+
'domainname=s' => \$domainname,
28+
'domaintype=s' => \$domaintype,
29+
'filter=s' => \$filter,
30+
'verbose' => \$verbose
31+
) or printUsage();
32+
33+
if (!$fqdn_hostname || !$username || !$password) {
34+
printUsage();
35+
}
36+
37+
if($verbose){
38+
print "\nRecieved the following parameters : \n";
39+
print " FQDN Hostname : $fqdn_hostname\n";
40+
print " Username : $username\n";
41+
print " Password : $password\n";
42+
if ($domainname) {
43+
print " Domain Name : $domainname\n";
44+
}
45+
if ($domaintype) {
46+
print " Domain Type : $domaintype\n";
47+
}
48+
if ($filter) {
49+
print " Filter : $filter\n";
50+
}
51+
}
52+
53+
print "\n";
54+
my $myToken;
55+
my $jsonstring;
56+
57+
if ($domainname && $domaintype) {
58+
$myToken = netbackup::login($fqdn_hostname, $username, $password, $domainname, $domaintype);
59+
} else {
60+
$myToken = netbackup::login($fqdn_hostname, $username, $password);
61+
}
62+
63+
if ($filter) {
64+
$jsonstring = eventlogs::getNotificationsByFilter($fqdn_hostname, $myToken, $filter);
65+
} else {
66+
$jsonstring = eventlogs::getNotificationsByFilter($fqdn_hostname, $myToken);
67+
}
68+
69+
70+
print "\nNotifications:\n";
71+
eventlogs::displayNotifications($jsonstring);
72+
73+
netbackup::logout($fqdn_hostname, $myToken);
74+
print "\n";
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#Load module netbackup.pm
2+
use lib"../.";
3+
4+
use netbackup;
5+
use gateway;
6+
use eventlogs::eventlogs;
7+
use Getopt::Long qw(GetOptions);
8+
sub printUsage {
9+
print "\nUsage : perl post_notifications.pl -nbmaster <master_server> -username <username> -password <password> -payload <payload file path> [-domainname <domain_name>] [-domaintype <domain_type>]\n\n";
10+
die;
11+
}
12+
13+
my $master_server;
14+
my $username;
15+
my $password;
16+
my $payload;
17+
my $domainname;
18+
my $domaintype;
19+
20+
GetOptions(
21+
'nbmaster=s' => \$fqdn_hostname,
22+
'username=s' => \$username,
23+
'password=s' => \$password,
24+
'domainname=s' => \$domainname,
25+
'domaintype=s' => \$domaintype,
26+
'payload=s' => \$payload,
27+
'verbose' => \$verbose
28+
) or printUsage();
29+
30+
31+
if (!$fqdn_hostname || !$username || !$password || !$payload) {
32+
printUsage();
33+
}
34+
35+
if($verbose){
36+
print "\nRecieved the following parameters : \n";
37+
print " FQDN Hostname : $fqdn_hostname\n";
38+
print " Username : $username\n";
39+
print " Password : $password\n";
40+
if ($domainname) {
41+
print " Domain Name : $domainname\n";
42+
}
43+
if ($domaintype) {
44+
print " Domain Type : $domaintype\n";
45+
}
46+
if ($filter) {
47+
print " Filter : $filter\n";
48+
}
49+
}
50+
51+
print "\n";
52+
my $myToken;
53+
my $jsonstring;
54+
55+
if ($domainname && $domaintype) {
56+
$myToken = netbackup::login($fqdn_hostname, $username, $password, $domainname, $domaintype);
57+
} else {
58+
$myToken = netbackup::login($fqdn_hostname, $username, $password);
59+
}
60+
61+
my $jsonString = eventlogs::postNotifications($fqdn_hostname, $myToken, $payload);
62+
print "$jsonString\n";
63+
64+
gateway::perform_logout($fqdn_hostname, $myToken);

snippets/perl/netbackup.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ sub displayAssets {
906906

907907
#
908908
## This function create the Json payload for the Asset Cleanup API
909-
## It receives 2 paramters, the Json response from the GetAssetByFilter
909+
## It receives 2 parameters, the Json response from the GetAssetByFilter
910910
## and the cleanupTime. The response of this function is a proper payload
911911
## with all Assets from the filter.
912912
##

0 commit comments

Comments
 (0)