-
Notifications
You must be signed in to change notification settings - Fork 2
/
deviceGroups_to_file.py
executable file
·69 lines (58 loc) · 1.8 KB
/
deviceGroups_to_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# Author: Jane Curry
# Date Sept 11th 2012
# Description: Output all Groups with device instances to a file supplied by user
# Output gives Group and device id
# Output is sorted on Group and then device id
# Parameters: File name for output
# Updates:
#
import sys
import time
from optparse import OptionParser
import Globals
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
parser = OptionParser()
parser.add_option("-f", "--file", dest="outputFile",
help="Please specify full path to output file", metavar="FILE")
(options, args) = parser.parse_args()
if not options.outputFile:
parser.print_help()
sys.exit()
of = open(options.outputFile, "w")
localtime = time.asctime( time.localtime(time.time()) )
of.write(localtime + "\n\n")
# Need noopts=True or it barfs with the script options
dmd = ZenScriptBase(connect=True, noopts=True).dmd
dclist=[]
def traverse(dc, of):
dclist.append(dc)
for subdc in dc.children():
traverse.level +=1
traverse(subdc, of)
traverse.level -=1
return dclist.sort()
def printTree(dclist, of):
# First sort the device classes by path name
listNames=[]
for dc in dclist:
listNames.append(dc.getOrganizerName())
listNames.sort()
for dcname in listNames:
dc=root.getOrganizer(dcname)
of.write('Group %s \n' % (dc.getOrganizerName()))
of.write(' Devices: for Group %s ' % (dc.getOrganizerName()))
devlist=[]
for d in dc.getSubDevices():
devlist.append(d.id)
# Need to get a sorted list of devices
devlist.sort()
for dev in devlist:
d=dmd.Devices.findDevice(dev)
of.write(' %s ,' % (d.id))
of.write('\n\n')
traverse.level = 1
root = dmd.getDmdRoot('Groups')
traverse(root, of)
printTree(dclist, of)
of.close()