2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2008 Florian octo Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at verplant.org>
28 static char *status_file = "/etc/openvpn/openvpn-status.log";
30 static const char *config_keys[] =
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
36 /* copy-n-pasted from common.c - changed delim to "," */
37 static int openvpn_strsplit (char *string, char **fields, size_t size)
46 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
56 } /* int openvpn_strsplit */
58 static void openvpn_submit (char *name, counter_t rx, counter_t tx)
61 value_list_t vl = VALUE_LIST_INIT;
63 values[0].counter = rx;
64 values[1].counter = tx;
67 vl.values_len = STATIC_ARRAY_SIZE (values);
68 vl.time = time (NULL);
69 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
70 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
71 sstrncpy (vl.type_instance, name, sizeof (vl.type_instance));
72 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
74 plugin_dispatch_values (&vl);
75 } /* void openvpn_submit */
77 static int openvpn_read (void)
84 const int max_fields = sizeof(fields)/sizeof(fields[0]);
86 static const char *prefix = "CLIENT_LIST,";
88 fh = fopen (status_file, "r");
92 /* status file is generated by openvpn/multi.c:multi_print_status()
93 * this plugin requires server.conf: "status-version 2"
94 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
96 while (fgets (buffer, sizeof (buffer), fh) != NULL)
98 if (strncmp(buffer, prefix, strlen(prefix)) != 0)
103 fields_num = openvpn_strsplit (buffer, fields, max_fields);
104 if (fields_num != max_fields)
108 name = fields[1]; /* "Common Name" */
109 rx = atoll (fields[4]); /* "Bytes Received */
110 tx = atoll (fields[5]); /* "Bytes Sent" */
111 openvpn_submit (name, rx, tx);
116 } /* int openvpn_read */
118 static int openvpn_config (const char *key, const char *value)
120 if (strcasecmp ("StatusFile", key) == 0)
122 status_file = strdup(value);
129 } /* int openvpn_config */
131 void module_register (void)
133 plugin_register_config ("openvpn", openvpn_config,
134 config_keys, config_keys_num);
135 plugin_register_read ("openvpn", openvpn_read);
136 } /* void module_register */