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 *****************************************************************************/
9 #include "rrd_rpncalc.h"
10 #include "rrd_client.h"
13 int rrd_lastupdate (int argc, char **argv)
18 unsigned long ds_count, i;
21 char *opt_daemon = NULL;
24 opterr = 0; /* initialize getopt */
29 static struct option long_options[] = {
30 {"daemon", required_argument, 0, 'd'},
34 opt = getopt_long (argc, argv, "d:", long_options, &option_index);
41 if (opt_daemon != NULL)
43 opt_daemon = strdup (optarg);
44 if (opt_daemon == NULL)
46 rrd_set_error ("strdup failed.");
52 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
59 if ((argc - optind) != 1) {
60 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
65 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
66 if (opt_daemon) free (opt_daemon);
67 if (status) return (-1);
69 status = rrd_lastupdate_r (argv[optind],
70 &last_update, &ds_count, &ds_names, &last_ds);
74 for (i = 0; i < ds_count; i++)
75 printf(" %s", ds_names[i]);
78 printf ("%10lu:", last_update);
79 for (i = 0; i < ds_count; i++) {
80 printf(" %s", last_ds[i]);
90 } /* int rrd_lastupdate */
92 int rrd_lastupdate_r(const char *filename,
93 time_t *ret_last_update,
94 unsigned long *ret_ds_count,
100 rrd_file_t *rrd_file;
102 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
103 if (rrd_file == NULL) {
108 *ret_last_update = rrd.live_head->last_up;
109 *ret_ds_count = rrd.stat_head->ds_cnt;
110 *ret_ds_names = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
111 if (*ret_ds_names == NULL) {
112 rrd_set_error ("malloc fetch ret_ds_names array");
113 rrd_close (rrd_file);
117 memset (*ret_ds_names, 0, rrd.stat_head->ds_cnt * sizeof(char *));
119 *ret_last_ds = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
120 if (*ret_last_ds == NULL) {
121 rrd_set_error ("malloc fetch ret_last_ds array");
122 free (*ret_ds_names);
123 *ret_ds_names = NULL;
124 rrd_close (rrd_file);
128 memset (*ret_last_ds, 0, rrd.stat_head->ds_cnt * sizeof(char *));
130 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
131 (*ret_ds_names)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
132 (*ret_last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
134 if (((*ret_ds_names)[i] == NULL) || ((*ret_last_ds)[i] == NULL))
138 /* Check if all names and values could be copied and free everything if
140 if (i < rrd.stat_head->ds_cnt) {
141 rrd_set_error ("sprintf_alloc failed");
142 for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
143 if ((*ret_ds_names)[i] != NULL)
145 free ((*ret_ds_names)[i]);
146 (*ret_ds_names)[i] = NULL;
148 if ((*ret_last_ds)[i] != NULL)
150 free ((*ret_last_ds)[i]);
151 (*ret_last_ds)[i] = NULL;
154 free (*ret_ds_names);
155 *ret_ds_names = NULL;
158 rrd_close (rrd_file);
166 } /* int rrd_lastupdate_r */