| Trees | Indices | Help |
|
|---|
|
|
1 ###########################################################################
2 #
3 # This program is part of Zenoss Core, an open source monitoring platform.
4 # Copyright (C) 2007, Zenoss Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License version 2 or (at your
8 # option) any later version as published by the Free Software Foundation.
9 #
10 # For complete information please visit: http://www.zenoss.com/oss/
11 #
12 ###########################################################################
13
14 import os
15 import logging
16 log = logging.getLogger('zen.Mibs')
17
18 from Globals import DTMLFile
19 from Globals import InitializeClass
20 from AccessControl import ClassSecurityInfo
21 from AccessControl import Permissions
22 from Products.Jobber.jobs import ShellCommandJob
23 from Products.ZenModel.ZenossSecurity import *
24
25 from Products.ZenRelations.RelSchema import *
26 from Products.ZenUtils.Search import makeCaseInsensitiveKeywordIndex
27 from Products.ZenWidgets import messaging
28 from Products.ZenUtils.Utils import binPath
29 from Organizer import Organizer
30 from MibModule import MibModule
31 from ZenPackable import ZenPackable
32
34 """make a device class"""
35 sc = MibOrganizer(id)
36 context._setObject(id, sc)
37 sc = context._getOb(id)
38 if REQUEST is not None:
39 REQUEST['RESPONSE'].redirect(context.absolute_url() + '/manage_main')
40
41 addMibOrganizer = DTMLFile('dtml/addMibOrganizer',globals())
42
43
45 """Return a name for an oid. This function is extracted out of the
46 MibOrganizer class and takes mibSearch as a parameter to make it easier to
47 unit test.
48 """
49 oid = oid.strip('.')
50
51 if exactMatch:
52 brains = mibSearch(oid=oid)
53 if len(brains) > 0:
54 return brains[0].id
55 else:
56 return ""
57
58 oidlist = oid.split('.')
59 for i in range(len(oidlist), 0, -1):
60 brains = mibSearch(oid='.'.join(oidlist[:i]))
61 if len(brains) < 1: continue
62 if len(oidlist[i:]) > 0 and not strip:
63 return "%s.%s" % (brains[0].id, '.'.join(oidlist[i:]))
64 else:
65 return brains[0].id
66 return ""
67
68
70 """
71 DeviceOrganizer is the base class for device organizers.
72 It has lots of methods for rolling up device statistics and information.
73 """
74 meta_type = "MibOrganizer"
75 dmdRootName = "Mibs"
76 default_catalog = 'mibSearch'
77
78 security = ClassSecurityInfo()
79
80 _relations = Organizer._relations + ZenPackable._relations + (
81 ("mibs", ToManyCont(ToOne,"Products.ZenModel.MibModule","miborganizer")),
82 )
83
84
85 # Screen action bindings (and tab definitions)
86 factory_type_information = (
87 {
88 'immediate_view' : 'mibOrganizerOverview',
89 'actions' :
90 (
91 { 'id' : 'overview'
92 , 'name' : 'Overview'
93 , 'action' : 'mibOrganizerOverview'
94 , 'permissions' : ( Permissions.view, )
95 },
96 { 'id' : 'viewHistory'
97 , 'name' : 'Modifications'
98 , 'action' : 'viewNewHistory'
99 , 'permissions' : (ZEN_VIEW_MODIFICATIONS,)
100 },
101 )
102 },
103 )
104
105
107 if not id: id = self.dmdRootName
108 super(MibOrganizer, self).__init__(id, description)
109 if self.id == self.dmdRootName:
110 self.createCatalog()
111
113 return MibOrganizer
114
116 """Return a count of all our contained children."""
117 count = len(self.mibs())
118 for child in self.children():
119 count += child.countMibs()
120 return count
121
123 """Return a name for an oid.
124 """
125 return _oid2name(self.getDmdRoot("Mibs").mibSearch, oid,
126 exactMatch, strip)
127
128
130 """Return an oid based on a name in the form MIB::name.
131 """
132 brains = self.getDmdRoot("Mibs").mibSearch({'id': name})
133 if len(brains) > 0: return brains[0].oid
134 return ""
135
136
138 """Count all mibs with in a MibOrganizer.
139 """
140 count = self.mibs.countObjects()
141 for group in self.children():
142 count += group.countClasses()
143 return count
144
145
147 """Create a MibModule
148 """
149 mibs = self.getDmdRoot(self.dmdRootName)
150 mod = None
151 if not mod:
152 modorg = mibs.createOrganizer(path)
153 mod = MibModule(name)
154 modorg.mibs._setObject(mod.id, mod)
155 mod = modorg.mibs._getOb(mod.id)
156 return mod
157
158
160 """Create a new service class in this Organizer.
161 """
162 mm = MibModule(id)
163 self.mibs._setObject(id, mm)
164 if REQUEST:
165 messaging.IMessageSender(self).sendToBrowser(
166 'Mib Module Created',
167 'Mib module %s was created.' % id
168 )
169 return self.callZenScreen(REQUEST)
170 else:
171 return self.mibs._getOb(id)
172
173
175 """Remove MibModules from an EventClass.
176 """
177 if not ids: return self()
178 if isinstance(ids, basestring): ids = (ids,)
179 for id in ids:
180 self.mibs._delObject(id)
181 if REQUEST:
182 messaging.IMessageSender(self).sendToBrowser(
183 'Mib Module Deleted',
184 'Mib modules deleted: %s' % ', '.join(ids)
185 )
186 return self()
187
188
190 """Move MibModules from this organizer to moveTarget.
191 """
192 if not moveTarget or not ids: return self()
193 if isinstance(ids, basestring): ids = (ids,)
194 target = self.getChildMoveTarget(moveTarget)
195 for id in ids:
196 rec = self.mibs._getOb(id)
197 rec._operation = 1 # moving object state
198 self.mibs._delObject(id)
199 target.mibs._setObject(id, rec)
200 if REQUEST:
201 messaging.IMessageSender(self).sendToBrowser(
202 'Mib Module Moved',
203 'Mib modules moved to %s.' % moveTarget
204 )
205 REQUEST['RESPONSE'].redirect(target.getPrimaryUrlPath())
206
207
209 """Go through all devices in this tree and reindex them."""
210 zcat = self._getOb(self.default_catalog)
211 zcat.manage_catalogClear()
212 for org in [self,] + self.getSubOrganizers():
213 for mib in org.mibs():
214 for thing in mib.nodes() + mib.notifications():
215 thing.index_object()
216
217
219 """Create a catalog for mibs searching"""
220 from Products.ZCatalog.ZCatalog import manage_addZCatalog
221
222 # XXX update to use ManagableIndex
223 manage_addZCatalog(self, self.default_catalog, self.default_catalog)
224 zcat = self._getOb(self.default_catalog)
225 cat = zcat._catalog
226 cat.addIndex('oid', makeCaseInsensitiveKeywordIndex('oid'))
227 cat.addIndex('id', makeCaseInsensitiveKeywordIndex('id'))
228 cat.addIndex('summary', makeCaseInsensitiveKeywordIndex('summary'))
229 zcat.addColumn('getPrimaryId')
230 zcat.addColumn('id')
231 zcat.addColumn('oid')
232
234 """
235 Assumes the file to be a mib so we need to create a mib module with
236 its contents
237 File will be available with REQUEST.upload
238 """
239 filename = REQUEST.upload.filename
240 mibs = REQUEST.upload.read()
241 # write it to a temp file
242 path = '/tmp/%s' % filename
243 with open(path, 'w') as f:
244 f.write(mibs)
245
246 # create the job
247 mypath = self.absolute_url_path().replace('/zport/dmd/Mibs', '')
248 if not mypath:
249 mypath = '/'
250 commandArgs = [binPath('zenmib'), 'run', path,
251 '--path=%s' % mypath]
252 jobStatus = self.dmd.JobManager.addJob(ShellCommandJob, cmd=commandArgs)
253
254 # send a user flare with the job id as a link
255 joblink = "<a href=\"%s\"> View Job Log </a>" % (jobStatus.absolute_url_path() + '/viewlog')
256 messaging.IMessageSender(self).sendToBrowser(
257 'Job Created',
258 'Successfully uploaded the MIB file, beginning processing now ' + joblink,
259 priority=messaging.INFO
260 )
261
262
263 InitializeClass(MibOrganizer)
264
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1.1812 on Thu Sep 1 19:03:51 2011 | http://epydoc.sourceforge.net |