freeswitch plugin: Added initial pthread stuff
[collectd.git] / src / freeswitch.c
1 /**
2  * collectd - src/freeswitch.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 /*
27 #include "utils_match.h"
28 */
29 #include <pthread.h>
30 #include <esl.h>
31
32 #define FREESWITCH_DEF_HOST "127.0.0.1"
33 #define FREESWITCH_DEF_PORT "8021"
34 #define FREESWITCH_DEF_PASSWORD "ClueCon"
35
36 static const char *config_keys[] = 
37 {
38         "Host",
39         "Port",
40         "Password"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43
44 typedef struct profilename
45 {
46         char *name;
47         struct profilename *next;
48 } profilename_t;
49
50 static pthread_t esl_thread;
51 static int esl_thread_init = 0;
52
53 // static profilename_t *first_profilename = NULL;
54 static char *freeswitch_host = NULL;
55 static char freeswitch_port[16];
56 static char *freeswitch_password = NULL;
57
58 static void freeswitch_submit (const char *profile, const char *type, gauge_t inbound, gauge_t outbound)
59 {
60         value_t values[2];
61         value_list_t vl = VALUE_LIST_INIT;
62
63         values[0].gauge = inbound;
64         values[1].gauge = outbound;
65
66         vl.values = values;
67         vl.values_len = 2;
68         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
69         sstrncpy (vl.plugin, "freeswitch", sizeof (vl.plugin));
70         sstrncpy (vl.type, type, sizeof (vl.type));
71         sstrncpy (vl.type_instance, profile, sizeof (vl.type_instance));
72
73         plugin_dispatch_values (&vl);
74 } /* void freeswitch_submit */
75
76 static int freeswitch_read (void)
77 {
78         const char *host;
79         const char *port;
80         const char *password;
81
82         /* Set some default configuration variables */
83         host = freeswitch_host;
84         if (host == NULL)
85                 host = FREESWITCH_DEF_HOST;
86
87         port = freeswitch_port;
88         if (port == NULL)
89                 port = FREESWITCH_DEF_PORT;
90
91         password = freeswitch_password;
92         if (password == NULL)
93                 password = FREESWITCH_DEF_PASSWORD;
94
95         esl_handle_t handle = {{0}};
96
97         /* Connect from freeswitch_init for a persistent ESL connection */
98         if (esl_connect(&handle, host, atoi(port), password)) {
99                 DEBUG ("Error connecting to FreeSWITCH ESL interface [%s]\n", handle.err);
100                 return -1;
101         }
102
103         esl_send_recv(&handle, "api show channels\n\n");
104
105         if (handle.last_sr_event && handle.last_sr_event->body) {
106                 // handle.last_sr_event->body now contains the string with all active channels...
107         }
108         
109         /* Disconnect from freeswitch_shutdown for a persistent ESL connection */
110         esl_disconnect(&handle);
111
112         freeswitch_submit ("res-public", "fs_channels", 3, 5);
113
114         return (0);
115 } /* int freeswitch_read */
116
117 static int freeswitch_config (const char *key, const char *value)
118 {
119         if (strcasecmp ("Host", key) == 0)
120         {
121                 if (freeswitch_host != NULL)
122                         free (freeswitch_host);
123                 freeswitch_host = strdup (value);
124         }
125         else if (strcasecmp ("Port", key) == 0)
126         {
127                 int port = (int) (atof (value));
128                 if ((port > 0) && (port <= 65535))
129                         ssnprintf (freeswitch_port, sizeof (freeswitch_port),
130                                         "%i", port);
131                 else
132                         sstrncpy (freeswitch_port, value, sizeof (freeswitch_port));
133         }
134         else if (strcasecmp ("Password", key) == 0)
135         {
136                 if (freeswitch_password != NULL)
137                         free (freeswitch_password);
138                 freeswitch_password = strdup (value);
139         }
140         else
141         {
142                 return (-1);
143         }
144         return (0);
145 } /* int freeswitch_config */
146
147 static void *esl_child_loop (void __attribute__((unused)) *dummy)
148 {
149
150         DEBUG ("child is exiting");
151
152         esl_thread_init = 0;
153         pthread_exit (NULL);
154
155         return (NULL);
156 } /* void *esl_child_loop */
157
158 static int freeswitch_init (void)
159 {
160         /* clean up an old thread */
161         int status;
162
163 /*
164         pthread_mutex_lock (&traffic_mutex);
165         tr_queries   = 0;
166         tr_responses = 0;
167         pthread_mutex_unlock (&traffic_mutex);
168 */
169
170         if (esl_thread_init != 0)
171                 return (-1);
172
173         status = pthread_create (&esl_thread, NULL, esl_child_loop,
174                         (void *) 0);
175         if (status != 0)
176         {
177                 char errbuf[1024];
178                 ERROR ("freeswitch plugin: pthread_create failed: %s",
179                         sstrerror (errno, errbuf, sizeof (errbuf)));
180                 return (-1);
181         }
182
183         esl_thread_init = 1;
184
185         return(0);
186 } /* int freeswitch_init */
187
188 static int freeswitch_shutdown (void)
189 {
190         return(0);
191 } /* int freeswitch_shutdown */
192
193 void module_register (void)
194 {
195         plugin_register_config ("freeswitch", freeswitch_config,
196                         config_keys, config_keys_num);
197         plugin_register_init ("freeswitch", freeswitch_init);
198         plugin_register_shutdown ("freeswitch", freeswitch_shutdown);
199         plugin_register_read ("freeswitch", freeswitch_read);
200 } /* void module_register */