Added a call back interface to rrd_dump to be able to call the function from c. See...
[rrdtool.git] / src / rrd.h
1 /*****************************************************************************
2  * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * rrdlib.h   Public header file for librrd
5  *****************************************************************************
6  * $Id$
7  * $Log$
8  * Revision 1.9  2005/02/13 16:13:33  oetiker
9  * let rrd_graph return the actual value range it picked ...
10  * -- Henrik Stoerner <henrik@hswn.dk>
11  *
12  * Revision 1.8  2004/05/26 22:11:12  oetiker
13  * reduce compiler warnings. Many small fixes. -- Mike Slifcak <slif@bellsouth.net>
14  *
15  * Revision 1.7  2003/11/12 22:14:26  oetiker
16  * allow to pass an open filehandle into rrd_graph as an extra argument
17  *
18  * Revision 1.6  2003/11/11 19:46:21  oetiker
19  * replaced time_value with rrd_time_value as MacOS X introduced a struct of that name in their standard headers
20  *
21  * Revision 1.5  2003/04/25 18:35:08  jake
22  * Alternate update interface, updatev. Returns info about CDPs written to disk as result of update. Output format is similar to rrd_info, a hash of key-values.
23  *
24  * Revision 1.4  2003/04/01 22:52:23  jake
25  * Fix Win32 build. VC++ 6.0 and 7.0 now use the thread-safe code.
26  *
27  * Revision 1.3  2003/02/13 07:05:27  oetiker
28  * Find attached the patch I promised to send to you. Please note that there
29  * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c
30  * and src/rrd_not_thread_safe.c) and the introduction of librrd_th. This
31  * library is identical to librrd, but it contains support code for per-thread
32  * global variables currently used for error information only. This is similar
33  * to how errno per-thread variables are implemented.  librrd_th must be linked
34  * alongside of libpthred
35  *
36  * There is also a new file "THREADS", holding some documentation.
37  *
38  * -- Peter Stamfest <peter@stamfest.at>
39  *
40  * Revision 1.2  2002/05/07 21:58:32  oetiker
41  * new command rrdtool xport integrated
42  * --  Wolfgang Schrimm <Wolfgang.Schrimm@urz.uni-heidelberg.de>
43  *
44  * Revision 1.1.1.1  2001/02/25 22:25:05  oetiker
45  * checkin
46  *
47  *****************************************************************************/
48 #ifdef  __cplusplus
49 extern    "C" {
50 #endif
51
52 #ifndef _RRDLIB_H
53 #define _RRDLIB_H
54
55 #include <sys/types.h>  /* for off_t */
56
57 #ifndef WIN32
58 #include <unistd.h>     /* for off_t */
59 #else
60         typedef size_t ssize_t;
61         typedef long off_t;
62 #endif 
63
64 #include <time.h>
65 #include <stdio.h>      /* for FILE */
66 #include <string.h>
67
68 /* Formerly rrd_nan_inf.h */
69 #ifndef DNAN
70 # define DNAN rrd_set_to_DNAN()
71 #endif
72
73 #ifndef DINF
74 # define DINF rrd_set_to_DINF()
75 #endif
76     double    rrd_set_to_DNAN(
77     void);
78     double    rrd_set_to_DINF(
79     void);
80 /* end of rrd_nan_inf.h */
81
82 /* Transplanted from rrd_format.h */
83     typedef double rrd_value_t; /* the data storage type is
84                                  * double */
85 /* END rrd_format.h */
86
87 /* information about an rrd file */
88     typedef struct rrd_file_t {
89         size_t     header_len;   /* length of the header of this rrd file */
90         size_t     file_len; /* total size of the rrd file */
91         size_t     pos;  /* current pos in file */
92         void      *pvt;
93     } rrd_file_t;
94
95 /* information used for the conventional file access methods */
96     typedef struct rrd_simple_file_t {
97         int       fd;  /* file descriptor of this rrd file */
98 #ifdef HAVE_MMAP
99         char     *file_start;   /* start address of an open rrd file */
100         int       mm_prot;
101         int       mm_flags;
102 #endif
103     } rrd_simple_file_t;
104
105 /* rrd info interface */
106     typedef struct rrd_blob_t {
107         unsigned long size; /* size of the blob */
108         unsigned char *ptr; /* pointer */
109     } rrd_blob_t;
110
111     typedef enum rrd_info_type { RD_I_VAL = 0,
112         RD_I_CNT,
113         RD_I_STR,
114         RD_I_INT,
115         RD_I_BLO
116     } rrd_info_type_t;
117
118     typedef union rrd_infoval {
119         unsigned long u_cnt;
120         rrd_value_t u_val;
121         char     *u_str;
122         int       u_int;
123         rrd_blob_t u_blo;
124     } rrd_infoval_t;
125
126     typedef struct rrd_info_t {
127         char     *key;
128         rrd_info_type_t type;
129         rrd_infoval_t value;
130         struct rrd_info_t *next;
131     } rrd_info_t;
132
133     typedef size_t (* rrd_output_callback_t)(
134     const void *,
135     size_t,
136     void *);
137
138 /* main function blocks */
139     int       rrd_create(
140     int,
141     char **);
142     rrd_info_t *rrd_info(
143     int,
144     char **);
145     rrd_info_t *rrd_info_push(
146     rrd_info_t *,
147     char *,
148     rrd_info_type_t,
149     rrd_infoval_t);
150     void      rrd_info_print(
151     rrd_info_t * data);
152     void      rrd_info_free(
153     rrd_info_t *);
154     int       rrd_update(
155     int,
156     char **);
157     rrd_info_t *rrd_update_v(
158     int,
159     char **);
160     int       rrd_graph(
161     int,
162     char **,
163     char ***,
164     int *,
165     int *,
166     FILE *,
167     double *,
168     double *);
169     rrd_info_t *rrd_graph_v(
170     int,
171     char **);
172
173     int       rrd_fetch(
174     int,
175     char **,
176     time_t *,
177     time_t *,
178     unsigned long *,
179     unsigned long *,
180     char ***,
181     rrd_value_t **);
182     int       rrd_restore(
183     int,
184     char **);
185     int       rrd_dump(
186     int,
187     char **);
188     int       rrd_tune(
189     int,
190     char **);
191     time_t    rrd_last(
192     int,
193     char **);
194     int rrd_lastupdate(int argc, char **argv);
195     time_t    rrd_first(
196     int,
197     char **);
198     int       rrd_resize(
199     int,
200     char **);
201     char     *rrd_strversion(
202     void);
203     double    rrd_version(
204     void);
205     int       rrd_xport(
206     int,
207     char **,
208     int *,
209     time_t *,
210     time_t *,
211     unsigned long *,
212     unsigned long *,
213     char ***,
214     rrd_value_t **);
215     int       rrd_flushcached (int argc, char **argv);
216
217     void      rrd_freemem(
218     void *mem);
219
220 /* thread-safe (hopefully) */
221     int       rrd_create_r(
222     const char *filename,
223     unsigned long pdp_step,
224     time_t last_up,
225     int argc,
226     const char **argv);
227     rrd_info_t *rrd_info_r(
228     char *);
229 /* NOTE: rrd_update_r are only thread-safe if no at-style time
230    specifications get used!!! */
231
232     int       rrd_update_r(
233     const char *filename,
234     const char *_template,
235     int argc,
236     const char **argv);
237     int rrd_fetch_r (
238             const char *filename,
239             const char *cf,
240             time_t *start,
241             time_t *end,
242             unsigned long *step,
243             unsigned long *ds_cnt,
244             char ***ds_namv,
245             rrd_value_t **data);
246     int       rrd_dump_r(
247     const char *filename,
248     char *outname);
249     time_t    rrd_last_r (const char *filename);
250     int rrd_lastupdate_r (const char *filename,
251             time_t *ret_last_update,
252             unsigned long *ret_ds_count,
253             char ***ret_ds_names,
254             char ***ret_last_ds);
255     time_t    rrd_first_r(
256     const char *filename,
257     int rraindex);
258
259     int rrd_dump_cb_r(
260     const char *filename,
261     int opt_header,
262     rrd_output_callback_t cb,
263     void *user);
264
265 /* Transplanted from rrd_parsetime.h */
266     typedef enum {
267         ABSOLUTE_TIME,
268         RELATIVE_TO_START_TIME,
269         RELATIVE_TO_END_TIME
270     } rrd_timetype_t;
271
272 #define TIME_OK NULL
273
274     typedef struct rrd_time_value {
275         rrd_timetype_t type;
276         long      offset;
277         struct tm tm;
278     } rrd_time_value_t;
279
280     char     *rrd_parsetime(
281     const char *spec,
282     rrd_time_value_t * ptv);
283 /* END rrd_parsetime.h */
284
285     typedef struct rrd_context {
286         char      lib_errstr[256];
287         char      rrd_error[4096];
288     } rrd_context_t;
289
290 /* returns the current per-thread rrd_context */
291     rrd_context_t *rrd_get_context(void);
292
293 #ifdef WIN32
294 /* this was added by the win32 porters Christof.Wegmann@exitgames.com */
295     rrd_context_t *rrd_force_new_context(void);
296 #endif
297
298 int       rrd_proc_start_end(
299     rrd_time_value_t *,
300     rrd_time_value_t *,
301     time_t *,
302     time_t *);
303
304 /* HELPER FUNCTIONS */
305     void      rrd_set_error(
306     char *,
307     ...);
308     void      rrd_clear_error(
309     void);
310     int       rrd_test_error(
311     void);
312     char     *rrd_get_error(
313     void);
314
315     /* rrd_strerror is thread safe, but still it uses a global buffer
316        (but one per thread), thus subsequent calls within a single
317        thread overwrite the same buffer */
318     const char *rrd_strerror(
319     int err);
320
321 /** MULTITHREADED HELPER FUNCTIONS */
322     rrd_context_t *rrd_new_context(
323     void);
324     void      rrd_free_context(
325     rrd_context_t * buf);
326
327 /* void   rrd_set_error_r  (rrd_context_t *, char *, ...); */
328 /* void   rrd_clear_error_r(rrd_context_t *); */
329 /* int    rrd_test_error_r (rrd_context_t *); */
330 /* char  *rrd_get_error_r  (rrd_context_t *); */
331
332 /** UTILITY FUNCTIONS */
333
334     long rrd_random(void);
335
336     int rrd_add_ptr(void ***dest, size_t *dest_size, void *src);
337     int rrd_add_strdup(char ***dest, size_t *dest_size, char *src);
338     void rrd_free_ptrs(void ***src, size_t *cnt);
339
340 /*
341  * The following functions are _internal_ functions needed to read the raw RRD
342  * files. Since they are _internal_ they may change with the file format and
343  * will be replaced with a more general interface in RRDTool 1.4. Don't use
344  * these functions unless you have good reasons to do so. If you do use these
345  * functions you will have to adapt your code for RRDTool 1.4!
346  *
347  * To enable the deprecated functions define `RRD_EXPORT_DEPRECATED' before
348  * including <rrd_test.h>. You have been warned! If you come back to the
349  * RRDTool mailing list and whine about your broken application, you will get
350  * hit with something smelly!
351  */
352 #if defined(_RRD_TOOL_H) || defined(RRD_EXPORT_DEPRECATED)
353
354 # if defined(_RRD_TOOL_H)
355 #  include "rrd_format.h"
356 # else
357 #  include <rrd_format.h>
358 # endif
359
360 #if defined(__GNUC__) && defined (RRD_EXPORT_DEPRECATED)
361 # define RRD_DEPRECATED __attribute__((deprecated))
362 #else
363 # define RRD_DEPRECATED          /**/
364 #endif
365      void     rrd_free(
366     rrd_t *rrd)
367               RRD_DEPRECATED;
368     void      rrd_init(
369     rrd_t *rrd)
370               RRD_DEPRECATED;
371
372     rrd_file_t *rrd_open(
373     const char *const file_name,
374     rrd_t *rrd,
375     unsigned rdwr)
376               RRD_DEPRECATED;
377
378     void      rrd_dontneed(
379     rrd_file_t *rrd_file,
380     rrd_t *rrd)
381               RRD_DEPRECATED;
382     int       rrd_close(
383     rrd_file_t *rrd_file)
384               RRD_DEPRECATED;
385     ssize_t   rrd_read(
386     rrd_file_t *rrd_file,
387     void *buf,
388     size_t count)
389               RRD_DEPRECATED;
390     ssize_t   rrd_write(
391     rrd_file_t *rrd_file,
392     const void *buf,
393     size_t count)
394               RRD_DEPRECATED;
395     void      rrd_flush(
396     rrd_file_t *rrd_file)
397               RRD_DEPRECATED;
398     off_t     rrd_seek(
399     rrd_file_t *rrd_file,
400     off_t off,
401     int whence)
402               RRD_DEPRECATED;
403     off_t     rrd_tell(
404     rrd_file_t *rrd_file)
405               RRD_DEPRECATED;
406     int       rrd_lock(
407     rrd_file_t *file)
408               RRD_DEPRECATED;
409     void      rrd_notify_row(
410     rrd_file_t *rrd_file,
411     int rra_idx,
412     unsigned long rra_row,
413     time_t rra_time)
414               RRD_DEPRECATED;
415     unsigned long rrd_select_initial_row(
416     rrd_file_t *rrd_file,
417     int rra_idx,
418     rra_def_t *rra
419     )
420               RRD_DEPRECATED;
421 #endif                  /* defined(_RRD_TOOL_H) || defined(RRD_EXPORT_DEPRECATED) */
422
423 #endif                  /* _RRDLIB_H */
424
425 #ifdef  __cplusplus
426 }
427 #endif