4 * RRDTool Python binding
6 * Author : Hye-Shik Chang <perky@fallin.lv>
7 * Date : $Date: 2003/02/22 07:41:19 $
8 * Created : 23 May 2002
12 * ==========================================================================
13 * This file is part of py-rrdtool.
15 * py-rrdtool is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published
17 * by the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * py-rrdtool is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with Foobar; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #elif defined(__GNUC__)
33 # define UNUSED(x) x __attribute__((unused))
34 #elif defined(__LCLINT__)
35 # define UNUSED(x) /*@unused@*/ x
40 static const char *__version__ = "$Revision: 1.14 $";
44 #include "rrd_extra.h"
46 static PyObject *ErrorObject;
50 /* forward declaration to keep compiler happy */
51 /*void initrrdtool(void);*/
52 void initrrdtoolmodule(void);
55 create_args(char *command, PyObject *args, int *argc, char ***argv)
60 size = PyTuple_Size(args);
61 *argv = PyMem_New(char *, size + 1);
65 for (i = 0; i < size; i++) {
66 o = PyTuple_GET_ITEM(args, i);
67 if (PyString_Check(o))
68 (*argv)[i + 1] = PyString_AS_STRING(o);
71 PyErr_Format(PyExc_TypeError, "argument %d must be string", i);
78 /* reset getopt state */
85 destroy_args(char ***argv)
91 static char PyRRD_create__doc__[] =
92 "create(args..): Set up a new Round Robin Database\n\
93 create filename [--start|-b start time] \
94 [--step|-s step] [DS:ds-name:DST:heartbeat:min:max] \
95 [RRA:CF:xff:steps:rows]";
98 PyRRD_create(PyObject UNUSED(*self), PyObject *args)
104 if (create_args("create", args, &argc, &argv) < 0)
107 if (rrd_create(argc, argv) == -1) {
108 PyErr_SetString(ErrorObject, rrd_get_error());
120 static char PyRRD_update__doc__[] =
121 "update(args..): Store a new set of values into the rrd\n"
122 " update filename [--template|-t ds-name[:ds-name]...] "
123 "N|timestamp:value[:value...] [timestamp:value[:value...] ...]";
126 PyRRD_update(PyObject UNUSED(*self), PyObject *args)
132 if (create_args("update", args, &argc, &argv) < 0)
135 if (rrd_update(argc, argv) == -1) {
136 PyErr_SetString(ErrorObject, rrd_get_error());
148 static char PyRRD_fetch__doc__[] =
149 "fetch(args..): fetch data from an rrd.\n"
150 " fetch filename CF [--resolution|-r resolution] "
151 "[--start|-s start] [--end|-e end]";
154 PyRRD_fetch(PyObject UNUSED(*self), PyObject *args)
157 rrd_value_t *data, *datai;
158 unsigned long step, ds_cnt;
161 char **argv, **ds_namv;
163 if (create_args("fetch", args, &argc, &argv) < 0)
166 if (rrd_fetch(argc, argv, &start, &end, &step,
167 &ds_cnt, &ds_namv, &data) == -1) {
168 PyErr_SetString(ErrorObject, rrd_get_error());
173 ((start, end, step), (name1, name2, ...), [(data1, data2, ..), ...]) */
174 PyObject *range_tup, *dsnam_tup, *data_list, *t;
175 unsigned long i, j, row;
178 row = ((end - start) / step + 1);
181 range_tup = PyTuple_New(3);
182 dsnam_tup = PyTuple_New(ds_cnt);
183 data_list = PyList_New(row);
184 PyTuple_SET_ITEM(r, 0, range_tup);
185 PyTuple_SET_ITEM(r, 1, dsnam_tup);
186 PyTuple_SET_ITEM(r, 2, data_list);
190 PyTuple_SET_ITEM(range_tup, 0, PyInt_FromLong((long)start));
191 PyTuple_SET_ITEM(range_tup, 1, PyInt_FromLong((long)end));
192 PyTuple_SET_ITEM(range_tup, 2, PyInt_FromLong((long)step));
194 for (i = 0; i < ds_cnt; i++)
195 PyTuple_SET_ITEM(dsnam_tup, i, PyString_FromString(ds_namv[i]));
197 for (i = 0; i < row; i ++) {
198 t = PyTuple_New(ds_cnt);
199 PyList_SET_ITEM(data_list, i, t);
201 for (j = 0; j < ds_cnt; j++) {
204 PyTuple_SET_ITEM(t, j, Py_None);
207 PyTuple_SET_ITEM(t, j, PyFloat_FromDouble((double)dv));
212 for (i = 0; i < ds_cnt; i++)
214 free(ds_namv); /* rrdtool don't use PyMem_Malloc :) */
222 static char PyRRD_graph__doc__[] =
223 "graph(args..): Create a graph based on data from one or several RRD\n"
224 " graph filename [-s|--start seconds] "
225 "[-e|--end seconds] [-x|--x-grid x-axis grid and label] "
226 "[-y|--y-grid y-axis grid and label] [--alt-y-grid] [--alt-y-mrtg] "
227 "[--alt-autoscale] [--alt-autoscale-max] [--units-exponent] value "
228 "[-v|--vertical-label text] [-w|--width pixels] [-h|--height pixels] "
230 "[-f|--imginfo formatstring] [-a|--imgformat GIF|PNG|GD] "
231 "[-B|--background value] [-O|--overlay value] "
232 "[-U|--unit value] [-z|--lazy] [-o|--logarithmic] "
233 "[-u|--upper-limit value] [-l|--lower-limit value] "
234 "[-g|--no-legend] [-r|--rigid] [--step value] "
235 "[-b|--base value] [-c|--color COLORTAG#rrggbb] "
236 "[-t|--title title] [DEF:vname=rrd:ds-name:CF] "
237 "[CDEF:vname=rpn-expression] [PRINT:vname:CF:format] "
238 "[GPRINT:vname:CF:format] [COMMENT:text] "
239 "[HRULE:value#rrggbb[:legend]] [VRULE:time#rrggbb[:legend]] "
240 "[LINE{1|2|3}:vname[#rrggbb[:legend]]] "
241 "[AREA:vname[#rrggbb[:legend]]] "
242 "[STACK:vname[#rrggbb[:legend]]]";
245 PyRRD_graph(PyObject UNUSED(*self), PyObject *args)
248 char **argv, **calcpr;
249 int argc, xsize, ysize, i;
251 if (create_args("graph", args, &argc, &argv) < 0)
254 if (rrd_graph(argc, argv, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax) == -1) {
255 PyErr_SetString(ErrorObject, rrd_get_error());
261 PyTuple_SET_ITEM(r, 0, PyInt_FromLong((long)xsize));
262 PyTuple_SET_ITEM(r, 1, PyInt_FromLong((long)ysize));
268 PyTuple_SET_ITEM(r, 2, e);
270 for(i = 0; calcpr[i]; i++) {
271 t = PyString_FromString(calcpr[i]);
279 PyTuple_SET_ITEM(r, 2, Py_None);
287 static char PyRRD_tune__doc__[] =
288 "tune(args...): Modify some basic properties of a Round Robin Database\n"
289 " tune filename [--heartbeat|-h ds-name:heartbeat] "
290 "[--minimum|-i ds-name:min] [--maximum|-a ds-name:max] "
291 "[--data-source-type|-d ds-name:DST] [--data-source-rename|-r old-name:new-name]";
294 PyRRD_tune(PyObject UNUSED(*self), PyObject *args)
300 if (create_args("tune", args, &argc, &argv) < 0)
303 if (rrd_tune(argc, argv) == -1) {
304 PyErr_SetString(ErrorObject, rrd_get_error());
316 static char PyRRD_last__doc__[] =
317 "last(filename): Return the timestamp of the last data sample in an RRD";
320 PyRRD_last(PyObject UNUSED(*self), PyObject *args)
326 if (create_args("last", args, &argc, &argv) < 0)
329 if ((ts = rrd_last(argc, argv)) == -1) {
330 PyErr_SetString(ErrorObject, rrd_get_error());
334 r = PyInt_FromLong((long)ts);
340 static char PyRRD_resize__doc__[] =
341 "resize(args...): alters the size of an RRA.\n"
342 " resize filename rra-num GROW|SHRINK rows";
345 PyRRD_resize(PyObject UNUSED(*self), PyObject *args)
351 if (create_args("resize", args, &argc, &argv) < 0)
354 if ((ts = rrd_resize(argc, argv)) == -1) {
355 PyErr_SetString(ErrorObject, rrd_get_error());
367 static char PyRRD_info__doc__[] =
368 "info(filename): extract header information from an rrd";
371 PyRRD_info(PyObject UNUSED(*self), PyObject *args)
373 PyObject *r, *t, *ds;
379 if (! PyArg_ParseTuple(args, "s:info", &filename))
382 if (rrd_open(filename, &in_file, &rrd, RRD_READONLY) == -1) {
383 PyErr_SetString(ErrorObject, rrd_get_error());
389 #define DICTSET_STR(dict, name, value) \
390 t = PyString_FromString(value); \
391 PyDict_SetItemString(dict, name, t); \
394 #define DICTSET_CNT(dict, name, value) \
395 t = PyInt_FromLong((long)value); \
396 PyDict_SetItemString(dict, name, t); \
399 #define DICTSET_VAL(dict, name, value) \
400 t = isnan(value) ? (Py_INCREF(Py_None), Py_None) : \
401 PyFloat_FromDouble((double)value); \
402 PyDict_SetItemString(dict, name, t); \
407 DICTSET_STR(r, "filename", filename);
408 DICTSET_STR(r, "rrd_version", rrd.stat_head->version);
409 DICTSET_CNT(r, "step", rrd.stat_head->pdp_step);
410 DICTSET_CNT(r, "last_update", rrd.live_head->last_up);
413 PyDict_SetItemString(r, "ds", ds);
416 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
420 PyDict_SetItemString(ds, rrd.ds_def[i].ds_nam, d);
423 DICTSET_STR(d, "ds_name", rrd.ds_def[i].ds_nam);
424 DICTSET_STR(d, "type", rrd.ds_def[i].dst);
425 DICTSET_CNT(d, "minimal_heartbeat", rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
426 DICTSET_VAL(d, "min", rrd.ds_def[i].par[DS_min_val].u_val);
427 DICTSET_VAL(d, "max", rrd.ds_def[i].par[DS_max_val].u_val);
428 DICTSET_STR(d, "last_ds", rrd.pdp_prep[i].last_ds);
429 DICTSET_VAL(d, "value", rrd.pdp_prep[i].scratch[PDP_val].u_val);
430 DICTSET_CNT(d, "unknown_sec", rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
433 ds = PyList_New(rrd.stat_head->rra_cnt);
434 PyDict_SetItemString(r, "rra", ds);
437 for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
441 PyList_SET_ITEM(ds, i, d);
443 DICTSET_STR(d, "cf", rrd.rra_def[i].cf_nam);
444 DICTSET_CNT(d, "rows", rrd.rra_def[i].row_cnt);
445 DICTSET_CNT(d, "pdp_per_row", rrd.rra_def[i].pdp_cnt);
446 DICTSET_VAL(d, "xff", rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
448 cdp = PyList_New(rrd.stat_head->ds_cnt);
449 PyDict_SetItemString(d, "cdp_prep", cdp);
452 for (j = 0; j < rrd.stat_head->ds_cnt; j++) {
456 PyList_SET_ITEM(cdp, j, cdd);
458 DICTSET_VAL(cdd, "value",
459 rrd.cdp_prep[i*rrd.stat_head->ds_cnt+j].scratch[CDP_val].u_val);
460 DICTSET_CNT(cdd, "unknown_datapoints",
461 rrd.cdp_prep[i*rrd.stat_head->ds_cnt+j].scratch[CDP_unkn_pdp_cnt].u_cnt);
470 /* List of methods defined in the module */
471 #define meth(name, func, doc) {name, (PyCFunction)func, METH_VARARGS, doc}
473 static PyMethodDef _rrdtool_methods[] = {
474 meth("create", PyRRD_create, PyRRD_create__doc__),
475 meth("update", PyRRD_update, PyRRD_update__doc__),
476 meth("fetch", PyRRD_fetch, PyRRD_fetch__doc__),
477 meth("graph", PyRRD_graph, PyRRD_graph__doc__),
478 meth("tune", PyRRD_tune, PyRRD_tune__doc__),
479 meth("last", PyRRD_last, PyRRD_last__doc__),
480 meth("resize", PyRRD_resize, PyRRD_resize__doc__),
481 meth("info", PyRRD_info, PyRRD_info__doc__),
485 #define SET_INTCONSTANT(dict, value) \
486 t = PyInt_FromLong((long)value); \
487 PyDict_SetItemString(dict, #value, t); \
489 #define SET_STRCONSTANT(dict, value) \
490 t = PyString_FromString(value); \
491 PyDict_SetItemString(dict, #value, t); \
494 /* Initialization function for the module */
496 /*initrrdtool(void)*/
497 initrrdtoolmodule(void)
501 /* Create the module and add the functions */
502 m = Py_InitModule("rrdtoolmodule", _rrdtool_methods);
504 /* Add some symbolic constants to the module */
505 d = PyModule_GetDict(m);
507 SET_STRCONSTANT(d, __version__);
508 ErrorObject = PyErr_NewException("rrdtoolmodule.error", NULL, NULL);
509 PyDict_SetItemString(d, "error", ErrorObject);
511 /* Check for errors */
512 if (PyErr_Occurred())
513 Py_FatalError("can't initialize the rrdtool module");
517 * $Id: _rrdtoolmodule.c,v 1.14 2003/02/22 07:41:19 perky Exp $