c214d07459cdd304cbdf7a4b34016684402e7dd2
[collectd.git] / src / utils_cmd_flush.c
1 /**
2  * collectd - src/utils_cmd_flush.c
3  * Copyright (C) 2008  Sebastian Harl
4  * Copyright (C) 2008  Florian Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Sebastian "tokkee" Harl <sh at tokkee.org>
21  *   Florian "octo" Forster <octo at verplant.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 struct flush_info_s
29 {
30         char **plugins;
31         int plugins_num;
32         int timeout;
33 };
34 typedef struct flush_info_s flush_info_t;
35
36 static int parse_option_plugin (flush_info_t *fi, const char *option)
37 {
38         char **temp;
39
40         temp = (char **) realloc (fi->plugins,
41                         (fi->plugins_num + 1) * sizeof (char *));
42         if (temp == NULL)
43         {
44                 ERROR ("utils_cmd_flush: parse_option_plugin: realloc failed.");
45                 return (-1);
46         }
47         fi->plugins = temp;
48
49         fi->plugins[fi->plugins_num] = strdup (option + strlen ("plugin="));
50         if (fi->plugins[fi->plugins_num] == NULL)
51         {
52                 /* fi->plugins is freed in handle_flush in this case */
53                 ERROR ("utils_cmd_flush: parse_option_plugin: strdup failed.");
54                 return (-1);
55         }
56         fi->plugins_num++;
57
58         return (0);
59 } /* int parse_option_plugin */
60
61 static int parse_option_timeout (flush_info_t *fi, const char *option)
62 {
63         const char *value_ptr = option + strlen ("timeout=");
64         char *endptr = NULL;
65         int timeout;
66
67         timeout = strtol (value_ptr, &endptr, 0);
68         if (value_ptr == endptr)
69                 return (-1);
70
71         fi->timeout = (timeout <= 0) ? (-1) : timeout;
72
73         return (0);
74 } /* int parse_option_timeout */
75
76 static int parse_option (flush_info_t *fi, const char *option)
77 {
78         if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0)
79                 return (parse_option_plugin (fi, option));
80         else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0)
81                 return (parse_option_timeout (fi, option));
82         else
83                 return (-1);
84 } /* int parse_option */
85
86 int handle_flush (FILE *fh, char **fields, int fields_num)
87 {
88         flush_info_t fi;
89         int status;
90         int i;
91
92         memset (&fi, '\0', sizeof (fi));
93         fi.timeout = -1;
94
95         for (i = 1; i < fields_num; i++)
96         {
97                 status = parse_option (&fi, fields[i]);
98                 if (status != 0)
99                 {
100                         fprintf (fh, "-1 Cannot parse option %s\n", fields[i]);
101                         fflush (fh);
102                         return (-1);
103                 }
104         }
105
106         if (fi.plugins_num > 0)
107         {
108                 int success = 0;
109                 for (i = 0; i < fi.plugins_num; i++)
110                 {
111                         status = plugin_flush_one (fi.timeout, fi.plugins[i]);
112                         if (status == 0)
113                                 success++;
114                 }
115                 fprintf (fh, "0 Done: %i successful, %i errors\n",
116                                 success, fi.plugins_num - success);
117         }
118         else
119         {
120                 plugin_flush_all (fi.timeout);
121                 fprintf (fh, "0 Done\n");
122         }
123         fflush (fh);
124
125         for (i = 0; i < fi.plugins_num; i++)
126         {
127                 sfree (fi.plugins[i]);
128         }
129         sfree (fi.plugins);
130
131         return (0);
132 } /* int handle_flush */
133
134 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
135