Skip to content

Commit 63e4355

Browse files
committed
NetBackup Config Management and Admin APIs code samples
1 parent e630db9 commit 63e4355

File tree

10 files changed

+545
-0
lines changed

10 files changed

+545
-0
lines changed

recipes/python/admin/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### NetBackup Hosts Administration APIs Code Samples
2+
3+
This directory contains sample Python scripts that list the details of NetBackup processes and services running on a host using the NetBackup Hosts Administration APIs.
4+
5+
#### Disclaimer
6+
7+
These samples are provided only for reference and not meant for production use.
8+
9+
#### Executing the script
10+
11+
Pre-requisites:
12+
- NetBackup 8.2 or higher
13+
- Python 3.5 or higher
14+
- Python modules: `requests`.
15+
16+
17+
Use the following commands to run the scripts. The commands should be run from the parent directory of this 'admin' directory.
18+
- Get the details of NetBackup processes running on a host (specified by hostName): `python -W ignore -m admin.list_nb_processes -hostName <hostName> -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
19+
- Get the details/status of NetBackup services on a host (specified by hostName): `python -W ignore -m admin.list_nb_services -hostName <hostName> -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import sys
2+
import login.login_api as login_api
3+
import config.hosts_api as hosts_api
4+
import admin.processes_api as processes_api
5+
6+
nbmaster = ""
7+
username = ""
8+
password = ""
9+
domainName = ""
10+
domainType = ""
11+
hostName = ""
12+
13+
def print_disclaimer():
14+
print("\n-------------------------------------------------------------------------------------------------")
15+
print("-- This script requires Python3.5 or higher. --")
16+
print("-- The system where this script is run should have Python 3.5 or higher version installed. --")
17+
print("-------------------------------------------------------------------------------------------------")
18+
print("The script requires 'requests' library to make the API calls.")
19+
print("You can install the library using the command: pip install requests")
20+
print("-------------------------------------------------------------------------------------------------")
21+
22+
def print_usage():
23+
print("\nCommand-line usage (should be run from the parent directory of the 'admin' directory):")
24+
print("\tpython -Wignore -m admin.list_nb_processes -hostName <hostName> -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]")
25+
print("Note: hostName is the name of the NetBackup host to get the list of processes.\n")
26+
print("-------------------------------------------------------------------------------------------------")
27+
28+
def read_command_line_arguments():
29+
if len(sys.argv)%2 == 0:
30+
print("\nInvalid command!")
31+
print_usage()
32+
exit()
33+
34+
global nbmaster
35+
global username
36+
global password
37+
global domainName
38+
global domainType
39+
global hostName
40+
41+
for i in range(1, len(sys.argv), 2):
42+
if sys.argv[i] == "-nbmaster":
43+
nbmaster = sys.argv[i + 1]
44+
elif sys.argv[i] == "-username":
45+
username = sys.argv[i + 1]
46+
elif sys.argv[i] == "-password":
47+
password = sys.argv[i + 1]
48+
elif sys.argv[i] == "-domainName":
49+
domainName = sys.argv[i + 1]
50+
elif sys.argv[i] == "-domainType":
51+
domainType = sys.argv[i + 1]
52+
elif sys.argv[i] == "-hostName":
53+
hostName = sys.argv[i + 1]
54+
else:
55+
print("\nInvalid command!")
56+
print_usage()
57+
exit()
58+
59+
if nbmaster == "":
60+
print("Please provide the value for 'nbmaster'\n")
61+
exit()
62+
elif username == "":
63+
print("Please provide the value for 'username'\n")
64+
exit()
65+
elif password == "":
66+
print("Please provide the value for 'password'\n")
67+
exit()
68+
elif domainName == "":
69+
print("Please provide the value for 'domainName'\n")
70+
exit()
71+
elif domainType == "":
72+
print("Please provide the value for 'domainType'\n")
73+
exit()
74+
elif hostName == "":
75+
print("Please provide the value for 'hostName'\n")
76+
exit()
77+
78+
def print_nbprocesses_details(processes):
79+
print("NB PROCESS NAME".rjust(20), end = "\t")
80+
print("PID".rjust(10), end = "\t")
81+
print("MEMORY USAGE (MB)".rjust(10), end = "\t\t")
82+
print("START TIME".rjust(10), end = "\t\t")
83+
print("ELAPSED TIME".rjust(10), end = "\t")
84+
print("PRIORITY".rjust(10))
85+
print("-----------------------------------------"*3)
86+
for process in processes:
87+
process_attributes = process['attributes']
88+
print(process_attributes['processName'].rjust(20), end = "\t")
89+
print(str(process_attributes['pid']).rjust(10), end = "\t")
90+
print(str(process_attributes['memoryUsageMB']).rjust(10), end = "\t\t")
91+
print(process_attributes['startTime'].rjust(10), end = "\t\t")
92+
print(process_attributes['elapsedTime'].rjust(10), end = "\t")
93+
print(str(process_attributes['priority']).rjust(10))
94+
print()
95+
96+
print_disclaimer()
97+
98+
print_usage()
99+
100+
read_command_line_arguments()
101+
102+
base_url = "https://" + nbmaster + "/netbackup"
103+
104+
jwt = login_api.perform_login(base_url, username, password, domainName, domainType)
105+
106+
host_uuid = hosts_api.get_host_uuid(base_url, jwt, nbmaster)
107+
108+
processes = processes_api.get_all_nb_processes(base_url, jwt, host_uuid)
109+
110+
processes_data = processes['data']
111+
112+
if len(processes_data) > 0:
113+
print_nbprocesses_details(processes_data)
114+
else:
115+
print("\nNo NB processes exist on the specified host!")
116+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import sys
2+
import login.login_api as login_api
3+
import config.hosts_api as hosts_api
4+
import admin.services_api as services_api
5+
6+
nbmaster = ""
7+
username = ""
8+
password = ""
9+
domainName = ""
10+
domainType = ""
11+
hostName = ""
12+
13+
def print_disclaimer():
14+
print("\n-------------------------------------------------------------------------------------------------")
15+
print("-- This script requires Python3.5 or higher. --")
16+
print("-- The system where this script is run should have Python 3.5 or higher version installed. --")
17+
print("-------------------------------------------------------------------------------------------------")
18+
print("The script requires 'requests' library to make the API calls.")
19+
print("You can install the library using the command: pip install requests")
20+
print("-------------------------------------------------------------------------------------------------")
21+
22+
def print_usage():
23+
print("\nCommand-line usage (should be run from the parent directory of the 'admin' directory):")
24+
print("\tpython -Wignore -m admin.list_nb_services -hostName <hostName> -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]")
25+
print("Note: hostName is the name of the NetBackup host to get the list of services.\n")
26+
print("-------------------------------------------------------------------------------------------------")
27+
28+
def read_command_line_arguments():
29+
if len(sys.argv)%2 == 0:
30+
print("\nInvalid command!")
31+
print_usage()
32+
exit()
33+
34+
global nbmaster
35+
global username
36+
global password
37+
global domainName
38+
global domainType
39+
global hostName
40+
41+
for i in range(1, len(sys.argv), 2):
42+
if sys.argv[i] == "-nbmaster":
43+
nbmaster = sys.argv[i + 1]
44+
elif sys.argv[i] == "-username":
45+
username = sys.argv[i + 1]
46+
elif sys.argv[i] == "-password":
47+
password = sys.argv[i + 1]
48+
elif sys.argv[i] == "-domainName":
49+
domainName = sys.argv[i + 1]
50+
elif sys.argv[i] == "-domainType":
51+
domainType = sys.argv[i + 1]
52+
elif sys.argv[i] == "-hostName":
53+
hostName = sys.argv[i + 1]
54+
else:
55+
print("\nInvalid command!")
56+
print_usage()
57+
exit()
58+
59+
if nbmaster == "":
60+
print("Please provide the value for 'nbmaster'\n")
61+
exit()
62+
elif username == "":
63+
print("Please provide the value for 'username'\n")
64+
exit()
65+
elif password == "":
66+
print("Please provide the value for 'password'\n")
67+
exit()
68+
elif domainName == "":
69+
print("Please provide the value for 'domainName'\n")
70+
exit()
71+
elif domainType == "":
72+
print("Please provide the value for 'domainType'\n")
73+
exit()
74+
elif hostName == "":
75+
print("Please provide the value for 'hostName'\n")
76+
exit()
77+
78+
def print_nbservices_details(services_data):
79+
print("NB SERVICE NAME".rjust(20), end = "\t")
80+
print("STATUS".rjust(10))
81+
print("----------------------------------------")
82+
for service in services_data:
83+
print(service['id'].rjust(20), end = "\t")
84+
print(service['attributes']['status'].rjust(10))
85+
print()
86+
87+
print_disclaimer()
88+
89+
print_usage()
90+
91+
read_command_line_arguments()
92+
93+
base_url = "https://" + nbmaster + "/netbackup"
94+
95+
jwt = login_api.perform_login(base_url, username, password, domainName, domainType)
96+
97+
host_uuid = hosts_api.get_host_uuid(base_url, jwt, nbmaster)
98+
99+
services = services_api.get_all_nb_services(base_url, jwt, host_uuid)
100+
101+
data = services['data']
102+
103+
if len(data) > 0:
104+
print_nbservices_details(data)
105+
else:
106+
print("\nNo NB services exist on the specified host!")
107+
108+
##service = admin.services_api_requests.get_specific_nb_service(jwt, uuid, base_url)
109+
##
110+
##data = service['data']
111+
##
112+
##if len(data) > 0:
113+
## print_nbservice_details(data)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import requests
2+
3+
content_type = "application/vnd.netbackup+json; version=3.0"
4+
5+
def get_all_nb_processes(base_url, jwt, host_uuid):
6+
url = base_url + "/admin/hosts/" + host_uuid + "/processes"
7+
headers = {'Content-Type': content_type, 'Authorization': jwt}
8+
print("\nCalling NB Processes API to list the details of all NB processes on the host: {}\n".format(host_uuid))
9+
10+
resp = requests.get(url, headers=headers, verify=False)
11+
12+
if resp.status_code != 200:
13+
print("GET NB Processes API failed with status code {} and {}\n".format(resp.status_code, resp.json()))
14+
raise SystemExit("\n\n")
15+
16+
return resp.json()
17+
18+
19+
def get_specific_nb_service(base_url, jwt, host_uuid, processName):
20+
url = base_url + "/admin/hosts" + host_uuid + "/processes"
21+
headers = {'Content-Type': content_type, 'Authorization': jwt}
22+
query_params = {'filter': "processName eq '{}'".format(processName)}
23+
24+
print("\nCalling NB Processes API to get the details of the NB process {} on the host {}".format(processName, host_uuid))
25+
26+
resp = requests.get(url, headers=headers, params=query_params, verify=False)
27+
28+
if resp.status_code != 200:
29+
print('\nGET NB Process API failed with status code {} and {}\n'.format(resp.status_code, resp.json()))
30+
raise SystemExit("\n\n")
31+
32+
return resp.json()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import requests
2+
3+
content_type = "application/vnd.netbackup+json; version=3.0"
4+
5+
def get_all_nb_services(base_url, jwt, host_uuid):
6+
url = base_url + "/admin/hosts/" + host_uuid + "/services"
7+
headers = {'Content-Type': content_type, 'Authorization': jwt}
8+
print("\nCalling NB Services API to get the details/status of all NB services on the host: {}\n".format(host_uuid))
9+
10+
resp = requests.get(url, headers=headers, verify=False)
11+
12+
if resp.status_code != 200:
13+
print("GET NB Services API failed with status code {} and {}\n".format(resp.status_code, resp.json()))
14+
raise SystemExit("\n\n")
15+
16+
return resp.json()
17+
18+
19+
def get_specific_nb_service(base_url, jwt, host_uuid, serviceName):
20+
url = base_url + "/admin/hosts" + host_uuid + "/services/" + serviceName
21+
headers = {'Content-Type': content_type, 'Authorization': jwt}
22+
23+
print("\nCalling NB Services API to get the details/status of the NB service {} on the host {}".format(serviceName, host_uuid))
24+
25+
resp = requests.get(url, headers=headers, verify=False)
26+
27+
if resp.status_code != 200:
28+
print("GET NB Service API failed with status code {} and {}\n".format(resp.status_code, resp.json()))
29+
raise SystemExit("\n\n")
30+
31+
return resp.json()

