ff9e7b4f833f7404356a295fe94ee510e9179d63
[rrdtool.git] / src / rrd_last.c
1 /*****************************************************************************
2  * RRDtool 1.4.0  Copyright by Tobi Oetiker, 1997-2009
3  *****************************************************************************
4  * rrd_last.c
5  *****************************************************************************
6  * Initial version by Russ Wright, @Home Network, 9/28/98
7  *****************************************************************************/
8
9 #include "rrd_tool.h"
10 #include "rrd_client.h"
11
12 time_t rrd_last(
13     int argc,
14     char **argv)
15 {
16     char *opt_daemon = NULL;
17     int status;
18
19     optind = 0;
20     opterr = 0;         /* initialize getopt */
21
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         };
29
30         opt = getopt_long(argc, argv, "d:", long_options, &option_index);
31
32         if (opt == EOF)
33             break;
34
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;
46
47         default:
48             rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
49                     argv[0]);
50             return (-1);
51             break;
52         }
53     }                   /* while (42) */
54
55     if ((argc - optind) != 1) {
56         rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
57                 argv[0]);
58         return (-1);
59     }
60
61     status = rrdc_flush_if_daemon(opt_daemon, argv[optind]);
62     if (opt_daemon) free(opt_daemon);
63     if (status) return (-1);
64
65     return (rrd_last_r (argv[optind]));
66 }
67
68 time_t rrd_last_r(
69     const char *filename)
70 {
71     time_t    lastup = -1;
72     rrd_file_t *rrd_file;
73
74     rrd_t     rrd;
75
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 }