This file is indexed.

/usr/share/pyshared/pyrrd/backend/external.py is in python-pyrrd 0.1.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import sys
from subprocess import Popen, PIPE

from pyrrd.backend import common
from pyrrd.exceptions import ExternalCommandError
from pyrrd.util import XML


def _cmd(command, args=""):
    if sys.platform == 'win32':
        close_fds = False
    else:
        close_fds = True
    command = "rrdtool %s %s" % (command, args)
    process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE,
                    close_fds=close_fds)
    (stdout, stderr) = process.communicate()
    if stderr:
        print command
        #import pdb;pdb.set_trace()
        raise ExternalCommandError(stderr.strip())
    if process.returncode != 0:
        errmsg = "Return code from '%s' was %s." % (
            command, process.returncode)
        raise ExternalCommandError(errmsg)
    return stdout


def concat(args):
    if isinstance(args, list):
        args = " ".join(args)
    return args


def create(filename, parameters):
    """
    >>> import tempfile
    >>> rrdfile = tempfile.NamedTemporaryFile()
    >>> parameters = ' --start 920804400'
    >>> parameters += ' DS:speed:COUNTER:600:U:U'
    >>> parameters += ' RRA:AVERAGE:0.5:1:24'
    >>> parameters += ' RRA:AVERAGE:0.5:6:10'
    >>> create(rrdfile.name, parameters)
    >>> import os
    >>> os.path.exists(rrdfile.name)
    True
    """
    parameters = "%s %s" % (filename, concat(parameters))
    output = _cmd('create', parameters)


def update(filename, data, debug=False):
    """
    >>> import tempfile
    >>> rrdfile = tempfile.NamedTemporaryFile()
    >>> parameters = ' --start 920804400'
    >>> parameters += ' DS:speed:COUNTER:600:U:U'
    >>> parameters += ' RRA:AVERAGE:0.5:1:24'
    >>> parameters += ' RRA:AVERAGE:0.5:6:10'
    >>> create(rrdfile.name, parameters)

    >>> import os
    >>> os.path.exists(rrdfile.name)
    True

    >>> update(rrdfile.name,
    ...   '920804700:12345 920805000:12357 920805300:12363')
    >>> update(rrdfile.name,
    ...   '920805600:12363 920805900:12363 920806200:12373')
    >>> update(rrdfile.name,
    ...   '920806500:12383 920806800:12393 920807100:12399')
    >>> update(rrdfile.name,
    ...   '920807400:12405 920807700:12411 920808000:12415')
    >>> update(rrdfile.name,
    ...   '920808300:12420 920808600:12422 920808900:12423')

    """
    parameters = "%s %s" % (filename, concat(data))
    if debug:
        _cmd('updatev', parameters)
    else:
        _cmd('update', parameters)


def fetchRaw(filename, query):
    parameters = "%s %s" % (filename, concat(query))
    return _cmd('fetch', parameters).strip()


def fetch(filename, query):
    """
    >>> import tempfile
    >>> rrdfile = tempfile.NamedTemporaryFile()
    >>> parameters = ' --start 920804400'
    >>> parameters += ' DS:speed:COUNTER:600:U:U'
    >>> parameters += ' RRA:AVERAGE:0.5:1:24'
    >>> parameters += ' RRA:AVERAGE:0.5:6:10'
    >>> create(rrdfile.name, parameters)

    >>> import os
    >>> os.path.exists(rrdfile.name)
    True

    >>> update(rrdfile.name, '920804700:12345 920805000:12357 920805300:12363')
    >>> update(rrdfile.name, '920805600:12363 920805900:12363 920806200:12373')
    >>> update(rrdfile.name, '920806500:12383 920806800:12393 920807100:12399')
    >>> update(rrdfile.name, '920807400:12405 920807700:12411 920808000:12415')
    >>> update(rrdfile.name, '920808300:12420 920808600:12422 920808900:12423')

    >>> results = fetch(rrdfile.name, 'AVERAGE --start 920804400 --end 920809200')

    # Results are provided in two ways, one of which is by the data source
    # name:
    >>> sorted(results["ds"].keys())
    ['speed']

    # accessing a DS entry like this gives of a (time, data) tuple:
    >>> results["ds"]["speed"][0]
    (920805000, 0.04)

    # The other way of accessing the results data is by data source time
    # entries:
    >>> keys = sorted(results["time"].keys())
    >>> len(keys)
    16
    >>> keys[0:6]
    [920805000, 920805300, 920805600, 920805900, 920806200, 920806500]
    >>> results["time"][920805000]
    {'speed': 0.04}

    The benefits of using an approach like this become obvious when the RRD
    file has multiple DSs and RRAs.
    """
    output = fetchRaw(filename, concat(query))
    lines = [line for line in output.split('\n') if line]
    dsNames = lines[0].split()
    results = {
        "ds": {},
        "time": {},
        }
    for line in lines[2:]:
        time, data = line.split(":")
        data = [common.coerce(datum) for datum in data.split()]
        results["time"][int(time)] = dict(zip(dsNames, data))
        for dsName, datum in zip(dsNames, data):
            results["ds"].setdefault(dsName, [])
            results["ds"][dsName].append((int(time), common.coerce(datum)))
    return results