recipes/python/config/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
### NetBackup Hosts Configuration Management API Code Samples
2+
3+
This directory contains Python scripts demonstrating the use of NetBackup Hosts Configuration Management APIs to update exclude list on a NetBackup host.
4+
5+
#### Disclaimer
6+
7+
These samples are provided only for reference and not meant for production use.
8+
9+
#### Executing the script
10+
11+
Pre-requisites:
12+
- NetBackup 8.2 or higher
13+
- Python 3.5 or higher
14+
- Python modules: `requests`.
15+
16+
17+
Use the following command to run the script. The command should be run from the parent directory of this 'config' directory.
18+
- `python -W ignore -m config.hosts_exclude_list -hostName <hostName> -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]
19+
Note: hostName is the name of the NetBackup host to set the exclude configuration. The exclude list is specified in the config/exclude_list file.`
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
EXCLUDE_LIST = ("C:\\Program Files\\Veritas\\NetBackup\\bin\\*.lock",
3+
"C:\\Program Files\\Veritas\\NetBackup\\bin\\bprd.d\\*.lock",
4+
"C:\\Program Files\\Veritas\\NetBackup\\bin\\bpsched.d\\*.lock",
5+
"C:\\Program Files\\Veritas\\Volmgr\\misc\\*",
6+
"C:\\Program Files\\Veritas\\NetBackupDB\\data\\*",
7+
"D:\\privatedata\\exclude",
8+
"D:\\tempdata\\exclude")

