1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""BasicDataSource
15
16 Defines attributes for how a datasource will be graphed
17 and builds the nessesary DEF and CDEF statements for it.
18 """
19
20 from Products.ZenModel import RRDDataSource
21 from AccessControl import ClassSecurityInfo, Permissions
22 from Globals import InitializeClass
23 from Products.ZenEvents.ZenEventClasses import Cmd_Fail
24 from Products.ZenUtils.Utils import executeStreamCommand
25 from Products.ZenWidgets import messaging
26 from copy import copy
27 import cgi, time
28
29 snmptemplate = ("snmpwalk -c%(zSnmpCommunity)s "
30 "-%(zSnmpVer)s %(manageIp)s %(oid)s")
31
33 import string
34 for c in string.whitespace:
35 oid = oid.replace(c, '')
36 oid = oid.strip('.')
37 numbers = oid.split('.')
38 map(int, numbers)
39 if len(numbers) < 3:
40 raise ValueError("OID too short")
41 return oid
42
43
45
46 __pychecker__='no-override'
47
48 sourcetypes = ('SNMP', 'COMMAND')
49
50 sourcetype = 'SNMP'
51 eventClass = Cmd_Fail
52 oid = ''
53 parser = "Auto"
54
55 usessh = False
56
57 _properties = RRDDataSource.RRDDataSource._properties + (
58 {'id':'oid', 'type':'string', 'mode':'w'},
59 {'id':'usessh', 'type':'boolean', 'mode':'w'},
60 {'id':'parser', 'type':'string', 'mode':'w'},
61 )
62
63 _relations = RRDDataSource.RRDDataSource._relations + (
64 )
65
66
67 factory_type_information = (
68 {
69 'immediate_view' : 'editBasicDataSource',
70 'actions' :
71 (
72 { 'id' : 'edit'
73 , 'name' : 'Data Source'
74 , 'action' : 'editBasicDataSource'
75 , 'permissions' : ( Permissions.view, )
76 },
77 )
78 },
79 )
80
81 security = ClassSecurityInfo()
82
90
100
101
103 if self.sourcetype == 'COMMAND':
104 return True
105 return False
106
107
125
126 security.declareProtected('Change Device', 'manage_testDataSource')
128 ''' Test the datasource by executing the command and outputting the
129 non-quiet results.
130 '''
131 out = REQUEST.RESPONSE
132
133 def write(lines):
134 ''' Output (maybe partial) result text.
135 '''
136
137
138 startLine = '<tr><td class="tablevalues">'
139 endLine = '</td></tr>\n'
140 if out:
141 if not isinstance(lines, list):
142 lines = [lines]
143 for l in lines:
144 if not isinstance(l, str):
145 l = str(l)
146 l = l.strip()
147 l = cgi.escape(l)
148 l = l.replace('\n', endLine + startLine)
149 out.write(startLine + l + endLine)
150
151
152 device = None
153 if testDevice:
154
155 device = self.findDevice(testDevice)
156 if not device:
157 messaging.IMessageSender(self).sendToBrowser(
158 'No device found',
159 'Cannot find device matching %s.' % testDevice,
160 priority=messaging.WARNING
161 )
162 return self.callZenScreen(REQUEST)
163 elif hasattr(self, 'device'):
164
165 device = self.device()
166 elif hasattr(self, 'getSubDevicesGen'):
167
168 try:
169 device = self.getSubDevicesGen().next()
170 except StopIteration:
171
172 pass
173 if not device:
174 messaging.IMessageSender(self).sendToBrowser(
175 'No Testable Device',
176 'Cannot determine a device against which to test.',
177 priority=messaging.WARNING
178 )
179 return self.callZenScreen(REQUEST)
180
181
182 command = None
183 if self.sourcetype=='COMMAND':
184 command = self.getCommand(device)
185 elif self.sourcetype=='SNMP':
186 snmpinfo = copy(device.getSnmpConnInfo().__dict__)
187 snmpinfo['oid'] = self.getDescription()
188 command = snmptemplate % snmpinfo
189 else:
190 messaging.IMessageSender(self).sendToBrowser(
191 'Test Failed',
192 'Unable to test %s datasources' % self.sourcetype,
193 priority=messaging.WARNING
194 )
195 return self.callZenScreen(REQUEST)
196 if not command:
197 messaging.IMessageSender(self).sendToBrowser(
198 'Test Failed',
199 'Unable to create test command.',
200 priority=messaging.WARNING
201 )
202 return self.callZenScreen(REQUEST)
203
204
205 header, footer = self.commandTestOutput().split('OUTPUT_TOKEN')
206 out.write(str(header))
207
208 write("Executing command\n%s\n against %s" % (command, device.id))
209 write('')
210 start = time.time()
211 try:
212 executeStreamCommand(command, write)
213 except:
214 import sys
215 write('exception while executing command')
216 write('type: %s value: %s' % tuple(sys.exc_info()[:2]))
217 write('')
218 write('')
219 write('DONE in %s seconds' % long(time.time() - start))
220 out.write(str(footer))
221
225
226
227
228 InitializeClass(BasicDataSource)
229