dc7eb9e8d183374a4f7f5397a6ef2a064fa13b8a
[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  *   Leon de Rooij <leon@scarlet-internet.nl>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 /*
28 #include "utils_match.h"
29 */
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 esl_handle_t handle = {{0}};
51 // static int thread_running = 0;
52
53 static char *freeswitch_host = NULL;
54 static char *freeswitch_port = NULL;
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 /*
78         const char *host;
79         const char *port;
80         const char *password;
81 */
82
83
84         esl_send_recv(&handle, "api show channels\n\n");
85         if (handle.last_sr_event && handle.last_sr_event->body) {
86                 DEBUG ("OUTPUT FROM FREESWITCH:\n%s\n\n", handle.last_sr_event->body);
87         }
88
89         freeswitch_submit ("res-public", "fs_channels", 3, 5);
90
91         return (0);
92 } /* int freeswitch_read */
93
94 static int freeswitch_config (const char *key, const char *value)
95 {
96         if (strcasecmp ("Host", key) == 0)
97         {
98                 if (freeswitch_host != NULL)
99                         free (freeswitch_host);
100                 freeswitch_host = strdup (value);
101         }
102         else if (strcasecmp ("Port", key) == 0)
103         {
104                 if (freeswitch_port != NULL)
105                         free (freeswitch_port);
106                 freeswitch_port = strdup (value);
107         }
108         else if (strcasecmp ("Password", key) == 0)
109         {
110                 if (freeswitch_password != NULL)
111                         free (freeswitch_password);
112                 freeswitch_password = strdup (value);
113         }
114         else
115         {
116                 return (-1);
117         }
118         return (0);
119 } /* int freeswitch_config */
120
121 /*
122 static void *msg_thread_run(esl_thread_t *me, void *obj)
123 {
124         esl_handle_t *handle = (esl_handle_t *) obj;
125         thread_running = 1;
126
127         // Maybe do some more in this loop later, like receive subscribed events,
128         // and create statistics of them
129         // see fs_cli.c function static void *msg_thread_run(), around line 198
130         while (thread_running && handle->connected)
131         {
132                 esl_status_t status = esl_recv_event_timed(handle, 10, 1, NULL);
133                 if (status == ESL_FAIL)
134                 {
135                         //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
136                         DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
137                         thread_running = 0;
138                 }
139                 usleep(1000);
140         }
141
142         thread_running = 0;
143         return (NULL);
144 } */ /* void *msg_thread_run */
145
146 static int freeswitch_init (void)
147 {
148         /* Set some default configuration variables */
149         if (freeswitch_host == NULL)
150                 freeswitch_host = FREESWITCH_DEF_HOST;
151
152         if (freeswitch_port == NULL)
153                 freeswitch_port = "8021";
154
155         if (freeswitch_password == NULL)
156                 freeswitch_password = FREESWITCH_DEF_PASSWORD;
157
158         /* Connect to FreeSWITCH over ESL */
159         DEBUG ("Making ESL connection to %s %s %s\n", freeswitch_host, freeswitch_port, freeswitch_password);
160         esl_connect(&handle, freeswitch_host, atoi(freeswitch_port), freeswitch_password);
161
162         /* Start a seperate thread for incoming events */
163         //esl_thread_create_detached(msg_thread_run, &handle);
164
165         return(0);
166 } /* int freeswitch_init */
167
168 void module_register (void)
169 {
170         plugin_register_config ("freeswitch", freeswitch_config,
171                         config_keys, config_keys_num);
172         plugin_register_init ("freeswitch", freeswitch_init);
173         plugin_register_read ("freeswitch", freeswitch_read);
174 } /* void module_register */