Merge branch 'collectd-4.3' into collectd-4.4
[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 int handle_flush (FILE *fh, char **fields, int fields_num)
29 {
30         int success = 0;
31         int error   = 0;
32
33         int timeout = -1;
34
35         int i;
36
37         for (i = 1; i < fields_num; i++)
38         {
39                 char *option = fields[i];
40                 int   status = 0;
41
42                 if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0)
43                 {
44                         char *plugin = option + strlen ("plugin=");
45
46                         if (0 == plugin_flush_one (timeout, plugin))
47                                 ++success;
48                         else
49                                 ++error;
50                 }
51                 else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0)
52                 {
53                         char *endptr = NULL;
54                         char *value  = option + strlen ("timeout=");
55
56                         errno = 0;
57                         timeout = strtol (value, &endptr, 0);
58
59                         if ((endptr == value) || (0 != errno))
60                                 status = -1;
61                         else if (0 >= timeout)
62                                 timeout = -1;
63                 }
64                 else
65                         status = -1;
66
67                 if (status != 0)
68                 {
69                         fprintf (fh, "-1 Cannot parse option %s\n", option);
70                         fflush (fh);
71                         return (-1);
72                 }
73         }
74
75         if ((success + error) > 0)
76         {
77                 fprintf (fh, "0 Done: %i successful, %i errors\n", success, error);
78         }
79         else
80         {
81                 plugin_flush_all (timeout);
82                 fprintf (fh, "0 Done\n");
83         }
84         fflush (fh);
85
86         return (0);
87 } /* int handle_flush */
88
89 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
90