def checkSnapStateOK(vmid): snapStateOk = True qsnaps = curlRHV('/vms/' + vmid + '/snapshots') replytree = ElementTree.fromstring(qsnaps) vm_snaps = replytree.find(".") #print('Snapshots:') for vm_snap in vm_snaps.iter('snapshot'): #ElementTree.dump(vm_snap); vm_snap_id = vm_snap.attrib['id'] vm_snap_desc = vm_snap.find('description') vm_snap_stat = vm_snap.find('snapshot_status') #print(' - "' + vm_snap_desc.text + '" = ' + vm_snap_id + ' (' + vm_snap_stat.text + ')'); if vm_snap_stat.text != 'ok': snapStateOk = False return snapStateOk
def listSnapsToDelete(vmid, keepN=4): snaps = [] qsnaps = curlRHV('/vms/' + vmid + '/snapshots') replytree = ElementTree.fromstring(qsnaps) vm_snaps = replytree.find(".") #print('Snapshots:') for vm_snap in vm_snaps.iter('snapshot'): #ElementTree.dump(vm_snap); vm_snap_id = vm_snap.attrib['id'] vm_snap_desc = vm_snap.find('description') vm_snap_stat = vm_snap.find('snapshot_status') #print(' - "' + vm_snap_desc.text + '" = ' + vm_snap_id + ' (' + vm_snap_stat.text + ')'); if len(vm_snap_desc.text) >= splen: if config['snap_prefix'] in vm_snap_desc.text[:splen]: # Found an autosnap snapshot snaps.append(vm_snap_desc.text + '=' + vm_snap_id) # We only want to keep the last N (keepN). snaps = sorted(snaps) if len(snaps) > keepN: snaps = sorted(snaps) snaps = snaps[:-keepN] else: snaps = [] return snaps
nrpe_rc = NRPE_OK nrpe_rc_text = NRPE_OK_TEXT # Arguments? if len(argv) > 1: # Help? if argv[1] in ['-?', '-h', '-help', '--help']: print('Syntax: secrets.') exit(3) # Version? if argv[1] in ['-V', '-v', '-version', '--version']: print('Client version: ' + VERSION) exit(3) # Get data on all VMs cr = curlRHV('/vms') #print(cr) # Parse ReplyXML to ElementTree replyXML = ElementTree.fromstring(cr).find(".") # Header when printing all VM states. if report_all: print('VM States:') # Loop over the VMs for vm in replyXML.iter('vm'): #ElementTree.dump(vm); vm_name = vm.find('name') vm_id = vm.attrib['id'] vm_state = vm.find('status')
# Arguments? if len(argv) > 1: # Help? if argv[1] in ['-?', '-h', '-help', '--help']: print('Syntax: secrets.') exit(0) # Version? if argv[1] in ['-V', '-v', '-version', '--version']: print('Client version: ' + VERSION) exit(0) # If we get here, assume the argument is a VM ID. searchString = argv[1] # Search for VM cr = curlRHV('/vms/?search=' + searchString) #print(cr) # Parse ReplyXML to ElementTree replyXML = ElementTree.fromstring(cr).find(".") # Loop over the matches print('Matches:') for vm in replyXML.iter('vm'): #ElementTree.dump(vm); vm_name = vm.find('name') vm_id = vm.attrib['id'] print(' - "' + vm_name.text + '" = ' + vm_id) # EOF
# This is a quick hack for cross-cluster migration (only available in the RHV API now). VERSION = '0.0.1' from curlRHV import curlRHV XMLT = "<action>\n" XMLT += ' <cluster id="TARGETCLUSTERID"/>\n' XMLT += "</action>" # cluster1 = 8826f188-af59-44bc-a257-ca1df19fbbd0 # cluster2 = bc28b818-0c82-43c7-b27e-f0bab706ff46 XML = XMLT.replace("TARGETCLUSTERID", "bc28b818-0c82-43c7-b27e-f0bab706ff46") vmid = "02557f24-502b-49ab-afba-73995e148d75" # Search for VM cr = curlRHV('/vms/' + vmid + '/migrate', XML) print(cr) # EOF
nrpe_rc = NRPE_OK nrpe_rc_text = NRPE_OK_TEXT # Arguments? if len(argv) > 1: # Help? if argv[1] in ['-?', '-h', '-help', '--help']: print('Syntax: secrets.') exit(0) # Version? if argv[1] in ['-V', '-v', '-version', '--version']: print('Client version: ' + VERSION) exit(0) # Get data on all VMs cr = curlRHV('/hosts') #print(cr) # Parse ReplyXML to ElementTree replyXML = ElementTree.fromstring(cr).find(".") # Header when printing all VM states. if report_all: print('Host States:') # Loop over the VMs for host in replyXML.iter('host'): #ElementTree.dump(vm); host_name = host.find('name') host_id = host.attrib['id'] host_state = host.find('status')
for vm in config['backups']: # Now to query snapshots. print('VM=' + vm['name'] + ', VMID=' + vm['vmid']) snapStateOk = checkSnapStateOK(vm['vmid']) if not snapStateOk: print('!!! Snapshot status not OK. Forcing DRYRUN mode...') dryrun = True # Replace the placeholders with the proper values dtstamp = datetime.datetime.now().__format__(config['snap_date_format']) xmlreq = xmlreqt.replace('THISDTS', dtstamp) #print(xmlreq); # Use Curl to send the request. if not dryrun: csnap = curlRHV('/vms/' + vm['vmid'] + '/snapshots/', xmlreq) replytree = ElementTree.fromstring(csnap) replyroot = replytree.find(".") print('New snapshot: name=' + replyroot.find('description').text + ', status=' + replyroot.find('creation_status').text) else: print('Dryrun! No snapshot taken (hypothetical snapshot name: ' + config['snap_prefix'] + '_' + dtstamp + ').') # Wait for snapshot to complete. if not dryrun: done_waiting = False while not done_waiting: sleep(config['refresh_timer']) snapStateOk = checkSnapStateOK(vm['vmid']) if snapStateOk: