Difference between revisions of "ZenDMD Tip - Bulk Device Removal"

From Zenoss Wiki
Jump to: navigation, search
(Added the code)
(formatted the content)
Line 1: Line 1:
 
Below is the script to achieve bulk devices deletion on Zenoss :
 
Below is the script to achieve bulk devices deletion on Zenoss :
  
Copy and paste the content in .py format . Make sure file named devicelist.txt is in same directory as this script .  
+
Copy and paste the content in .py format . Make sure file named devicelist.txt is in same directory as this script . Script can be downloaded from https://gist.github.com/hackeys/6367951#file-bulkdevicedeletion-py
<pre>
+
  
#!/usr/bin/env python# import the stuff that zendmd needs and create the dmd contextimport Globalsfrom Products.ZenUtils.ZenScriptBase import ZenScriptBasefrom transaction import commitdmd = ZenScriptBase(connect=True, noopts=True).dmd # import the stuff that zep needsfrom Products.Zuul import getFacade, listFacadesfrom zenoss.protocols.jsonformat import from_dictfrom zenoss.protocols.protobufs.zep_pb2 import EventSummaryfrom Products.ZenEvents.events2.proxy import EventSummaryProxy # function to delete devicedef delDevice(host): zep = getFacade('zep')  zep_host_filter = zep.createEventFilter(element_identifier=host)  zep.closeEventSummaries(eventFilter=zep_host_filter) dev = dmd.Devices.findDeviceByIdExact(host) if dev:   dev.deleteDevice()  commit() # read list of devices from a file and delete them if they existf = open('devicelist.txt')lines = f.read().splitlines()for l in lines: d = None  d = dmd.Devices.findDevice(l)  if d:   print 'Deleting: %s (%s)' % (d.getDeviceName(), d.getManageIp())    delDevice(l)  else:    print 'Device %s does not exist.' % (l)f.close()
+
<pre>
</pre>
+
#!/usr/bin/env python#
 +
#import the stuff that zendmd needs and create the dmd context
 +
import Globalsfrom Products.ZenUtils.ZenScriptBase
 +
import ZenScriptBasefrom transaction
 +
import commitdmd = ZenScriptBase(connect=True, noopts=True).dmd
 +
# import the stuff that zep needsfrom Products.Zuul
 +
import getFacade, listFacadesfrom zenoss.protocols.jsonformat
 +
import from_dictfrom zenoss.protocols.protobufs.zep_pb2
 +
import EventSummaryfrom Products.ZenEvents.events2.proxy  
 +
import EventSummaryProxy
 +
# function to delete device
 +
def delDevice(host):
 +
  zep = getFacade('zep')  
 +
  zep_host_filter = zep.createEventFilter(element_identifier=host)
 +
  zep.closeEventSummaries(eventFilter=zep_host_filter)
 +
  dev = dmd.Devices.findDeviceByIdExact(host)
 +
  if dev:  
 +
dev.deleteDevice()  
 +
  commit()  
 +
# read list of devices from a file and delete them if they exist
 +
f = open('devicelist.txt')
 +
lines = f.read().splitlines()
 +
for l in lines:
 +
  d = None  
 +
  d = dmd.Devices.findDevice(l)  
 +
  if d:  
 +
print 'Deleting: %s (%s)' % (d.getDeviceName(), d.getManageIp())    delDevice(l)  else:    print 'Device %s does not exist.' % (l)f.close()

Revision as of 16:30, 28 August 2013

Below is the script to achieve bulk devices deletion on Zenoss :

Copy and paste the content in .py format . Make sure file named devicelist.txt is in same directory as this script . Script can be downloaded from https://gist.github.com/hackeys/6367951#file-bulkdevicedeletion-py

#!/usr/bin/env python#
 #import the stuff that zendmd needs and create the dmd context
import Globalsfrom Products.ZenUtils.ZenScriptBase
 import ZenScriptBasefrom transaction
import commitdmd = ZenScriptBase(connect=True, noopts=True).dmd
 # import the stuff that zep needsfrom Products.Zuul
 import getFacade, listFacadesfrom zenoss.protocols.jsonformat
import from_dictfrom zenoss.protocols.protobufs.zep_pb2
 import EventSummaryfrom Products.ZenEvents.events2.proxy 
import EventSummaryProxy
 # function to delete device
def delDevice(host):
  zep = getFacade('zep') 
 zep_host_filter = zep.createEventFilter(element_identifier=host)
 zep.closeEventSummaries(eventFilter=zep_host_filter)
  dev = dmd.Devices.findDeviceByIdExact(host)
  if dev: 
 dev.deleteDevice() 
 commit() 
# read list of devices from a file and delete them if they exist
f = open('devicelist.txt')
lines = f.read().splitlines()
for l in lines:
  d = None 
 d = dmd.Devices.findDevice(l) 
 if d:   
 print 'Deleting: %s (%s)' % (d.getDeviceName(), d.getManageIp())    delDevice(l)  else:    print 'Device %s does not exist.' % (l)f.close()