-static int version_detect (const char *filename)
-{
- FILE *fh;
- char buffer[1024];
- int version = 0;
-
- /* Sanity checking. We're called from the config handling routine, so
- * better play it save. */
- if ((filename == NULL) || (*filename == 0))
- return (0);
-
- fh = fopen (filename, "r");
- if (fh == NULL)
- {
- char errbuf[1024];
- WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (0);
- }
-
- /* now search for the specific multimode data format */
- while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
- {
- /* we look at the first line searching for SINGLE mode configuration */
- if (strcmp (buffer, VSSTRING) == 0)
- {
- DEBUG ("openvpn plugin: found status file version SINGLE");
- version = SINGLE;
- break;
- }
- /* searching for multi version 1 */
- else if (strcmp (buffer, V1STRING) == 0)
- {
- DEBUG ("openvpn plugin: found status file version MULTI1");
- version = MULTI1;
- break;
- }
- /* searching for multi version 2 */
- else if (strcmp (buffer, V2STRING) == 0)
- {
- DEBUG ("openvpn plugin: found status file version MULTI2");
- version = MULTI2;
- break;
- }
- /* searching for multi version 3 */
- else if (strcmp (buffer, V3STRING) == 0)
- {
- DEBUG ("openvpn plugin: found status file version MULTI3");
- version = MULTI3;
- break;
- }
- /* searching for multi version 4 */
- else if (strcmp (buffer, V4STRING) == 0)
- {
- DEBUG ("openvpn plugin: found status file version MULTI4");
- version = MULTI4;
- break;
- }
- }
-
- if (version == 0)
- {
- /* This is only reached during configuration, so complaining to
- * the user is in order. */
- NOTICE ("openvpn plugin: %s: Unknown file format, please "
- "report this as bug. Make sure to include "
- "your status file, so the plugin can "
- "be adapted.", filename);
- }
-
- fclose (fh);
-
- return version;
-} /* int version_detect */
-
-static int openvpn_config (const char *key, const char *value)
-{
- if (strcasecmp ("StatusFile", key) == 0)
- {
- char *status_file, *status_name, *filename;
- int status_version, i;
- vpn_status_t *temp;
-
- /* try to detect the status file format */
- status_version = version_detect (value);
-
- if (status_version == 0)
- {
- WARNING ("openvpn plugin: unable to detect status version, \
- discarding status file \"%s\".", value);
- return (1);
- }
-
- status_file = sstrdup (value);
- if (status_file == NULL)
- {
- char errbuf[1024];
- WARNING ("openvpn plugin: sstrdup failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- return (1);
- }
-
- /* it determines the file name as string starting at location filename + 1 */
- filename = strrchr (status_file, (int) '/');
- if (filename == NULL)
- {
- /* status_file is already the file name only */
- status_name = status_file;
- }
- else
- {
- /* doesn't waste memory, uses status_file starting at filename + 1 */
- status_name = filename + 1;
- }
-
- /* scan the list looking for a clone */
- for (i = 0; i < vpn_num; i++)
- {
- if (strcasecmp (vpn_list[i]->name, status_name) == 0)
- {
- WARNING ("openvpn plugin: status filename \"%s\" "
- "already used, please choose a "
- "different one.", status_name);
- sfree (status_file);
- return (1);
- }
- }
-
- /* create a new vpn element since file, version and name are ok */
- temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
- if (temp == NULL)
- {
- char errbuf[1024];
- ERROR ("openvpn plugin: malloc failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
- sfree (status_file);
- return (1);
- }
- temp->file = status_file;
- temp->version = status_version;
- temp->name = status_name;
-
- vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
- if (vpn_list == NULL)
- {
- char errbuf[1024];
- ERROR ("openvpn plugin: realloc failed: %s",
- sstrerror (errno, errbuf, sizeof (errbuf)));
-
- sfree (temp->file);
- sfree (temp);
- return (1);
- }
-
- vpn_list[vpn_num] = temp;
- vpn_num++;
-
- DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
-
- } /* if (strcasecmp ("StatusFile", key) == 0) */
- else if ((strcasecmp ("CollectCompression", key) == 0)
- || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
- {
- if (IS_FALSE (value))
- collect_compression = 0;
- else
- collect_compression = 1;
- } /* if (strcasecmp ("CollectCompression", key) == 0) */
- else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
- {
- if (IS_TRUE (value))
- {
- DEBUG ("openvpn plugin: using the new naming schema");
- new_naming_schema = 1;
- }
- else
- {
- new_naming_schema = 0;
- }
- } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
- else if (strcasecmp("CollectUserCount", key) == 0)
- {
- if (IS_TRUE(value))
- collect_user_count = 1;
- else
- collect_user_count = 0;
- } /* if (strcasecmp("CollectUserCount", key) == 0) */
- else if (strcasecmp("CollectIndividualUsers", key) == 0)
- {
- if (IS_FALSE (value))
- collect_individual_users = 0;
- else
- collect_individual_users = 1;
- } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
- else
- {
- return (-1);
- }
-
- return (0);
+static int openvpn_config(const char *key, const char *value) {
+ if (strcasecmp("StatusFile", key) == 0) {
+ char callback_name[3 * DATA_MAX_NAME_LEN];
+ char *status_name;
+
+ char *status_file = strdup(value);
+ if (status_file == NULL) {
+ ERROR("openvpn plugin: strdup failed: %s", STRERRNO);
+ return 1;
+ }
+
+ /* it determines the file name as string starting at location filename + 1
+ */
+ char *filename = strrchr(status_file, (int)'/');
+ if (filename == NULL) {
+ /* status_file is already the file name only */
+ status_name = status_file;
+ } else {
+ /* doesn't waste memory, uses status_file starting at filename + 1 */
+ status_name = filename + 1;
+ }
+
+ /* create a new vpn element */
+ vpn_status_t *instance = calloc(1, sizeof(*instance));
+ if (instance == NULL) {
+ ERROR("openvpn plugin: malloc failed: %s", STRERRNO);
+ sfree(status_file);
+ return 1;
+ }
+ instance->file = status_file;
+ instance->name = status_name;
+
+ snprintf(callback_name, sizeof(callback_name), "openvpn/%s", status_name);
+
+ int status = plugin_register_complex_read(
+ /* group = */ "openvpn",
+ /* name = */ callback_name,
+ /* callback = */ openvpn_read,
+ /* interval = */ 0, &(user_data_t){
+ .data = instance, .free_func = openvpn_free,
+ });
+
+ if (status == EINVAL) {
+ WARNING("openvpn plugin: status filename \"%s\" "
+ "already used, please choose a "
+ "different one.",
+ status_name);
+ return -1;
+ }
+
+ DEBUG("openvpn plugin: status file \"%s\" added", instance->file);
+ } /* if (strcasecmp ("StatusFile", key) == 0) */
+ else if ((strcasecmp("CollectCompression", key) == 0) ||
+ (strcasecmp("Compression", key) == 0)) /* old, deprecated name */
+ {
+ if (IS_FALSE(value))
+ collect_compression = 0;
+ else
+ collect_compression = 1;
+ } /* if (strcasecmp ("CollectCompression", key) == 0) */
+ else if (strcasecmp("ImprovedNamingSchema", key) == 0) {
+ if (IS_TRUE(value)) {
+ DEBUG("openvpn plugin: using the new naming schema");
+ new_naming_schema = 1;
+ } else {
+ new_naming_schema = 0;
+ }
+ } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
+ else if (strcasecmp("CollectUserCount", key) == 0) {
+ if (IS_TRUE(value))
+ collect_user_count = 1;
+ else
+ collect_user_count = 0;
+ } /* if (strcasecmp("CollectUserCount", key) == 0) */
+ else if (strcasecmp("CollectIndividualUsers", key) == 0) {
+ if (IS_FALSE(value))
+ collect_individual_users = 0;
+ else
+ collect_individual_users = 1;
+ } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
+ else {
+ return -1;
+ }
+
+ return 0;