prepare for the release of rrdtool-1.2.16
[rrdtool.git] / src / rrd_lastupdate.c
1 /*****************************************************************************
2  * RRDtool 1.2.16  Copyright by Tobi Oetiker, 1997-2006
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
12 rrd_lastupdate(int argc, char **argv, time_t *last_update,
13                  unsigned long *ds_cnt, char ***ds_namv, char ***last_ds) {
14     unsigned long i=0;
15     char         *filename;
16     FILE         *in_file;
17     rrd_t        rrd;
18
19     if(argc < 2){
20         rrd_set_error("please specify an rrd");
21         return -1;
22     }
23     filename = argv[1];
24
25     if(rrd_open(filename,&in_file,&rrd, RRD_READONLY)==-1){
26         return(-1);
27     }
28     fclose(in_file);
29
30     *last_update=rrd.live_head->last_up;
31     *ds_cnt = rrd.stat_head->ds_cnt;
32     if (((*ds_namv) =
33                 (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
34         rrd_set_error("malloc fetch ds_namv array");
35         rrd_free(&rrd);
36         return(-1);
37     } 
38
39     if (((*last_ds) =
40                 (char **) malloc(rrd.stat_head->ds_cnt * sizeof(char*)))==NULL){
41         rrd_set_error("malloc fetch last_ds array");
42         rrd_free(&rrd);
43         free(*ds_namv);
44         return(-1);
45     } 
46
47     for(i=0;i<rrd.stat_head->ds_cnt;i++){
48         (*ds_namv)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
49         (*last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
50     }
51
52     rrd_free(&rrd);
53     return(0); 
54 }