09879c1ba09c13f7fe8d53ef799108aaeb276f64
1 /*****************************************************************************
2 * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008
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;
19 optind = 0;
20 opterr = 0; /* initialize getopt */
22 while (42) {
23 int opt;
24 int option_index = 0;
25 static struct option long_options[] = {
26 {"daemon", required_argument, 0, 'd'},
27 {0, 0, 0, 0}
28 };
30 opt = getopt_long(argc, argv, "d:", long_options, &option_index);
32 if (opt == EOF)
33 break;
35 switch (opt) {
36 case 'd':
37 if (opt_daemon != NULL)
38 free (opt_daemon);
39 opt_daemon = strdup (optarg);
40 if (opt_daemon == NULL)
41 {
42 rrd_set_error ("strdup failed.");
43 return (-1);
44 }
45 break;
47 default:
48 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
49 argv[0]);
50 return (-1);
51 break;
52 }
53 } /* while (42) */
55 if ((argc - optind) != 1) {
56 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
57 argv[0]);
58 return (-1);
59 }
61 status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
62 if (opt_daemon) free(opt_daemon);
63 if (status) return (-1);
65 return (rrd_last_r (argv[optind]));
66 }
68 time_t rrd_last_r(
69 const char *filename)
70 {
71 time_t lastup = -1;
72 rrd_file_t *rrd_file;
74 rrd_t rrd;
76 rrd_init(&rrd);
77 rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
78 if (rrd_file != NULL) {
79 lastup = rrd.live_head->last_up;
80 rrd_close(rrd_file);
81 }
82 rrd_free(&rrd);
83 return (lastup);
84 }