Created
July 11, 2012 17:07
-
-
Save danielrobbins/3091770 to your computer and use it in GitHub Desktop.
A script to help repair data integrity problems with your Zenoss install
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import Globals | |
from Products.ZenUtils.ZenScriptBase import ZenScriptBase | |
from ZODB.transact import transact | |
def checkIp(dev): | |
print "Checking Ips for %s" % dev.titleOrId() | |
for interface in dev.os.interfaces(): | |
interface.checkRelations(repair=True) | |
for ip in interface.ipaddresses(): | |
try: | |
ip.urlLink(ip.getIpAddress()) | |
except: | |
print "Removing ip %s from %s" % ( ip.id,dev.titleOrId()) | |
try: | |
interface.ipaddresses._delObject(ip.id) | |
#commit() # don't commit inside the @transact | |
except: | |
interface.ipaddresses._remove(ip) | |
#commit() # don't commit inside the @transact | |
def checkRoute(dev): | |
print "Checking Routes for %s" % dev.titleOrId() | |
for route in dev.os.routes(): | |
route.checkRelations(repair=True) | |
def checkLayer3(dev): | |
print "Checking Layer 3 Catalog for %s" % dev.titleOrId() | |
cat = dmd.ZenLinkManager._getCatalog(layer=3) | |
brains = cat(deviceId=dev.id) | |
for brain in brains: | |
try: | |
unused = brain.getObject() | |
except: | |
print "Removing bad Catalog object for path %s" % brain.getPath() | |
cat.uncatalog_object(brain.getPath()) | |
def checkLayer2(dev): | |
print "Checking Layer 2 Catalog for %s" % dev.titleOrId() | |
cat = dmd.ZenLinkManager._getCatalog(layer=2) | |
brains = cat(deviceId=dev.id) | |
for brain in brains: | |
try: | |
unused = brain.getObject() | |
except: | |
print "Removing bad Catalog object for path %s" % brain.getPath() | |
cat.uncatalog_object(brain.getPath()) | |
def checkGlobalCatalog(dev): | |
try: | |
print "Checking Global Catalog for %s" % dev.titleOrId() | |
cat = dmd.global_catalog | |
brains = cat(deviceId=dev.id) | |
for brain in brains: | |
try: | |
unused = brain.getObject() | |
except: | |
print "Removing bad Catalog object for path %s" % brain.getPath() | |
cat.uncatalog_object(brain.getPath()) | |
except: | |
print "Skipping Global catalog check, does not exist in dmd." | |
def checkDeviceClass(dev): | |
print "Checking Device Class linkage for %s" % dev.titleOrId() | |
if dev.deviceClass() == None: | |
print "Repairing Device Class linkage for %s" % dev.titleOrId() | |
dc = dev.getPrimaryParent().getPrimaryParent() | |
dev.deviceClass._add(dc) | |
# Will commit at the end, and retry up to 5 times if there's a database conflict error. | |
@transact | |
def processOneDevice(dev): | |
print "\n\nChecking %s" % dev.titleOrId() | |
dev.checkRelations(repair=True) | |
dev.hw.checkRelations(repair=True) | |
dev.os.checkRelations(repair=True) | |
checkDeviceClass(dev) | |
checkIp(dev) | |
checkRoute(dev) | |
checkGlobalCatalog(dev) | |
checkLayer2(dev) | |
checkLayer3(dev) | |
dev.reindex_all() | |
if __name__ == '__main__': | |
dmd = ZenScriptBase(connect=True).dmd | |
for dev in dmd.Devices.getSubDevicesGen(): # This reads them from the catalog. | |
processOneDevice(dev) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment