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"
41 * Regex "\\<CALLS-IN\s+(\d+)\\>"
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; // sizeof(*buffer)
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 int fs_config_add_match_dstype (int *dstype_ret, oconfig_item_t *ci)
122 DEBUG ("freeswitch plugin: in fs_config_add_match_dstype");
126 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
128 WARNING ("freeswitch plugin: 'DSType' needs exactly one string argument.");
132 if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
134 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
135 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
136 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
137 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
138 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
139 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
140 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
141 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
142 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
146 else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
148 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
149 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
150 dstype |= UTILS_MATCH_CF_COUNTER_SET;
151 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
152 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
153 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
154 dstype |= UTILS_MATCH_CF_COUNTER_INC;
165 WARNING ("freeswitch plugin: `%s' is not a valid argument to `DSType'.",
166 ci->values[0].value.string);
170 *dstype_ret = dstype;
172 } /* int fs_config_add_match_dstype */
174 static int fs_config_add_match (fs_command_t *fs_command, oconfig_item_t *ci)
176 DEBUG ("freeswitch plugin: in fs_config_add_match");
178 fs_match_t *fs_match;
182 if (ci->values_num != 0)
184 WARNING ("freeswitch plugin: Ignoring arguments for the 'Match' block.");
187 fs_match = (fs_match_t *) malloc (sizeof (*fs_match));
188 if (fs_match == NULL)
190 ERROR ("freeswitch plugin: malloc failed.");
193 memset (fs_match, 0, sizeof (*fs_match));
196 for (i = 0; i < ci->children_num; i++)
198 oconfig_item_t *child = ci->children + i;
200 if (strcasecmp ("Regex", child->key) == 0)
201 status = fs_config_add_string ("Regex", &fs_match->regex, child);
202 else if (strcasecmp ("DSType", child->key) == 0)
203 status = fs_config_add_match_dstype (&fs_match->dstype, child);
204 else if (strcasecmp ("Type", child->key) == 0)
205 status = fs_config_add_string ("Type", &fs_match->type, child);
206 else if (strcasecmp ("Instance", child->key) == 0)
207 status = fs_config_add_string ("Instance", &fs_match->instance, child);
210 WARNING ("freeswitch plugin: Option `%s' not allowed here.", child->key);
216 } /* for (i = 0; i < ci->children_num; i++) */
220 if (fs_match->regex == NULL)
222 WARNING ("freeswitch plugin: `Regex' missing in `Match' block.");
226 if (fs_match->type == NULL)
228 WARNING ("freeswitch plugin: `Type' missing in `Match' block.");
232 if (fs_match->dstype == 0)
234 WARNING ("freeswitch plugin: `DSType' missing in `Match' block.");
239 } /* while (status == 0) */
244 fs_match->match = match_create_simple (fs_match->regex, fs_match->dstype);
245 if (fs_match->match == NULL)
247 ERROR ("freeswitch plugin: tail_match_add_match_simple failed.");
248 fs_match_free (fs_match);
255 prev = fs_command->matches;
256 while ((prev != NULL) && (prev->next != NULL))
260 fs_command->matches = fs_match;
262 prev->next = fs_match;
266 } /* int fs_config_add_match */
268 static int fs_config_add_command (oconfig_item_t *ci)
270 DEBUG ("freeswitch plugin: in fs_config_add_command");
272 fs_command_t *command;
276 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
278 WARNING ("freeswitch plugin: 'Command' blocks need exactly one string argument.");
282 command = (fs_command_t *) malloc (sizeof (*command));
285 ERROR ("freeswitch plugin: malloc failed.");
288 memset (command, 0, sizeof (*command));
290 command->line = NULL;
291 command->line = strdup (ci->values[0].value.string);
292 if (command->line == NULL)
294 ERROR ("freeswitch plugin: strdup failed.");
299 /* Process all children */
301 for (i = 0; i < ci->children_num; i++)
303 oconfig_item_t *child = ci->children + i;
305 if (strcasecmp ("Instance", child->key) == 0)
306 status = fs_config_add_string ("Instance", &command->instance, child);
307 else if (strcasecmp ("Match", child->key) == 0)
308 fs_config_add_match (command, child);
311 WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
319 /* Add the new command to the linked list */
320 if (fs_commands_g == NULL)
321 fs_commands_g = command;
326 prev = fs_commands_g;
327 while ((prev != NULL) && (prev->next != NULL))
329 prev->next = command;
333 } /* int fs_config_add_command */
335 static int fs_complex_config (oconfig_item_t *ci)
342 DEBUG ("freeswitch plugin: reading config");
347 for (i = 0; i < ci->children_num; i++)
349 oconfig_item_t *child = ci->children + i;
351 if (strcasecmp ("Host", child->key) == 0)
353 if (fs_host != NULL) free (fs_host);
354 fs_host = strdup(child->values[0].value.string);
356 else if (strcasecmp ("Port", child->key) == 0)
358 if (fs_port != NULL) free (fs_port);
359 fs_port = strdup(child->values[0].value.string);
361 else if (strcasecmp ("Pass", child->key) == 0)
363 if (fs_pass != NULL) free (fs_pass);
364 fs_pass = strdup(child->values[0].value.string);
366 else if (strcasecmp ("Command", child->key) == 0)
368 status = fs_config_add_command(child);
376 WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
381 if ((success == 0) && (errors > 0))
383 ERROR ("freeswitch plugin: All statements failed.");
388 } /* int fs_complex_config */
390 static void fs_submit (const fs_command_t *fc,
391 const fs_match_t *fm, const cu_match_value_t *fmv)
393 DEBUG ("freeswitch plugin: in fs_submit");
395 DEBUG ("fc->instance");
396 DEBUG (fc->instance);
401 DEBUG ("fmv->value");
405 value_list_t vl = VALUE_LIST_INIT;
407 values[0] = fmv->value;
411 vl.time = time (NULL);
413 strncpy (vl.host, hostname_g, sizeof (vl.host));
414 strncpy (vl.plugin, "freeswitch", sizeof (vl.plugin));
415 strncpy (vl.plugin_instance, fc->instance, sizeof (vl.plugin_instance));
416 strncpy (vl.type, fm->type, sizeof (vl.type));
417 strncpy (vl.type_instance, fm->instance, sizeof (vl.type_instance));
419 plugin_dispatch_values (&vl);
420 } /* void fs_submit */
422 static int fs_read_command (fs_command_t *fc)
424 DEBUG ("freeswitch plugin: in fs_read_command");
429 /* can't the following be done nicer ? */
431 line = (char *) malloc (strlen(fc->line)+2);
432 snprintf(line, strlen(fc->line)+2, "%s\n\n", fc->line);
433 esl_send_recv(&esl_handle, line);
437 if (esl_handle.last_sr_event && esl_handle.last_sr_event->body)
439 DEBUG ("freeswitch plugin: output from esl (truncated):\n%s\n\n", esl_handle.last_sr_event->body);
441 fc->buffer = strdup(esl_handle.last_sr_event->body);
442 fc->buffer_fill = 1; // ??
445 for (fm = fc->matches; fm != NULL; fm = fm->next)
447 cu_match_value_t *mv;
449 status = match_apply (fm->match, fc->buffer);
452 WARNING ("freeswitch plugin: match_apply failed.");
456 mv = match_get_user_data (fm->match);
459 WARNING ("freeswitch plugin: match_get_user_data returned NULL.");
463 fs_submit (fc, fm, mv);
464 } /* for (fm = fc->matches; fm != NULL; fm = fm->next) */
467 } /* int fs_read_command */
469 static int fs_read (void)
473 DEBUG ("freeswitch plugin: read poll");
475 for (fc = fs_commands_g; fc != NULL; fc = fc->next)
476 fs_read_command (fc);
482 static void *msg_thread_run(esl_thread_t *me, void *obj)
484 esl_handle_t *esl_handle = (esl_handle_t *) obj;
487 // Maybe do some more in this loop later, like receive subscribed events,
488 // and create statistics of them
489 // see fs_cli.c function static void *msg_thread_run(), around line 198
490 while (thread_running && esl_handle->connected)
492 esl_status_t status = esl_recv_event_timed(esl_handle, 10, 1, NULL);
493 if (status == ESL_FAIL)
495 //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
496 DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
504 } */ /* void *msg_thread_run */
506 static int fs_init (void)
508 /* Set some default configuration variables */
509 if (fs_host == NULL) fs_host = FS_DEF_HOST;
510 if (fs_port == NULL) fs_port = FS_DEF_PORT;
511 if (fs_pass == NULL) fs_pass = FS_DEF_PASS;
513 /* Connect to FreeSWITCH over ESL */
514 DEBUG ("freeswitch plugin: making ESL connection to %s %s %s\n", fs_host, fs_port, fs_pass);
515 esl_connect(&esl_handle, fs_host, atoi(fs_port), fs_pass);
517 /* Start a seperate thread for incoming events here */
518 //esl_thread_create_detached(msg_thread_run, &esl_handle);
523 static int fs_shutdown (void)
525 DEBUG ("freeswitch plugin: disconnecting");
526 esl_disconnect(&esl_handle);
528 } /* int fs_shutdown */
530 void module_register (void)
532 plugin_register_complex_config ("freeswitch", fs_complex_config);
533 plugin_register_init ("freeswitch", fs_init);
534 plugin_register_read ("freeswitch", fs_read);
535 plugin_register_shutdown ("freeswitch", fs_shutdown);
536 } /* void module_register */