1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""GraphDefinition
15
16 GraphDefinition defines the global options for a graph.
17 """
18
19 import sys
20 from sets import Set
21 import string
22
23 from Products.ZenRelations.RelSchema import *
24 from Products.ZenModel.ZenossSecurity import ZEN_MANAGE_DMD
25 from Globals import InitializeClass
26 from AccessControl import ClassSecurityInfo, Permissions
27 from ZenModelRM import ZenModelRM
28 from Products.ZenWidgets import messaging
29 from ZenPackable import ZenPackable
30 import logging
31 log = logging.getLogger("zen.Device")
32 from Acquisition import aq_base
33
34
46
48 isFake = True
49 - def __init__(self, name):
51 - def __getattr__(self, name):
53 - def __call__(self, *unused, **ignored):
55 - def __getitem__(self, key):
63 - def __nonzero__(self):
69
71 '''
72 '''
73
74 meta_type = 'GraphDefinition'
75
76 height = 100
77 width = 500
78 units = ""
79 log = False
80 base = False
81
82 miny = -1
83 maxy = -1
84 custom = ""
85 hasSummary = True
86 sequence = 0
87
88 _properties = (
89 {'id':'height', 'type':'int', 'mode':'w'},
90 {'id':'width', 'type':'int', 'mode':'w'},
91 {'id':'units', 'type':'string', 'mode':'w'},
92 {'id':'log', 'type':'boolean', 'mode':'w'},
93 {'id':'base', 'type':'boolean', 'mode':'w'},
94
95 {'id':'miny', 'type':'int', 'mode':'w'},
96 {'id':'maxy', 'type':'int', 'mode':'w'},
97 {'id':'custom', 'type':'text', 'mode':'w'},
98 {'id':'hasSummary', 'type':'boolean', 'mode':'w'},
99 {'id':'sequence', 'type':'long', 'mode':'w'},
100 )
101
102 _relations = (
103 ("rrdTemplate",
104 ToOne(ToManyCont,"Products.ZenModel.RRDTemplate", "graphDefs")),
105 ('report',
106 ToOne(ToManyCont, 'Products.ZenModel.MultiGraphReport', 'graphDefs')),
107 ('graphPoints',
108 ToManyCont(ToOne, 'Products.ZenModel.GraphPoint', 'graphDef')),
109
110 ('reportClass',
111 ToOne(ToManyCont, 'Products.ZenModel.MultiGraphReportClass', 'graphDefs')),
112 )
113
114
115
116 factory_type_information = (
117 {
118 'immediate_view' : 'editGraphDefinition',
119 'actions' :
120 (
121 { 'id' : 'edit'
122 , 'name' : 'Graph Definition'
123 , 'action' : 'editGraphDefinition'
124 , 'permissions' : ( Permissions.view, )
125 },
126 { 'id' : 'editCustom'
127 , 'name' : 'Graph Custom Definition'
128 , 'action' : 'editCustGraphDefinition'
129 , 'permissions' : ( Permissions.view, )
130 },
131 { 'id' : 'viewCommands'
132 , 'name' : 'Graph Commands'
133 , 'action' : 'viewGraphCommands'
134 , 'permissions' : ( Permissions.view, )
135 },
136 )
137 },
138 )
139
140 security = ClassSecurityInfo()
141
142
143
145 ''' Return ordered list of graph points
146 '''
147 def cmpGraphPoints(a, b):
148 try:
149 a = int(a.sequence)
150 except ValueError:
151 a = sys.maxint
152 try:
153 b = int(b.sequence)
154 except ValueError:
155 b = sys.maxint
156 return cmp(a, b)
157 gps = [gp for gp in self.graphPoints()
158 if includeThresholds or not gp.isThreshold]
159 gps.sort(cmpGraphPoints)
160 return gps
161
162
164 ''' Get ordered list of threshold graph points
165 '''
166 gps = [gp for gp in self.getGraphPoints() if gp.isThreshold]
167 return gps
168
169
171 ''' Return true if there is a thresholdgraphpoint with threshId=threshid
172 '''
173 for gp in self.getThresholdGraphPoints():
174 if gp.threshId == threshId:
175 return True
176 return False
177
178
189
190
191
192
193
203
204
206 """Checks a valid id
207 """
208
209
210
211
212 if len(id) > 200:
213 return 'GraphPoint names can not be longer than 200 characters.'
214 allowed = Set(list(string.ascii_letters)
215 + list(string.digits)
216 + ['_'])
217 attempted = Set(list(id))
218 if not attempted.issubset(allowed):
219 return 'Only letters, digits and underscores are allowed' + \
220 ' in GraphPoint names.'
221 return ZenModelRM.checkValidId(self, id, prep_id)
222
223
226
227
229 ''' Return list of graph point ids
230 '''
231 return [gp.id for gp in self.getGraphPoints()]
232
233
235 """
236 Return a string that lists the names of the graphpoints used in this
237 graph definition. If this graph definition has a perf template then
238 note in the string which graphpoints are broken (in that they refer
239 to nonexistent datapoints.)
240 """
241 names = []
242 for gp in self.getGraphPoints():
243 if hasattr(aq_base(gp), 'isBroken') and gp.isBroken():
244 names.append('%s(<span style="color: red">missing</span>)' %
245 gp.id)
246 else:
247 names.append(gp.id)
248 return ', '.join(names)
249
250
252 ''' Used by dialog_addGraphPoint to construct the list of
253 available graphpoint types.
254 '''
255 return (('DefGraphPoint', 'DEF'),
256 ('VdefGraphPoint', 'VDEF'),
257 ('CdefGraphPoint', 'CDEF'),
258 ('PrintGraphPoint', 'PRINT'),
259 ('GprintGraphPoint', 'GPRINT'),
260 ('CommentGraphPoint', 'COMMENT'),
261 ('VruleGraphPoint', 'VRULE'),
262 ('HruleGraphPoint', 'HRULE'),
263 ('LineGraphPoint', 'LINE'),
264 ('AreaGraphPoint', 'AREA'),
265 ('TickGraphPoint', 'TICK'),
266 ('ShiftGraphPoint', 'SHIFT'))
267
268
270 ''' Create the graphpoint with the given id or something similar
271 and add to self.graphPoints
272 '''
273 def getUniqueId(container, base):
274 ids = container.objectIds()
275 new = base
276 i = 2
277 while new in ids:
278 new = '%s%s' % (base, i)
279 i += 1
280 return new
281 newId = getUniqueId(self.graphPoints, newId)
282 gp = cls(newId)
283
284 if gp.isThreshold:
285 gp.sequence = -1
286 else:
287 gp.sequence = len(self.graphPoints())
288
289 if self.report() and hasattr(gp, 'legend'):
290
291
292
293 gp.legend = gp.DEFAULT_MULTIGRAPH_LEGEND
294 self.graphPoints._setObject(gp.id, gp)
295 gp = self.graphPoints._getOb(gp.id)
296 if gp.sequence == -1:
297 self.manage_resequenceGraphPoints()
298 return gp
299
300
301 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addCustomGraphPoint')
313
314
315 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addDataPointGraphPoints')
341
342
358
359
360 security.declareProtected(ZEN_MANAGE_DMD, 'manage_addThresholdGraphPoints')
379
380
381 security.declareProtected(ZEN_MANAGE_DMD, 'manage_deleteGraphPoints')
398
399
400 security.declareProtected(ZEN_MANAGE_DMD, 'manage_resequenceGraphPoints')
407
408
410 ''' Return a list of (value, name) tuples for the list of datapoints
411 which the user selects from to create new graphpoints.
412 '''
413 return [(dp.name(), dp.name())
414 for dp in self.rrdTemplate.getRRDDataPoints()]
415
416
418 ''' Return a list of (value, name) tuples for the list of thresholds
419 which the user selects from to create new graphpoints.
420 '''
421 return [(t.id, t.id) for t in self.rrdTemplate.thresholds()]
422
423
424
425
426
427 - def getGraphCmds(self, context, rrdDir, multiid=-1, upToPoint=None,
428 includeSetup=True, includeThresholds=True,
429 prefix='', cmds=None, idxOffset=0):
430 """build the graph opts for a single rrdfile"""
431 from Products.ZenUtils.ZenTales import talesEval
432 if not cmds:
433 cmds = []
434 if includeSetup:
435 cmds += self.graphsetup()
436
437
438
439 if includeThresholds:
440 threshGps = [gp for gp in self.getThresholdGraphPoints()
441 if upToPoint is None or gp.sequence < upToPoint]
442 if threshGps:
443 for index, gp in enumerate(threshGps):
444 try:
445 cmds = gp.getGraphCmds(cmds, context, rrdDir,
446 self.hasSummary, index+idxOffset,
447 multiid, prefix)
448 except (KeyError, NameError), e:
449 cmds.append('COMMENT: UNKOWN VALUE IN '
450 'GRAPHPOINT %s\: %s' % (gp.id, str(e)))
451 gpList = [gp for gp in self.getGraphPoints(includeThresholds=False)
452 if upToPoint is None or gp.sequence < upToPoint]
453 for index, gp in enumerate(gpList):
454 try:
455 cmds = gp.getGraphCmds(cmds, context, rrdDir,
456 self.hasSummary, index+idxOffset,
457 multiid, prefix)
458 except (KeyError, NameError), e:
459 cmds.append('COMMENT: UNKNOWN VALUE IN GRAPHPOINT '
460 '%s\: %s' % (gp.id, str(e)))
461 if self.custom and includeSetup \
462 and not upToPoint:
463 try:
464 res = talesEval("string:"+str(self.custom), context)
465 except (KeyError, NameError), e:
466 res = 'COMMENT:UNKNOWN VALUE IN CUSTOM COMMANDS\: %s' % str(e)
467 res = [l for l in res.split('\n') if l.strip()]
468 cmds.extend(res)
469
470
471
472 return cmds
473
474
476 ''' Return list of rrd variable names that are defined by DEF, CDEF
477 or VDEF statements in the rrd commands. If upToPoint is not None then
478 only consider statements generated by graphoints where
479 sequence < upToPoint
480 '''
481 cmds = self.getFakeGraphCmds(upToPoint=upToPoint)
482 names = [line[line.find(':')+1:line.find('=')]
483 for line in cmds.split('\n')
484 if line[:line.find(':')] in ('DEF', 'CDEF', 'VDEF')]
485 return names
486
487
496
497
499 """Setup global graph parameters.
500 """
501 gopts = ['-F', '-E']
502 if self.height:
503 gopts.append('--height=%d' % self.height)
504 if self.width:
505 gopts.append('--width=%d' % self.width)
506 if self.log:
507 gopts.append('--logarithmic')
508 if self.maxy > -1:
509 gopts.append('--upper-limit=%d' % self.maxy)
510 gopts.append('--rigid')
511 if self.miny > -1:
512 gopts.append('--lower-limit=%d' % self.miny)
513 gopts.append('--rigid')
514
515
516 gopts.append('--vertical-label=%s' % (self.units or ' '))
517 if self.units == 'percentage':
518 if not self.maxy > -1:
519 gopts.append('--upper-limit=100')
520 if not self.miny > -1:
521 gopts.append('--lower-limit=0')
522 if self.base:
523 gopts.append('--base=1024')
524 gopts = [str(o) for o in gopts]
525 return gopts
526
527
535
536
538 '''
539 Get a list of all unique datapoint names
540 '''
541 from sets import Set
542 dpNames = Set()
543 for t in self.dmd.Devices.getAllRRDTemplates():
544 for ds in t.datasources():
545
546
547 if hasattr(ds, 'datapoints'):
548 for dp in ds.datapoints():
549 dpNames.add(dp.name())
550 if limit and len(dpNames) >= limit:
551 break
552 dpNames = list(dpNames)
553 dpNames.sort()
554 return dpNames
555
556
558 '''
559 Get a list of all unique threshold names
560 '''
561 from sets import Set
562 names = Set()
563 for t in self.dmd.Devices.getAllRRDTemplates():
564 for thresh in t.thresholds():
565 names.add(thresh.id)
566 if len(names) >= limit:
567 break
568 if len(names) >= limit:
569 break
570 names = list(names)
571 names.sort()
572 return names
573
574
575 InitializeClass(GraphDefinition)
576