Skip to content

Commit fb3a4cd

Browse files
Merge pull request #35 from VeritasOS/redacted_mammoth_perl_scripts
NetBackup 8.2 API Perl scripts for Config, Admin APIs
2 parents a5f6a18 + e0013a8 commit fb3a4cd

14 files changed

+647
-28
lines changed

recipes/perl/admin/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### NetBackup API Code Samples for perl
2+
3+
This directory contains code samples to invoke NetBackup admininstration REST APIs using perl.
4+
5+
#### Disclaimer
6+
7+
These scripts are only meant to be used as a reference. If you intend to use them in production, use it at your own risk.
8+
9+
#### Pre-requisites:
10+
11+
- NetBackup 8.2 or higher
12+
- Perl 5.20.2 or higher
13+
14+
#### Executing the recipes in perl
15+
16+
Use the following commands to run the perl samples.
17+
- `perl get_processes.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] -client <client>`
18+
- `perl get_services.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] -client <client>`
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env perl
2+
3+
use LWP::UserAgent;
4+
use LWP::Protocol::https;
5+
print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n";
6+
print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n";
7+
use JSON;
8+
use Getopt::Long qw(GetOptions);
9+
10+
require 'api_requests.pl';
11+
12+
#
13+
# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token
14+
# and use this token in your Authorization header for all subsequent requests. Token validity
15+
# is fixed at 24 hours
16+
#
17+
my $token;
18+
19+
my $content_type_v3 = "application/vnd.netbackup+json; version=3.0";
20+
my $protocol = "https";
21+
my $port = "1556";
22+
my $nbmaster;
23+
my $username;
24+
my $password;
25+
my $domainName;
26+
my $domainType;
27+
my $token;
28+
my $base_url;
29+
my $client;
30+
31+
my $ua = LWP::UserAgent->new(
32+
ssl_opts => { verify_hostname => 0, verify_peer => 0},
33+
);
34+
35+
# subroutines for printing usage and library information required to run the script.
36+
sub print_usage {
37+
print("\n\nUsage:");
38+
print("\nperl get_processes -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] -client <client>\n\n\n");
39+
}
40+
41+
sub print_disclaimer {
42+
print("--------------------------------------------------------\n");
43+
print("-- This script requires Perl 5.20.2 or later --\n");
44+
print("--------------------------------------------------------\n");
45+
print("Executing this library requires some additional libraries like \n\t'LWP' \n\t'JSON'\ \n\t'Getopt'\ \n\n");
46+
print("You can specify the 'nbmaster', 'username', 'password', 'domainName', 'domainType' and 'client' as command-line parameters\n");
47+
print_usage();
48+
}
49+
50+
51+
# subroutine to process user input
52+
sub user_input {
53+
GetOptions(
54+
'nbmaster=s' => \$nbmaster,
55+
'username=s' => \$username,
56+
'password=s' => \$password,
57+
'domainName=s' => \$domainName,
58+
'domainType=s' => \$domainType,
59+
'client=s' => \$client,
60+
) or die print_usage();
61+
62+
if ($nbmaster eq "") {
63+
print("Please provide the value for 'nbmaster'");
64+
exit;
65+
}
66+
67+
if ($username eq "") {
68+
print("Please provide the value for 'username'");
69+
exit;
70+
}
71+
72+
if ($password eq "") {
73+
print("Please provide the value for 'password'");
74+
exit;
75+
}
76+
77+
if ($client eq "") {
78+
print("Please provide the value for 'client'");
79+
exit;
80+
}
81+
82+
$base_url = "$protocol://$nbmaster:$port/netbackup";
83+
}
84+
85+
# subroutine to get hostid for a given host name
86+
sub get_hostid {
87+
my $uuid;
88+
my @argument_list = @_;
89+
$host_name = $argument_list[0];
90+
91+
my $url = "$base_url/config/hosts?filter=hostName eq '$host_name'";
92+
my $req = HTTP::Request->new(GET => $url);
93+
$req->header('Accept' => $content_type_v3);
94+
$req->header('Authorization' => $token);
95+
print "\n\n**************************************************************";
96+
print "\n\n Making GET Request to get host id \n\n";
97+
98+
my $resp = $ua->request($req);
99+
if ($resp->is_success) {
100+
my $message = $resp->decoded_content;
101+
print "Get host succeeded with status code: ", $resp->code, "\n";
102+
my $json = decode_json($message);
103+
my @hosts = @{$json->{'hosts'}};
104+
$uuid = @hosts[0]->{'uuid'};
105+
print "uuid=$uuid\n";
106+
}
107+
else {
108+
print "HTTP GET error code: ", $resp->code, "\n";
109+
print "HTTP GET error message: ", $resp->decoded_content, "\n";
110+
}
111+
return $uuid;
112+
}
113+
114+
# subroutine to get processes
115+
sub get_processes {
116+
my @argument_list = @_;
117+
my $uuid = $argument_list[0];
118+
my $url = "$base_url/admin/hosts/$uuid/processes";
119+
120+
my $req = HTTP::Request->new(GET => $url);
121+
$req->header('Accept' => $content_type_v3);
122+
$req->header('Authorization' => $token);
123+
124+
print "\n\n**************************************************************";
125+
print "\n\n Making GET Request to get processes\n\n";
126+
127+
my $resp = $ua->request($req);
128+
if ($resp->is_success) {
129+
my $message = $resp->decoded_content;
130+
print "Get processes succeeded with status code: ", $resp->code, "\n\n\n";
131+
print "Process List for NetBackup Client \"$client\"\n\n";
132+
print "pid processName priority memoryUsageMB startTime elapsedTime\n";
133+
print "=======.================.========.=============.======================.======================\n";
134+
135+
my $json = decode_json($message);
136+
my @processes = @{$json->{'data'}};
137+
foreach (@processes) {
138+
my $process = $_;
139+
my $pid = $process->{'attributes'}->{'pid'};
140+
my $processName = $process->{'attributes'}->{'processName'};
141+
my $priority = $process->{'attributes'}->{'priority'};
142+
my $memoryUsageMB = $process->{'attributes'}->{'memoryUsageMB'};
143+
my $startTime = $process->{'attributes'}->{'startTime'};
144+
my $elapsedTime = $process->{'attributes'}->{'elapsedTime'};
145+
printf("%7s %-16s %8s %13s %22s %22s\n", $pid, $processName, $priority, $memoryUsageMB, $startTime, $elapsedTime);
146+
}
147+
}
148+
else {
149+
print "HTTP GET error code: ", $resp->code, "\n";
150+
print "HTTP GET error message: ", $resp->decoded_content, "\n";
151+
}
152+
}
153+
154+
print_disclaimer();
155+
156+
user_input();
157+
158+
$token = perform_login($base_url, $username, $password, $domainName, $domainType);
159+
160+
my $uuid = get_hostid($client);
161+
if ($uuid eq "") {
162+
print("\nUnable to read host uuid for client $client\n");
163+
exit;
164+
}
165+
166+
get_processes($uuid);

