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[] = {
44 "Value", "IgnoreSelected",
46 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
48 static ignorelist_t *values_list = NULL;
53 static void submit(const char *protocol_name, const char *str_key,
54 const char *str_value) {
56 value_list_t vl = VALUE_LIST_INIT;
59 status = parse_value(str_value, &value, DS_TYPE_DERIVE);
61 ERROR("protocols plugin: Parsing string as integer failed: %s", str_value);
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) {
77 char key_buffer[4096];
78 char value_buffer[4096];
81 char *key_fields[256];
82 char *value_fields[256];
88 fh = fopen(path, "r");
90 ERROR("protocols plugin: fopen (%s) failed: %s.", path,
91 sstrerror(errno, key_buffer, sizeof(key_buffer)));
98 key_ptr = fgets(key_buffer, sizeof(key_buffer), fh);
99 if (key_ptr == NULL) {
103 } else if (ferror(fh) != 0) {
104 ERROR("protocols plugin: Reading from %s failed.", path);
107 ERROR("protocols plugin: fgets failed for an unknown reason.");
110 } /* if (key_ptr == NULL) */
112 value_ptr = fgets(value_buffer, sizeof(value_buffer), fh);
113 if (value_ptr == NULL) {
114 ERROR("protocols plugin: read_file (%s): Could not read values line.",
119 key_ptr = strchr(key_buffer, ':');
120 if (key_ptr == NULL) {
121 ERROR("protocols plugin: Could not find protocol name in keys line.");
127 value_ptr = strchr(value_buffer, ':');
128 if (value_ptr == NULL) {
129 ERROR("protocols plugin: Could not find protocol name "
136 if (strcmp(key_buffer, value_buffer) != 0) {
137 ERROR("protocols plugin: Protocol names in keys and values lines "
138 "don't match: `%s' vs. `%s'.",
139 key_buffer, value_buffer);
144 strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields));
146 strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields));
148 if (key_fields_num != value_fields_num) {
149 ERROR("protocols plugin: Number of fields in keys and values lines "
150 "don't match: %i vs %i.",
151 key_fields_num, value_fields_num);
155 for (i = 0; i < key_fields_num; i++) {
156 if (values_list != NULL) {
157 char match_name[2 * DATA_MAX_NAME_LEN];
159 ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer,
162 if (ignorelist_match(values_list, match_name))
164 } /* if (values_list != NULL) */
166 submit(key_buffer, key_fields[i], value_fields[i]);
167 } /* for (i = 0; i < key_fields_num; i++) */
173 } /* int read_file */
175 static int protocols_read(void) {
179 status = read_file(SNMP_FILE);
183 status = read_file(NETSTAT_FILE);
191 } /* int protocols_read */
193 static int protocols_config(const char *key, const char *value) {
194 if (values_list == NULL)
195 values_list = ignorelist_create(/* invert = */ 1);
197 if (strcasecmp(key, "Value") == 0) {
198 ignorelist_add(values_list, value);
199 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
203 ignorelist_set_invert(values_list, invert);
209 } /* int protocols_config */
211 void module_register(void) {
212 plugin_register_config("protocols", protocols_config, config_keys,
214 plugin_register_read("protocols", protocols_read);
215 } /* void module_register */