freeswitch plugin: Removed usage of pthreads directly, but am using esl_thread_create...
[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 <esl.h>
30
31 #define FREESWITCH_DEF_HOST "127.0.0.1"
32 #define FREESWITCH_DEF_PORT "8021"
33 #define FREESWITCH_DEF_PASSWORD "ClueCon"
34
35 static const char *config_keys[] = 
36 {
37         "Host",
38         "Port",
39         "Password"
40 };
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42
43 typedef struct profilename
44 {
45         char *name;
46         struct profilename *next;
47 } profilename_t;
48
49 static esl_handle_t handle = {{0}};
50 static int thread_running = 0;
51
52 // static profilename_t *first_profilename = NULL;
53 static char *freeswitch_host = NULL;
54 static char freeswitch_port[16];
55 static char *freeswitch_password = NULL;
56
57 static void freeswitch_submit (const char *profile, const char *type, gauge_t inbound, gauge_t outbound)
58 {
59         value_t values[2];
60         value_list_t vl = VALUE_LIST_INIT;
61
62         values[0].gauge = inbound;
63         values[1].gauge = outbound;
64
65         vl.values = values;
66         vl.values_len = 2;
67         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
68         sstrncpy (vl.plugin, "freeswitch", sizeof (vl.plugin));
69         sstrncpy (vl.type, type, sizeof (vl.type));
70         sstrncpy (vl.type_instance, profile, sizeof (vl.type_instance));
71
72         plugin_dispatch_values (&vl);
73 } /* void freeswitch_submit */
74
75 static int freeswitch_read (void)
76 {
77         const char *host;
78         const char *port;
79         const char *password;
80
81         /* Set some default configuration variables */
82         host = freeswitch_host;
83         if (host == NULL)
84                 host = FREESWITCH_DEF_HOST;
85
86         port = freeswitch_port;
87         if (port == NULL)
88                 port = FREESWITCH_DEF_PORT;
89
90         password = freeswitch_password;
91         if (password == NULL)
92                 password = FREESWITCH_DEF_PASSWORD;
93
94 //      esl_handle_t handle = {{0}};
95
96         /* Connect from freeswitch_init for a persistent ESL connection */
97 /*
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
107                 DEBUG ("OUTPUT FROM FREESWITCH:\n%s\n\n", handle.last_sr_event->body);
108
109                 // handle.last_sr_event->body now contains the string with all active channels...
110         }
111
112
113         /* Disconnect from freeswitch_shutdown for a persistent ESL connection */
114 //      esl_disconnect(&handle);
115
116         freeswitch_submit ("res-public", "fs_channels", 3, 5);
117
118         return (0);
119 } /* int freeswitch_read */
120
121 static int freeswitch_config (const char *key, const char *value)
122 {
123         if (strcasecmp ("Host", key) == 0)
124         {
125                 if (freeswitch_host != NULL)
126                         free (freeswitch_host);
127                 freeswitch_host = strdup (value);
128         }
129         else if (strcasecmp ("Port", key) == 0)
130         {
131                 int port = (int) (atof (value));
132                 if ((port > 0) && (port <= 65535))
133                         ssnprintf (freeswitch_port, sizeof (freeswitch_port),
134                                         "%i", port);
135                 else
136                         sstrncpy (freeswitch_port, value, sizeof (freeswitch_port));
137         }
138         else if (strcasecmp ("Password", key) == 0)
139         {
140                 if (freeswitch_password != NULL)
141                         free (freeswitch_password);
142                 freeswitch_password = strdup (value);
143         }
144         else
145         {
146                 return (-1);
147         }
148         return (0);
149 } /* int freeswitch_config */
150
151 static void *msg_thread_run(esl_thread_t *me, void *obj)
152 {
153         esl_handle_t *handle = (esl_handle_t *) obj;
154         thread_running = 1;
155
156         while (thread_running && handle->connected)
157         {
158                 //esl_status_t status = esl_recv_event_timed(handle, 10, 1, NULL);
159                 usleep(1000);
160         }
161
162         thread_running = 0;
163         return (NULL);
164 } /* void *msg_thread_run */
165
166 static int freeswitch_init (void)
167 {
168         esl_connect(&handle, "172.16.235.98", 8021, "ClueCon");
169         esl_thread_create_detached(msg_thread_run, &handle);
170         return(0);
171 } /* int freeswitch_init */
172
173 void module_register (void)
174 {
175         plugin_register_config ("freeswitch", freeswitch_config,
176                         config_keys, config_keys_num);
177         plugin_register_init ("freeswitch", freeswitch_init);
178         plugin_register_read ("freeswitch", freeswitch_read);
179 } /* void module_register */