recipes/perl/admin/get_services.pl

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/usr/bin/env perl
2+
3+
use LWP::UserAgent;
4+
use LWP::Protocol::https;
5+
print "LWP::UserAgent: ".LWP::UserAgent->VERSION,"\n";
6+
print "LWP::Protocol::https: ".LWP::Protocol::https->VERSION,"\n";
7+
use JSON;
8+
use Getopt::Long qw(GetOptions);
9+
10+
require 'api_requests.pl';
11+
12+
#
13+
# The token is the key to the NetBackup AuthN/AuthZ scheme. You must login and get a token
14+
# and use this token in your Authorization header for all subsequent requests. Token validity
15+
# is fixed at 24 hours
16+
#
17+
my $token;
18+
19+
my $content_type_v3 = "application/vnd.netbackup+json; version=3.0";
20+
my $protocol = "https";
21+
my $port = "1556";
22+
my $nbmaster;
23+
my $username;
24+
my $password;
25+
my $domainName;
26+
my $domainType;
27+
my $token;
28+
my $base_url;
29+
my $client;
30+
31+
my $ua = LWP::UserAgent->new(
32+
ssl_opts => { verify_hostname => 0, verify_peer => 0},
33+
);
34+
35+
# subroutines for printing usage and library information required to run the script.
36+
sub print_usage {
37+
print("\n\nUsage:");
38+
print("\nperl get_services -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] -client <client>\n\n\n");
39+
}
40+
41+
sub print_disclaimer {
42+
print("--------------------------------------------------------\n");
43+
print("-- This script requires Perl 5.20.2 or later --\n");
44+
print("--------------------------------------------------------\n");
45+
print("Executing this library requires some additional libraries like \n\t'LWP' \n\t'JSON'\ \n\t'Getopt'\ \n\n");
46+
print("You can specify the 'nbmaster', 'username', 'password', 'domainName', 'domainType' and 'client' as command-line parameters\n");
47+
print_usage();
48+
}
49+
50+
51+
# subroutine to process user input
52+
sub user_input {
53+
GetOptions(
54+
'nbmaster=s' => \$nbmaster,
55+
'username=s' => \$username,
56+
'password=s' => \$password,
57+
'domainName=s' => \$domainName,
58+
'domainType=s' => \$domainType,
59+
'client=s' => \$client,
60+
) or die print_usage();
61+
62+
if ($nbmaster eq "") {
63+
print("Please provide the value for 'nbmaster'");
64+
exit;
65+
}
66+
67+
if ($username eq "") {
68+
print("Please provide the value for 'username'");
69+
exit;
70+
}
71+
72+
if ($password eq "") {
73+
print("Please provide the value for 'password'");
74+
exit;
75+
}
76+
77+
if ($client eq "") {
78+
print("Please provide the value for 'client'");
79+
exit;
80+
}
81+
82+
$base_url = "$protocol://$nbmaster:$port/netbackup";
83+
}
84+
85+
# subroutine to get hostid for a given host name
86+
sub get_hostid {
87+
my $uuid;
88+
my @argument_list = @_;
89+
$host_name = $argument_list[0];
90+
91+
my $url = "$base_url/config/hosts?filter=hostName eq '$host_name'";
92+
my $req = HTTP::Request->new(GET => $url);
93+
$req->header('Accept' => $content_type_v3);
94+
$req->header('Authorization' => $token);
95+
print "\n\n**************************************************************";
96+
print "\n\n Making GET Request to get host id \n\n";
97+
98+
my $resp = $ua->request($req);
99+
if ($resp->is_success) {
100+
my $message = $resp->decoded_content;
101+
print "Get host succeeded with status code: ", $resp->code, "\n";
102+
my $json = decode_json($message);
103+
my @hosts = @{$json->{'hosts'}};
104+
$uuid = @hosts[0]->{'uuid'};
105+
print "uuid=$uuid\n";
106+
}
107+
else {
108+
print "HTTP GET error code: ", $resp->code, "\n";
109+
print "HTTP GET error message: ", $resp->decoded_content, "\n";
110+
}
111+
return $uuid;
112+
}
113+
114+
# subroutine to get services
115+
sub get_services {
116+
my @argument_list = @_;
117+
my $uuid = $argument_list[0];
118+
my $url = "$base_url/admin/hosts/$uuid/services";
119+
120+
my $req = HTTP::Request->new(GET => $url);
121+
$req->header('Accept' => $content_type_v3);
122+
$req->header('Authorization' => $token);
123+
124+
print "\n\n**************************************************************";
125+
print "\n\n Making GET Request to get services\n\n";
126+
127+
my $resp = $ua->request($req);
128+
if ($resp->is_success) {
129+
my $message = $resp->decoded_content;
130+
print "Get services succeeded with status code: ", $resp->code, "\n\n\n";
131+
print "Services List for NetBackup Client \"$client\"\n\n";
132+
print "id status\n";
133+
print "=======.================\n";
134+
135+
my $json = decode_json($message);
136+
my @services = @{$json->{'data'}};
137+
foreach (@services) {
138+
my $service = $_;
139+
my $id = $service->{'id'};
140+
my $status = $service->{'attributes'}->{'status'};
141+
printf("%7s %-16s\n", $id, $status);
142+
}
143+
}
144+
else {
145+
print "HTTP GET error code: ", $resp->code, "\n";
146+
print "HTTP GET error message: ", $resp->decoded_content, "\n";
147+
}
148+
149+
$url = "$base_url/admin/hosts/$uuid/services/bpcd";
150+
151+
$req = HTTP::Request->new(GET => $url);
152+
$req->header('Accept' => $content_type_v3);
153+
$req->header('Authorization' => $token);
154+
155+
print "\n\n**************************************************************";
156+
print "\n\n Making GET Request to get the service by name\n\n";
157+
158+
my $resp = $ua->request($req);
159+
if ($resp->is_success) {
160+
my $message = $resp->decoded_content;
161+
print "Get the service succeeded with status code: ", $resp->code, "\n\n\n";
162+
print "Services List for NetBackup Client \"$client\"\n\n";
163+
print "id status\n";
164+
print "=======.================\n";
165+
166+
my $json = decode_json($message);
167+
$id = $json->{'data'}->{'id'};
168+
$status = $json->{'data'}->{'attributes'}->{'status'};
169+
printf("%7s %-16s\n", $id, $status);
170+
}
171+
else {
172+
print "HTTP GET error code: ", $resp->code, "\n";
173+
print "HTTP GET error message: ", $resp->decoded_content, "\n";
174+
}
175+
}
176+
177+
print_disclaimer();
178+
179+
user_input();
180+
181+
$token = perform_login($base_url, $username, $password, $domainName, $domainType);
182+
183+
my $uuid = get_hostid($client);
184+
if ($uuid eq "") {
185+
print("\nUnable to read host uuid for client $client\n");
186+
exit;
187+
}
188+
189+
get_services($uuid);

recipes/perl/config/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### NetBackup API Code Samples for perl
2+
3+
This directory contains code samples to invoke NetBackup configuration REST APIs using perl.
4+
5+
#### Disclaimer
6+
7+
These scripts are only meant to be used as a reference. If you intend to use them in production, use it at your own risk.
8+
9+
#### Pre-requisites:
10+
11+
- NetBackup 8.2 or higher
12+
- Perl 5.20.2 or higher
13+
14+
#### Executing the recipes in perl
15+
16+
Use the following commands to run the perl samples.
17+
- `perl get_set_host_config.pl -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] -client <client>`

0 commit comments

Comments
 (0)