Add snprintf wrapper for GCC 8.2/3
[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 "plugin.h"
30 #include "utils/common/common.h"
31 #include "utils/ignorelist/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;
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     return;
62   }
63
64   vl.values = &value;
65   vl.values_len = 1;
66   sstrncpy(vl.plugin, "protocols", sizeof(vl.plugin));
67   sstrncpy(vl.plugin_instance, protocol_name, sizeof(vl.plugin_instance));
68   sstrncpy(vl.type, "protocol_counter", sizeof(vl.type));
69   sstrncpy(vl.type_instance, str_key, sizeof(vl.type_instance));
70
71   plugin_dispatch_values(&vl);
72 } /* void submit */
73
74 static int read_file(const char *path) {
75   FILE *fh;
76   char key_buffer[4096];
77   char value_buffer[4096];
78   char *key_ptr;
79   char *value_ptr;
80   char *key_fields[256];
81   char *value_fields[256];
82   int key_fields_num;
83   int value_fields_num;
84   int status;
85   int i;
86
87   fh = fopen(path, "r");
88   if (fh == NULL) {
89     ERROR("protocols plugin: fopen (%s) failed: %s.", path, STRERRNO);
90     return -1;
91   }
92
93   status = -1;
94   while (42) {
95     clearerr(fh);
96     key_ptr = fgets(key_buffer, sizeof(key_buffer), fh);
97     if (key_ptr == NULL) {
98       if (feof(fh) != 0) {
99         status = 0;
100         break;
101       } else if (ferror(fh) != 0) {
102         ERROR("protocols plugin: Reading from %s failed.", path);
103         break;
104       } else {
105         ERROR("protocols plugin: fgets failed for an unknown reason.");
106         break;
107       }
108     } /* if (key_ptr == NULL) */
109
110     value_ptr = fgets(value_buffer, sizeof(value_buffer), fh);
111     if (value_ptr == NULL) {
112       ERROR("protocols plugin: read_file (%s): Could not read values line.",
113             path);
114       break;
115     }
116
117     key_ptr = strchr(key_buffer, ':');
118     if (key_ptr == NULL) {
119       ERROR("protocols plugin: Could not find protocol name in keys line.");
120       break;
121     }
122     *key_ptr = 0;
123     key_ptr++;
124
125     value_ptr = strchr(value_buffer, ':');
126     if (value_ptr == NULL) {
127       ERROR("protocols plugin: Could not find protocol name "
128             "in values line.");
129       break;
130     }
131     *value_ptr = 0;
132     value_ptr++;
133
134     if (strcmp(key_buffer, value_buffer) != 0) {
135       ERROR("protocols plugin: Protocol names in keys and values lines "
136             "don't match: `%s' vs. `%s'.",
137             key_buffer, value_buffer);
138       break;
139     }
140
141     key_fields_num =
142         strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields));
143     value_fields_num =
144         strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields));
145
146     if (key_fields_num != value_fields_num) {
147       ERROR("protocols plugin: Number of fields in keys and values lines "
148             "don't match: %i vs %i.",
149             key_fields_num, value_fields_num);
150       break;
151     }
152
153     for (i = 0; i < key_fields_num; i++) {
154       if (values_list != NULL) {
155         char match_name[2 * DATA_MAX_NAME_LEN];
156
157         ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer,
158                  key_fields[i]);
159
160         if (ignorelist_match(values_list, match_name))
161           continue;
162       } /* if (values_list != NULL) */
163
164       submit(key_buffer, key_fields[i], value_fields[i]);
165     } /* for (i = 0; i < key_fields_num; i++) */
166   }   /* while (42) */
167
168   fclose(fh);
169
170   return status;
171 } /* int read_file */
172
173 static int protocols_read(void) {
174   int status;
175   int success = 0;
176
177   status = read_file(SNMP_FILE);
178   if (status == 0)
179     success++;
180
181   status = read_file(NETSTAT_FILE);
182   if (status == 0)
183     success++;
184
185   if (success == 0)
186     return -1;
187
188   return 0;
189 } /* int protocols_read */
190
191 static int protocols_config(const char *key, const char *value) {
192   if (values_list == NULL)
193     values_list = ignorelist_create(/* invert = */ 1);
194
195   if (strcasecmp(key, "Value") == 0) {
196     ignorelist_add(values_list, value);
197   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
198     int invert = 1;
199     if (IS_TRUE(value))
200       invert = 0;
201     ignorelist_set_invert(values_list, invert);
202   } else {
203     return -1;
204   }
205
206   return 0;
207 } /* int protocols_config */
208
209 void module_register(void) {
210   plugin_register_config("protocols", protocols_config, config_keys,
211                          config_keys_num);
212   plugin_register_read("protocols", protocols_read);
213 } /* void module_register */