freeswitch plugin: Added initial pthread stuff
[collectd.git] / src / freeswitch.c
index 52b7a1d..efab975 100644 (file)
@@ -25,8 +25,9 @@
 
 /*
 #include "utils_match.h"
-#include <esl.h>
 */
+#include <pthread.h>
+#include <esl.h>
 
 #define FREESWITCH_DEF_HOST "127.0.0.1"
 #define FREESWITCH_DEF_PORT "8021"
@@ -46,12 +47,15 @@ typedef struct profilename
        struct profilename *next;
 } profilename_t;
 
+static pthread_t esl_thread;
+static int esl_thread_init = 0;
+
 // static profilename_t *first_profilename = NULL;
 static char *freeswitch_host = NULL;
 static char freeswitch_port[16];
 static char *freeswitch_password = NULL;
 
-static void freeswitch_submit (const char *type_instance, gauge_t inbound, gauge_t outbound)
+static void freeswitch_submit (const char *profile, const char *type, gauge_t inbound, gauge_t outbound)
 {
        value_t values[2];
        value_list_t vl = VALUE_LIST_INIT;
@@ -63,8 +67,8 @@ static void freeswitch_submit (const char *type_instance, gauge_t inbound, gauge
        vl.values_len = 2;
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "freeswitch", sizeof (vl.plugin));
-       sstrncpy (vl.type, "freeswitch", sizeof (vl.type));
-        sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+        sstrncpy (vl.type_instance, profile, sizeof (vl.type_instance));
 
        plugin_dispatch_values (&vl);
 } /* void freeswitch_submit */
@@ -75,6 +79,7 @@ static int freeswitch_read (void)
        const char *port;
        const char *password;
 
+       /* Set some default configuration variables */
        host = freeswitch_host;
        if (host == NULL)
                host = FREESWITCH_DEF_HOST;
@@ -87,20 +92,24 @@ static int freeswitch_read (void)
        if (password == NULL)
                password = FREESWITCH_DEF_PASSWORD;
 
-/*
        esl_handle_t handle = {{0}};
-       esl_connect(&handle, host, port, password);
+
+       /* Connect from freeswitch_init for a persistent ESL connection */
+       if (esl_connect(&handle, host, atoi(port), password)) {
+               DEBUG ("Error connecting to FreeSWITCH ESL interface [%s]\n", handle.err);
+               return -1;
+       }
 
        esl_send_recv(&handle, "api show channels\n\n");
-*/
-       
-       // DO YOUR THING HERE TO PARSE &handle
+
+       if (handle.last_sr_event && handle.last_sr_event->body) {
+               // handle.last_sr_event->body now contains the string with all active channels...
+       }
        
-/*
+       /* Disconnect from freeswitch_shutdown for a persistent ESL connection */
        esl_disconnect(&handle);
-*/
 
-       freeswitch_submit ("profilename", 0, 0);
+       freeswitch_submit ("res-public", "fs_channels", 3, 5);
 
        return (0);
 } /* int freeswitch_read */
@@ -135,9 +144,57 @@ static int freeswitch_config (const char *key, const char *value)
         return (0);
 } /* int freeswitch_config */
 
+static void *esl_child_loop (void __attribute__((unused)) *dummy)
+{
+
+       DEBUG ("child is exiting");
+
+       esl_thread_init = 0;
+       pthread_exit (NULL);
+
+       return (NULL);
+} /* void *esl_child_loop */
+
+static int freeswitch_init (void)
+{
+       /* clean up an old thread */
+       int status;
+
+/*
+        pthread_mutex_lock (&traffic_mutex);
+        tr_queries   = 0;
+        tr_responses = 0;
+        pthread_mutex_unlock (&traffic_mutex);
+*/
+
+       if (esl_thread_init != 0)
+               return (-1);
+
+       status = pthread_create (&esl_thread, NULL, esl_child_loop,
+                       (void *) 0);
+       if (status != 0)
+       {
+               char errbuf[1024];
+               ERROR ("freeswitch plugin: pthread_create failed: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       esl_thread_init = 1;
+
+       return(0);
+} /* int freeswitch_init */
+
+static int freeswitch_shutdown (void)
+{
+       return(0);
+} /* int freeswitch_shutdown */
+
 void module_register (void)
 {
         plugin_register_config ("freeswitch", freeswitch_config,
                        config_keys, config_keys_num);
+       plugin_register_init ("freeswitch", freeswitch_init);
+       plugin_register_shutdown ("freeswitch", freeswitch_shutdown);
        plugin_register_read ("freeswitch", freeswitch_read);
 } /* void module_register */