Tree wide: Reformat with clang-format.
[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 values[1];
56   value_list_t vl = VALUE_LIST_INIT;
57   int status;
58
59   status = parse_value(str_value, values, 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 = values;
66   vl.values_len = 1;
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));
72
73   plugin_dispatch_values(&vl);
74 } /* void submit */
75
76 static int read_file(const char *path) {
77   FILE *fh;
78   char key_buffer[4096];
79   char value_buffer[4096];
80   char *key_ptr;
81   char *value_ptr;
82   char *key_fields[256];
83   char *value_fields[256];
84   int key_fields_num;
85   int value_fields_num;
86   int status;
87   int i;
88
89   fh = fopen(path, "r");
90   if (fh == NULL) {
91     ERROR("protocols plugin: fopen (%s) failed: %s.", path,
92           sstrerror(errno, key_buffer, sizeof(key_buffer)));
93     return (-1);
94   }
95
96   status = -1;
97   while (42) {
98     clearerr(fh);
99     key_ptr = fgets(key_buffer, sizeof(key_buffer), fh);
100     if (key_ptr == NULL) {
101       if (feof(fh) != 0) {
102         status = 0;
103         break;
104       } else if (ferror(fh) != 0) {
105         ERROR("protocols plugin: Reading from %s failed.", path);
106         break;
107       } else {
108         ERROR("protocols plugin: fgets failed for an unknown reason.");
109         break;
110       }
111     } /* if (key_ptr == NULL) */
112
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.",
116             path);
117       break;
118     }
119
120     key_ptr = strchr(key_buffer, ':');
121     if (key_ptr == NULL) {
122       ERROR("protocols plugin: Could not find protocol name in keys line.");
123       break;
124     }
125     *key_ptr = 0;
126     key_ptr++;
127
128     value_ptr = strchr(value_buffer, ':');
129     if (value_ptr == NULL) {
130       ERROR("protocols plugin: Could not find protocol name "
131             "in values line.");
132       break;
133     }
134     *value_ptr = 0;
135     value_ptr++;
136
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);
141       break;
142     }
143
144     key_fields_num =
145         strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields));
146     value_fields_num =
147         strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields));
148
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);
153       break;
154     }
155
156     for (i = 0; i < key_fields_num; i++) {
157       if (values_list != NULL) {
158         char match_name[2 * DATA_MAX_NAME_LEN];
159
160         ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer,
161                   key_fields[i]);
162
163         if (ignorelist_match(values_list, match_name))
164           continue;
165       } /* if (values_list != NULL) */
166
167       submit(key_buffer, key_fields[i], value_fields[i]);
168     } /* for (i = 0; i < key_fields_num; i++) */
169   }   /* while (42) */
170
171   fclose(fh);
172
173   return (status);
174 } /* int read_file */
175
176 static int protocols_read(void) {
177   int status;
178   int success = 0;
179
180   status = read_file(SNMP_FILE);
181   if (status == 0)
182     success++;
183
184   status = read_file(NETSTAT_FILE);
185   if (status == 0)
186     success++;
187
188   if (success == 0)
189     return (-1);
190
191   return (0);
192 } /* int protocols_read */
193
194 static int protocols_config(const char *key, const char *value) {
195   if (values_list == NULL)
196     values_list = ignorelist_create(/* invert = */ 1);
197
198   if (strcasecmp(key, "Value") == 0) {
199     ignorelist_add(values_list, value);
200   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
201     int invert = 1;
202     if (IS_TRUE(value))
203       invert = 0;
204     ignorelist_set_invert(values_list, invert);
205   } else {
206     return (-1);
207   }
208
209   return (0);
210 } /* int protocols_config */
211
212 void module_register(void) {
213   plugin_register_config("protocols", protocols_config, config_keys,
214                          config_keys_num);
215   plugin_register_read("protocols", protocols_read);
216 } /* void module_register */
217
218 /* vim: set sw=2 sts=2 et : */