freeswitch plugin: Removed some more unnecessary cruft
[collectd.git] / src / freeswitch.c
index be8a905..b483ad3 100644 (file)
@@ -24,7 +24,7 @@
 #include "common.h"
 #include "plugin.h"
 #include "utils_match.h"
-#include <esl.h>
+#include "esl.h"
 
 #define FS_DEF_HOST "127.0.0.1"
 #define FS_DEF_PORT "8021"
  *             Host "127.0.0.1"
  *             Port "8021"
  *             Pass "ClueCon"
- *             <Command "sofia status profile res-public">
+ *             <Command "api sofia status profile res-public">
  *                     Instance "profile-sofia-res-public"
  *                     <Match>
- *                             Regex "\\<CALLS-IN\s+(\d+)\\>"
- *                             DSType "CounterInc"
- *                             Type "counter"
  *                             Instance "calls-in"
+ *                             Regex "CALLS-IN\\s+([0-9]+)"
+ *                             DSType "GaugeLast"
+ *                             Type "gauge"
  *                     </Match>
  *             </Command>
  *     </Plugin>
@@ -66,10 +66,10 @@ struct fs_command_s;
 typedef struct fs_command_s fs_command_t;
 struct fs_command_s
 {
-       char *line;             // "sofia status profile res-public"
+       char *line;             // "api sofia status profile res-public"
        char *instance;         // "profile-sofia-res-public"
        char *buffer;           // <output from esl command as a char*>
-       size_t buffer_size;     // sizeof(*buffer)
+       size_t buffer_size;     // strlen(*buffer)+3
        size_t buffer_fill;     // 0 or 1
        fs_match_t *matches;
        fs_command_t *next;
@@ -117,6 +117,19 @@ static void fs_match_free (fs_match_t *fm)
        sfree (fm);
 } /* void fs_match_free */
 
+static void fs_command_free (fs_command_t *fc)
+{
+       if (fc == NULL)
+               return;
+
+       sfree (fc->line);
+       sfree (fc->instance);
+       sfree (fc->buffer);
+       fs_match_free (fc->matches);
+       fs_command_free (fc->next);
+       sfree (fc);
+} /* void fs_command_free */
+
 static int fs_config_add_match_dstype (int *dstype_ret, oconfig_item_t *ci)
 {
        int dstype;
@@ -280,10 +293,11 @@ static int fs_config_add_command (oconfig_item_t *ci)
                return (-1);
        }
        memset (command, 0, sizeof (*command));
+
        command->line = NULL;
+       command->line = strdup (ci->values[0].value.string);
 
-       command->instance = strdup (ci->values[0].value.string);
-       if (command->instance == NULL)
+       if (command->line == NULL)
        {
                ERROR ("freeswitch plugin: strdup failed.");
                sfree (command);
@@ -296,7 +310,9 @@ static int fs_config_add_command (oconfig_item_t *ci)
        {
                oconfig_item_t *child = ci->children + i;
 
-               if (strcasecmp ("Match", child->key) == 0)
+               if (strcasecmp ("Instance", child->key) == 0)
+                       status = fs_config_add_string ("Instance", &command->instance, child);
+               else if (strcasecmp ("Match", child->key) == 0)
                        fs_config_add_match (command, child);
                else
                {
@@ -308,6 +324,12 @@ static int fs_config_add_command (oconfig_item_t *ci)
                        break;
        }
 
+       if (status != 0)
+       {
+               fs_command_free (command);
+               return (status);
+       }
+
        /* Add the new command to the linked list */
        if (fs_commands_g == NULL)
                fs_commands_g = command;
@@ -378,12 +400,12 @@ static int fs_complex_config (oconfig_item_t *ci)
 } /* int fs_complex_config */
 
 static void fs_submit (const fs_command_t *fc,
-       const fs_match_t *fm, const cu_match_value_t *fmv)
+       const fs_match_t *fm, const cu_match_value_t *mv)
 {
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
 
-       values[0] = fmv->value;
+       values[0] = mv->value;
 
        vl.values = values;
        vl.values_len = 1;
@@ -403,15 +425,20 @@ static int fs_read_command (fs_command_t *fc)
        fs_match_t *fm;
        int status;
 
+       /* can't the following be done nicer ? */
+       char *line;
+       line = (char *) malloc (strlen(fc->line)+3);
+       snprintf(line, strlen(fc->line)+3, "%s\n\n", fc->line);
+       esl_send_recv(&esl_handle, line);
+
        fc->buffer_fill = 0;
-       esl_send_recv(&esl_handle, fc->line); /////////////////// NOTICE NO \n\n IS GIVEN AFTER fc->line !!!!!!!!!!!!!!!!!! THIS WON'T WORK!!!!!!!!!!
 
        if (esl_handle.last_sr_event && esl_handle.last_sr_event->body)
        {
-               DEBUG ("OUTPUT FROM FS:\n%s\n\n", esl_handle.last_sr_event->body);
                sfree(fc->buffer);
                fc->buffer = strdup(esl_handle.last_sr_event->body);
-               fc->buffer_fill = 1; // ??
+               fc->buffer_size = strlen(fc->buffer);
+               fc->buffer_fill = 1;
        }
 
        for (fm = fc->matches; fm != NULL; fm = fm->next)
@@ -481,7 +508,7 @@ static int fs_init (void)
        if (fs_pass == NULL) fs_pass = FS_DEF_PASS;
 
        /* Connect to FreeSWITCH over ESL */
-       DEBUG ("Making ESL connection to %s %s %s\n", fs_host, fs_port, fs_pass);
+       DEBUG ("freeswitch plugin: making ESL connection to %s %s %s\n", fs_host, fs_port, fs_pass);
        esl_connect(&esl_handle, fs_host, atoi(fs_port), fs_pass);
 
        /* Start a seperate thread for incoming events here */
@@ -492,7 +519,10 @@ static int fs_init (void)
 
 static int fs_shutdown (void)
 {
+       DEBUG ("freeswitch plugin: disconnecting");
        esl_disconnect(&esl_handle);
+       fs_command_free (fs_commands_g);
+       fs_commands_g = NULL;
        return (0);
 } /* int fs_shutdown */