1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
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;
103 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
104 if (rrd_file == NULL) {
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);
118 memset (*ret_ds_names, 0, rrd.stat_head->ds_cnt * sizeof(char *));
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);
129 memset (*ret_last_ds, 0, rrd.stat_head->ds_cnt * sizeof(char *));
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);
135 if (((*ret_ds_names)[i] == NULL) || ((*ret_last_ds)[i] == NULL))
139 /* Check if all names and values could be copied and free everything if
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)
146 free ((*ret_ds_names)[i]);
147 (*ret_ds_names)[i] = NULL;
149 if ((*ret_last_ds)[i] != NULL)
151 free ((*ret_last_ds)[i]);
152 (*ret_last_ds)[i] = NULL;
155 free (*ret_ds_names);
156 *ret_ds_names = NULL;
159 rrd_close (rrd_file);
167 } /* int rrd_lastupdate_r */