More updates from Bernhard Fischer
[rrdtool.git] / src / rrd_lastupdate.c
1 /*****************************************************************************
2  * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
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         return -1;
27     }
28     filename = argv[1];
29
30     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
31     if (rrd_file == NULL)
32         return (-1);
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         rrd_free(&rrd);
40         return (-1);
41     }
42
43     if (((*last_ds) =
44          (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
45         rrd_set_error("malloc fetch last_ds array");
46         rrd_free(&rrd);
47         free(*ds_namv);
48         return (-1);
49     }
50
51     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
52         (*ds_namv)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
53         (*last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
54     }
55
56     rrd_free(&rrd);
57     close(rrd_file->fd);
58     rrd_close(rrd_file);
59     return (0);
60 }