-
Notifications
You must be signed in to change notification settings - Fork 0
/
querySoftware.py
executable file
·135 lines (115 loc) · 4.64 KB
/
querySoftware.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/env python
###########################################################################
#
# This program is part of Zenoss Core, an open source monitoring platform.
# Copyright (C) 2007, Zenoss Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# For complete information please visit: http://www.zenoss.com/oss/
#
###########################################################################
import Globals
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from Products.ZenModel.DeviceOrganizer import DeviceOrganizer
import logging
class SoftwareRetriever(ZenScriptBase):
def __init__(self):
"""
create object and and coonect to the db
"""
ZenScriptBase.__init__(self, connect=True)
self.log = logging.getLogger("SR")
def _getSoftware(self, devobjects):
"""
for a bunch of device objects query their software
"""
self.log.debug("getting software for: %s" % devobjects)
softwarelist = {}
for d in devobjects:
softwarelist[d.titleOrId()] = [s.id for s in d.os.software()]
return softwarelist
def getSoftwareForDevices(self, servers):
"""
take an array of device ids, make sure to check their ids and
retrieve the device object
"""
if not servers: return
if isinstance(servers, str):
servers = [servers]
#prune the server ids to make sure they are all valid
vservers = []
for s in servers:
d = self.dmd.Devices.findDevice(s)
if d: vservers.append(d)
else: self.log.warning("invalid device id %s, skipping..." % s)
return self._getSoftware(vservers)
def getSoftwareForOrganizer(self, orgname):
"""
check the validity of a given oragnizer and retrive the devices under it
"""
org = None
try:
org = self.dmd.getObjByPath("/zport/dmd"+ orgname)
except:
pass
if not isinstance(org, DeviceOrganizer):
self.log.error("no organizer with the name %s exists" % orgname)
return None
return self._getSoftware(org.getSubDevices())
def buildOptions(self):
"""
add some additional options
"""
ZenScriptBase.buildOptions(self)
self.parser.add_option('--organizer',
dest="orgname",default=None,
help="specify the organizer for which you'd like to query software for, "\
"this setting will take presedence over the devices setting")
self.parser.add_option('--device',
dest="devices", type="str", default=None, action="append",
help="specify the device(s) you want to query software for")
self.parser.add_option('--settype', type="str",
dest="settype", default=None,
help="specify which type of set operation you'd like to perform, \
i.e. union, difference, intersection")
def main():
#retrieve the software
sq = SoftwareRetriever()
software = None
if sq.options.orgname:
sq.log.info("retieving software for %s" % sq.options.orgname)
software = sq.getSoftwareForOrganizer(sq.options.orgname)
elif sq.options.devices:
sq.log.info("retieving software for %s" % sq.options.orgname)
software = sq.getSoftwareForDevices(sq.options.devices)
else:
raise Exception("please specify at least one organizer of devicename")
#if we have software, let's do something with it
if software:
stype = sq.options.settype
myset = None
if stype:
myset = None
sq.log.info("determining the %s of the all the software" % stype)
if stype == "union":
myset = reduce(lambda a, b: set(a).union(b), software.values())
elif stype == "intersection":
myset = reduce(lambda a, b: set(a).intersection(b), software.values())
elif stype == "difference":
myset = reduce(lambda a, b: set(a).difference(b), software.values())
else:
raise Exception("set type %s is unsupported" % stype)
#print the output
for d,s in software.items():
print "-" * 50
print d
print ",".join(s)
if myset:
print "-" * 50
print "set type: %s" % stype
print ",".join(myset)
if __name__ == "__main__":
main()