openvpn plugin: Some small cleanups.
[collectd.git] / src / openvpn.c
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  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #define DEFAULT_STATUS_FILE "/etc/openvpn/openvpn-status.log"
29 #define CLIENT_LIST_PREFIX  "CLIENT_LIST,"
30
31 static char *status_file = NULL;
32
33 static const char *config_keys[] =
34 {
35         "StatusFile"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38
39 /* copy-n-pasted from common.c - changed delim to "," */
40 static int openvpn_strsplit (char *string, char **fields, size_t size)
41 {
42         size_t i;
43         char *ptr;
44         char *saveptr;
45
46         i = 0;
47         ptr = string;
48         saveptr = NULL;
49         while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
50         {
51                 ptr = NULL;
52                 i++;
53
54                 if (i >= size)
55                         break;
56         }
57
58         return (i);
59 } /* int openvpn_strsplit */
60
61 static void openvpn_submit (char *name, counter_t rx, counter_t tx)
62 {
63         value_t values[2];
64         value_list_t vl = VALUE_LIST_INIT;
65
66         values[0].counter = rx;
67         values[1].counter = tx;
68
69         vl.values = values;
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));
76
77         plugin_dispatch_values (&vl);
78 } /* void openvpn_submit */
79
80 static int openvpn_read (void)
81 {
82         char *name;
83         counter_t rx, tx;
84         FILE *fh;
85         char buffer[1024];
86         char *fields[10];
87         const int max_fields = STATIC_ARRAY_SIZE (fields);
88         int   fields_num;
89
90         fh = fopen ((status_file != NULL)
91                         ? status_file
92                         : DEFAULT_STATUS_FILE, "r");
93         if (fh == NULL)
94                 return (-1);
95
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
99          */
100         while (fgets (buffer, sizeof (buffer), fh) != NULL)
101         {
102                 if (strncmp (buffer, CLIENT_LIST_PREFIX,
103                                         strlen (CLIENT_LIST_PREFIX)) != 0)
104                         continue;
105
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);
109                 if (fields_num != 8)
110                         continue;
111
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);
116         }
117         fclose (fh);
118
119         return (0);
120 } /* int openvpn_read */
121
122 static int openvpn_config (const char *key, const char *value)
123 {
124         if (strcasecmp ("StatusFile", key) == 0)
125         {
126                 sfree (status_file);
127                 status_file = sstrdup (value);
128         }
129         else
130         {
131                 return (-1);
132         }
133         return (0);
134 } /* int openvpn_config */
135
136 void module_register (void)
137 {
138         plugin_register_config ("openvpn", openvpn_config,
139                                 config_keys, config_keys_num);
140         plugin_register_read ("openvpn", openvpn_read);
141 } /* void module_register */