1546f6a148237abfe28ee3af68df3bd02f72b955
[rrdtool.git] / src / rrd_lastupdate.c
1 /*****************************************************************************
2  * RRDtool 1.3.2  Copyright by Tobi Oetiker, 1997-2008
3  *                Copyright by Florian Forster, 2008
4  *****************************************************************************
5  * rrd_lastupdate  Get the last datum entered for each DS
6  *****************************************************************************/
7
8 #include "rrd_tool.h"
9 #include "rrd_rpncalc.h"
10 #include "rrd_client.h"
11 #include <stdarg.h>
12
13 int rrd_lastupdate (int argc, char **argv)
14 {
15     time_t    last_update;
16     char    **ds_names;
17     char    **last_ds;
18     unsigned long ds_count, i;
19     int status;
20
21     char *opt_daemon = NULL;
22
23     optind = 0;
24     opterr = 0;         /* initialize getopt */
25
26     while (42) {
27         int       opt;
28         int       option_index = 0;
29         static struct option long_options[] = {
30             {"daemon", required_argument, 0, 'd'},
31             {0, 0, 0, 0}
32         };
33
34         opt = getopt_long (argc, argv, "d:", long_options, &option_index);
35
36         if (opt == EOF)
37             break;
38
39         switch (opt) {
40         case 'd':
41             if (opt_daemon != NULL)
42                     free (opt_daemon);
43             opt_daemon = strdup (optarg);
44             if (opt_daemon == NULL)
45             {
46                 rrd_set_error ("strdup failed.");
47                 return (-1);
48             }
49             break;
50
51         default:
52             rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
53                     argv[0]);
54             return (-1);
55             break;
56         }
57     }                   /* while (42) */
58
59     if ((argc - optind) != 1) {
60         rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
61                 argv[0]);
62         return (-1);
63     }
64
65     status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
66     if (opt_daemon) free (opt_daemon);
67     if (status) return (-1);
68
69     status = rrd_lastupdate_r (argv[optind],
70             &last_update, &ds_count, &ds_names, &last_ds);
71     if (status != 0)
72         return (status);
73
74     for (i = 0; i < ds_count; i++)
75         printf(" %s", ds_names[i]);
76     printf ("\n\n");
77
78     printf ("%10lu:", last_update);
79     for (i = 0; i < ds_count; i++) {
80         printf(" %s", last_ds[i]);
81         free(last_ds[i]);
82         free(ds_names[i]);
83     }
84     printf("\n");
85
86     free(last_ds);
87     free(ds_names);
88
89     return (0);
90 } /* int rrd_lastupdate */
91
92 int rrd_lastupdate_r(const char *filename,
93         time_t *ret_last_update,
94         unsigned long *ret_ds_count,
95         char ***ret_ds_names,
96         char ***ret_last_ds)
97 {
98     unsigned long i = 0;
99     rrd_t     rrd;
100     rrd_file_t *rrd_file;
101
102     rrd_init(&rrd);
103     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
104     if (rrd_file == NULL) {
105         rrd_free(&rrd);
106         return (-1);
107     }
108
109     *ret_last_update = rrd.live_head->last_up;
110     *ret_ds_count = rrd.stat_head->ds_cnt;
111     *ret_ds_names = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
112     if (*ret_ds_names == NULL) {
113         rrd_set_error ("malloc fetch ret_ds_names array");
114         rrd_close (rrd_file);
115         rrd_free (&rrd);
116         return (-1);
117     }
118     memset (*ret_ds_names, 0, rrd.stat_head->ds_cnt * sizeof(char *));
119
120     *ret_last_ds = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
121     if (*ret_last_ds == NULL) {
122         rrd_set_error ("malloc fetch ret_last_ds array");
123         free (*ret_ds_names);
124         *ret_ds_names = NULL;
125         rrd_close (rrd_file);
126         rrd_free (&rrd);
127         return (-1);
128     }
129     memset (*ret_last_ds, 0, rrd.stat_head->ds_cnt * sizeof(char *));
130
131     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
132         (*ret_ds_names)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
133         (*ret_last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
134
135         if (((*ret_ds_names)[i] == NULL) || ((*ret_last_ds)[i] == NULL))
136             break;
137     }
138
139     /* Check if all names and values could be copied and free everything if
140      * not. */
141     if (i < rrd.stat_head->ds_cnt) {
142         rrd_set_error ("sprintf_alloc failed");
143         for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
144             if ((*ret_ds_names)[i] != NULL)
145             {
146                 free ((*ret_ds_names)[i]);
147                 (*ret_ds_names)[i] = NULL;
148             }
149             if ((*ret_last_ds)[i] != NULL)
150             {
151                 free ((*ret_last_ds)[i]);
152                 (*ret_last_ds)[i] = NULL;
153             }
154         }
155         free (*ret_ds_names);
156         *ret_ds_names = NULL;
157         free (*ret_last_ds);
158         *ret_last_ds = NULL;
159         rrd_close (rrd_file);
160         rrd_free (&rrd);
161         return (-1);
162     }
163
164     rrd_free(&rrd);
165     rrd_close(rrd_file);
166     return (0);
167 } /* int rrd_lastupdate_r */