2 * collectd - src/freeswitch.c
3 * Copyright (C) 2005-2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
20 * Leon de Rooij <leon@scarlet-internet.nl>
28 #include "utils_match.h"
32 #define FREESWITCH_DEF_HOST "127.0.0.1"
33 #define FREESWITCH_DEF_PORT "8021"
34 #define FREESWITCH_DEF_PASSWORD "ClueCon"
36 static const char *config_keys[] =
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44 typedef struct profilename
47 struct profilename *next;
50 static esl_handle_t handle = {{0}};
51 // static int thread_running = 0;
53 static char *freeswitch_host = NULL;
54 static char *freeswitch_port = NULL;
55 static char *freeswitch_password = NULL;
57 static void freeswitch_submit (const char *profile, const char *type, gauge_t inbound, gauge_t outbound)
60 value_list_t vl = VALUE_LIST_INIT;
62 values[0].gauge = inbound;
63 values[1].gauge = outbound;
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));
72 plugin_dispatch_values (&vl);
73 } /* void freeswitch_submit */
75 static int freeswitch_read (void)
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);
89 freeswitch_submit ("res-public", "fs_channels", 3, 5);
92 } /* int freeswitch_read */
94 static int freeswitch_config (const char *key, const char *value)
96 if (strcasecmp ("Host", key) == 0)
98 if (freeswitch_host != NULL)
99 free (freeswitch_host);
100 freeswitch_host = strdup (value);
102 else if (strcasecmp ("Port", key) == 0)
104 if (freeswitch_port != NULL)
105 free (freeswitch_port);
106 freeswitch_port = strdup (value);
108 else if (strcasecmp ("Password", key) == 0)
110 if (freeswitch_password != NULL)
111 free (freeswitch_password);
112 freeswitch_password = strdup (value);
119 } /* int freeswitch_config */
122 static void *msg_thread_run(esl_thread_t *me, void *obj)
124 esl_handle_t *handle = (esl_handle_t *) obj;
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)
132 esl_status_t status = esl_recv_event_timed(handle, 10, 1, NULL);
133 if (status == ESL_FAIL)
135 //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
136 DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
144 } */ /* void *msg_thread_run */
146 static int freeswitch_init (void)
148 /* Set some default configuration variables */
149 if (freeswitch_host == NULL)
150 freeswitch_host = FREESWITCH_DEF_HOST;
152 if (freeswitch_port == NULL)
153 freeswitch_port = "8021";
155 if (freeswitch_password == NULL)
156 freeswitch_password = FREESWITCH_DEF_PASSWORD;
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);
162 /* Start a seperate thread for incoming events */
163 //esl_thread_create_detached(msg_thread_run, &handle);
166 } /* int freeswitch_init */
168 void module_register (void)
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 */