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 #define DEFAULT_STATUS_FILE "/etc/openvpn/openvpn-status.log"
29 #define CLIENT_LIST_PREFIX "CLIENT_LIST,"
31 static char *status_file = NULL;
33 static const char *config_keys[] =
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 /* copy-n-pasted from common.c - changed delim to "," */
40 static int openvpn_strsplit (char *string, char **fields, size_t size)
49 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
59 } /* int openvpn_strsplit */
61 static void openvpn_submit (char *name, counter_t rx, counter_t tx)
64 value_list_t vl = VALUE_LIST_INIT;
66 values[0].counter = rx;
67 values[1].counter = tx;
70 vl.values_len = STATIC_ARRAY_SIZE (values);
71 vl.time = time (NULL);
72 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
73 sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
74 sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
75 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
77 plugin_dispatch_values (&vl);
78 } /* void openvpn_submit */
80 static int openvpn_read (void)
87 const int max_fields = STATIC_ARRAY_SIZE (fields);
90 fh = fopen ((status_file != NULL)
92 : DEFAULT_STATUS_FILE, "r");
96 /* status file is generated by openvpn/multi.c:multi_print_status()
97 * this plugin requires server.conf: "status-version 2"
98 * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
100 while (fgets (buffer, sizeof (buffer), fh) != NULL)
102 if (strncmp (buffer, CLIENT_LIST_PREFIX,
103 strlen (CLIENT_LIST_PREFIX)) != 0)
106 /* The line we're expecting has 8 fields. We ignore all lines
107 * with more or less fields. */
108 fields_num = openvpn_strsplit (buffer, fields, max_fields);
112 name = fields[1]; /* "Common Name" */
113 rx = atoll (fields[4]); /* "Bytes Received */
114 tx = atoll (fields[5]); /* "Bytes Sent" */
115 openvpn_submit (name, rx, tx);
120 } /* int openvpn_read */
122 static int openvpn_config (const char *key, const char *value)
124 if (strcasecmp ("StatusFile", key) == 0)
127 status_file = sstrdup (value);
134 } /* int openvpn_config */
136 void module_register (void)
138 plugin_register_config ("openvpn", openvpn_config,
139 config_keys, config_keys_num);
140 plugin_register_read ("openvpn", openvpn_read);
141 } /* void module_register */