2 * collectd - src/protocols.c
3 * Copyright (C) 2009,2010 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;
56 status = parse_value (str_value, values, DS_TYPE_DERIVE);
59 ERROR ("protocols plugin: Parsing string as integer failed: %s",
66 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
67 sstrncpy (vl.plugin, "protocols", sizeof (vl.plugin));
68 sstrncpy (vl.plugin_instance, protocol_name, sizeof (vl.plugin_instance));
69 sstrncpy (vl.type, "protocol_counter", sizeof (vl.type));
70 sstrncpy (vl.type_instance, str_key, sizeof (vl.type_instance));
72 plugin_dispatch_values (&vl);
75 static int read_file (const char *path)
78 char key_buffer[4096];
79 char value_buffer[4096];
82 char *key_fields[256];
83 char *value_fields[256];
89 fh = fopen (path, "r");
92 ERROR ("protocols plugin: fopen (%s) failed: %s.",
93 path, sstrerror (errno, key_buffer, sizeof (key_buffer)));
101 key_ptr = fgets (key_buffer, sizeof (key_buffer), fh);
109 else if (ferror (fh) != 0)
111 ERROR ("protocols plugin: Reading from %s failed.", path);
116 ERROR ("protocols plugin: fgets failed for an unknown reason.");
119 } /* if (key_ptr == NULL) */
121 value_ptr = fgets (value_buffer, sizeof (value_buffer), fh);
122 if (value_ptr == NULL)
124 ERROR ("protocols plugin: read_file (%s): Could not read values line.",
129 key_ptr = strchr (key_buffer, ':');
132 ERROR ("protocols plugin: Could not find protocol name in keys line.");
138 value_ptr = strchr (value_buffer, ':');
139 if (value_ptr == NULL)
141 ERROR ("protocols plugin: Could not find protocol name "
148 if (strcmp (key_buffer, value_buffer) != 0)
150 ERROR ("protocols plugin: Protocol names in keys and values lines "
151 "don't match: `%s' vs. `%s'.",
152 key_buffer, value_buffer);
157 key_fields_num = strsplit (key_ptr,
158 key_fields, STATIC_ARRAY_SIZE (key_fields));
159 value_fields_num = strsplit (value_ptr,
160 value_fields, STATIC_ARRAY_SIZE (value_fields));
162 if (key_fields_num != value_fields_num)
164 ERROR ("protocols plugin: Number of fields in keys and values lines "
165 "don't match: %i vs %i.",
166 key_fields_num, value_fields_num);
170 for (i = 0; i < key_fields_num; i++)
172 if (values_list != NULL)
174 char match_name[2 * DATA_MAX_NAME_LEN];
176 ssnprintf (match_name, sizeof (match_name), "%s:%s",
177 key_buffer, key_fields[i]);
179 if (ignorelist_match (values_list, match_name))
181 } /* if (values_list != NULL) */
183 submit (key_buffer, key_fields[i], value_fields[i]);
184 } /* for (i = 0; i < key_fields_num; i++) */
190 } /* int read_file */
192 static int protocols_read (void)
197 status = read_file (SNMP_FILE);
201 status = read_file (NETSTAT_FILE);
209 } /* int protocols_read */
211 static int protocols_config (const char *key, const char *value)
213 if (values_list == NULL)
214 values_list = ignorelist_create (/* invert = */ 1);
216 if (strcasecmp (key, "Value") == 0)
218 ignorelist_add (values_list, value);
220 else if (strcasecmp (key, "IgnoreSelected") == 0)
225 ignorelist_set_invert (values_list, invert);
233 } /* int protocols_config */
235 void module_register (void)
237 plugin_register_config ("protocols", protocols_config,
238 config_keys, config_keys_num);
239 plugin_register_read ("protocols", protocols_read);
240 } /* void module_register */
242 /* vim: set sw=2 sts=2 et : */