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

From Zenoss Wiki
Jump to: navigation, search
m (format)
(bulk device deletion,)
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Below is the script to achieve bulk devices deletion on Zenoss : Thanks to Ryan Matt, Daniel Rich of Zenoss Community for contribution .<br />
+
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
+
Copy and paste the content in .py format . Make sure file named devicelist.txt is in same directory as this script .  
  
<syntaxhighlight lang=python>
+
:#!/usr/bin/env python
#!/usr/bin/env python#
+
:
#import the stuff that zendmd needs and create the dmd context
+
:# import the stuff that zendmd needs and create the dmd context
import Globalsfrom Products.ZenUtils.ZenScriptBase
+
:import Globals
import ZenScriptBase
+
:from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit
+
:from transaction import commit
dmd = ZenScriptBase(connect=True, noopts=True).dmd
+
:dmd = ZenScriptBase(connect=True, noopts=True).dmd
 
+
:
# import the stuff that zep needs
+
:# import the stuff that zep needs
from Products.Zuul import getFacade, listFacades
+
:from Products.Zuul import getFacade, listFacades
from zenoss.protocols.jsonformat import from_dict
+
:from zenoss.protocols.jsonformat import from_dict
from zenoss.protocols.protobufs.zep_pb2 import EventSummary
+
:from zenoss.protocols.protobufs.zep_pb2 import EventSummary
from Products.ZenEvents.events2.proxy import EventSummaryProxy
+
:from Products.ZenEvents.events2.proxy import EventSummaryProxy
 
+
:
# function to delete device
+
:# function to delete device
def delDevice(host):
+
:def delDevice(host):
  zep = getFacade('zep')  
+
zep = getFacade('zep')
  zep_host_filter = zep.createEventFilter(element_identifier=host)
+
zep_host_filter = zep.createEventFilter(element_identifier=host)
  zep.closeEventSummaries(eventFilter=zep_host_filter)
+
zep.closeEventSummaries(eventFilter=zep_host_filter)
  dev = dmd.Devices.findDeviceByIdExact(host)
+
dev = dmd.Devices.findDeviceByIdExact(host)
  if dev:  
+
if dev:
    dev.deleteDevice()  
+
:    dev.deleteDevice()
  commit()  
+
commit()
# read list of devices from a file and delete them if they exist
+
:
f = open('devicelist.txt')
+
:# read list of devices from a file and delete them if they exist
lines = f.read().splitlines()
+
:f = open('devicelist.txt')
for l in lines:
+
:lines = f.read().splitlines()
  d = None  
+
:for l in lines:
  d = dmd.Devices.findDevice(l)  
+
d = None
  if d:  
+
d = dmd.Devices.findDevice(l)
    print 'Deleting: %s (%s)' % (d.getDeviceName(), d.getManageIp())    delDevice(l)  
+
if d:
  else:
+
:    print 'Deleting: %s (%s)' % (d.getDeviceName(), d.getManageIp())
    print 'Device %s does not exist.' % (l)f.close()
+
:   delDevice(l)
</syntaxhighlight>
+
else:
 
+
:    print 'Device %s does not exist.' % (l)
[[Category:Tips]]
+
:f.close()
[[Category:ZenDMD]]
+

Revision as of 16:17, 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 .

  1. !/usr/bin/env python
  1. import the stuff that zendmd needs and create the dmd context
import Globals
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit
dmd = ZenScriptBase(connect=True, noopts=True).dmd
  1. import the stuff that zep needs
from Products.Zuul import getFacade, listFacades
from zenoss.protocols.jsonformat import from_dict
from zenoss.protocols.protobufs.zep_pb2 import EventSummary
from Products.ZenEvents.events2.proxy import EventSummaryProxy
  1. 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()
  1. 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()