93b0fb57b05f32d356d2afd6145974fd88b8dbdd
1 /**
2 * RRDTool - src/rrd_flushcached.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 **/
22 #include "rrd_tool.h"
23 #include "rrd_client.h"
25 int rrd_flushcached (int argc, char **argv)
26 {
27 char *opt_daemon = NULL;
28 int status;
29 int i;
31 /* initialize getopt */
32 optind = 0;
33 opterr = 0;
35 while (42)
36 {
37 int opt;
38 static struct option long_options[] =
39 {
40 {"daemon", required_argument, 0, 'd'},
41 {0, 0, 0, 0}
42 };
44 opt = getopt_long(argc, argv, "d:", long_options, NULL);
46 if (opt == -1)
47 break;
49 switch (opt)
50 {
51 case 'd':
52 if (opt_daemon != NULL)
53 free (opt_daemon);
54 opt_daemon = strdup (optarg);
55 if (opt_daemon == NULL)
56 {
57 rrd_set_error ("strdup failed.");
58 return (-1);
59 }
60 break;
62 default:
63 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file>",
64 argv[0]);
65 return (-1);
66 }
67 } /* while (42) */
69 if ((argc - optind) < 1)
70 {
71 rrd_set_error ("Usage: rrdtool %s [--daemon <addr>] <file> [<file> ...]", argv[0]);
72 return (-1);
73 }
75 /* try to connect to rrdcached */
76 status = rrdc_connect(opt_daemon);
77 if (status != 0) goto out;
79 if (! rrdc_is_connected(opt_daemon))
80 {
81 rrd_set_error ("Daemon address unknown. Please use the \"--daemon\" "
82 "option to set an address on the command line or set the "
83 "\"%s\" environment variable.",
84 ENV_RRDCACHED_ADDRESS);
85 status = -1;
86 goto out;
87 }
89 status = 0;
90 for (i = optind; i < argc; i++)
91 {
92 status = rrdc_flush(argv[i]);
93 if (status)
94 {
95 char *error;
96 int remaining;
98 error = strdup(rrd_get_error());
99 remaining = argc - optind - 1;
101 rrd_set_error("Flushing of file \"%s\" failed: %s. Skipping "
102 "remaining %i file%s.", argv[i],
103 ((! error) || (*error == '\0')) ? "unknown error" : error,
104 remaining, (remaining == 1) ? "" : "s");
105 free(error);
106 break;
107 }
108 }
110 out:
111 if (opt_daemon) free(opt_daemon);
113 return status;
114 } /* int rrd_flush */
116 /*
117 * vim: set sw=4 sts=4 et fdm=marker :
118 */