f71fbb1d7dd348b082ea3f67836454a17c8dc477
[rrdtool.git] / bindings / python / rrdtoolmodule.c
1 /*
2  * rrdtoolmodule.c
3  *
4  * RRDTool Python binding
5  *
6  * Author  : Hye-Shik Chang <perky@fallin.lv>
7  * Date    : $Date: 2003/02/22 07:41:19 $
8  * Created : 23 May 2002
9  *
10  * $Revision: 1.14 $
11  *
12  *  ==========================================================================
13  *  This file is part of py-rrdtool.
14  *
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.
19  *
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.
24  *
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
28  *
29  */
30
31 #ifdef UNUSED
32 #elif defined(__GNUC__)
33 # define UNUSED(x) x __attribute__((unused))
34 #elif defined(__LCLINT__)
35 # define UNUSED(x) /*@unused@*/ x
36 #else
37 # define UNUSED(x) x
38 #endif
39
40 static const char *__version__ = "$Revision: 1.14 $";
41
42 #include "Python.h"
43 #include "rrd.h"
44 #include "rrd_extra.h"
45
46 static PyObject *ErrorObject;
47 extern int optind;
48 extern int opterr;
49
50 /* forward declaration to keep compiler happy */
51 void initrrdtool(void);
52
53 static int
54 create_args(char *command, PyObject *args, int *argc, char ***argv)
55 {
56     PyObject        *o;
57     int              size, i;
58     
59     size    = PyTuple_Size(args);
60     *argv   = PyMem_New(char *, size + 1);
61     if (*argv == NULL)
62         return -1;
63
64     for (i = 0; i < size; i++) {
65         o = PyTuple_GET_ITEM(args, i);
66         if (PyString_Check(o))
67             (*argv)[i + 1] = PyString_AS_STRING(o);
68         else {
69             PyMem_Del(*argv);
70             PyErr_Format(PyExc_TypeError, "argument %d must be string", i);
71             return -1;
72         }
73     }
74     (*argv)[0] = command;
75     *argc = size + 1;
76
77     /* reset getopt state */
78     opterr = optind = 0;
79
80     return 0;
81 }
82
83 static void
84 destroy_args(char ***argv)
85 {
86     PyMem_Del(*argv);
87     *argv = NULL;
88 }
89
90 static char PyRRD_create__doc__[] =
91 "create(args..): Set up a new Round Robin Database\n\
92     create filename [--start|-b start time] \
93 [--step|-s step] [DS:ds-name:DST:heartbeat:min:max] \
94 [RRA:CF:xff:steps:rows]";
95
96 static PyObject *
97 PyRRD_create(PyObject UNUSED(*self), PyObject *args)
98 {
99     PyObject        *r;
100     char           **argv;
101     int              argc;
102
103     if (create_args("create", args, &argc, &argv) < 0)
104         return NULL;
105
106     if (rrd_create(argc, argv) == -1) {
107         PyErr_SetString(ErrorObject, rrd_get_error());
108         rrd_clear_error();
109         r = NULL;
110     } else {
111         Py_INCREF(Py_None);
112         r = Py_None;
113     }
114
115     destroy_args(&argv);
116     return r;
117 }
118
119 static char PyRRD_update__doc__[] =
120 "update(args..): Store a new set of values into the rrd\n"
121 "    update filename [--template|-t ds-name[:ds-name]...] "
122 "N|timestamp:value[:value...] [timestamp:value[:value...] ...]";
123
124 static PyObject *
125 PyRRD_update(PyObject UNUSED(*self), PyObject *args)
126 {
127     PyObject        *r;
128     char           **argv;
129     int              argc;
130
131     if (create_args("update", args, &argc, &argv) < 0)
132         return NULL;
133
134     if (rrd_update(argc, argv) == -1) {
135         PyErr_SetString(ErrorObject, rrd_get_error());
136         rrd_clear_error();
137         r = NULL;
138     } else {
139         Py_INCREF(Py_None);
140         r = Py_None;
141     }
142
143     destroy_args(&argv);
144     return r;
145 }
146
147 static char PyRRD_fetch__doc__[] =
148 "fetch(args..): fetch data from an rrd.\n"
149 "    fetch filename CF [--resolution|-r resolution] "
150 "[--start|-s start] [--end|-e end]";
151
152 static PyObject *
153 PyRRD_fetch(PyObject UNUSED(*self), PyObject *args)
154 {
155     PyObject        *r;
156     rrd_value_t     *data, *datai;
157     unsigned long    step, ds_cnt;
158     time_t           start, end;
159     int              argc;
160     char           **argv, **ds_namv;
161
162     if (create_args("fetch", args, &argc, &argv) < 0)
163         return NULL;
164
165     if (rrd_fetch(argc, argv, &start, &end, &step,
166                   &ds_cnt, &ds_namv, &data) == -1) {
167         PyErr_SetString(ErrorObject, rrd_get_error());
168         rrd_clear_error();
169         r = NULL;
170     } else {
171         /* Return :
172           ((start, end, step), (name1, name2, ...), [(data1, data2, ..), ...]) */
173         PyObject    *range_tup, *dsnam_tup, *data_list, *t;
174         unsigned long          i, j, row;
175         rrd_value_t  dv;
176
177         row = ((end - start) / step + 1);
178
179         r = PyTuple_New(3);
180         range_tup = PyTuple_New(3);
181         dsnam_tup = PyTuple_New(ds_cnt);
182         data_list = PyList_New(row);
183         PyTuple_SET_ITEM(r, 0, range_tup);
184         PyTuple_SET_ITEM(r, 1, dsnam_tup);
185         PyTuple_SET_ITEM(r, 2, data_list);
186
187         datai = data;
188
189         PyTuple_SET_ITEM(range_tup, 0, PyInt_FromLong((long)start));
190         PyTuple_SET_ITEM(range_tup, 1, PyInt_FromLong((long)end));
191         PyTuple_SET_ITEM(range_tup, 2, PyInt_FromLong((long)step));
192
193         for (i = 0; i < ds_cnt; i++)
194             PyTuple_SET_ITEM(dsnam_tup, i, PyString_FromString(ds_namv[i]));
195
196         for (i = 0; i < row; i ++) {
197             t = PyTuple_New(ds_cnt);
198             PyList_SET_ITEM(data_list, i, t);
199
200             for (j = 0; j < ds_cnt; j++) {
201                 dv = *(datai++);
202                 if (isnan(dv)) {
203                     PyTuple_SET_ITEM(t, j, Py_None);
204                     Py_INCREF(Py_None);
205                 } else {
206                     PyTuple_SET_ITEM(t, j, PyFloat_FromDouble((double)dv));
207                 }
208             }
209         }
210
211         for (i = 0; i < ds_cnt; i++)
212             free(ds_namv[i]);
213         free(ds_namv); /* rrdtool don't use PyMem_Malloc :) */
214         free(data);
215     }
216
217     destroy_args(&argv);
218     return r;
219 }
220
221 static char PyRRD_graph__doc__[] =
222 "graph(args..): Create a graph based on data from one or several RRD\n"
223 "    graph filename [-s|--start seconds] "
224 "[-e|--end seconds] [-x|--x-grid x-axis grid and label] "
225 "[-y|--y-grid y-axis grid and label] [--alt-y-grid] [--alt-y-mrtg] "
226 "[--alt-autoscale] [--alt-autoscale-max] [--units-exponent] value "
227 "[-v|--vertical-label text] [-w|--width pixels] [-h|--height pixels] "
228 "[-i|--interlaced] "
229 "[-f|--imginfo formatstring] [-a|--imgformat GIF|PNG|GD] "
230 "[-B|--background value] [-O|--overlay value] "
231 "[-U|--unit value] [-z|--lazy] [-o|--logarithmic] "
232 "[-u|--upper-limit value] [-l|--lower-limit value] "
233 "[-g|--no-legend] [-r|--rigid] [--step value] "
234 "[-b|--base value] [-c|--color COLORTAG#rrggbb] "
235 "[-t|--title title] [DEF:vname=rrd:ds-name:CF] "
236 "[CDEF:vname=rpn-expression] [PRINT:vname:CF:format] "
237 "[GPRINT:vname:CF:format] [COMMENT:text] "
238 "[HRULE:value#rrggbb[:legend]] [VRULE:time#rrggbb[:legend]] "
239 "[LINE{1|2|3}:vname[#rrggbb[:legend]]] "
240 "[AREA:vname[#rrggbb[:legend]]] "
241 "[STACK:vname[#rrggbb[:legend]]]";
242
243 static PyObject *
244 PyRRD_graph(PyObject UNUSED(*self), PyObject *args)
245 {
246     PyObject        *r;
247     char           **argv, **calcpr;
248     int              argc, xsize, ysize, i;
249     double          ymin, ymax;
250     if (create_args("graph", args, &argc, &argv) < 0)
251         return NULL;
252
253     if (rrd_graph(argc, argv, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax) == -1) {
254         PyErr_SetString(ErrorObject, rrd_get_error());
255         rrd_clear_error();
256         r = NULL;
257     } else {
258         r = PyTuple_New(3);
259
260         PyTuple_SET_ITEM(r, 0, PyInt_FromLong((long)xsize));
261         PyTuple_SET_ITEM(r, 1, PyInt_FromLong((long)ysize));
262
263         if (calcpr) {
264             PyObject    *e, *t;
265             
266             e = PyList_New(0);
267             PyTuple_SET_ITEM(r, 2, e);
268
269             for(i = 0; calcpr[i]; i++) {
270                 t = PyString_FromString(calcpr[i]);
271                 PyList_Append(e, t);
272                 Py_DECREF(t);
273                 free(calcpr[i]);
274             }
275             free(calcpr);
276         } else {
277             Py_INCREF(Py_None);
278             PyTuple_SET_ITEM(r, 2, Py_None);
279         }
280     }
281
282     destroy_args(&argv);
283     return r;
284 }
285
286 static char PyRRD_tune__doc__[] =
287 "tune(args...): Modify some basic properties of a Round Robin Database\n"
288 "    tune filename [--heartbeat|-h ds-name:heartbeat] "
289 "[--minimum|-i ds-name:min] [--maximum|-a ds-name:max] "
290 "[--data-source-type|-d ds-name:DST] [--data-source-rename|-r old-name:new-name]";
291
292 static PyObject *
293 PyRRD_tune(PyObject UNUSED(*self), PyObject *args)
294 {
295     PyObject        *r;
296     char           **argv;
297     int              argc;
298
299     if (create_args("tune", args, &argc, &argv) < 0)
300         return NULL;
301
302     if (rrd_tune(argc, argv) == -1) {
303         PyErr_SetString(ErrorObject, rrd_get_error());
304         rrd_clear_error();
305         r = NULL;
306     } else {
307         Py_INCREF(Py_None);
308         r = Py_None;
309     }
310
311     destroy_args(&argv);
312     return r;
313 }
314
315 static char PyRRD_last__doc__[] =
316 "last(filename): Return the timestamp of the last data sample in an RRD";
317
318 static PyObject *
319 PyRRD_last(PyObject UNUSED(*self), PyObject *args)
320 {
321     PyObject        *r;
322     int              argc, ts;
323     char           **argv;
324
325     if (create_args("last", args, &argc, &argv) < 0)
326         return NULL;
327
328     if ((ts = rrd_last(argc, argv)) == -1) {
329         PyErr_SetString(ErrorObject, rrd_get_error());
330         rrd_clear_error();
331         r = NULL;
332     } else
333         r = PyInt_FromLong((long)ts);
334
335     destroy_args(&argv);
336     return r;
337 }
338
339 static char PyRRD_resize__doc__[] =
340 "resize(args...): alters the size of an RRA.\n"
341 "    resize filename rra-num GROW|SHRINK rows";
342
343 static PyObject *
344 PyRRD_resize(PyObject UNUSED(*self), PyObject *args)
345 {
346     PyObject        *r;
347     char           **argv;
348     int              argc, ts;
349
350     if (create_args("resize", args, &argc, &argv) < 0)
351         return NULL;
352
353     if ((ts = rrd_resize(argc, argv)) == -1) {
354         PyErr_SetString(ErrorObject, rrd_get_error());
355         rrd_clear_error();
356         r = NULL;
357     } else {
358         Py_INCREF(Py_None);
359         r = Py_None;
360     }
361
362     destroy_args(&argv);
363     return r;
364 }
365
366 static char PyRRD_info__doc__[] =
367 "info(filename): extract header information from an rrd";
368
369 static PyObject *
370 PyRRD_info(PyObject UNUSED(*self), PyObject *args)
371 {
372     PyObject        *r, *t, *ds;
373     rrd_t            rrd;
374     FILE            *in_file;
375     char            *filename;
376     unsigned long   i, j;
377
378     if (! PyArg_ParseTuple(args, "s:info", &filename))
379         return NULL;
380
381     if (rrd_open(filename, &in_file, &rrd, RRD_READONLY) == -1) {
382         PyErr_SetString(ErrorObject, rrd_get_error());
383         rrd_clear_error();
384         return NULL;
385     }
386     fclose(in_file);
387
388 #define DICTSET_STR(dict, name, value) \
389     t = PyString_FromString(value); \
390     PyDict_SetItemString(dict, name, t); \
391     Py_DECREF(t);
392
393 #define DICTSET_CNT(dict, name, value) \
394     t = PyInt_FromLong((long)value); \
395     PyDict_SetItemString(dict, name, t); \
396     Py_DECREF(t);
397
398 #define DICTSET_VAL(dict, name, value) \
399     t = isnan(value) ? (Py_INCREF(Py_None), Py_None) :  \
400         PyFloat_FromDouble((double)value); \
401     PyDict_SetItemString(dict, name, t); \
402     Py_DECREF(t);
403
404     r = PyDict_New();
405
406     DICTSET_STR(r, "filename", filename);
407     DICTSET_STR(r, "rrd_version", rrd.stat_head->version);
408     DICTSET_CNT(r, "step", rrd.stat_head->pdp_step);
409     DICTSET_CNT(r, "last_update", rrd.live_head->last_up);
410
411     ds = PyDict_New();
412     PyDict_SetItemString(r, "ds", ds);
413     Py_DECREF(ds);
414
415     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
416         PyObject    *d;
417
418         d = PyDict_New();
419         PyDict_SetItemString(ds, rrd.ds_def[i].ds_nam, d);
420         Py_DECREF(d);
421
422         DICTSET_STR(d, "ds_name", rrd.ds_def[i].ds_nam);
423         DICTSET_STR(d, "type", rrd.ds_def[i].dst);
424         DICTSET_CNT(d, "minimal_heartbeat", rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
425         DICTSET_VAL(d, "min", rrd.ds_def[i].par[DS_min_val].u_val);
426         DICTSET_VAL(d, "max", rrd.ds_def[i].par[DS_max_val].u_val);
427         DICTSET_STR(d, "last_ds", rrd.pdp_prep[i].last_ds);
428         DICTSET_VAL(d, "value", rrd.pdp_prep[i].scratch[PDP_val].u_val);
429         DICTSET_CNT(d, "unknown_sec", rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
430     }
431
432     ds = PyList_New(rrd.stat_head->rra_cnt);
433     PyDict_SetItemString(r, "rra", ds);
434     Py_DECREF(ds);
435
436     for (i = 0; i < rrd.stat_head->rra_cnt; i++) {
437         PyObject    *d, *cdp;
438
439         d = PyDict_New();
440         PyList_SET_ITEM(ds, i, d);
441
442         DICTSET_STR(d, "cf", rrd.rra_def[i].cf_nam);
443         DICTSET_CNT(d, "rows", rrd.rra_def[i].row_cnt);
444         DICTSET_CNT(d, "pdp_per_row", rrd.rra_def[i].pdp_cnt);
445         DICTSET_VAL(d, "xff", rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
446
447         cdp = PyList_New(rrd.stat_head->ds_cnt);
448         PyDict_SetItemString(d, "cdp_prep", cdp);
449         Py_DECREF(cdp);
450
451         for (j = 0; j < rrd.stat_head->ds_cnt; j++) {
452             PyObject    *cdd;
453
454             cdd = PyDict_New();
455             PyList_SET_ITEM(cdp, j, cdd);
456
457             DICTSET_VAL(cdd, "value",
458                     rrd.cdp_prep[i*rrd.stat_head->ds_cnt+j].scratch[CDP_val].u_val);
459             DICTSET_CNT(cdd, "unknown_datapoints",
460                     rrd.cdp_prep[i*rrd.stat_head->ds_cnt+j].scratch[CDP_unkn_pdp_cnt].u_cnt);
461         }
462     }
463
464     rrd_free(&rrd);
465
466     return r;
467 }
468
469 /* List of methods defined in the module */
470 #define meth(name, func, doc) {name, (PyCFunction)func, METH_VARARGS, doc}
471
472 static PyMethodDef _rrdtool_methods[] = {
473     meth("create",  PyRRD_create,   PyRRD_create__doc__),
474     meth("update",  PyRRD_update,   PyRRD_update__doc__),
475     meth("fetch",   PyRRD_fetch,    PyRRD_fetch__doc__),
476     meth("graph",   PyRRD_graph,    PyRRD_graph__doc__),
477     meth("tune",    PyRRD_tune,     PyRRD_tune__doc__),
478     meth("last",    PyRRD_last,     PyRRD_last__doc__),
479     meth("resize",  PyRRD_resize,   PyRRD_resize__doc__),
480     meth("info",    PyRRD_info,     PyRRD_info__doc__),
481     {NULL, NULL}
482 };
483
484 #define SET_INTCONSTANT(dict, value) \
485             t = PyInt_FromLong((long)value); \
486             PyDict_SetItemString(dict, #value, t); \
487             Py_DECREF(t);
488 #define SET_STRCONSTANT(dict, value) \
489             t = PyString_FromString(value); \
490             PyDict_SetItemString(dict, #value, t); \
491             Py_DECREF(t);
492
493 /* Initialization function for the module */
494 void
495 initrrdtool(void)
496 {
497     PyObject    *m, *d, *t;
498
499     /* Create the module and add the functions */
500     m = Py_InitModule("rrdtool", _rrdtool_methods);
501
502     /* Add some symbolic constants to the module */
503     d = PyModule_GetDict(m);
504
505     SET_STRCONSTANT(d, __version__);
506     ErrorObject = PyErr_NewException("_rrdtool.error", NULL, NULL);
507     PyDict_SetItemString(d, "error", ErrorObject);
508
509     /* Check for errors */
510     if (PyErr_Occurred())
511         Py_FatalError("can't initialize the rrdtool module");
512 }
513
514 /*
515  * $Id: _rrdtoolmodule.c,v 1.14 2003/02/22 07:41:19 perky Exp $
516  * ex: ts=8 sts=4 et
517  */