1 /**
2 * collectd - src/protocols.c
3 * Copyright (C) 2009,2010 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
27 #if !KERNEL_LINUX
28 # error "No applicable input method."
29 #endif
31 #define SNMP_FILE "/proc/net/snmp"
32 #define NETSTAT_FILE "/proc/net/netstat"
34 /*
35 * Global variables
36 */
37 static const char *config_keys[] =
38 {
39 "Value",
40 "IgnoreSelected",
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44 static ignorelist_t *values_list = NULL;
46 /*
47 * Functions
48 */
49 static void submit (const char *protocol_name,
50 const char *str_key, const char *str_value)
51 {
52 value_t values[1];
53 value_list_t vl = VALUE_LIST_INIT;
54 int status;
56 status = parse_value (str_value, values, DS_TYPE_DERIVE);
57 if (status != 0)
58 {
59 ERROR ("protocols plugin: Parsing string as integer failed: %s",
60 str_value);
61 return;
62 }
64 vl.values = values;
65 vl.values_len = 1;
66 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
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);
73 } /* void submit */
75 static int read_file (const char *path)
76 {
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;
89 fh = fopen (path, "r");
90 if (fh == NULL)
91 {
92 ERROR ("protocols plugin: fopen (%s) failed: %s.",
93 path, sstrerror (errno, key_buffer, sizeof (key_buffer)));
94 return (-1);
95 }
97 status = -1;
98 while (42)
99 {
100 clearerr (fh);
101 key_ptr = fgets (key_buffer, sizeof (key_buffer), fh);
102 if (key_ptr == NULL)
103 {
104 if (feof (fh) != 0)
105 {
106 status = 0;
107 break;
108 }
109 else if (ferror (fh) != 0)
110 {
111 ERROR ("protocols plugin: Reading from %s failed.", path);
112 break;
113 }
114 else
115 {
116 ERROR ("protocols plugin: fgets failed for an unknown reason.");
117 break;
118 }
119 } /* if (key_ptr == NULL) */
121 value_ptr = fgets (value_buffer, sizeof (value_buffer), fh);
122 if (value_ptr == NULL)
123 {
124 ERROR ("protocols plugin: read_file (%s): Could not read values line.",
125 path);
126 break;
127 }
129 key_ptr = strchr (key_buffer, ':');
130 if (key_ptr == NULL)
131 {
132 ERROR ("protocols plugin: Could not find protocol name in keys line.");
133 break;
134 }
135 *key_ptr = 0;
136 key_ptr++;
138 value_ptr = strchr (value_buffer, ':');
139 if (value_ptr == NULL)
140 {
141 ERROR ("protocols plugin: Could not find protocol name "
142 "in values line.");
143 break;
144 }
145 *value_ptr = 0;
146 value_ptr++;
148 if (strcmp (key_buffer, value_buffer) != 0)
149 {
150 ERROR ("protocols plugin: Protocol names in keys and values lines "
151 "don't match: `%s' vs. `%s'.",
152 key_buffer, value_buffer);
153 break;
154 }
157 key_fields_num = strsplit (key_ptr,
158 key_fields, STATIC_ARRAY_SIZE (key_fields));
159 value_fields_num = strsplit (value_ptr,
160 value_fields, STATIC_ARRAY_SIZE (value_fields));
162 if (key_fields_num != value_fields_num)
163 {
164 ERROR ("protocols plugin: Number of fields in keys and values lines "
165 "don't match: %i vs %i.",
166 key_fields_num, value_fields_num);
167 break;
168 }
170 for (i = 0; i < key_fields_num; i++)
171 {
172 if (values_list != NULL)
173 {
174 char match_name[2 * DATA_MAX_NAME_LEN];
176 ssnprintf (match_name, sizeof (match_name), "%s:%s",
177 key_buffer, key_fields[i]);
179 if (ignorelist_match (values_list, match_name))
180 continue;
181 } /* if (values_list != NULL) */
183 submit (key_buffer, key_fields[i], value_fields[i]);
184 } /* for (i = 0; i < key_fields_num; i++) */
185 } /* while (42) */
187 fclose (fh);
189 return (status);
190 } /* int read_file */
192 static int protocols_read (void)
193 {
194 int status;
195 int success = 0;
197 status = read_file (SNMP_FILE);
198 if (status == 0)
199 success++;
201 status = read_file (NETSTAT_FILE);
202 if (status == 0)
203 success++;
205 if (success == 0)
206 return (-1);
208 return (0);
209 } /* int protocols_read */
211 static int protocols_config (const char *key, const char *value)
212 {
213 if (values_list == NULL)
214 values_list = ignorelist_create (/* invert = */ 1);
216 if (strcasecmp (key, "Value") == 0)
217 {
218 ignorelist_add (values_list, value);
219 }
220 else if (strcasecmp (key, "IgnoreSelected") == 0)
221 {
222 int invert = 1;
223 if (IS_TRUE (value))
224 invert = 0;
225 ignorelist_set_invert (values_list, invert);
226 }
227 else
228 {
229 return (-1);
230 }
232 return (0);
233 } /* int protocols_config */
235 void module_register (void)
236 {
237 plugin_register_config ("protocols", protocols_config,
238 config_keys, config_keys_num);
239 plugin_register_read ("protocols", protocols_read);
240 } /* void module_register */
242 /* vim: set sw=2 sts=2 et : */