Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / protocols.c
1 /**
2  * collectd - src/protocols.c
3  * Copyright (C) 2009,2010  Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_ignorelist.h"
32
33 #if !KERNEL_LINUX
34 #error "No applicable input method."
35 #endif
36
37 #define SNMP_FILE "/proc/net/snmp"
38 #define NETSTAT_FILE "/proc/net/netstat"
39
40 /*
41  * Global variables
42  */
43 static const char *config_keys[] = {
44     "Value", "IgnoreSelected",
45 };
46 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
47
48 static ignorelist_t *values_list = NULL;
49
50 /*
51  * Functions
52  */
53 static void submit(const char *protocol_name, const char *str_key,
54                    const char *str_value) {
55   value_t value;
56   value_list_t vl = VALUE_LIST_INIT;
57   int status;
58
59   status = parse_value(str_value, &value, DS_TYPE_DERIVE);
60   if (status != 0) {
61     ERROR("protocols plugin: Parsing string as integer failed: %s", str_value);
62     return;
63   }
64
65   vl.values = &value;
66   vl.values_len = 1;
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));
71
72   plugin_dispatch_values(&vl);
73 } /* void submit */
74
75 static int read_file(const char *path) {
76   FILE *fh;
77   char key_buffer[4096];
78   char value_buffer[4096];
79   char *key_ptr;
80   char *value_ptr;
81   char *key_fields[256];
82   char *value_fields[256];
83   int key_fields_num;
84   int value_fields_num;
85   int status;
86   int i;
87
88   fh = fopen(path, "r");
89   if (fh == NULL) {
90     ERROR("protocols plugin: fopen (%s) failed: %s.", path,
91           sstrerror(errno, key_buffer, sizeof(key_buffer)));
92     return -1;
93   }
94
95   status = -1;
96   while (42) {
97     clearerr(fh);
98     key_ptr = fgets(key_buffer, sizeof(key_buffer), fh);
99     if (key_ptr == NULL) {
100       if (feof(fh) != 0) {
101         status = 0;
102         break;
103       } else if (ferror(fh) != 0) {
104         ERROR("protocols plugin: Reading from %s failed.", path);
105         break;
106       } else {
107         ERROR("protocols plugin: fgets failed for an unknown reason.");
108         break;
109       }
110     } /* if (key_ptr == NULL) */
111
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.",
115             path);
116       break;
117     }
118
119     key_ptr = strchr(key_buffer, ':');
120     if (key_ptr == NULL) {
121       ERROR("protocols plugin: Could not find protocol name in keys line.");
122       break;
123     }
124     *key_ptr = 0;
125     key_ptr++;
126
127     value_ptr = strchr(value_buffer, ':');
128     if (value_ptr == NULL) {
129       ERROR("protocols plugin: Could not find protocol name "
130             "in values line.");
131       break;
132     }
133     *value_ptr = 0;
134     value_ptr++;
135
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);
140       break;
141     }
142
143     key_fields_num =
144         strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields));
145     value_fields_num =
146         strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields));
147
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);
152       break;
153     }
154
155     for (i = 0; i < key_fields_num; i++) {
156       if (values_list != NULL) {
157         char match_name[2 * DATA_MAX_NAME_LEN];
158
159         snprintf(match_name, sizeof(match_name), "%s:%s", key_buffer,
160                  key_fields[i]);
161
162         if (ignorelist_match(values_list, match_name))
163           continue;
164       } /* if (values_list != NULL) */
165
166       submit(key_buffer, key_fields[i], value_fields[i]);
167     } /* for (i = 0; i < key_fields_num; i++) */
168   }   /* while (42) */
169
170   fclose(fh);
171
172   return status;
173 } /* int read_file */
174
175 static int protocols_read(void) {
176   int status;
177   int success = 0;
178
179   status = read_file(SNMP_FILE);
180   if (status == 0)
181     success++;
182
183   status = read_file(NETSTAT_FILE);
184   if (status == 0)
185     success++;
186
187   if (success == 0)
188     return -1;
189
190   return 0;
191 } /* int protocols_read */
192
193 static int protocols_config(const char *key, const char *value) {
194   if (values_list == NULL)
195     values_list = ignorelist_create(/* invert = */ 1);
196
197   if (strcasecmp(key, "Value") == 0) {
198     ignorelist_add(values_list, value);
199   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
200     int invert = 1;
201     if (IS_TRUE(value))
202       invert = 0;
203     ignorelist_set_invert(values_list, invert);
204   } else {
205     return -1;
206   }
207
208   return 0;
209 } /* int protocols_config */
210
211 void module_register(void) {
212   plugin_register_config("protocols", protocols_config, config_keys,
213                          config_keys_num);
214   plugin_register_read("protocols", protocols_read);
215 } /* void module_register */