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 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 tintri_login(vmstore, user, password): # Login and get session_id try: ret = tintri.api_version(vmstore) json_info = ret.json() print_info("API Version: " + json_info['preferredVersion']) # Login to VMstore session_id = tintri.api_login(vmstore, user, password) return session_id except tintri.TintriRequestsException as tre: print_error(tre.__str__()) exit(-1) except tintri.TintriApiException as tae: print_error(tae.__str__()) exit(-2)
password = sys.argv[3] graphite_server = sys.argv[4] graphite_port = int(sys.argv[5]) print("Using paramerts passed in on the command line\n") else: print("\nPrints VMstore information\n") print( "Usage: " + sys.argv[0] + " vmstore_fqdn vmstore_username vmstore_password graphite_fqdn graphite_port\n" ) sys.exit(-1) graphite_formatted_server_name = server_name.replace(".", "_") # Get the preferred version r = tintri.api_version(server_name) 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())
if os.path.isfile(file_loc): with open(file_loc, 'r') as csv_file: vm_csv_list = csv.reader(csv_file) # Gets a list of VMs from a CSV file. # Should be able to process both Windows and Linux files. for row in vm_csv_list: for item in row: if item == "": continue vm_list.append(item.lstrip(' ')) else: raise tt.TintriRequestsException("Could not find file " + file_loc) # Get the API version and product. r = tt.api_version(server_name) json_info = r.json() preferred_version = json_info['preferredVersion'] product_name = json_info['productName'] # Check for correct product if product_name != "Tintri Global Center": raise tt.TintriRequestsException( "Tintri server should be Tintri Global Center, not " + product_name) # Check API version. versions = preferred_version.split(".") major_version = versions[0] minor_version = int(versions[1]) if major_version != "v310":
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"
def main(): global NAGIOS_OK, NAGIOS_WARNING, NAGIOS_CRITICAL, NAGIOS_UNKNOWN, NAGIOS_MESSAGE, NAGIOS_STATE global verbose # Nagios Plugin Return Codes NAGIOS_OK = 0 NAGIOS_WARNING = 1 NAGIOS_CRITICAL = 2 NAGIOS_UNKNOWN = 3 # The Nagios Message NAGIOS_MESSAGE = "" # Everything is fine by default NAGIOS_STATE = NAGIOS_OK args = parse_args() if args.verbose: verbose = True else: verbose = False # Get the product name try: r = tintri.api_version(args.host) json_info = r.json() product_name = json_info['productName'] # The expected product name is "Tintri VMstore" if (product_name != "Tintri VMstore"): print("Incompatible VMstore has been detected!") exit(NAGIOS_CRITICAL) # Login to Tintri VMStore session_id = tintri.api_login(args.host, args.user, args.password) atexit.register(tintri_logout, args.host, session_id) except tintri.TintriRequestsException as tre: print_error(tre.__str__()) exit(NAGIOS_CRITICAL) except tintri.TintriApiException as tae: print_error(tae.__str__()) exit(NAGIOS_CRITICAL) if (get_maintenance_mode(args.host, session_id)): print("Maintenance mode active!") exit(NAGIOS_WARNING) # Checking free space and such get_datastore_stats(args.host, session_id, args.warning, args.error) # Checking alerts in the Inbox get_alert_count(args.host, session_id) # Checking if there are any failed components # Note: It takes a lot of time to collect this information get_failed_components(args.host, session_id) # Checking generic status values get_operational_status(args.host, session_id) # Exiting with the calculated state print("{0} ({1})".format(NAGIOS_MESSAGE, NAGIOS_STATE)) exit(NAGIOS_STATE)
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)