c5277e456b5cbb21acca13dcb7cf25cf7fec96c7
[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 #include "utils_match.h"
27 #include "esl.h"
28
29 #define FS_DEF_HOST "127.0.0.1"
30 #define FS_DEF_PORT "8021"
31 #define FS_DEF_PASS "ClueCon"
32
33 /*
34  *      <Plugin freeswitch>
35  *              Host "127.0.0.1"
36  *              Port "8021"
37  *              Pass "ClueCon"
38  *              <Command "api sofia status profile res-public">
39  *                      Instance "profile-sofia-res-public"
40  *                      <Match>
41  *                              Instance "calls-in"
42  *                              Regex "CALLS-IN\\s+([0-9]+)"
43  *                              DSType "GaugeLast"
44  *                              Type "gauge"
45  *                      </Match>
46  *              </Command>
47  *      </Plugin>
48  */
49
50 /*
51  * Data types
52  */
53 struct fs_match_s;
54 typedef struct fs_match_s fs_match_t;
55 struct fs_match_s
56 {
57         char *regex;
58         int dstype;
59         char *type;
60         char *instance;
61         cu_match_t *match;
62         fs_match_t *next;
63 };
64
65 struct fs_command_s;
66 typedef struct fs_command_s fs_command_t;
67 struct fs_command_s
68 {
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
74         fs_match_t *matches;
75         fs_command_t *next;
76 };
77
78 static fs_command_t *fs_commands_g = NULL;
79
80 static char *fs_host = NULL;
81 static char *fs_port = NULL;
82 static char *fs_pass = NULL;
83
84 static esl_handle_t esl_handle = {{0}};
85 // static int thread_running = 0; // for when subscribing to esl events
86
87 /*
88  * Private functions
89  */
90
91 static int fs_config_add_string (const char *name, char **dest, oconfig_item_t *ci)
92 {
93         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
94         {
95                 WARNING ("freeswitch plugin: '%s' needs exactly one string argument.", name);
96                 return (-1);
97         }
98
99         sfree (*dest);
100         *dest = strdup (ci->values[0].value.string);
101         if (*dest == NULL)
102                 return (-1);
103
104         return (0);
105 } /* int fs_config_add_string */
106
107 static void fs_match_free (fs_match_t *fm)
108 {
109         if (fm == NULL)
110                 return;
111
112         sfree (fm->regex);
113         sfree (fm->type);
114         sfree (fm->instance);
115         match_destroy (fm->match);
116         fs_match_free (fm->next);
117         sfree (fm);
118 } /* void fs_match_free */
119
120 static void fs_command_free (fs_command_t *fc)
121 {
122         if (fc == NULL)
123                 return;
124
125         sfree (fc->line);
126         sfree (fc->instance);
127         sfree (fc->buffer);
128         fs_match_free (fc->matches);
129         fs_command_free (fc->next);
130         sfree (fc);
131 } /* void fs_command_free */
132
133 static int fs_config_add_match_dstype (int *dstype_ret, oconfig_item_t *ci)
134 {
135         int dstype;
136
137         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
138         {
139                 WARNING ("freeswitch plugin: 'DSType' needs exactly one string argument.");
140                 return (-1);
141         }
142
143         if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
144         {
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;
154                 else
155                         dstype = 0;
156         }
157         else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
158         {
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;
166                 else
167                         dstype = 0;
168         }
169         else
170         {
171                 dstype = 0;
172         }
173
174         if (dstype == 0)
175         {
176                 WARNING ("freeswitch plugin: `%s' is not a valid argument to `DSType'.",
177                 ci->values[0].value.string);
178                 return (-1);
179         }
180
181         *dstype_ret = dstype;
182         return (0);
183 } /* int fs_config_add_match_dstype */
184
185 static int fs_config_add_match (fs_command_t *fs_command, oconfig_item_t *ci)
186 {
187         fs_match_t *fs_match;
188         int status;
189         int i;
190
191         if (ci->values_num != 0)
192         {
193                 WARNING ("freeswitch plugin: Ignoring arguments for the 'Match' block.");
194         }
195
196         fs_match = (fs_match_t *) malloc (sizeof (*fs_match));
197         if (fs_match == NULL)
198         {
199                 ERROR ("freeswitch plugin: malloc failed.");
200                 return (-1);
201         }
202         memset (fs_match, 0, sizeof (*fs_match));
203
204         status = 0;
205         for (i = 0; i < ci->children_num; i++)
206         {
207                 oconfig_item_t *child = ci->children + i;
208
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);
217                 else
218                 {
219                         WARNING ("freeswitch plugin: Option `%s' not allowed here.", child->key);
220                         status = -1;
221                 }
222
223                 if (status != 0)
224                         break;
225         } /* for (i = 0; i < ci->children_num; i++) */
226
227         while (status == 0)
228         {
229                 if (fs_match->regex == NULL)
230                 {
231                         WARNING ("freeswitch plugin: `Regex' missing in `Match' block.");
232                         status = -1;
233                 }
234
235                 if (fs_match->type == NULL)
236                 {
237                         WARNING ("freeswitch plugin: `Type' missing in `Match' block.");
238                         status = -1;
239                 }
240
241                 if (fs_match->dstype == 0)
242                 {
243                         WARNING ("freeswitch plugin: `DSType' missing in `Match' block.");
244                         status = -1;
245                 }
246
247                 break;
248         } /* while (status == 0) */
249
250         if (status != 0)
251         return (status);
252
253         fs_match->match = match_create_simple (fs_match->regex, fs_match->dstype);
254         if (fs_match->match == NULL)
255         {
256                 ERROR ("freeswitch plugin: tail_match_add_match_simple failed.");
257                 fs_match_free (fs_match);
258                 return (-1);
259         }
260         else
261         {
262                 fs_match_t *prev;
263
264                 prev = fs_command->matches;
265                 while ((prev != NULL) && (prev->next != NULL))
266                         prev = prev->next;
267
268                 if (prev == NULL)
269                         fs_command->matches = fs_match;
270                 else
271                         prev->next = fs_match;
272         }
273
274         return (0);
275 } /* int fs_config_add_match */
276
277 static int fs_config_add_command (oconfig_item_t *ci)
278 {
279         fs_command_t *command;
280         int status;
281         int i;
282
283         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
284         {
285                 WARNING ("freeswitch plugin: 'Command' blocks need exactly one string argument.");
286                 return (-1);
287         }
288
289         command = (fs_command_t *) malloc (sizeof (*command));
290         if (command == NULL)
291         {
292                 ERROR ("freeswitch plugin: malloc failed.");
293                 return (-1);
294         }
295         memset (command, 0, sizeof (*command));
296
297         command->line = NULL;
298         command->line = strdup (ci->values[0].value.string);
299
300         if (command->line == NULL)
301         {
302                 ERROR ("freeswitch plugin: strdup failed.");
303                 sfree (command);
304                 return (-1);
305         }
306
307         /* Process all children */
308         status = 0;
309         for (i = 0; i < ci->children_num; i++)
310         {
311                 oconfig_item_t *child = ci->children + i;
312
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);
317                 else
318                 {
319                         WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
320                         status = -1;
321                 }
322
323                 if (status != 0)
324                         break;
325         }
326
327         if (status != 0)
328         {
329                 fs_command_free (command);
330                 return (status);
331         }
332
333         /* Add the new command to the linked list */
334         if (fs_commands_g == NULL)
335                 fs_commands_g = command;
336         else
337         {
338                 fs_command_t *prev;
339
340                 prev = fs_commands_g;
341                 while ((prev != NULL) && (prev->next != NULL))
342                         prev = prev->next;
343                 prev->next = command;
344         }
345
346         return (0);
347 } /* int fs_config_add_command */
348
349 static int fs_complex_config (oconfig_item_t *ci)
350 {
351         int success;
352         int errors;
353         int status;
354         int i;
355
356         success = 0;
357         errors = 0;
358
359         for (i = 0; i < ci->children_num; i++)
360         {
361                 oconfig_item_t *child = ci->children + i;
362
363                 if (strcasecmp ("Host", child->key) == 0)
364                 {
365                         if (fs_host != NULL) free (fs_host);
366                         fs_host = strdup(child->values[0].value.string);
367                 }
368                 else if (strcasecmp ("Port", child->key) == 0)
369                 {
370                         if (fs_port != NULL) free (fs_port);
371                         fs_port = strdup(child->values[0].value.string);
372                 }
373                 else if (strcasecmp ("Pass", child->key) == 0)
374                 {
375                         if (fs_pass != NULL) free (fs_pass);
376                         fs_pass = strdup(child->values[0].value.string);
377                 }
378                 else if (strcasecmp ("Command", child->key) == 0)
379                 {
380                         status = fs_config_add_command(child);
381                         if (status == 0)
382                                 success++;
383                         else
384                                 errors++;
385                 }
386                 else
387                 {
388                         WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
389                         errors++;
390                 }
391         }
392
393         if ((success == 0) && (errors > 0))
394         {
395                 ERROR ("freeswitch plugin: All statements failed.");
396                 return (-1);
397         }
398
399         return (0);
400 } /* int fs_complex_config */
401
402 static void fs_submit (const fs_command_t *fc,
403         const fs_match_t *fm, const cu_match_value_t *mv)
404 {
405         value_t values[1];
406         value_list_t vl = VALUE_LIST_INIT;
407
408         values[0] = mv->value;
409
410         vl.values = values;
411         vl.values_len = 1;
412         vl.time = time (NULL);
413
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));
419
420         plugin_dispatch_values (&vl);
421 } /* void fs_submit */
422
423 static int fs_read_command (fs_command_t *fc)
424 {
425         fs_match_t *fm;
426         int status;
427
428         /* can't the following be done nicer ? */
429         char *line;
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);
433
434         fc->buffer_fill = 0;
435
436         if (esl_handle.last_sr_event && esl_handle.last_sr_event->body)
437         {
438                 sfree(fc->buffer);
439                 fc->buffer = strdup(esl_handle.last_sr_event->body);
440                 fc->buffer_size = strlen(fc->buffer);
441                 fc->buffer_fill = 1;
442         }
443
444         for (fm = fc->matches; fm != NULL; fm = fm->next)
445         {
446                 cu_match_value_t *mv;
447
448 printf("BUFFER SIZE = %lu\n", fc->buffer_size);
449
450 printf("### Trying match\n");
451
452                 status = match_apply (fm->match, fc->buffer);
453                 if (status != 0)
454                 {
455                         WARNING ("freeswitch plugin: match_apply failed.");
456                         continue;
457                 }
458
459                 mv = match_get_user_data (fm->match);
460                 if (mv == NULL)
461                 {
462                         WARNING ("freeswitch plugin: match_get_user_data returned NULL.");
463                         continue;
464                 }
465
466                 fs_submit (fc, fm, mv);
467         } /* for (fm = fc->matches; fm != NULL; fm = fm->next) */
468
469         return (0);
470 } /* int fs_read_command */
471
472 static int fs_read (void)
473 {
474         fs_command_t *fc;
475
476         for (fc = fs_commands_g; fc != NULL; fc = fc->next)
477                 fs_read_command (fc);
478
479         return (0);
480 } /* int fs_read */
481
482 /*
483 static void *msg_thread_run(esl_thread_t *me, void *obj)
484 {
485         esl_handle_t *esl_handle = (esl_handle_t *) obj;
486         thread_running = 1;
487
488         // Maybe do some more in this loop later, like receive subscribed events,
489         // and create statistics of them
490         // see fs_cli.c function static void *msg_thread_run(), around line 198
491         while (thread_running && esl_handle->connected)
492         {
493                 esl_status_t status = esl_recv_event_timed(esl_handle, 10, 1, NULL);
494                 if (status == ESL_FAIL)
495                 {
496                         //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
497                         DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
498                         thread_running = 0;
499                 }
500                 usleep(1000);
501         }
502
503         thread_running = 0;
504         return (NULL);
505 } */ /* void *msg_thread_run */
506
507 static int fs_init (void)
508 {
509         /* Set some default configuration variables */
510         if (fs_host == NULL) fs_host = FS_DEF_HOST;
511         if (fs_port == NULL) fs_port = FS_DEF_PORT;
512         if (fs_pass == NULL) fs_pass = FS_DEF_PASS;
513
514         /* Connect to FreeSWITCH over ESL */
515         DEBUG ("freeswitch plugin: making ESL connection to %s %s %s\n", fs_host, fs_port, fs_pass);
516         esl_connect(&esl_handle, fs_host, atoi(fs_port), fs_pass);
517
518         /* Start a seperate thread for incoming events here */
519         //esl_thread_create_detached(msg_thread_run, &esl_handle);
520
521         return(0);
522 } /* int fs_init */
523
524 static int fs_shutdown (void)
525 {
526         DEBUG ("freeswitch plugin: disconnecting");
527         esl_disconnect(&esl_handle);
528         fs_command_free (fs_commands_g);
529         fs_commands_g = NULL;
530         return (0);
531 } /* int fs_shutdown */
532
533 void module_register (void)
534 {
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 */