recipes/python/config/hosts_api.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import requests
2+
3+
config_hosts_url = "/config/hosts/"
4+
content_type_header = "application/vnd.netbackup+json;version=3.0"
5+
accept_header = "application/vnd.netbackup+json;version=3.0"
6+
7+
def get_host_uuid(base_url, jwt, host_name):
8+
headers = {'Accept': accept_header, 'Authorization': jwt}
9+
queryparams = {'filter':"hostName eq '{}'".format(host_name)}
10+
print("\nCalling Config Hosts API to get the uuid of the host {}.".format(host_name))
11+
response = requests.get(base_url + config_hosts_url, headers=headers, params=queryparams, verify=False)
12+
13+
if response.status_code != 200:
14+
print("\nGET Host API failed with status code {} and {}".format(response.status_code, response.json()))
15+
raise SystemExit("\n\n")
16+
17+
print("GET Hosts API returned status: {}".format(response.status_code))
18+
host_uuid = response.json()['hosts'][0]['uuid']
19+
print("Returning the host uuid: " + host_uuid)
20+
21+
return host_uuid
22+
23+
def get_host_configuration(base_url, jwt, host_uuid, config_name):
24+
headers = {'Accept': accept_header, 'Authorization': jwt}
25+
print("\nCalling Config Hosts API to get the '{}' configuration setting on the host '{}'.".format(config_name, host_uuid))
26+
27+
host_config_url = base_url + config_hosts_url + host_uuid + "/configurations/" + config_name
28+
response = requests.get(host_config_url, headers=headers, verify=False)
29+
return response
30+
31+
def create_host_configuration(base_url, jwt, host_uuid, config_name, config_value):
32+
headers = {'Content-Type': content_type_header, 'Authorization': jwt}
33+
print("\nCalling Config Hosts API to create the '{}' configuration setting on the host '{}'.".format(config_name, host_uuid))
34+
35+
host_config_url = base_url + config_hosts_url + host_uuid + "/configurations"
36+
data = {'data':{'type':'hostConfiguration', 'id':config_name, 'attributes':{'value':config_value}}}
37+
38+
response = requests.post(host_config_url, headers=headers, json=data, verify=False)
39+
return response
40+
41+
def update_host_configuration(base_url, jwt, host_uuid, config_name, config_value):
42+
headers = {'Content-Type': content_type_header, 'Authorization': jwt}
43+
print("\nCalling Config Hosts API to update the '{}' configuration setting on the host '{}'.".format(config_name, host_uuid))
44+
45+
host_config_url = base_url + config_hosts_url + host_uuid + "/configurations/" + config_name
46+
data = {'data':{'type':'hostConfiguration', 'id':config_name, 'attributes':{'value':config_value}}}
47+
48+
response = requests.put(host_config_url, headers=headers, json=data, verify=False)
49+
return response
50+

0 commit comments

Comments
 (0)