src/rrd_flush.c: Add the `flush' command to the `rrdtool' application.
[rrdtool.git] / src / rrd_flush.c
1 /**
2  * RRDTool - src/rrd_flush.c
3  * Copyright (C) 2008 Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "rrd_tool.h"
23 #include "rrd_client.h"
24
25 int rrd_cmd_flush (int argc, char **argv)
26 {
27     char *opt_daemon = NULL;
28     int status;
29
30     /* initialize getopt */
31     optind = 0;
32     opterr = 0;
33
34     while (42)
35     {
36         int opt;
37         static struct option long_options[] =
38         {
39             {"daemon", required_argument, 0, 'd'},
40             {0, 0, 0, 0}
41         };
42
43         opt = getopt_long(argc, argv, "d:", long_options, NULL);
44
45         if (opt == -1)
46             break;
47
48         switch (opt)
49         {
50             case 'd':
51                 if (opt_daemon != NULL)
52                     free (opt_daemon);
53                 opt_daemon = strdup (optarg);
54                 if (opt_daemon == NULL)
55                 {
56                     rrd_set_error ("strdup failed.");
57                     return (-1);
58                 }
59                 break;
60
61             default:
62                 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
63                         argv[0]);
64                 return (-1);
65         }
66     } /* while (42) */
67
68     if ((argc - optind) != 1)
69     {
70         rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>", argv[0]);
71         return (-1);
72     }
73
74     if (opt_daemon == NULL)
75     {
76         char *temp;
77
78         temp = getenv (ENV_RRDCACHED_ADDRESS);
79         if (temp != NULL)
80         {
81             opt_daemon = strdup (temp);
82             if (opt_daemon == NULL)
83             {
84                 rrd_set_error("strdup failed.");
85                 return (-1);
86             }
87         }
88     }
89
90     if (opt_daemon == NULL)
91     {
92         rrd_set_error ("Daemon address unknown. Please use the \"--daemon\" "
93                 "option to set an address on the command line or set the "
94                 "\"%s\" environment variable.",
95                 ENV_RRDCACHED_ADDRESS);
96         return (-1);
97     }
98
99     status = rrdc_connect (opt_daemon);
100     if (status != 0)
101     {
102         rrd_set_error ("rrdc_connect failed with status %i.", status);
103         return (-1);
104     }
105
106     status = rrdc_flush (argv[optind]);
107     if (status != 0)
108         rrd_set_error ("rrdc_flush (%s) failed with status %i.",
109                 argv[optind], status);
110
111     rrdc_disconnect ();
112
113     return ((status == 0) ? 0 : -1);
114 } /* int rrd_flush */
115
116 /*
117  * vim: set sw=4 sts=4 et fdm=marker :
118  */