Skip to content

Instantly share code, notes, and snippets.

@danielrobbins
Created July 12, 2012 19:01
Show Gist options
  • Save danielrobbins/3100158 to your computer and use it in GitHub Desktop.
Save danielrobbins/3100158 to your computer and use it in GitHub Desktop.
A script to remove broken ZenPacks
packs = None
if hasattr(dmd, 'ZenPackManager'):
packs = dmd.ZenPackManager.packs
else:
packs = dmd.packs
phase2 = False
for pack in packs():
try:
unused = pack.primaryAq()
print "%s is fine." % pack.id
except AttributeError:
print "Problem with %s ZenPack. Forcing removal." % pack.id
try:
packs._remove(pack)
print "Removed %s ZenPack." % pack.id
except AttributeError:
print "Unable to remove this ZenPack."
phase2 = True
# There were some packs we could not remove using the _remove method. Fall back to
# a deeper removal approach using pack ID.
if phase2:
print
print "Starting Phase 2 removal"
print
all_pack_ids = []
for pack_id in dmd.ZenPackManager.packs._objects:
all_pack_ids.append(pack_id)
valid_pack_ids = []
for pack in dmd.ZenPackManager.packs():
try:
valid_packs.append(pack.id)
except:
pass
# iterate over all packs, if missing from valid packs, remove:
for pack_id in all_pack_ids:
if pack_id not in valid_pack_ids:
print "Forced removal of %s" % pack_id
del packs._objects[pack_id]
commit()
@JosefJezek
Copy link

This delete list of Zenpacks from DB. :-(

@rcocchiararo
Copy link

at least on my zenoss 5.0.3 (upgraded from 5.0.2) with a broken vmware esxi monitor zenpack, this script removes ALL the zenpacks (on the 2nd pass, on the first one it fails to remove the broken one)

@ragmanii
Copy link

The above code has a fundamental flaw - it removes ALL Zenpacks. Here is one I hacked together to solve my issue. It only removes zenpacks that are listed as failed. If you want my script to remove all zenpacks just comment out the line valid_pack_ids.append(pack.id), that will force it to reiterate all packs and force remove them during phase 2.

Code Follows - unfortunately this site wants to reformat my comment and not post it like I wrote it. too bad, it will be up to you to put in the correct spacing

Never mind, I figured out what was wrong in the original script, change the line:
valid_packs.append(pack.id)
to
valid_pack_ids.append(pack.id)

Make sure you have the spacing correct, dont remove the leading spaces or tabs.

here was my original script which only itterates the packs in phase one and not twice. I would just use the above script with the change I mentioned to get it to work correctly.

I am including this only for completeness:

packs = None
if hasattr(dmd, 'ZenPackManager'):
packs = dmd.ZenPackManager.packs
else:
packs = dmd.packs

phase2 = False
valid_pack_ids = []

for pack in packs():
try:
unused = pack.primaryAq()
print "%s is fine." % pack.id
valid_pack_ids.append(pack.id)
except AttributeError:
print "Problem with %s ZenPack. Forcing removal." % pack.id
try:
packs._remove(pack)
print "Removed %s ZenPack." % pack.id
except AttributeError:
print "Unable to remove this ZenPack."
phase2 = True

if phase2:
print
print "Starting Phase 2 removal"
print
all_pack_ids = []
for pack_id in dmd.ZenPackManager.packs._objects:
all_pack_ids.append(pack_id)

    for pack_id in all_pack_ids:
            if pack_id not in valid_pack_ids:
                    print "Forced removal of %s" % pack_id
                    del packs._objects[pack_id]
    commit()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment