tcpconns plugin: Added a new plugin that counts the number of TCP connections to...
[collectd.git] / src / tcpconns.c
1 /**
2  * collectd - src/tcpconns.c
3  * Copyright (C) 2007  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  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if !KERNEL_LINUX
27 # error "No applicable input method."
28 #endif
29
30 #define PORT_COLLECT_LOCAL  0x01
31 #define PORT_COLLECT_REMOTE 0x02
32 #define PORT_IS_LISTENING   0x04
33
34 typedef struct port_entry_s
35 {
36   uint16_t port;
37   uint16_t flags;
38   uint32_t count_local;
39   uint32_t count_remote;
40   struct port_entry_s *next;
41 } port_entry_t;
42
43 static const char *config_keys[] =
44 {
45   "ListeningPorts",
46   "LocalPort",
47   "RemotePort"
48 };
49 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
50
51 static int port_collect_listening = 0;
52 static port_entry_t *port_list_head = NULL;
53
54 static void conn_submit_port_entry (port_entry_t *pe)
55 {
56   value_t values[1];
57   value_list_t vl = VALUE_LIST_INIT;
58
59   vl.values = values;
60   vl.values_len = 1;
61   vl.time = time (NULL);
62   strcpy (vl.host, hostname_g);
63   strcpy (vl.plugin, "tcpconns");
64   snprintf (vl.type_instance, sizeof (vl.type_instance), "%hu", pe->port);
65   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
66
67   if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
68       || (pe->flags & PORT_COLLECT_LOCAL))
69   {
70     values[0].gauge = pe->count_local;
71     strcpy (vl.plugin_instance, "local");
72     plugin_dispatch_values ("tcp_connections", &vl);
73   }
74   if (pe->flags & PORT_COLLECT_REMOTE)
75   {
76     values[0].gauge = pe->count_remote;
77     strcpy (vl.plugin_instance, "remote");
78     plugin_dispatch_values ("tcp_connections", &vl);
79   }
80 } /* void conn_submit */
81
82 static void conn_submit_all (void)
83 {
84   port_entry_t *pe;
85
86   for (pe = port_list_head; pe != NULL; pe = pe->next)
87     conn_submit_port_entry (pe);
88 } /* void conn_submit_all */
89
90 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
91 {
92   port_entry_t *ret;
93
94   ret = port_list_head;
95   while (ret != NULL)
96   {
97     if (ret->port == port)
98       break;
99     ret = ret->next;
100   }
101
102   if ((ret == NULL) && (create != 0))
103   {
104     ret = (port_entry_t *) malloc (sizeof (port_entry_t));
105     if (ret == NULL)
106       return (NULL);
107     memset (ret, '\0', sizeof (port_entry_t));
108
109     ret->port = port;
110     ret->next = port_list_head;
111     port_list_head = ret;
112   }
113
114   return (ret);
115 } /* port_entry_t *conn_get_port_entry */
116
117 static void conn_reset_port_entry (void)
118 {
119   port_entry_t *prev = NULL;
120   port_entry_t *pe = port_list_head;
121
122   while (pe != NULL)
123   {
124     /* If this entry was created while reading the files (ant not when handling
125      * the configuration) remove it now. */
126     if ((pe->flags & (PORT_COLLECT_LOCAL | PORT_COLLECT_REMOTE)) == 0)
127     {
128       port_entry_t *next = pe->next;
129
130       DEBUG ("tcpconns plugin: Removing temporary entry "
131           "for listening port %hu", pe->port);
132
133       if (prev == NULL)
134         port_list_head = next;
135       else
136         prev->next = next;
137
138       sfree (pe);
139       pe = next;
140
141       continue;
142     }
143
144     pe->count_local = 0;
145     pe->count_remote = 0;
146
147     pe->flags &= ~PORT_IS_LISTENING;
148
149     pe = pe->next;
150   }
151 } /* void conn_reset_port_entry */
152
153 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
154 {
155   /* Listening sockets */
156   if ((state == 0x0a) && (port_collect_listening != 0))
157   {
158     port_entry_t *pe;
159
160     DEBUG ("tcpconns plugin: Adding listening port %hu", port_local);
161
162     pe = conn_get_port_entry (port_local, 1 /* create */);
163     if (pe != NULL)
164     {
165       pe->count_local++;
166       pe->flags |= PORT_IS_LISTENING;
167     }
168   }
169   /* Established connections */
170   else if (state == 0x01)
171   {
172     port_entry_t *pe;
173
174     DEBUG ("tcpconns plugin: Established connection %hu <-> %hu",
175         port_local, port_remote);
176     
177     pe = conn_get_port_entry (port_local, 0 /* no create */);
178     if ((pe != NULL) && (pe->flags & PORT_COLLECT_LOCAL))
179       pe->count_local++;
180
181     pe = conn_get_port_entry (port_remote, 0 /* no create */);
182     if ((pe != NULL) && (pe->flags & PORT_COLLECT_REMOTE))
183       pe->count_remote++;
184   }
185   else
186   {
187     DEBUG ("tcpconns plugin: Ignoring unknown state 0x%x", state);
188   }
189
190   return (0);
191 } /* int conn_handle_ports */
192
193 static int conn_handle_line (char *buffer)
194 {
195   char *fields[32];
196   int   fields_len;
197
198   char *endptr;
199
200   char *port_local_str;
201   char *port_remote_str;
202   uint16_t port_local;
203   uint16_t port_remote;
204
205   uint8_t state;
206
207   int buffer_len = strlen (buffer);
208
209   while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
210     buffer[--buffer_len] = '\0';
211   if (buffer_len <= 0)
212     return (-1);
213
214   fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
215   if (fields_len < 12)
216   {
217     DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
218     return (-1);
219   }
220
221   port_local_str  = strchr (fields[1], ':');
222   port_remote_str = strchr (fields[2], ':');
223
224   if ((port_local_str == NULL) || (port_remote_str == NULL))
225     return (-1);
226   port_local_str++;
227   port_remote_str++;
228   if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
229     return (-1);
230
231   endptr = NULL;
232   port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
233   if ((endptr == NULL) || (*endptr != '\0'))
234     return (-1);
235
236   endptr = NULL;
237   port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
238   if ((endptr == NULL) || (*endptr != '\0'))
239     return (-1);
240
241   endptr = NULL;
242   state = (uint8_t) strtol (fields[3], &endptr, 16);
243   if ((endptr == NULL) || (*endptr != '\0'))
244     return (-1);
245
246   return (conn_handle_ports (port_local, port_remote, state));
247 } /* int conn_handle_line */
248
249 static int conn_read_file (const char *file)
250 {
251   FILE *fh;
252   char buffer[1024];
253
254   fh = fopen (file, "r");
255   if (fh == NULL)
256   {
257     char errbuf[1024];
258     ERROR ("tcpconns plugin: fopen (%s) failed: %s",
259         file, sstrerror (errno, errbuf, sizeof (errbuf)));
260     return (-1);
261   }
262
263   while (fgets (buffer, sizeof (buffer), fh) != NULL)
264   {
265     conn_handle_line (buffer);
266   } /* while (fgets) */
267
268   fclose (fh);
269
270   return (0);
271 } /* int conn_read_file */
272
273 static int conn_config (const char *key, const char *value)
274 {
275   if (strcasecmp (key, "ListeningPorts") == 0)
276   {
277     if ((strcasecmp (value, "Yes") == 0)
278         || (strcasecmp (value, "True") == 0)
279         || (strcasecmp (value, "On") == 0))
280       port_collect_listening = 1;
281     else
282       port_collect_listening = 0;
283   }
284   else if ((strcasecmp (key, "LocalPort") == 0)
285       || (strcasecmp (key, "RemotePort") == 0))
286   {
287       port_entry_t *pe;
288       int port = atoi (value);
289
290       if ((port < 1) || (port > 65535))
291       {
292         ERROR ("tcpconns plugin: Invalid port: %i", port);
293         return (1);
294       }
295
296       pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
297       if (pe == NULL)
298       {
299         ERROR ("tcpconns plugin: conn_get_port_entry failed.");
300         return (1);
301       }
302
303       if (strcasecmp (key, "LocalPort") == 0)
304         pe->flags |= PORT_COLLECT_LOCAL;
305       else
306         pe->flags |= PORT_COLLECT_REMOTE;
307   }
308   else
309   {
310     return (-1);
311   }
312
313   return (0);
314 } /* int conn_config */
315
316 static int conn_init (void)
317 {
318   if (port_list_head == NULL)
319     port_collect_listening = 1;
320
321   return (0);
322 } /* int conn_init */
323
324 static int conn_read (void)
325 {
326   conn_reset_port_entry ();
327
328   conn_read_file ("/proc/net/tcp");
329   conn_read_file ("/proc/net/tcp6");
330
331   conn_submit_all ();
332
333   return (0);
334 } /* int conn_read */
335
336 void module_register (void)
337 {
338         plugin_register_config ("tcpconns", conn_config,
339                         config_keys, config_keys_num);
340         plugin_register_init ("tcpconns", conn_init);
341         plugin_register_read ("tcpconns", conn_read);
342 } /* void module_register */
343
344 /*
345  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
346  */