def dump(filename, outfile="", parameters=""):
    """
    >>> import tempfile
    >>> rrdfile = tempfile.NamedTemporaryFile()
    >>> parameters = ' --start 920804400'
    >>> parameters += ' DS:speed:COUNTER:600:U:U'
    >>> parameters += ' RRA:AVERAGE:0.5:1:24'
    >>> parameters += ' RRA:AVERAGE:0.5:6:10'
    >>> create(rrdfile.name, parameters)

    >>> xml = dump(rrdfile.name)
    >>> xmlBytes = len(xml)
    >>> 3300 < xmlBytes < 4000
    True
    >>> xmlCommentCheck = '<!-- Round Robin Database Dump'
    >>> xmlCommentCheck in xml[0:200]
    True

    >>> xmlfile = tempfile.NamedTemporaryFile()
    >>> dump(rrdfile.name, xmlfile.name)

    >>> import os
    >>> os.path.exists(xmlfile.name)
    True
    """
    parameters = "%s %s %s" % (filename, outfile, concat(parameters))
    output = _cmd('dump', parameters)
    if not outfile:
        return output.strip()


def load(filename):
    """
    Load RRD data via the RRDtool XML dump into an ElementTree.

    >>> import tempfile
    >>> rrdfile = tempfile.NamedTemporaryFile()
    >>> parameters = ' --start 920804400'
    >>> parameters += ' DS:speed:COUNTER:600:U:U'
    >>> parameters += ' RRA:AVERAGE:0.5:1:24'
    >>> parameters += ' RRA:AVERAGE:0.5:6:10'
    >>> create(rrdfile.name, parameters)
    >>> tree = load(rrdfile.name)
    >>> [x.tag for x in tree]
    ['version', 'step', 'lastupdate', 'ds', 'rra', 'rra']
    """
    return XML(dump(filename))


def info(filename, obj, **kwargs):
    """
    """
    obj.printInfo()

def graph(filename, parameters):
    """
    >>> import tempfile
    >>> rrdfile = tempfile.NamedTemporaryFile()
    >>> graphfile = tempfile.NamedTemporaryFile()
    >>> parameters = ' --start 920804400'
    >>> parameters += ' DS:speed:COUNTER:600:U:U'
    >>> parameters += ' RRA:AVERAGE:0.5:1:24'
    >>> parameters += ' RRA:AVERAGE:0.5:6:10'
    >>> create(rrdfile.name, parameters)

    >>> import os
    >>> os.path.exists(rrdfile.name)
    True

    >>> update(rrdfile.name, '920804700:12345 920805000:12357 920805300:12363')
    >>> update(rrdfile.name, '920805600:12363 920805900:12363 920806200:12373')
    >>> update(rrdfile.name, '920806500:12383 920806800:12393 920807100:12399')
    >>> update(rrdfile.name, '920807400:12405 920807700:12411 920808000:12415')
    >>> update(rrdfile.name, '920808300:12420 920808600:12422 920808900:12423')

    >>> parameters = ' --start 920804400 --end 920808000'
    >>> parameters += ' --vertical-label km/h'
    >>> parameters += ' DEF:myspeed=%s:speed:AVERAGE' % rrdfile.name
    >>> parameters += ' CDEF:realspeed=myspeed,1000,*'
    >>> parameters += ' CDEF:kmh=myspeed,3600,*'
    >>> parameters += ' CDEF:fast=kmh,100,GT,kmh,0,IF'
    >>> parameters += ' CDEF:good=kmh,100,GT,0,kmh,IF'
    >>> parameters += ' HRULE:100#0000FF:"Maximum allowed"'
    >>> parameters += ' AREA:good#00FF00:"Good speed"'
    >>> parameters += ' AREA:fast#00FFFF:"Too fast"'
    >>> parameters += ' LINE2:realspeed#FF0000:Unadjusted'
    >>> graph(graphfile.name, parameters)
    >>> os.path.exists(graphfile.name)
    True
    """
    parameters = "%s %s" % (filename, concat(parameters))
    _cmd('graph', parameters)


def prepareObject(function, obj):
    """
    This is a funtion that serves to make interacting with the
    backend as transparent as possible. It's sole purpose it to
    prepare the attributes and data of the various pyrrd objects
    for use by the functions that call out to rrdtool.

    For all of the rrdtool-methods in this module, we need a filename
    and then parameters -- both as strings. That's it.

    This function will get called by methods in the pyrrd wrapper
    objects. For instance, most of the methods of pyrrd.rrd.RRD
    will call this function. In graph, Pretty much only the method
    pyrrd.graph.Graph.write() will call this function.
    """
    if function == 'create':
        validParams = ['start', 'step']
        params = common.buildParameters(obj, validParams)
        data = [unicode(x) for x in obj.ds]
        data += [unicode(x) for x in obj.rra]
        return (obj.filename, params + data)

    elif function == 'update':
        validParams = ['template']
        params = common.buildParameters(obj, validParams)
        FIRST_VALUE = 0
        DATA = 1
        TIME_OR_DATA = 0
        if obj.values[FIRST_VALUE][DATA]:
            data = ['%s:%s' % (time, values)
                    for time, values in obj.values]
        else:
            data = [data for data, nil in obj.values]
        return (obj.filename, params + data)

    elif function == 'fetch':
        validParams = ['resolution', 'start', 'end']
        params = common.buildParameters(obj, validParams)
        return (obj.filename, [obj.cf] + params)

    elif function == 'info':
        return (obj.filename, obj)

    elif function == 'graph':
        validParams = ['start', 'end', 'step', 'title',
            'vertical_label', 'width', 'height', 'only_graph',
            'upper_limit', 'lower_limit', 'rigid', 'alt_autoscale',
            'alt_autoscale_max', 'no_gridfit', 'x_grid', 'y_grid',
            'alt_y_grid', 'logarithmic', 'units_exponent', 'zoom',
            'font', 'font_render_mode', 'interlaced', 'no_legend',
            'force_rules_legend', 'tabwidth', 'base', 'color', 'imgformat',
            'slope_mode']
        params = common.buildParameters(obj, validParams)
        data = [unicode(x) for x in obj.data]
        return (obj.filename, params + data)


if __name__ == "__main__":
    import doctest
    doctest.testmod()