def get_vmstore(session_id): # Dictionary of VMstore objects vmstore_stats = {} # URL for VMstore stats get_vmstore_url = "/v310/datastore/default/statsRealtime" count = 1 # Invoke the API r = tintri.api_get(server_name, get_vmstore_url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an error if r.status_code != 200: print_error("The HTTP response for the get invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + url) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) # Get and store the VM items and save in a vmstore object. vmstore_result = r.json() vmstore_stats = vmstore_result["items"][0]["sortedStats"] return vmstore_stats
def get_current_reco(server, tgc_sess_id, pool): print_debug("Looking for recommendation on pool " + pool.get_name()) reco_url = "/v310/vmstorePool/" + pool.get_uuid( ) + "/recommendation/current" r = tintri.api_get(server, reco_url, tgc_sess_id) print_debug("The JSON response of the reco get invoke to the server " + server + " is: " + r.text) reco = r.json() return reco
def get_vm_uuids(vm_list): uuid_list = [] vms_found = [] result = tt.api_get(server_name, "/v310/vm", session_id) all_vms = result.json() for vm in all_vms["items"]: if vm["vmware"]["name"] in vm_list: uuid_list.append(vm["uuid"]["uuid"]) vms_found.append(vm["vmware"]["name"]) return (uuid_list, vms_found)
def get_vm_names(server, tgc_sess_id, vm_uuids): vm_names = [] for vm_uuid in vm_uuids: vm_url = "/v310/vm/" + vm_uuid r = tintri.api_get(server, vm_url, tgc_sess_id) print_debug("The JSON response of the vm get invoke to the server " + \ server + " is: " + r.text) vm_info = r.json() vm_names.append(vm_info["vmware"]["name"]) return vm_names
def get_dns_info(server_name, session_id): url = APPLIANCE_URL + "/dns" # Make the get call r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an error if r.status_code != 200: message = "The HTTP response for the get invoke to the server is not 200." tintri.api_logout(server_name, session_id) raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) appliance_dns = r.json() return appliance_dns
def get_info(vmstore, session_id, url): try: # Get infomation from api ret = tintri.api_get(vmstore, url, session_id) print_debug("The JSON response of the get invoke to the server " + vmstore + " is: " + ret.text) return ret.json() except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(vmstore, session_id) exit(-10) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(vmstore, session_id) exit(-11)
def get_maintenance_mode(server_name, session_id): url = APPLIANCE_URL + "/maintenanceMode" # Make the get call r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an error if r.status_code != 200: message = "The HTTP response for the get invoke to the server is not 200." raise tintri.TintriApiException(message, r.status_code, url, "no request", r.text) maintenance_mode = r.json() return maintenance_mode
def get_ip_configs(server_name, session_id): url = APPLIANCE_URL + "/ips" try: # Make the get call r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) except tintri.TintriRequestsException as tre: message = "HTTP error for the get IP addresses invoke to the server." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) except tintri.TintriApiException as tae: message = "The HTTP response for the get IP addresses invoke to the server is not 200." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) ip_configs = r.json() return ip_configs
def get_datastore_stats(session_id, stats): get_datastore_stats_base_url = "/v310/datastore/default/" stats_url = get_datastore_stats_base_url + stats # Invoke the API r = tintri.api_get(server_name, stats_url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an error if r.status_code != 200: print_error("The HTTP response for the get invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + url) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) # Get and store the VM items and save in a vmstore object. vmstore_result = r.json() return vmstore_result
def get_vms(server_name, session_id): vms = {} # Get a list of VMs, but return a page size at a time get_vm_url = "/v310/vm" count = 1 page_size = 100 vm_paginated_result = {'next': "offset=0&limit=" + str(page_size)} print_info("Collecting VMs from TGC.") # While there are more Vms, go get them while 'next' in vm_paginated_result: url = get_vm_url + "?" + vm_paginated_result['next'] print_debug("Next GET VM URL: " + str(count) + ": " + url) r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # For each VM in the page, print the VM name and UUID. vm_paginated_result = r.json() # Check for the first time through the loop and # print the total number of VMs. if count == 1: num_vms = vm_paginated_result["filteredTotal"] if num_vms == 0: raise tintri.TintriRequestsException("No VMs present") print_debug(str(num_vms) + " VMs present") items = vm_paginated_result["items"] for vm in items: vm_name = vm["vmware"]["name"] vm_uuid = vm["uuid"]["uuid"] print_debug(str(count) + ": " + vm_name + ", " + vm_uuid) count += 1 vms[vm_name] = vm_uuid print_info("Collected " + str(count) + " VMs.") print_info(str(count) + "VMs collected from TGC.") return vms
def get_json(host, session_id, url): try: r = tintri.api_get(host, url, session_id) print_verbose("Response: " + r.text) # If the status_code != 200 a TintriApiException is raised, # there is no need to do any checks regarding except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(host, session_id) exit(NAGIOS_CRITICAL) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(host, session_id) exit(NAGIOS_CRITICAL) return r.text
def get_sg_by_name(server_name, session_id, service_group): url = "/v310/servicegroup" r = tintri.api_get(server_name, url, session_id) print_debug( "The JSON response of the service group get invoke to the server " + server_name + " is: " + r.text) sg_paginated_result = r.json() num_sgs = int(sg_paginated_result["absoluteTotal"]) if num_sgs == 0: raise tintri.TintriRequestsException("No service groups present") # Look for a qualifying service group items = sg_paginated_result["items"] for sg in items: if (sg["name"] == service_group): return sg["uuid"]["uuid"] return ""
def get_pools(server, tgc_sess_id): vmstore_pools = [] url = "/v310/vmstorePool" r = tintri.api_get(server, url, tgc_sess_id) print_debug("The JSON response of the get invoke to the server " + server + " is: " + r.text) vm_paginated_result = r.json() num_pools = int(vm_paginated_result["filteredTotal"]) if (num_pools) == 0: raise tintri.TintriRequestsException("No VMstore Pools present") # Load up the pools items = vm_paginated_result["items"] for pool in items: print_info(pool["name"] + ": " + pool["uuid"]["uuid"]) vmstore_pool = VmstorePool(pool["name"], pool["uuid"]["uuid"]) vmstore_pools.append(vmstore_pool) return vmstore_pools
def get_service_group(server_name, session_id, service_group): # Get a list of service groups url = "/v310/servicegroup" r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) sg_paginated_result = r.json() num_sgs = int(sg_paginated_result["absoluteTotal"]) if num_sgs == 0: raise tintri.TintriRequestsException("No Service Groups present") print_debug(str(num_sgs) + " Service Groups present") # Initialze the member list sg_uuid = "" found = False # Look for service group that matches the input name items = sg_paginated_result["items"] count = 1 for sg in items: sg_name = sg["name"] sg_uuid = sg["uuid"]["uuid"] sg_member_count = sg["memberCount"] print_debug( str(count) + ": " + sg_name + "(" + str(sg_member_count) + "): " + sg_uuid) if sg_name == service_group: found = True break count += 1 if not found: raise tintri.TintriRequestsException("Service Group " + service_group + " not found.") return sg_uuid
print("Fields to report:\n" + str(attributes)) # Credentials Gathering - support Python 2.X and 3.X try: user_name = raw_input("Enter user name: ") except NameError: user_name = input("Enter user name: ") password = getpass.getpass("Enter password: "******"") session_id = None # Try to login into the TGC. try: r = tintri.api_get(server_name, '/info') json_info = r.json() product_name = json_info['productName'] # Check for correct product if product_name != "Tintri Global Center": raise tintri.TintriRequestException(server_name + "is not a Tintri Global Center server.") session_id = tintri.api_login(server_name, user_name, password) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) sys.exit(1) except tintri.TintriApiException as tae: print_error(tae.__str__()) sys.exit(2)
# Login session_id = tt.api_login(server_name, user_name, passWord) except tt.TintriRequestsException as tre: print_error(tre.__str__()) sys.exit(1) except tt.TintriApiException as tae: print_error(tae.__str__()) sys.exit(1) print("") try: # Get List of Service Groups api = "/v310/servicegroup" results = tt.api_get(server_name, api, session_id) service_groups = results.json() service_group_exists = False # setup API for adding VMs to proper Service Group for item in service_groups["items"]: if item["name"] == service_group: service_group_exists = True target_sg = item["uuid"]["uuid"] break if service_group_exists: target_sg_api = "/v310/servicegroup/" + target_sg + "/members/static" print("Service Group Found") else:
product_name = json_info['productName'] # Login to Tintri server session_id = tintri.api_login(server_name, user_name, password) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) exit(-2) except tintri.TintriApiException as tae: print_error(tae.__str__()) exit(-3) try: # Get appliance info url = "/v310/appliance/default/info" r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(server_name, session_id) exit(-10) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(server_name, session_id) exit(-11) # log out tintri.api_logout(server_name, session_id)
def get_vms(session_id): page_size = 25 # default # dictionary of VM objects vms = {} # Get a list of VMs a page size at a time get_vm_url = "/v310/vm" count = 1 vm_paginated_result = { 'live': "TRUE", 'next': "offset=0&limit=" + str(page_size) } # While there are more VMs, go get them while 'next' in vm_paginated_result: url = get_vm_url + "?" + vm_paginated_result['next'] # This is a work-around for a TGC bug. chop_i = url.find("&replicationHasIssue") if chop_i != -1: url = url[:chop_i] print_debug("Fixing URL") print_debug("Next GET VM URL: " + str(count) + ": " + url) # Invoke the API r = tintri.api_get(server_name, url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an error if r.status_code != 200: print_error("The HTTP response for the get invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + url) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) # For each VM in the page, print the VM name and UUID. vm_paginated_result = r.json() # Check for the first time through the loop and # print the total number of VMs. if count == 1: num_filtered_vms = vm_paginated_result["filteredTotal"] if num_filtered_vms == 0: print_error("No VMs present") tintri.api_logout(server_name, session_id) sys_exit(-99) # Get and store the VM items and save in a VM object. items = vm_paginated_result["items"] for vm in items: vm_name = vm["vmware"]["name"] vm_uuid = vm["uuid"]["uuid"] vm_stats = VmStat(vm_name, vm_uuid, vm["stat"]["sortedStats"][0]) print_debug(str(count) + ": " + vm_name + ", " + vm_uuid) count += 1 # Store the VM stats object keyed by VM name. vms[vm_name] = vm_stats return vms
print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 204 then raise an exception if r.status_code != 204: print_error("The HTTP response for the put invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + modify_qos_url) print_error("payload = " + str(MS_Request)) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) # Get VM 1 value to show that it changed. vm1_url = "/v310/vm/" + vm1.get_uuid() r = tintri.api_get(server_name, vm1_url, session_id) print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) # if HTTP Response is not 200 then raise an exception if r.status_code != 200: print_error("The HTTP response for the get invoke to the server " + server_name + " is not 200, but is: " + str(r.status_code)) print_error("url = " + vm1_url) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) sys.exit(-10) vm1_info = r.json() # Set the new values in the VM1 object
return # main if len(sys.argv) < 4: print("\nPrints VM information\n") print("Usage: " + sys.argv[0] + " server_name user_name password\n") sys.exit(-1) server_name = sys.argv[1] user_name = sys.argv[2] password = sys.argv[3] # Get the preferred version try: r = tintri.api_get(server_name, '/info') json_info = r.json() print_info("API Version: " + json_info['preferredVersion']) # Login to VMstore session_id = tintri.api_login(server_name, user_name, password) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) exit(-1) except tintri.TintriApiException as tae: print_error(tae.__str__()) exit(-2) try: