2 * collectd - src/protocols.c
3 * Copyright (C) 2009,2010 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
31 #include "utils_ignorelist.h"
34 # error "No applicable input method."
37 #define SNMP_FILE "/proc/net/snmp"
38 #define NETSTAT_FILE "/proc/net/netstat"
43 static const char *config_keys[] =
48 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
50 static ignorelist_t *values_list = NULL;
55 static void submit (const char *protocol_name,
56 const char *str_key, const char *str_value)
59 value_list_t vl = VALUE_LIST_INIT;
62 status = parse_value (str_value, &value, DS_TYPE_DERIVE);
65 ERROR ("protocols plugin: Parsing string as integer failed: %s",
72 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
73 sstrncpy (vl.plugin, "protocols", sizeof (vl.plugin));
74 sstrncpy (vl.plugin_instance, protocol_name, sizeof (vl.plugin_instance));
75 sstrncpy (vl.type, "protocol_counter", sizeof (vl.type));
76 sstrncpy (vl.type_instance, str_key, sizeof (vl.type_instance));
78 plugin_dispatch_values (&vl);
81 static int read_file (const char *path)
84 char key_buffer[4096];
85 char value_buffer[4096];
88 char *key_fields[256];
89 char *value_fields[256];
95 fh = fopen (path, "r");
98 ERROR ("protocols plugin: fopen (%s) failed: %s.",
99 path, sstrerror (errno, key_buffer, sizeof (key_buffer)));
107 key_ptr = fgets (key_buffer, sizeof (key_buffer), fh);
115 else if (ferror (fh) != 0)
117 ERROR ("protocols plugin: Reading from %s failed.", path);
122 ERROR ("protocols plugin: fgets failed for an unknown reason.");
125 } /* if (key_ptr == NULL) */
127 value_ptr = fgets (value_buffer, sizeof (value_buffer), fh);
128 if (value_ptr == NULL)
130 ERROR ("protocols plugin: read_file (%s): Could not read values line.",
135 key_ptr = strchr (key_buffer, ':');
138 ERROR ("protocols plugin: Could not find protocol name in keys line.");
144 value_ptr = strchr (value_buffer, ':');
145 if (value_ptr == NULL)
147 ERROR ("protocols plugin: Could not find protocol name "
154 if (strcmp (key_buffer, value_buffer) != 0)
156 ERROR ("protocols plugin: Protocol names in keys and values lines "
157 "don't match: `%s' vs. `%s'.",
158 key_buffer, value_buffer);
163 key_fields_num = strsplit (key_ptr,
164 key_fields, STATIC_ARRAY_SIZE (key_fields));
165 value_fields_num = strsplit (value_ptr,
166 value_fields, STATIC_ARRAY_SIZE (value_fields));
168 if (key_fields_num != value_fields_num)
170 ERROR ("protocols plugin: Number of fields in keys and values lines "
171 "don't match: %i vs %i.",
172 key_fields_num, value_fields_num);
176 for (i = 0; i < key_fields_num; i++)
178 if (values_list != NULL)
180 char match_name[2 * DATA_MAX_NAME_LEN];
182 ssnprintf (match_name, sizeof (match_name), "%s:%s",
183 key_buffer, key_fields[i]);
185 if (ignorelist_match (values_list, match_name))
187 } /* if (values_list != NULL) */
189 submit (key_buffer, key_fields[i], value_fields[i]);
190 } /* for (i = 0; i < key_fields_num; i++) */
196 } /* int read_file */
198 static int protocols_read (void)
203 status = read_file (SNMP_FILE);
207 status = read_file (NETSTAT_FILE);
215 } /* int protocols_read */
217 static int protocols_config (const char *key, const char *value)
219 if (values_list == NULL)
220 values_list = ignorelist_create (/* invert = */ 1);
222 if (strcasecmp (key, "Value") == 0)
224 ignorelist_add (values_list, value);
226 else if (strcasecmp (key, "IgnoreSelected") == 0)
231 ignorelist_set_invert (values_list, invert);
239 } /* int protocols_config */
241 void module_register (void)
243 plugin_register_config ("protocols", protocols_config,
244 config_keys, config_keys_num);
245 plugin_register_read ("protocols", protocols_read);
246 } /* void module_register */
248 /* vim: set sw=2 sts=2 et : */