2 * collectd - src/protocols.c
3 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "utils_ignorelist.h"
28 # error "No applicable input method."
31 #define SNMP_FILE "/proc/net/snmp"
32 #define NETSTAT_FILE "/proc/net/netstat"
37 static const char *config_keys[] =
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44 static ignorelist_t *values_list = NULL;
49 static void submit (const char *protocol_name,
50 const char *str_key, const char *str_value)
53 value_list_t vl = VALUE_LIST_INIT;
59 values[0].counter = (counter_t) strtoll (str_value, &tmp_ptr,
61 if ((errno != 0) || (tmp_ptr == str_value))
63 ERROR ("protocols plugin: Parsing string as integer failed: %s",
70 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
71 sstrncpy (vl.plugin, "protocols", sizeof (vl.plugin));
72 sstrncpy (vl.plugin_instance, protocol_name, sizeof (vl.plugin_instance));
73 sstrncpy (vl.type, "protocol_counter", sizeof (vl.type));
74 sstrncpy (vl.type_instance, str_key, sizeof (vl.type_instance));
76 plugin_dispatch_values (&vl);
79 static int read_file (const char *path)
82 char key_buffer[4096];
83 char value_buffer[4096];
86 char *key_fields[256];
87 char *value_fields[256];
93 fh = fopen (path, "r");
96 ERROR ("protocols plugin: fopen (%s) failed: %s.",
97 path, sstrerror (errno, key_buffer, sizeof (key_buffer)));
105 key_ptr = fgets (key_buffer, sizeof (key_buffer), fh);
113 else if (ferror (fh) != 0)
115 ERROR ("protocols plugin: Reading from %s failed.", path);
120 ERROR ("protocols plugin: fgets failed for an unknown reason.");
123 } /* if (key_ptr == NULL) */
125 value_ptr = fgets (value_buffer, sizeof (value_buffer), fh);
126 if (value_ptr == NULL)
128 ERROR ("protocols plugin: read_file (%s): Could not read values line.",
133 key_ptr = strchr (key_buffer, ':');
136 ERROR ("protocols plugin: Could not find protocol name in keys line.");
142 value_ptr = strchr (value_buffer, ':');
143 if (value_ptr == NULL)
145 ERROR ("protocols plugin: Could not find protocol name "
152 if (strcmp (key_buffer, value_buffer) != 0)
154 ERROR ("protocols plugin: Protocol names in keys and values lines "
155 "don't match: `%s' vs. `%s'.",
156 key_buffer, value_buffer);
161 key_fields_num = strsplit (key_ptr,
162 key_fields, STATIC_ARRAY_SIZE (key_fields));
163 value_fields_num = strsplit (value_ptr,
164 value_fields, STATIC_ARRAY_SIZE (value_fields));
166 if (key_fields_num != value_fields_num)
168 ERROR ("protocols plugin: Number of fields in keys and values lines "
169 "dont match: %i vs %i.",
170 key_fields_num, value_fields_num);
174 for (i = 0; i < key_fields_num; i++)
176 if (values_list != NULL)
178 char match_name[2 * DATA_MAX_NAME_LEN];
180 ssnprintf (match_name, sizeof (match_name), "%s:%s",
181 key_buffer, key_fields[i]);
183 if (ignorelist_match (values_list, match_name))
185 } /* if (values_list != NULL) */
187 submit (key_buffer, key_fields[i], value_fields[i]);
188 } /* for (i = 0; i < key_fields_num; i++) */
194 } /* int read_file */
196 static int protocols_read (void)
201 status = read_file (SNMP_FILE);
205 status = read_file (NETSTAT_FILE);
213 } /* int protocols_read */
215 static int protocols_config (const char *key, const char *value)
217 if (values_list == NULL)
218 values_list = ignorelist_create (/* invert = */ 1);
220 if (strcasecmp (key, "Value") == 0)
222 ignorelist_add (values_list, value);
224 else if (strcasecmp (key, "IgnoreSelected") == 0)
227 if ((strcasecmp (value, "True") == 0)
228 || (strcasecmp (value, "Yes") == 0)
229 || (strcasecmp (value, "On") == 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 : */