1 /*****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 *****************************************************************************
4 * rrd_last.c
5 *****************************************************************************
6 * Initial version by Russ Wright, @Home Network, 9/28/98
7 *****************************************************************************/
9 #include "rrd_tool.h"
10 #include "rrd_client.h"
12 time_t rrd_last(
13 int argc,
14 char **argv)
15 {
16 char *opt_daemon = NULL;
17 int status;
18 time_t lastupdate;
19 int flushfirst = 1;
21 optind = 0;
22 opterr = 0; /* initialize getopt */
24 while (42) {
25 int opt;
26 int option_index = 0;
27 static struct option long_options[] = {
28 {"daemon", required_argument, 0, 'd'},
29 {"noflush", no_argument, 0, 'F'},
30 {0, 0, 0, 0}
31 };
33 opt = getopt_long(argc, argv, "d:F", long_options, &option_index);
35 if (opt == EOF)
36 break;
38 switch (opt) {
39 case 'd':
40 if (opt_daemon != NULL)
41 free (opt_daemon);
42 opt_daemon = strdup (optarg);
43 if (opt_daemon == NULL)
44 {
45 rrd_set_error ("strdup failed.");
46 return (-1);
47 }
48 break;
50 case 'F':
51 flushfirst = 0;
52 break;
54 default:
55 rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
56 argv[0]);
57 return (-1);
58 break;
59 }
60 } /* while (42) */
62 if ((argc - optind) != 1) {
63 rrd_set_error ("Usage: rrdtool %s [--daemon <addr> [--noflush]] <file>",
64 argv[0]);
65 return (-1);
66 }
68 if(flushfirst) {
69 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
70 if (status) return (-1);
71 }
73 rrdc_connect (opt_daemon);
74 if (rrdc_is_connected (opt_daemon))
75 lastupdate = rrdc_last (argv[optind]);
77 else
78 lastupdate = rrd_last_r(argv[optind]);
80 return (lastupdate);
81 }
83 time_t rrd_last_r(
84 const char *filename)
85 {
86 time_t lastup = -1;
87 rrd_file_t *rrd_file;
89 rrd_t rrd;
91 rrd_init(&rrd);
92 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
93 if (rrd_file != NULL) {
94 lastup = rrd.live_head->last_up;
95 rrd_close(rrd_file);
96 }
97 rrd_free(&rrd);
98 return (lastup);
99 }