📄 presenter.py
字号:
# -*- coding: ISO8859-1 -*-# $Id: presenter.py 3315 2005-07-27 12:12:47Z mortenv $## Copyright 2003 Norwegian University of Science and Technology## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: Erlend Mjaavatten <mjaavatt@itea.ntnu.no>#"""Abstraction of the rrd_file and rrd_datasource fields in the main NAV database.Consists of three classes, presentation beeing the one of most interest to other developers.Quick example:>>> a = presenter.presentation() # instansiate presentation object>>> a.addDs(454) # Add rrd_datasourceid nr. 454 from rrd_datasource table, returns the default legend'brev.stud - imap responsetime'>>> a.timeLast('week') # We are interested in the data from a week ago and until today>>> a.average()[0.0064152292421062644] # imap responed with an average of 6milli.. hmm, whats the unit?>>> a.units()['s'] # Ah. seconds, 6 ms then.>>> a.addDs(427) # Add another datasource'brev.stud - pop3 responsetime'>>> a.average() # It still works[1.0, 0.0028113913067105887]>>> a.title = 'My Graph' # You can set the title to what you want>>> a.graphUrl()'http://isbre.itea.ntnu.no/rrd/rrdBrowser/graph?id=348552316' # Returns a link to an image representing the two datasources. This link is valid for about ten minutes"""configfile = 'rrdBrowser.conf'import nav.dbimport nav.configfrom nav.web import urlbuilderimport timeimport rrdtoolimport randomimport globimport osimport warningsimport operatorfrom mx import DateTimefrom os import pathunitmap = {'s' : 'Seconds', '%' : 'Percent', '100%': 'Percent', }class rrd_file: """Class representing an rrd-file""" def __init__(self,rrd_fileid): cursor = nav.db.getConnection('rrdpresenter').cursor() cursor.execute("select * from rrd_file natural join netbox where rrd_fileid=%s"% rrd_fileid) result = cursor.dictfetchone() self.path = result['path'] self.filename = result['filename'] self.netboxid = result['netboxid'] self.key = result['key'] self.value = result['value'] self.subsystem= result['subsystem'] self.sysname = result['sysname'] def fullPath(self): rrd_file_path = path.join(self.path,self.filename) return rrd_file_pathclass datasource: """ Class representing a datasource. Can perform simple calculations on the datasource""" def __init__(self,rrd_datasourceid,linetype='LINE2'): cursor = nav.db.getConnection('rrdpresenter').cursor() cursor.execute("select * from rrd_datasource where rrd_datasourceid=%s"% rrd_datasourceid) result = cursor.dictfetchone() self.name = result['name'] self.descr = result['descr'] self.dstype = result['dstype'] self.units = result['units'] self.rrd_datasourceid = result['rrd_datasourceid'] self.linetype = linetype self.rrd_fileobj = rrd_file(result['rrd_fileid']) self.sysname = self.rrd_fileobj.sysname self.legend = '%s - %s' % (self.rrd_fileobj.sysname,self.descr) cursor.close() def getId(self): return self.rrd_datasourceid def __eq__(self,obj): return obj.rrd_datasourceid == self.rrd_datasourceid def __str__(self): return "%s - %s" % (self.name, self.descr) def __repr__(self): return "%s - %s" % (self.name, self.descr) def fullPath(self): return self.rrd_fileobj.fullPath() class presentation: def __init__(self, tf='day', ds=''): self.datasources = [] self.none = None self.graphHeight = 150 self.graphWidth = 500 self.title = '' self.timeLast(tf) self.timeframe = tf self.showmax = 0 self.yaxis = 0 if ds != '': self.addDs(ds) def serialize(self): repr = {} repr['datasources'] = [] for ds in self.datasources: repr['datasources'].append(ds.getId()) repr['timeframe'] = self.timeframe return repr def _updateTitle(): for i in self.datasources: blapp def units(self): """Returns the units of the rrd_datasources contained in the presentation object""" units = [] for i in self.datasources: units.append(i.units) return units def addDs(self,ds_id): """Adds a datasource to the presentation, returns the default legend""" ds = datasource(ds_id) self.datasources.append(ds) return ds.legend def __str__(self): return str(self.datasources) def __repr__(self): return str(self.datasources) def fetchValid(self): """Return the raw rrd-data as a list of dictionaries {'start':starttime in unixtime, 'stop':stoptime in unixtime, 'data':[data,as,list]}""" returnList = [] for datasource in self.datasources: try: raw = rrdtool.fetch(datasource.fullPath(), 'AVERAGE','-s '+self.fromTime, '-e '+self.toTime) returnDict = {} returnDict['start'] = raw[0][0] returnDict['stop'] = raw[0][1] returnDict['deltaT'] = raw[0][2] row = list(raw [1]).index(datasource.name) invalid = 0 data = [] for i in raw[2]: if type(i[row]) == type(None): # data.append(self.none) invalid += 1 else: data.append(i[row]) returnDict['data'] = data returnDict['invalid'] = invalid except rrdtool.error: returnDict = {} returnDict['start'] = '' returnDict['stop'] = '' returnDict['deltaT'] = '' returnDict['data'] = '' returnDict['invalid'] = '' returnList.append(returnDict) return returnList def sum(self): """Returns the sum of the valid rrd-data""" sumList = [] dataList = self.fetchValid() for data in dataList: sum = 0 for i in data['data']: sum += i sumList.append(sum) return sumList def average(self): """Returns the average of the valid rrd-data""" averages = [] dataList = self.fetchValid() for data in dataList: sum = 0 if not data['data']: average=0 else: sum = reduce(operator.add, data['data']) average = sum / len(data['data']) averages.append(average) return averages def max(self): """Returns the local maxima of the valid rrd-data""" maxList = [] for presentation in self.fetchValid(): maxList.append(max(presentation['data'])) return maxList def min(self): """Returns the local minima of the valid rrd-data""" minList = [] for presentation in self.fetchValid(): minList.append(min(presentation['data'])) return minList def validPoints(self): """Returns list of [number of points,number of invalid points, invalid/number of points]""" valid = [] a = self.fetchValid() for i in a: ret = [len(i['data'])] ret.append(i['data'].count(self.none)) ret.append(ret[1]/float(ret[0])) valid.append(ret) return valid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -