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, values, DS_TYPE_DERIVE);
61 ERROR("protocols plugin: Parsing string as integer failed: %s", str_value);
67 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
68 sstrncpy(vl.plugin, "protocols", sizeof(vl.plugin));
69 sstrncpy(vl.plugin_instance, protocol_name, sizeof(vl.plugin_instance));
70 sstrncpy(vl.type, "protocol_counter", sizeof(vl.type));
71 sstrncpy(vl.type_instance, str_key, sizeof(vl.type_instance));
73 plugin_dispatch_values(&vl);
76 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");
91 ERROR("protocols plugin: fopen (%s) failed: %s.", path,
92 sstrerror(errno, key_buffer, sizeof(key_buffer)));
99 key_ptr = fgets(key_buffer, sizeof(key_buffer), fh);
100 if (key_ptr == NULL) {
104 } else if (ferror(fh) != 0) {
105 ERROR("protocols plugin: Reading from %s failed.", path);
108 ERROR("protocols plugin: fgets failed for an unknown reason.");
111 } /* if (key_ptr == NULL) */
113 value_ptr = fgets(value_buffer, sizeof(value_buffer), fh);
114 if (value_ptr == NULL) {
115 ERROR("protocols plugin: read_file (%s): Could not read values line.",
120 key_ptr = strchr(key_buffer, ':');
121 if (key_ptr == NULL) {
122 ERROR("protocols plugin: Could not find protocol name in keys line.");
128 value_ptr = strchr(value_buffer, ':');
129 if (value_ptr == NULL) {
130 ERROR("protocols plugin: Could not find protocol name "
137 if (strcmp(key_buffer, value_buffer) != 0) {
138 ERROR("protocols plugin: Protocol names in keys and values lines "
139 "don't match: `%s' vs. `%s'.",
140 key_buffer, value_buffer);
145 strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields));
147 strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields));
149 if (key_fields_num != value_fields_num) {
150 ERROR("protocols plugin: Number of fields in keys and values lines "
151 "don't match: %i vs %i.",
152 key_fields_num, value_fields_num);
156 for (i = 0; i < key_fields_num; i++) {
157 if (values_list != NULL) {
158 char match_name[2 * DATA_MAX_NAME_LEN];
160 ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer,
163 if (ignorelist_match(values_list, match_name))
165 } /* if (values_list != NULL) */
167 submit(key_buffer, key_fields[i], value_fields[i]);
168 } /* for (i = 0; i < key_fields_num; i++) */
174 } /* int read_file */
176 static int protocols_read(void) {
180 status = read_file(SNMP_FILE);
184 status = read_file(NETSTAT_FILE);
192 } /* int protocols_read */
194 static int protocols_config(const char *key, const char *value) {
195 if (values_list == NULL)
196 values_list = ignorelist_create(/* invert = */ 1);
198 if (strcasecmp(key, "Value") == 0) {
199 ignorelist_add(values_list, value);
200 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
204 ignorelist_set_invert(values_list, invert);
210 } /* int protocols_config */
212 void module_register(void) {
213 plugin_register_config("protocols", protocols_config, config_keys,
215 plugin_register_read("protocols", protocols_read);
216 } /* void module_register */
218 /* vim: set sw=2 sts=2 et : */