I stripped down a 'PatchPolicy_2_CSV.py' script we created awhile back, there may be a few extra not needed lines in this script, but it works to print patch information of patches in a patch policy:
#!/opt/opsware/bin/python2
#####
# list_pp_contents.py
# http://www.augustschell.com
#####
import sys
import types
sys.path.append("/opt/opsware/pylibs2")
from pytwist import *
TWISTHOST = "localhost"
patchPolicyName = 'IAVA Patch Policy for 2008 R2 Servers (64bit)'
def fetchAttr(objectZed, strAttr):
listFilterStrings = ["|", "\r", "\n"]
if "." in strAttr:
(parentAttr, childAttr) = strAttr.split(".", 1)
parentReturn = getattr(objectZed, parentAttr)
attrReturn = fetchAttr(parentReturn, childAttr)
else:
if objectZed:
attrReturn = getattr(objectZed, strAttr)
else:
attrReturn = objectZed
if attrReturn is None:
attrReturn = ''
if attrReturn is True:
attrReturn = 'True'
if attrReturn is False:
attrReturn = 'False'
if type(attrReturn) == types.InstanceType and dir(attrReturn) != []:
if "id" and "idAsLong" and "name" in dir(attrReturn):
attrReturn = attrReturn.name
if type(attrReturn) == types.StringType:
for filterStr in listFilterStrings:
attrReturn = attrReturn.replace(filterStr, "")
return str(attrReturn)
def walkAttrs(objectZed, listAttrs):
"""
used to return a list with the results for a list of given attributes
"""
return [fetchAttr(objectZed, strAttr) for strAttr in listAttrs]
def getPatchesFromPatchPolicy(policyName=None):
"""
used to fetch list of VO's for every patch attached to a given patch policy
"""
myFilter = com.opsware.search.Filter()
if policyName:
myFilter.expression = '{PatchPolicyVO.name = "%s"}' % (policyName)
ppservice = ts.swmgmt.PatchPolicyService
listPatchPolicyRefs = ppservice.findPatchPolicyRefs(myFilter)
if len(listPatchPolicyRefs) == 0:
print "Policy '%s' not found in HP SA!" % (policyName)
sys.exit(4)
if len(listPatchPolicyRefs) > 1:
print "Duplicate patch policy '%s' in HP SA!" % (policyName)
sys.exit(4)
patchPolicyVO = ppservice.getPatchPolicyVO(listPatchPolicyRefs[0])
listPatchRefs = ppservice.getPatches(listPatchPolicyRefs)
listPatchVOs = ts.pkg.UnitService.getUnitVOs(listPatchRefs)
return (patchPolicyVO, listPatchVOs)
def getHotfixInfo(listHotfixVOs):
listHotfixes = []
for hotfixVO in listHotfixVOs:
dictHotfixInfo = walkAttrs(hotfixVO, listPatchAttrs)
if (dictHotfixInfo in listHotfixes) and warnDupes:
print "!!!DUPLICATE FOUND: %s" % (dictHotfixInfo)
listHotfixes.append(dictHotfixInfo)
return listHotfixes
ts = twistserver.TwistServer(TWISTHOST)
listPatchAttrs = [
"name", "platform", "softwareRelease", "version", "updateId",
"location", "downloadUrl", "fileName", "metadataSource",
"createdBy", "checksum", "patchStatus"]
try:
(patchPolicyVO, listHotfixVOs) = getPatchesFromPatchPolicy(patchPolicyName)
listHotfixes = getHotfixInfo(listHotfixVOs)
for entry in listHotfixes:
print entry
except:
print "Something Didn't Work"
Sample Output:
['Q2679255', 'Windows Server 2008 R2 x64', '7382edbe-0388-40a1-ab7d-9d05cddf309e', '2', 'com.hp.hpln.mps.ba19383f1c49358d0fedc18fcdcf7841', '/packages/any/nt/6.1-X64/Windows6.1-KB2679255-v2-x64.msu', 'http://download.microsoft.com/download/6/6/B/66B6D917-1CF1-4F97-A13A-1FF9C7F4FBED/Windows6.1-KB2679255-v2-x64.msu', 'Windows6.1-KB2679255-v2-x64.msu', 'OPSWWPDB-2012.4.10', 'opswpatch import', 'bebd9119d6887e045856564452d97957155e3588', 'AVAILABLE']
['Q2703157', 'Windows Server 2008 R2 x64', 'fe9db004-9d38-4a7c-9658-f606a1ec149b', '2', 'com.hp.hpln.mps.cc00efa894ba1504262cf961e126713f', '/packages/any/nt/6.1-X64/Windows6.1-KB2703157-x64.msu', 'http://download.microsoft.com/download/6/E/1/6E1C23D9-45CF-42B3-AD4E-1E6A3E0CC32E/Windows6.1-KB2703157-x64.msu', 'Windows6.1-KB2703157-x64.msu', 'OPSWWPDB-2012.6.12', 'opswpatch import', '4ac0ca0bb05b86cd7aac155598a05a003132fad6', 'AVAILABLE']