|
| 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); |
0 commit comments