prepare for the release of rrdtool-1.3.0
[rrdtool.git] / src / rrd_lastupdate.c
1 /*****************************************************************************
2  * RRDtool 1.3.0  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * rrd_lastupdate  Get the last datum entered for each DS
5  *****************************************************************************/
6
7 #include "rrd_tool.h"
8 #include "rrd_rpncalc.h"
9 #include <stdarg.h>
10
11 int rrd_lastupdate(
12     int argc,
13     char **argv,
14     time_t *last_update,
15     unsigned long *ds_cnt,
16     char ***ds_namv,
17     char ***last_ds)
18 {
19     unsigned long i = 0;
20     char     *filename;
21     rrd_t     rrd;
22     rrd_file_t *rrd_file;
23
24     if (argc < 2) {
25         rrd_set_error("please specify an rrd");
26         goto err_out;
27     }
28     filename = argv[1];
29
30     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
31     if (rrd_file == NULL)
32         goto err_free;
33
34     *last_update = rrd.live_head->last_up;
35     *ds_cnt = rrd.stat_head->ds_cnt;
36     if (((*ds_namv) =
37          (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
38         rrd_set_error("malloc fetch ds_namv array");
39         goto err_close;
40     }
41
42     if (((*last_ds) =
43          (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
44         rrd_set_error("malloc fetch last_ds array");
45         goto err_free_ds_namv;
46     }
47
48     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
49         (*ds_namv)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
50         (*last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
51     }
52
53     rrd_free(&rrd);
54     rrd_close(rrd_file);
55     return (0);
56
57   err_free_ds_namv:
58     free(*ds_namv);
59   err_close:
60     rrd_close(rrd_file);
61   err_free:
62     rrd_free(&rrd);
63   err_out:
64     return (-1);
65 }