1 /*****************************************************************************
2  * RRDtool 1.7.2 Copyright by Tobi Oetiker
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 
rrd_last(int argc,char ** argv)12 time_t rrd_last(
13     int argc,
14     char **argv)
15 {
16     char *opt_daemon = NULL;
17     time_t lastupdate;
18     struct optparse_long longopts[] = {
19         {"daemon", 'd', OPTPARSE_REQUIRED},
20         {0},
21     };
22     struct optparse options;
23     int opt;
24 
25     optparse_init(&options, argc, argv);
26     while ((opt = optparse_long(&options, longopts, NULL)) != -1) {
27         switch (opt) {
28         case 'd':
29             if (opt_daemon != NULL) {
30                     free (opt_daemon);
31             }
32             opt_daemon = strdup(options.optarg);
33             if (opt_daemon == NULL)
34             {
35                 rrd_set_error ("strdup failed.");
36                 return (-1);
37             }
38             break;
39 
40         case '?':
41             rrd_set_error("%s", options.errmsg);
42             if (opt_daemon != NULL) {
43             	free (opt_daemon);
44             }
45             return -1;
46         }
47     }                   /* while (opt) */
48 
49     if ((options.argc - options.optind) != 1) {
50         rrd_set_error ("Usage: rrdtool %s [--daemon|-d <addr>] <file>",
51                 options.argv[0]);
52         if (opt_daemon != NULL) {
53             free (opt_daemon);
54         }
55         return -1;
56     }
57 
58     rrdc_connect (opt_daemon);
59     if (rrdc_is_connected (opt_daemon))
60         lastupdate = rrdc_last(options.argv[options.optind]);
61 
62     else
63         lastupdate = rrd_last_r(options.argv[options.optind]);
64 
65     if (opt_daemon != NULL) {
66     	free(opt_daemon);
67     }
68     return (lastupdate);
69 }
70 
rrd_last_r(const char * filename)71 time_t rrd_last_r(
72     const char *filename)
73 {
74     time_t    lastup = -1;
75     rrd_file_t *rrd_file;
76 
77     rrd_t     rrd;
78 
79     rrd_init(&rrd);
80     rrd_file = rrd_open(filename, &rrd, RRD_READONLY | RRD_LOCK);
81     if (rrd_file != NULL) {
82         lastup = rrd.live_head->last_up;
83         rrd_close(rrd_file);
84     }
85     rrd_free(&rrd);
86     return (lastup);
87 }
88