1
2
3
4
5
6
7
8
9
10
11 import logging
12 from urllib2 import URLError
13
14 from Products import Zuul
15 from Products.ZenMessaging.audit import audit
16 from Products.Zuul.routers import TreeRouter
17 from Products.ZenUtils.Ext import DirectResponse
18 from Products.Zuul.form.interfaces import IFormBuilder
19 from Products.Zuul.interfaces import IInfo, ITreeNode
20
21 log = logging.getLogger('zen.ApplicationRouter')
22
23
25 """
26 """
27
29 return Zuul.getFacade('applications', self.context)
30
32 return Zuul.getFacade('monitors', self.context)
33
35 """
36 Returns the tree structure of the application and collector
37 hierarchy.
38
39 @type id: string
40 @param id: Id of the root node of the tree to be returned
41 @rtype: [dictionary]
42 @return: Object representing the tree
43 """
44 try:
45 appfacade = self._getFacade()
46 monitorfacade = Zuul.getFacade("monitors", self.context)
47 nodes = [ITreeNode(m) for m in monitorfacade.query()]
48 for monitor in nodes:
49 apps = appfacade.queryMonitorDaemons(monitor.name)
50 for app in apps:
51 monitor.addChild(IInfo(app))
52 apps = appfacade.queryMasterDaemons()
53 for app in apps:
54 nodes.append(IInfo(app))
55 return Zuul.marshal(nodes)
56 except URLError as e:
57 log.exception(e)
58 return DirectResponse.fail(
59 "Error fetching daemons list: " + str(e.reason)
60 )
61
78
80 """
81 Will issue the command to start the selected ids
82 @type uids: Array[Strings]
83 @param uids: List of valid daemon ids that will need to started
84 @rtype: DirectResposne
85 @return: DirectReponse of success if no errors are encountered
86 """
87 facade = self._getFacade()
88 for uid in uids:
89 facade.start(uid)
90 audit('UI.Applications.Start', id)
91 if len(uids) > 1:
92 return DirectResponse.succeed("Started %s daemons" % len(uids))
93 return DirectResponse.succeed()
94
95 - def stop(self, uids):
96 """
97 Will issue the command to stop the selected ids
98 @type uids: Array[Strings]
99 @param uids: List of valid daemon ids that will need to stopped
100 @rtype: DirectResposne
101 @return: DirectReponse of success if no errors are encountered
102 """
103 facade = self._getFacade()
104 for uid in uids:
105 facade.stop(uid)
106 audit('UI.Applications.Stop', id)
107 if len(uids) > 1:
108 return DirectResponse.succeed("Stopped %s daemons" % len(uids))
109 return DirectResponse.succeed()
110
112 """
113 Will issue the command to restart the selected ids
114 @type uids: Array[Strings]
115 @param uids: List of valid daemon ids that will need to restarted
116 @rtype: DirectResposne
117 @return: DirectReponse of success if no errors are encountered
118 """
119 facade = self._getFacade()
120 for uid in uids:
121 facade.restart(uid)
122 audit('UI.Applications.Restart', id)
123 if len(uids) > 1:
124 return DirectResponse.succeed("Restarted %s daemons" % len(uids))
125 return DirectResponse.succeed()
126
128 """
129 Enables or disables autostart on all applications passed into uids.
130 If it is already in that state it is a noop.
131 @type uids: Array[Strings]
132 @param uids: List of valid daemon ids that will need to enabled
133 @type enabled: boolean
134 @param uids: true for enabled or false for disabled
135 @rtype: DirectResposne
136 @return: DirectReponse of success if no errors are encountered
137 """
138 facade = self._getFacade()
139 applications = facade.query()
140 for app in applications:
141 if app.id in uids:
142 app.autostart = enabled
143 audit('UI.Applications.AutoStart', id, {'autostart': enabled})
144 return DirectResponse.succeed()
145
147 """
148 Returns the serialized info object for the given id
149 @type: id: String
150 @param id: Valid id of a application
151 @rtype: DirectResponse
152 @return: DirectResponse with data of the application
153 """
154 facade = self._getFacade()
155 app = facade.get(id)
156 data = Zuul.marshal(IInfo(app))
157 return DirectResponse.succeed(data=data)
158
160 """
161 Returns a list of resource pool identifiers.
162 @rtype: DirectResponse
163 @return: B{Properties}:
164 - data: ([String]) List of resource pool identifiers
165 """
166 pools = self._monitorFacade().queryPools()
167 ids = (dict(name=p.id) for p in pools)
168 return DirectResponse.succeed(data=Zuul.marshal(ids))
169
171 """
172 Returns all the configuration files for an application
173 """
174 facade = self._getFacade()
175 info = IInfo(facade.get(id))
176 files = info.configFiles
177 return DirectResponse.succeed(data=Zuul.marshal(files))
178
180 """
181 Updates the configuration files for an application specified by id.
182 The configFiles parameters is an array of dictionaries of the form:
183 {
184 filename: "blah",
185 contents: "line 1\nline 2\n..."
186 }
187 The filename parameter serves as the "id" of each configFile
188 passed in.
189 """
190 facade = self._getFacade()
191 info = IInfo(facade.get(id))
192 for f in configFiles:
193 configFile = info.getConfigFileByFilename(f['filename'])
194 if configFile:
195 configFile.content = f['content']
196 facade.updateService(id)
197 return DirectResponse.succeed()
198