3e0c871a56459b7cdf9b169ddab0615679e7178e
[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 #include <unistd.h>     /* for off_t */
57 #include <time.h>
58 #include <stdio.h>      /* for FILE */
59
60
61 /* Formerly rrd_nan_inf.h */
62 #ifndef DNAN
63 # define DNAN rrd_set_to_DNAN()
64 #endif
65
66 #ifndef DINF
67 # define DINF rrd_set_to_DINF()
68 #endif
69     double    rrd_set_to_DNAN(
70     void);
71     double    rrd_set_to_DINF(
72     void);
73 /* end of rrd_nan_inf.h */
74
75 /* Transplanted from rrd_format.h */
76     typedef double rrd_value_t; /* the data storage type is
77                                  * double */
78 /* END rrd_format.h */
79
80 /* information about an rrd file */
81     typedef struct rrd_file_t {
82         off_t     header_len;   /* length of the header of this rrd file */
83         off_t     file_len; /* total size of the rrd file */
84         off_t     pos;  /* current pos in file */
85         void      *pvt;
86     } rrd_file_t;
87
88 /* information used for the conventional file access methods */
89     typedef struct rrd_simple_file_t {
90         int       fd;  /* file descriptor of this rrd file */
91 #ifdef HAVE_MMAP
92         char     *file_start;   /* start address of an open rrd file */
93         int       mm_prot;
94         int       mm_flags;
95 #endif
96     } rrd_simple_file_t;
97
98 /* rrd info interface */
99     typedef struct rrd_blob_t {
100         unsigned long size; /* size of the blob */
101         unsigned char *ptr; /* pointer */
102     } rrd_blob_t;
103
104     typedef enum rrd_info_type { RD_I_VAL = 0,
105         RD_I_CNT,
106         RD_I_STR,
107         RD_I_INT,
108         RD_I_BLO
109     } rrd_info_type_t;
110
111     typedef union rrd_infoval {
112         unsigned long u_cnt;
113         rrd_value_t u_val;
114         char     *u_str;
115         int       u_int;
116         rrd_blob_t u_blo;
117     } rrd_infoval_t;
118
119     typedef struct rrd_info_t {
120         char     *key;
121         rrd_info_type_t type;
122         rrd_infoval_t value;
123         struct rrd_info_t *next;
124     } rrd_info_t;
125
126
127 /* main function blocks */
128     int       rrd_create(
129     int,
130     char **);
131     rrd_info_t *rrd_info(
132     int,
133     char **);
134     rrd_info_t *rrd_info_push(
135     rrd_info_t *,
136     char *,
137     rrd_info_type_t,
138     rrd_infoval_t);
139     void      rrd_info_print(
140     rrd_info_t * data);
141     void      rrd_info_free(
142     rrd_info_t *);
143     int       rrd_update(
144     int,
145     char **);
146     rrd_info_t *rrd_update_v(
147     int,
148     char **);
149     int       rrd_graph(
150     int,
151     char **,
152     char ***,
153     int *,
154     int *,
155     FILE *,
156     double *,
157     double *);
158     rrd_info_t *rrd_graph_v(
159     int,
160     char **);
161
162     int       rrd_fetch(
163     int,
164     char **,
165     time_t *,
166     time_t *,
167     unsigned long *,
168     unsigned long *,
169     char ***,
170     rrd_value_t **);
171     int       rrd_restore(
172     int,
173     char **);
174     int       rrd_dump(
175     int,
176     char **);
177     int       rrd_tune(
178     int,
179     char **);
180     time_t    rrd_last(
181     int,
182     char **);
183     int rrd_lastupdate(int argc, char **argv);
184     time_t    rrd_first(
185     int,
186     char **);
187     int       rrd_resize(
188     int,
189     char **);
190     char     *rrd_strversion(
191     void);
192     double    rrd_version(
193     void);
194     int       rrd_xport(
195     int,
196     char **,
197     int *,
198     time_t *,
199     time_t *,
200     unsigned long *,
201     unsigned long *,
202     char ***,
203     rrd_value_t **);
204     int       rrd_cmd_flush (int argc, char **argv);
205
206     void      rrd_freemem(
207     void *mem);
208
209 /* thread-safe (hopefully) */
210     int       rrd_create_r(
211     const char *filename,
212     unsigned long pdp_step,
213     time_t last_up,
214     int argc,
215     const char **argv);
216     rrd_info_t *rrd_info_r(
217     char *);
218 /* NOTE: rrd_update_r are only thread-safe if no at-style time
219    specifications get used!!! */
220
221     int       rrd_update_r(
222     const char *filename,
223     const char *_template,
224     int argc,
225     const char **argv);
226     int rrd_fetch_r (
227             const char *filename,
228             const char *cf,
229             time_t *start,
230             time_t *end,
231             unsigned long *step,
232             unsigned long *ds_cnt,
233             char ***ds_namv,
234             rrd_value_t **data);
235     int       rrd_dump_r(
236     const char *filename,
237     char *outname);
238     time_t    rrd_last_r (const char *filename);
239     int rrd_lastupdate_r (const char *filename,
240             time_t *ret_last_update,
241             unsigned long *ret_ds_count,
242             char ***ret_ds_names,
243             char ***ret_last_ds);
244     time_t    rrd_first_r(
245     const char *filename,
246     int rraindex);
247
248 /* Transplanted from rrd_parsetime.h */
249     typedef enum {
250         ABSOLUTE_TIME,
251         RELATIVE_TO_START_TIME,
252         RELATIVE_TO_END_TIME
253     } rrd_timetype_t;
254
255 #define TIME_OK NULL
256
257     typedef struct rrd_time_value {
258         rrd_timetype_t type;
259         long      offset;
260         struct tm tm;
261     } rrd_time_value_t;
262
263     char     *rrd_parsetime(
264     const char *spec,
265     rrd_time_value_t * ptv);
266 /* END rrd_parsetime.h */
267
268     typedef struct rrd_context {
269         char      lib_errstr[256];
270         char      rrd_error[4096];
271     } rrd_context_t;
272
273 /* returns the current per-thread rrd_context */
274     rrd_context_t *rrd_get_context(
275     void);
276
277
278     int       rrd_proc_start_end(
279     rrd_time_value_t *,
280     rrd_time_value_t *,
281     time_t *,
282     time_t *);
283
284 /* HELPER FUNCTIONS */
285     void      rrd_set_error(
286     char *,
287     ...);
288     void      rrd_clear_error(
289     void);
290     int       rrd_test_error(
291     void);
292     char     *rrd_get_error(
293     void);
294
295     /* rrd_strerror is thread safe, but still it uses a global buffer
296        (but one per thread), thus subsequent calls within a single
297        thread overwrite the same buffer */
298     const char *rrd_strerror(
299     int err);
300
301 /** MULTITHREADED HELPER FUNCTIONS */
302     rrd_context_t *rrd_new_context(
303     void);
304     void      rrd_free_context(
305     rrd_context_t * buf);
306
307 /* void   rrd_set_error_r  (rrd_context_t *, char *, ...); */
308 /* void   rrd_clear_error_r(rrd_context_t *); */
309 /* int    rrd_test_error_r (rrd_context_t *); */
310 /* char  *rrd_get_error_r  (rrd_context_t *); */
311
312 /*
313  * The following functions are _internal_ functions needed to read the raw RRD
314  * files. Since they are _internal_ they may change with the file format and
315  * will be replaced with a more general interface in RRDTool 1.4. Don't use
316  * these functions unless you have good reasons to do so. If you do use these
317  * functions you will have to adapt your code for RRDTool 1.4!
318  *
319  * To enable the deprecated functions define `RRD_EXPORT_DEPRECATED' before
320  * including <rrd_test.h>. You have been warned! If you come back to the
321  * RRDTool mailing list and whine about your broken application, you will get
322  * hit with something smelly!
323  */
324 #if defined(_RRD_TOOL_H) || defined(RRD_EXPORT_DEPRECATED)
325
326 # if defined(_RRD_TOOL_H)
327 #  include "rrd_format.h"
328 # else
329 #  include <rrd_format.h>
330 # endif
331
332 #if defined(__GNUC__) && defined (RRD_EXPORT_DEPRECATED)
333 # define RRD_DEPRECATED __attribute__((deprecated))
334 #else
335 # define RRD_DEPRECATED          /**/
336 #endif
337      void     rrd_free(
338     rrd_t *rrd)
339               RRD_DEPRECATED;
340     void      rrd_init(
341     rrd_t *rrd)
342               RRD_DEPRECATED;
343
344     rrd_file_t *rrd_open(
345     const char *const file_name,
346     rrd_t *rrd,
347     unsigned rdwr)
348               RRD_DEPRECATED;
349
350     void      rrd_dontneed(
351     rrd_file_t *rrd_file,
352     rrd_t *rrd)
353               RRD_DEPRECATED;
354     int       rrd_close(
355     rrd_file_t *rrd_file)
356               RRD_DEPRECATED;
357     ssize_t   rrd_read(
358     rrd_file_t *rrd_file,
359     void *buf,
360     size_t count)
361               RRD_DEPRECATED;
362     ssize_t   rrd_write(
363     rrd_file_t *rrd_file,
364     const void *buf,
365     size_t count)
366               RRD_DEPRECATED;
367     void      rrd_flush(
368     rrd_file_t *rrd_file)
369               RRD_DEPRECATED;
370     off_t     rrd_seek(
371     rrd_file_t *rrd_file,
372     off_t off,
373     int whence)
374               RRD_DEPRECATED;
375     off_t     rrd_tell(
376     rrd_file_t *rrd_file)
377               RRD_DEPRECATED;
378     int       rrd_lock(
379     rrd_file_t *file)
380               RRD_DEPRECATED;
381 #endif                  /* defined(_RRD_TOOL_H) || defined(RRD_EXPORT_DEPRECATED) */
382
383 #endif                  /* _RRDLIB_H */
384
385 #ifdef  __cplusplus
386 }
387 #endif