LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2014, 05:27 PM   #1
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Python Faceplant - Newbie level help requested


Hey Everyone,

Thanks for taking the time to look at what I'm doing here. My disclaimer: I very, very rarely code. I've done a few python and perl scripts in my day but it has never been my strength. I have completed the "learn python the hard way" tutorial so the fundamentals are there just can't seem to put it together.


This script uses vCenter API calls to get a list of all the VMs on our vCenter server. I then want to execute a shell subprocess to query against a proprietary database and compare the two to find any that exist in the vCenter API call but not in the database.

I am able to successfully execute the subprocess against the database and get the results from the API call but am having trouble getting the two to compare properly, I'm making backwards progress now from attempting multiple different methods found on google so apologize for any sloppiness.

Code:
#!/usr/bin/python

from pysphere import VIServer
import re

import shlex, subprocess, sys


def compare_vms(x, y):
        return frozenset(x).intersection(y)


vCenterServer = "REMOVED"
username = 'REMOVED'
password = 'REMOVED'

### Perform the vCenter API call, store results in vmList. Working as intended

server = VIServer()
server.connect(vCenterServer,username,password)
vmList = server.get_registered_vms()
server.disconnect()

regularVMs = []
templates = []

### Setup our query and the use shlex to split up the arguments. Working correctly.
vmsConfigured = 'mminfo -ak -q "savetime>3 days ago,pool=REMOVED" -r "vmname"'
args = shlex.split(vmsConfigured)

print args

# Execute the subprocess to query our DB. This works as intended but always displays the results to stdout. I could overlook that but would prefer to not see the entire 1000+ list returned to the CLI
query = subprocess.Popen(args)

#print query


### Sort our vmList set to pull out any VMs that are templates, I dont care about templates. This works as intended. I can do a for loop to print the regularVMs list with no errors.
for vm in vmList:
        m = re.search('\.vmt?x$', vm)
        if m.group(0) == '.vmtx':
                templates.append(vm)
        else:
                regularVMs.append(vm)


## And here is where I just fall on my face and have no idea what the best way to skin this cat is.
print "----New VMs-----" 
for vm in sorted(set(regularVMs) - set(query)):
       print vm

Any help or suggestions are very much appreciated!


This is difficult to test as it uses API calls and proprietary DB but sample data is below:


I probably need to split this or something to compare properly(havent really tried that yet)
Code:
vCenter API Call results:
----Regular VMs-----
[DATASTORE] VMNAME/VMCONFIGFILE.vmx
[DATASTORE] VMNAME/VMCONFIGFILE.vmx
[DATASTORE] VMNAME/VMCONFIGFILE.vmx
Code:
mminfo/db results:
VMNAME
VMNAME
VMNAME
 
Old 07-01-2014, 05:42 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
So you want:

Code:
[DATASTORE] VMNAME/VMCONFIGFILE.vmx
changed to:

Code:
VMNAME
for the comparison. Is that correct?

And in the code, regularVMs is the "[DATASTORE] VMNAME/VMCONFIGFILE.vmx" list and query is the "VMNAME" list, right?

In that case,

Code:
for vm in sorted(set(regularVMs) - set(query)):
       print vm
Becomes

Code:
regularVmS = [vm.split()[1].split('/')[0] for vm in regularVMS]
for vm in sorted(set(regularVMs) - set(query)):
       print vm

Last edited by dugan; 07-01-2014 at 05:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best higher level language for someone competent in C? (C ++,Obj C, Python)? D1ver Programming 2 07-02-2012 08:21 PM
LXer: MochiKit feels like Python but delivers high-level JavaScript LXer Syndicated Linux News 0 11-29-2006 08:21 AM
python: logging can't log info level? Chowroc Programming 2 05-07-2006 03:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration