]>
git.ipfire.org Git - people/ms/python-rrdtool.git/blob - RRDtool.py
2 # -*- coding: utf-8 -*-
4 # rrdtool-py3k, rrdtool bindings for Python 3.
5 # Based on the rrdtool Python bindings for Python 2 from
6 # Hye-Shik Chang <perky@fallin.lv>.
8 # Copyright 2012 Christian Jurk <commx@commx.ws>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU Lesser General Public License as
12 # published by the Free Software Foundation; either version 3 of the
13 # License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 from datetime
import datetime
28 from io
import BytesIO
31 from time
import mktime
33 def create(filename
, *args
):
34 "Create a Round Robin Database and return a RRD object on success."
35 rrdtool
.create(filename
, *args
)
37 if not os
.access(filename
, os
.F_OK
):
38 raise rrdtool
.OperationalError('RRD file was not created')
43 """An object-based interface to the rrdtool module."""
45 def __init__(self
, filename
, check_type
=True):
46 "Initialize the class instance with a filename."
48 if not os
.access(filename
, os
.F_OK | os
.R_OK
):
49 raise rrdtool
.OperationalError('RRD {!s} cannot be opened.' \
52 # Use rrdinfo to test whether the file is a valid RRD file
53 if check_type
is True:
54 rrdtool
.info(filename
)
56 self
.readonly
= not os
.access(filename
, os
.W_OK
)
57 self
.filename
= filename
59 def graph(self
, output_file
, *args
):
60 "Create a graph based on one or more RRDs."
64 # write straigt into file using wrapper functions
65 if isinstance(output_file
, str):
69 gdata
= rrdtool
.graph(outfile
, *args
)
71 if isinstance(gdata
, tuple) and len(gdata
) >= 4:
72 if output_file
is None:
74 elif isinstance(output_file
, BytesIO
):
75 output_file
.write(gdata
[3])
81 return rrdtool
.info(self
.filename
)
83 def update(self
, values
, *args
):
87 raise rrdtool
.OperationalError('RRD file is read-only: {!s}' \
88 .format(self
.filename
))
89 elif not isinstance(values
, (list, tuple)):
90 raise rrdtool
.ProgrammingError('The values parameter must be a ' \
94 if isinstance(row
, str):
96 elif isinstance(row
, (list, tuple)):
98 raise rrdtool
.ProgrammingError('Value {!r} has too ' \
99 'few elements in sequence object'.format(row
))
104 elif isinstance(ts
, datetime
):
105 ts
= int(mktime(ts
.timetuple()))
106 elif isinstance(ts
, str):
108 elif not isinstance(ts
, int):
109 raise ValueError('Unsupported type')
111 v
= '{}:{}'.format(ts
, ':'.join([str(x
) for x
in row
[1:]]))
114 arglist
= tuple(vl
+ list(args
))
115 return rrdtool
.update(self
.filename
, *arglist
)
118 return '<RRD {!r}>'.format(self
.filename
)