RRDcached patch. This implements an infrastructure, where rrd updates can be
[rrdtool.git] / src / rrd_last.c
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  *****************************************************************************/
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
18     optind = 0;
19     opterr = 0;         /* initialize getopt */
20
21     while (42) {
22         int       opt;
23         int       option_index = 0;
24         static struct option long_options[] = {
25             {"daemon", required_argument, 0, 'd'},
26             {0, 0, 0, 0}
27         };
28
29         opt = getopt_long(argc, argv, "d:", long_options, &option_index);
30
31         if (opt == EOF)
32             break;
33
34         switch (opt) {
35         case 'd':
36             if (opt_daemon != NULL)
37                     free (opt_daemon);
38             opt_daemon = strdup (optarg);
39             if (opt_daemon == NULL)
40             {
41                 rrd_set_error ("strdup failed.");
42                 return (-1);
43             }
44             break;
45
46         default:
47             rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
48                     argv[0]);
49             return (-1);
50             break;
51         }
52     }                   /* while (42) */
53
54     if ((argc - optind) != 1) {
55         rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
56                 argv[0]);
57         return (-1);
58     }
59
60     if (opt_daemon == NULL)
61     {
62         char *temp;
63
64         temp = getenv (ENV_RRDCACHED_ADDRESS);
65         if (temp != NULL)
66         {
67             opt_daemon = strdup (temp);
68             if (opt_daemon == NULL)
69             {
70                 rrd_set_error("strdup failed.");
71                 return (-1);
72             }
73         }
74     }
75
76     if (opt_daemon != NULL)
77     {
78         int status;
79
80         status = rrdc_connect (opt_daemon);
81         if (status != 0)
82         {
83             rrd_set_error ("rrdc_connect failed with status %i.", status);
84             return (-1);
85         }
86
87         status = rrdc_flush (argv[optind]);
88         if (status != 0)
89         {
90             rrd_set_error ("rrdc_flush (%s) failed with status %i.",
91                     argv[optind], status);
92             return (-1);
93         }
94
95         rrdc_disconnect ();
96     } /* if (opt_daemon) */
97
98     return (rrd_last_r (argv[optind]));
99 }
100
101 time_t rrd_last_r(
102     const char *filename)
103 {
104     time_t    lastup = -1;
105     rrd_file_t *rrd_file;
106
107     rrd_t     rrd;
108
109     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
110     if (rrd_file != NULL) {
111         lastup = rrd.live_head->last_up;
112         rrd_close(rrd_file);
113     }
114     rrd_free(&rrd);
115     return (lastup);
116 }