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>
26 #include "utils_match.h"
29 #define FS_DEF_HOST "127.0.0.1"
30 #define FS_DEF_PORT "8021"
31 #define FS_DEF_PASS "ClueCon"
38 * <Command "api sofia status profile res-public">
39 * Instance "profile-sofia-res-public"
42 * Regex "CALLS-IN\\s+([0-9]+)"
54 typedef struct fs_match_s fs_match_t;
66 typedef struct fs_command_s fs_command_t;
69 char *line; // "api sofia status profile res-public"
70 char *instance; // "profile-sofia-res-public"
71 char *buffer; // <output from esl command as a char*>
72 size_t buffer_size; // strlen(*buffer)+3
73 size_t buffer_fill; // 0 or 1
78 static fs_command_t *fs_commands_g = NULL;
80 static char *fs_host = NULL;
81 static char *fs_port = NULL;
82 static char *fs_pass = NULL;
84 static esl_handle_t esl_handle = {{0}};
85 // static int thread_running = 0; // for when subscribing to esl events
91 static int fs_config_add_string (const char *name, char **dest, oconfig_item_t *ci)
93 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
95 WARNING ("freeswitch plugin: '%s' needs exactly one string argument.", name);
100 *dest = strdup (ci->values[0].value.string);
105 } /* int fs_config_add_string */
107 static void fs_match_free (fs_match_t *fm)
114 sfree (fm->instance);
115 match_destroy (fm->match);
116 fs_match_free (fm->next);
118 } /* void fs_match_free */
120 static void fs_command_free (fs_command_t *fc)
126 sfree (fc->instance);
128 fs_match_free (fc->matches);
129 fs_command_free (fc->next);
131 } /* void fs_command_free */
133 static int fs_config_add_match_dstype (int *dstype_ret, oconfig_item_t *ci)
137 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
139 WARNING ("freeswitch plugin: 'DSType' needs exactly one string argument.");
143 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
145 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
146 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
147 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
148 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
149 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
150 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
151 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
152 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
153 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
157 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
159 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
160 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
161 dstype |= UTILS_MATCH_CF_COUNTER_SET;
162 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
163 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
164 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
165 dstype |= UTILS_MATCH_CF_COUNTER_INC;
176 WARNING ("freeswitch plugin: `%s' is not a valid argument to `DSType'.",
177 ci->values[0].value.string);
181 *dstype_ret = dstype;
183 } /* int fs_config_add_match_dstype */
185 static int fs_config_add_match (fs_command_t *fs_command, oconfig_item_t *ci)
187 fs_match_t *fs_match;
191 if (ci->values_num != 0)
193 WARNING ("freeswitch plugin: Ignoring arguments for the 'Match' block.");
196 fs_match = (fs_match_t *) malloc (sizeof (*fs_match));
197 if (fs_match == NULL)
199 ERROR ("freeswitch plugin: malloc failed.");
202 memset (fs_match, 0, sizeof (*fs_match));
205 for (i = 0; i < ci->children_num; i++)
207 oconfig_item_t *child = ci->children + i;
209 if (strcasecmp ("Regex", child->key) == 0)
210 status = fs_config_add_string ("Regex", &fs_match->regex, child);
211 else if (strcasecmp ("DSType", child->key) == 0)
212 status = fs_config_add_match_dstype (&fs_match->dstype, child);
213 else if (strcasecmp ("Type", child->key) == 0)
214 status = fs_config_add_string ("Type", &fs_match->type, child);
215 else if (strcasecmp ("Instance", child->key) == 0)
216 status = fs_config_add_string ("Instance", &fs_match->instance, child);
219 WARNING ("freeswitch plugin: Option `%s' not allowed here.", child->key);
225 } /* for (i = 0; i < ci->children_num; i++) */
229 if (fs_match->regex == NULL)
231 WARNING ("freeswitch plugin: `Regex' missing in `Match' block.");
235 if (fs_match->type == NULL)
237 WARNING ("freeswitch plugin: `Type' missing in `Match' block.");
241 if (fs_match->dstype == 0)
243 WARNING ("freeswitch plugin: `DSType' missing in `Match' block.");
248 } /* while (status == 0) */
253 fs_match->match = match_create_simple (fs_match->regex, fs_match->dstype);
254 if (fs_match->match == NULL)
256 ERROR ("freeswitch plugin: tail_match_add_match_simple failed.");
257 fs_match_free (fs_match);
264 prev = fs_command->matches;
265 while ((prev != NULL) && (prev->next != NULL))
269 fs_command->matches = fs_match;
271 prev->next = fs_match;
275 } /* int fs_config_add_match */
277 static int fs_config_add_command (oconfig_item_t *ci)
279 fs_command_t *command;
283 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
285 WARNING ("freeswitch plugin: 'Command' blocks need exactly one string argument.");
289 command = (fs_command_t *) malloc (sizeof (*command));
292 ERROR ("freeswitch plugin: malloc failed.");
295 memset (command, 0, sizeof (*command));
297 command->line = NULL;
298 command->line = strdup (ci->values[0].value.string);
300 if (command->line == NULL)
302 ERROR ("freeswitch plugin: strdup failed.");
307 /* Process all children */
309 for (i = 0; i < ci->children_num; i++)
311 oconfig_item_t *child = ci->children + i;
313 if (strcasecmp ("Instance", child->key) == 0)
314 status = fs_config_add_string ("Instance", &command->instance, child);
315 else if (strcasecmp ("Match", child->key) == 0)
316 fs_config_add_match (command, child);
319 WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
329 fs_command_free (command);
333 /* Add the new command to the linked list */
334 if (fs_commands_g == NULL)
335 fs_commands_g = command;
340 prev = fs_commands_g;
341 while ((prev != NULL) && (prev->next != NULL))
343 prev->next = command;
347 } /* int fs_config_add_command */
349 static int fs_complex_config (oconfig_item_t *ci)
359 for (i = 0; i < ci->children_num; i++)
361 oconfig_item_t *child = ci->children + i;
363 if (strcasecmp ("Host", child->key) == 0)
365 if (fs_host != NULL) free (fs_host);
366 fs_host = strdup(child->values[0].value.string);
368 else if (strcasecmp ("Port", child->key) == 0)
370 if (fs_port != NULL) free (fs_port);
371 fs_port = strdup(child->values[0].value.string);
373 else if (strcasecmp ("Pass", child->key) == 0)
375 if (fs_pass != NULL) free (fs_pass);
376 fs_pass = strdup(child->values[0].value.string);
378 else if (strcasecmp ("Command", child->key) == 0)
380 status = fs_config_add_command(child);
388 WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
393 if ((success == 0) && (errors > 0))
395 ERROR ("freeswitch plugin: All statements failed.");
400 } /* int fs_complex_config */
402 static void fs_submit (const fs_command_t *fc,
403 const fs_match_t *fm, const cu_match_value_t *mv)
406 value_list_t vl = VALUE_LIST_INIT;
408 values[0] = mv->value;
412 vl.time = time (NULL);
414 strncpy (vl.host, hostname_g, sizeof (vl.host));
415 strncpy (vl.plugin, "freeswitch", sizeof (vl.plugin));
416 strncpy (vl.plugin_instance, fc->instance, sizeof (vl.plugin_instance));
417 strncpy (vl.type, fm->type, sizeof (vl.type));
418 strncpy (vl.type_instance, fm->instance, sizeof (vl.type_instance));
420 plugin_dispatch_values (&vl);
421 } /* void fs_submit */
423 static int fs_read_command (fs_command_t *fc)
428 /* can't the following be done nicer ? */
430 line = (char *) malloc (strlen(fc->line)+3);
431 snprintf(line, strlen(fc->line)+3, "%s\n\n", fc->line);
432 esl_send_recv(&esl_handle, line);
436 if (esl_handle.last_sr_event && esl_handle.last_sr_event->body)
439 fc->buffer = strdup(esl_handle.last_sr_event->body);
440 fc->buffer_size = strlen(fc->buffer);
444 for (fm = fc->matches; fm != NULL; fm = fm->next)
446 cu_match_value_t *mv;
448 status = match_apply (fm->match, fc->buffer);
451 WARNING ("freeswitch plugin: match_apply failed.");
455 mv = match_get_user_data (fm->match);
458 WARNING ("freeswitch plugin: match_get_user_data returned NULL.");
462 fs_submit (fc, fm, mv);
463 } /* for (fm = fc->matches; fm != NULL; fm = fm->next) */
466 } /* int fs_read_command */
468 static int fs_read (void)
472 for (fc = fs_commands_g; fc != NULL; fc = fc->next)
473 fs_read_command (fc);
479 static void *msg_thread_run(esl_thread_t *me, void *obj)
481 esl_handle_t *esl_handle = (esl_handle_t *) obj;
484 // Maybe do some more in this loop later, like receive subscribed events,
485 // and create statistics of them
486 // see fs_cli.c function static void *msg_thread_run(), around line 198
487 while (thread_running && esl_handle->connected)
489 esl_status_t status = esl_recv_event_timed(esl_handle, 10, 1, NULL);
490 if (status == ESL_FAIL)
492 //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
493 DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
501 } */ /* void *msg_thread_run */
503 static int fs_init (void)
505 /* Set some default configuration variables */
506 if (fs_host == NULL) fs_host = FS_DEF_HOST;
507 if (fs_port == NULL) fs_port = FS_DEF_PORT;
508 if (fs_pass == NULL) fs_pass = FS_DEF_PASS;
510 /* Connect to FreeSWITCH over ESL */
511 DEBUG ("freeswitch plugin: making ESL connection to %s %s %s\n", fs_host, fs_port, fs_pass);
512 if (esl_connect(&esl_handle, fs_host, atoi(fs_port), fs_pass))
514 ERROR ("freeswitch plugin: connection failed [%s]", esl_handle.err);
518 /* Start a seperate thread for incoming events here */
519 //esl_thread_create_detached(msg_thread_run, &esl_handle);
524 static int fs_shutdown (void)
526 DEBUG ("freeswitch plugin: disconnecting");
527 esl_disconnect(&esl_handle);
528 fs_command_free (fs_commands_g);
529 fs_commands_g = NULL;
531 } /* int fs_shutdown */
533 void module_register (void)
535 plugin_register_complex_config ("freeswitch", fs_complex_config);
536 plugin_register_init ("freeswitch", fs_init);
537 plugin_register_read ("freeswitch", fs_read);
538 plugin_register_shutdown ("freeswitch", fs_shutdown);
539 } /* void module_register */