forked from collectd/collectd
/
utils_cmd_flush.c
177 lines (150 loc) · 4 KB
/
utils_cmd_flush.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* collectd - src/utils_cmd_flush.c
* Copyright (C) 2008 Sebastian Harl
* Copyright (C) 2008 Florian Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License is applicable.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Sebastian "tokkee" Harl <sh at tokkee.org>
* Florian "octo" Forster <octo at verplant.org>
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#include "utils_parse_option.h"
#define print_to_socket(fh, ...) \
if (fprintf (fh, __VA_ARGS__) < 0) { \
char errbuf[1024]; \
WARNING ("handle_flush: failed to write to socket #%i: %s", \
fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
return -1; \
}
static int add_to_array (char ***array, int *array_num, char *value)
{
char **temp;
temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
if (temp == NULL)
return (-1);
*array = temp;
(*array)[*array_num] = value;
(*array_num)++;
return (0);
} /* int add_to_array */
int handle_flush (FILE *fh, char *buffer)
{
int success = 0;
int error = 0;
int timeout = -1;
char **plugins = NULL;
int plugins_num = 0;
char **identifiers = NULL;
int identifiers_num = 0;
int i;
if ((fh == NULL) || (buffer == NULL))
return (-1);
DEBUG ("utils_cmd_flush: handle_flush (fh = %p, buffer = %s);",
(void *) fh, buffer);
if (strncasecmp ("FLUSH", buffer, strlen ("FLUSH")) != 0)
{
print_to_socket (fh, "-1 Cannot parse command.\n");
return (-1);
}
buffer += strlen ("FLUSH");
while (*buffer != 0)
{
char *opt_key;
char *opt_value;
int status;
opt_key = NULL;
opt_value = NULL;
status = parse_option (&buffer, &opt_key, &opt_value);
if (status != 0)
{
print_to_socket (fh, "-1 Parsing options failed.\n");
sfree (plugins);
sfree (identifiers);
return (-1);
}
if (strcasecmp ("plugin", opt_key) == 0)
{
add_to_array (&plugins, &plugins_num, opt_value);
}
else if (strcasecmp ("identifier", opt_key) == 0)
{
add_to_array (&identifiers, &identifiers_num, opt_value);
}
else if (strcasecmp ("timeout", opt_key) == 0)
{
char *endptr;
errno = 0;
endptr = NULL;
timeout = strtol (opt_value, &endptr, 0);
if ((endptr == opt_value) || (errno != 0))
{
print_to_socket (fh, "-1 Invalid value for option `timeout': "
"%s\n", opt_value);
sfree (plugins);
sfree (identifiers);
return (-1);
}
else if (timeout <= 0)
timeout = -1;
}
else
{
print_to_socket (fh, "-1 Cannot parse option %s\n", opt_key);
sfree (plugins);
sfree (identifiers);
return (-1);
}
} /* while (*buffer != 0) */
/* Add NULL entries for `any plugin' and/or `any value' if nothing was
* specified. */
if (plugins_num == 0)
add_to_array (&plugins, &plugins_num, NULL);
if (identifiers_num == 0)
add_to_array (&identifiers, &identifiers_num, NULL);
for (i = 0; i < plugins_num; i++)
{
char *plugin;
int j;
plugin = plugins[i];
for (j = 0; j < identifiers_num; j++)
{
char *identifier;
int status;
identifier = identifiers[j];
status = plugin_flush (plugin, timeout, identifier);
if (status == 0)
success++;
else
error++;
}
}
if ((success + error) > 0)
{
print_to_socket (fh, "0 Done: %i successful, %i errors\n",
success, error);
}
else
{
plugin_flush (NULL, timeout, NULL);
print_to_socket (fh, "0 Done\n");
}
sfree (plugins);
sfree (identifiers);
return (0);
} /* int handle_flush */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */