1 /**
2 * collectd - src/openvpn.c
3 * Copyright (C) 2008 Doug MacEachern
4 * Copyright (C) 2008 Florian octo Forster
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Doug MacEachern <dougm at hyperic.com>
21 * Florian octo Forster <octo at verplant.org>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 static char *status_file = "/etc/openvpn/openvpn-status.log";
30 static const char *config_keys[] =
31 {
32 "StatusFile"
33 };
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)
38 {
39 size_t i;
40 char *ptr;
41 char *saveptr;
43 i = 0;
44 ptr = string;
45 saveptr = NULL;
46 while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
47 {
48 ptr = NULL;
49 i++;
51 if (i >= size)
52 break;
53 }
55 return (i);
56 } /* int openvpn_strsplit */
58 static void openvpn_submit (char *name, counter_t rx, counter_t tx)
59 {
60 value_t values[2];
61 value_list_t vl = VALUE_LIST_INIT;
63 values[0].counter = rx;
64 values[1].counter = tx;
66 vl.values = values;
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)
78 {
79 char *name;
80 counter_t rx, tx;
81 FILE *fh;
82 char buffer[1024];
83 char *fields[8];
84 const int max_fields = sizeof(fields)/sizeof(fields[0]);
85 int fields_num;
86 static const char *prefix = "CLIENT_LIST,";
88 fh = fopen (status_file, "r");
89 if (fh == NULL)
90 return (-1);
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
95 */
96 while (fgets (buffer, sizeof (buffer), fh) != NULL)
97 {
98 if (strncmp(buffer, prefix, strlen(prefix)) != 0)
99 {
100 continue;
101 }
103 fields_num = openvpn_strsplit (buffer, fields, max_fields);
104 if (fields_num != max_fields)
105 {
106 continue;
107 }
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);
112 }
113 fclose (fh);
115 return (0);
116 } /* int openvpn_read */
118 static int openvpn_config (const char *key, const char *value)
119 {
120 if (strcasecmp ("StatusFile", key) == 0)
121 {
122 status_file = strdup(value);
123 }
124 else
125 {
126 return (-1);
127 }
128 return (0);
129 } /* int openvpn_config */
131 void module_register (void)
132 {
133 plugin_register_config ("openvpn", openvpn_config,
134 config_keys, config_keys_num);
135 plugin_register_read ("openvpn", openvpn_read);
136 } /* void module_register */