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 process_vmstore(vmstore_name, user_name, password, new_dns_primary): server_name = vmstore_name # Get the server type r = tintri.api_version(server_name) json_info = r.json() if json_info['productName'] != "Tintri VMstore": this_error = "Server needs to be a VMstore" return this_error session_id = tintri.api_login(server_name, user_name, password) dns_info = get_dns_info(server_name, session_id) print_dns_info(dns_info, server_name + " current: ") new_dns_secondary = dns_info['dnsSecondary'] # Create the ApplianceDns DTO. new_dns_info = \ {'typeId': 'com.tintri.api.rest.v310.dto.domain.beans.hardware.ApplianceDns', 'dnsPrimary': new_dns_primary, 'dnsSecondary': new_dns_secondary } # Create the Appliance object wit the new ApplianceDns DTO. new_appliance = \ {'typeId': 'com.tintri.api.rest.v310.dto.domain.Appliance', 'dnsConfig': new_dns_info } # Create the Request object with the Appliance DTO. Request = \ {'typeId': 'com.tintri.api.rest.v310.dto.Request', 'objectsWithNewValues': new_appliance, 'propertiesToBeUpdated': ['dnsConfig'] } url = APPLIANCE_URL r = tintri.api_put(server_name, url, Request, session_id) 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 exception if r.status_code != 204: tintri.api_logout(server_name, session_id) message = "The HTTP response for put call to the server is not 204." raise tintri.TintriApiException(message, r.status_code, url, str(Request), r.text) dns_info = get_dns_info(server_name, session_id) print_dns_info(dns_info, server_name + " now: ") # All pau, log out tintri.api_logout(server_name, session_id)
def set_vm_affinity(server_name, session_id, vm_uuids, affinity_rule): url = "/v310/vm/" for vm_uuid in vm_uuids: rule_url = url + vm_uuid + "/affinity" r = tintri.api_put(server_name, rule_url, affinity_rule, session_id) if r.status_code != 204: tintri.api_logout(server_name, session_id) message = "The HTTP response for put affinity rule to the server is not 204." raise tintri.TintriApiException(message, r.status_code, rule_url, str(affinity_rule), r.text) sys.stdout.write(".") print("")
def get_json_info(vmstore): try: # Get json info ret = tintri.api_version(vmstore) 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_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 set_qos(server_name, session_id, sg_uuid, new_min_value, new_max_value): # Create new QoS object with the fields to be changed modify_qos_info = { 'minNormalizedIops': int(new_min_value), 'maxNormalizedIops': int(new_max_value), 'typeId': 'com.tintri.api.rest.v310.dto.domain.beans.vm.VirtualMachineQoSConfig' } # Configure the QoS for the service group modify_qos_url = "/v310/servicegroup/" + sg_uuid + "/qosConfig" r = tintri.api_put(server_name, modify_qos_url, modify_qos_info, session_id) 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 204, but is: " + str(r.status_code)) print_error("url = " + modify_qos_url) print_error("payload = " + str(modify_qos_info)) print_error("response: " + r.text) tintri.api_logout(server_name, session_id) print_info("Error log off " + server_name) sys.exit(-20) # Apply the QoS values that were for the service group that # were configured above. apply_qos_url = "/v310/servicegroup/" + sg_uuid + "/qos" r = tintri.api_post(server_name, apply_qos_url, None, session_id) 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 post invoke to the server " + server_name + " is not 204, but is: " + str(r.status_code)) print_error("url = " + modify_qos_url) print_error("payload = None") print_error("response: " + r.text) tintri.api_logout(server_name, session_id) print_info("Error log off " + server_name) sys.exit(-21)
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_sg_members(server_name, session_id, sg_uuid): sg_members = [] # Create filter to obtain live VMs and VMs that belong in specified # the service group. sg_filter = {'live': 'TRUE', 'serviceGroupIds': sg_uuid} url = "/v310/vm" r = tintri.api_get_query(server_name, url, sg_filter, 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) member_paginated_result = r.json() num_members = int(member_paginated_result["filteredTotal"]) if num_members == 0: print_debug("No Service Groups members present") return sg_members print_debug(str(num_members) + " Service Group Members present") # For each live VM, create a VM info object items = member_paginated_result["items"] for vm in items: if not vm["isLive"]: continue member_vm = vm["vmware"]["name"] member_vmstore = vm["vmstoreName"] member_vm_uuid = vm["uuid"]["uuid"] print_debug(" " + member_vm + " (" + member_vmstore + ")") vm_info = VmInfo(member_vm, member_vm_uuid, member_vmstore) sg_members.append(vm_info) return sg_members
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_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
json_info = r.json() print_graphite_connection_info while True: print_debug("Starting VMstore collection...") # Login to VMstore session_id = tintri.api_login(server_name, user_name, password) # Gather stats vmstore_stats = get_vmstore(session_id) # Get epoch time epochtime = int(time.time()) # All done, log out tintri.api_logout(server_name, session_id) for stat in vmstore_stats: prefix = 'tintri.' + graphite_formatted_server_name + '.vmstore' parse_stat(prefix, vmstore_stats, stat, epochtime) print_debug("Collection finished") # Wait for x seconds print_debug("Sleeping for %s seconds..." % (interval)) time.sleep(interval) #end
def tintri_snapshot(args, vm): try: # Confirm the consistency type. if (args.tvmstore_consistency_type == "crash"): consistency_type = "CRASH_CONSISTENT" elif (args.tvmstore_consistency_type == "vm"): consistency_type = "VM_CONSISTENT" else: raise tintri.TintriRequestsException( "tvmstore_consistency_type is not 'crash' or 'vm': " + args.tvmstore_consistency_type) # Get the preferred version r = tintri.api_version(args.tvmstore) json_info = r.json() print_info("Tintri API Version: " + json_info['preferredVersion']) # Login to VMstore or TGC session_id = tintri.api_login(args.tvmstore, args.tvmstore_user, args.tvmstore_password) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) sys.exit(-10) except tintri.TintriApiException as tae: print_error(tae.__str__()) sys.exit(-11) try: # Create query filter to get the VM specified by the VM name. q_filter = {'name': vm.name} # Get the UUID of the specified VM vm_url = "/v310/vm" r = tintri.api_get_query(args.tvmstore, vm_url, q_filter, session_id) print_debug("The JSON response of the get invoke to the server " + args.tvmstore + " is: " + r.text) vm_paginated_result = r.json() num_vms = int(vm_paginated_result["filteredTotal"]) if num_vms == 0: raise tintri.TintriRequestsException("VM " + vm.name + " doesn't exist") # Get the information from the first item and hopefully the only item. items = vm_paginated_result["items"] vm = items[0] vm_name = vm["vmware"]["name"] vm_uuid = vm["uuid"]["uuid"] print_info(vm_name + " UUID: " + vm_uuid) # Get the time for the snapshot description. now = datetime.datetime.now() now_sec = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) snapshot_name = args.tvmstore_snapshot_name # Take a manual snapshot. take_snapshot(vm_uuid, snapshot_name, consistency_type, args.tvmstore_snapshot_lifetime, args.tvmstore, session_id) # All pau, log out. tintri.api_logout(args.tvmstore, session_id) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(args.tvmstore, session_id) sys.exit(-20) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(args.tvmstore, session_id) sys.exit(-21)
def tintri_logout(vmstore, session_id): # Logout tintri.api_logout(vmstore, session_id)
) (uuid_list, vms_found) = get_vm_uuids(vm_list) if len(vms_found) != len(vm_list): print_error("VMs found: " + str(len(vms_found)) + " != VMs read: " + str(len(vm_list))) print_error("VMs in list:\n " + ", ".join(vm_list)) print_error("VMs found:\n " + ", ".join(vms_found)) raise tt.TintriRequestsException( "The servers specified do not match what was returned") payload = {'typeId': 'com.tintri.api.rest.v310.dto.CollectionChangeRequest', \ 'objectIdsAdded': uuid_list } # Set the static members. tt.api_put(server_name, target_sg_api, payload, session_id) except tt.TintriRequestsException as tre: print_error(tre.__str__()) sys.exit(1) except tt.TintriApiException as tae: print_error(tae.__str__()) sys.exit(1) tt.api_logout(server_name, session_id) print("These VMs: " + ", ".join(vms_found) + ", were found and added to " + service_group)
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
def tintri_logout(host, session_id): print_verbose("Logout ({0})".format(host)) tintri.api_logout(host, session_id)
def set_qos(server_name, user_name, password, vm_uuids, new_min_value, new_max_value): # Get the preferred version r = tintri.api_version(server_name) json_info = r.json() preferred_version = json_info['preferredVersion'] # Verify the correct major and minor versions. versions = preferred_version.split(".") major_version = versions[0] minor_version = int(versions[1]) if major_version != "v310": print_error("Incorrect major version: " + major_version + ". Should be v310.") return "Error" if minor_version < 21: print_error("Incorrect minor Version: " + str(minor_version) + ". Should be 21 or greater") return "Error" # Login into the appropriate VMstore session_id = tintri.api_login(server_name, user_name, password) if session_id is None: return "Error" print_info("Logged onto " + server_name) # Create new QoS object with the fields to be changed modify_qos_info = { 'minNormalizedIops': int(new_min_value), 'maxNormalizedIops': int(new_max_value), 'typeId': 'com.tintri.api.rest.v310.dto.domain.beans.vm.VirtualMachineQoSConfig' } # Create the MultipleSelectionRequest object MS_Request = { 'typeId': 'com.tintri.api.rest.v310.dto.MultipleSelectionRequest', 'ids': vm_uuids, 'newValue': modify_qos_info, 'propertyNames': ["minNormalizedIops", "maxNormalizedIops"] } print_debug("Changing min and max QOS values to (" + str(new_min_value) + ", " + str(new_max_value) + ")") # Update the min and max QoS IOPs modify_qos_url = "/v310/vm/qosConfig" r = tintri.api_put(server_name, modify_qos_url, MS_Request, session_id) 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 204, 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) print_info("Error log off " + server_name) return "Error" tintri.api_logout(server_name, session_id) print_info("Sucesss log off " + server_name) return "OK"
print_debug("The JSON response of the get invoke to the server " + server_name + " is: " + r.text) vm_paginated_result = r.json() num_vms = int(vm_paginated_result["filteredTotal"]) if num_vms == 0: raise tintri.TintriRequestsException("No VMs present") print_info(str(num_vms) + " VMs present") # For each VM, print the VM name and UUID items = vm_paginated_result["items"] count = 1 for vm in items: vm_name = vm["vmware"]["name"] vm_uuid = vm["uuid"]["uuid"] print(str(count) + ": " + vm_name + ", " + vm_uuid) count += 1 # All pau, log out tintri.api_logout(server_name, session_id) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) tintri.api_logout(server_name, session_id) exit(-5) except tintri.TintriApiException as tae: print_error(tae.__str__()) tintri.api_logout(server_name, session_id) exit(-6)
def tintri_logout(host, session_id): print_verbose("Logout ({0})".format(host)) tintri.api_logout(host, session_id)