freeswitch plugin: Almost there \?
[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  *                              Regex "\\<CALLS-IN\s+(\d+)\\>"
42  *                              DSType "CounterInc"
43  *                              Type "counter"
44  *                              Instance "calls-in"
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 int fs_config_add_match_dstype (int *dstype_ret, oconfig_item_t *ci)
121 {
122         DEBUG ("freeswitch plugin: in fs_config_add_match_dstype");
123
124         int dstype;
125
126         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
127         {
128                 WARNING ("freeswitch plugin: 'DSType' needs exactly one string argument.");
129                 return (-1);
130         }
131
132         if (strncasecmp ("Gauge", ci->values[0].value.string, strlen ("Gauge")) == 0)
133         {
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;
143                 else
144                         dstype = 0;
145         }
146         else if (strncasecmp ("Counter", ci->values[0].value.string, strlen ("Counter")) == 0)
147         {
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;
155                 else
156                         dstype = 0;
157         }
158         else
159         {
160                 dstype = 0;
161         }
162
163         if (dstype == 0)
164         {
165                 WARNING ("freeswitch plugin: `%s' is not a valid argument to `DSType'.",
166                 ci->values[0].value.string);
167                 return (-1);
168         }
169
170         *dstype_ret = dstype;
171         return (0);
172 } /* int fs_config_add_match_dstype */
173
174 static int fs_config_add_match (fs_command_t *fs_command, oconfig_item_t *ci)
175 {
176         DEBUG ("freeswitch plugin: in fs_config_add_match");
177
178         fs_match_t *fs_match;
179         int status;
180         int i;
181
182         if (ci->values_num != 0)
183         {
184                 WARNING ("freeswitch plugin: Ignoring arguments for the 'Match' block.");
185         }
186
187         fs_match = (fs_match_t *) malloc (sizeof (*fs_match));
188         if (fs_match == NULL)
189         {
190                 ERROR ("freeswitch plugin: malloc failed.");
191                 return (-1);
192         }
193         memset (fs_match, 0, sizeof (*fs_match));
194
195         status = 0;
196         for (i = 0; i < ci->children_num; i++)
197         {
198                 oconfig_item_t *child = ci->children + i;
199
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);
208                 else
209                 {
210                         WARNING ("freeswitch plugin: Option `%s' not allowed here.", child->key);
211                         status = -1;
212                 }
213
214                 if (status != 0)
215                         break;
216         } /* for (i = 0; i < ci->children_num; i++) */
217
218         while (status == 0)
219         {
220                 if (fs_match->regex == NULL)
221                 {
222                         WARNING ("freeswitch plugin: `Regex' missing in `Match' block.");
223                         status = -1;
224                 }
225
226                 if (fs_match->type == NULL)
227                 {
228                         WARNING ("freeswitch plugin: `Type' missing in `Match' block.");
229                         status = -1;
230                 }
231
232                 if (fs_match->dstype == 0)
233                 {
234                         WARNING ("freeswitch plugin: `DSType' missing in `Match' block.");
235                         status = -1;
236                 }
237
238                 break;
239         } /* while (status == 0) */
240
241         if (status != 0)
242         return (status);
243
244         fs_match->match = match_create_simple (fs_match->regex, fs_match->dstype);
245         if (fs_match->match == NULL)
246         {
247                 ERROR ("freeswitch plugin: tail_match_add_match_simple failed.");
248                 fs_match_free (fs_match);
249                 return (-1);
250         }
251         else
252         {
253                 fs_match_t *prev;
254
255                 prev = fs_command->matches;
256                 while ((prev != NULL) && (prev->next != NULL))
257                         prev = prev->next;
258
259                 if (prev == NULL)
260                         fs_command->matches = fs_match;
261                 else
262                         prev->next = fs_match;
263         }
264
265         return (0);
266 } /* int fs_config_add_match */
267
268 static int fs_config_add_command (oconfig_item_t *ci)
269 {
270         DEBUG ("freeswitch plugin: in fs_config_add_command");
271
272         fs_command_t *command;
273         int status;
274         int i;
275
276         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
277         {
278                 WARNING ("freeswitch plugin: 'Command' blocks need exactly one string argument.");
279                 return (-1);
280         }
281
282         command = (fs_command_t *) malloc (sizeof (*command));
283         if (command == NULL)
284         {
285                 ERROR ("freeswitch plugin: malloc failed.");
286                 return (-1);
287         }
288         memset (command, 0, sizeof (*command));
289
290         command->line = NULL;
291         command->line = strdup (ci->values[0].value.string);
292         if (command->line == NULL)
293         {
294                 ERROR ("freeswitch plugin: strdup failed.");
295                 sfree (command);
296                 return (-1);
297         }
298
299         /* Process all children */
300         status = 0;
301         for (i = 0; i < ci->children_num; i++)
302         {
303                 oconfig_item_t *child = ci->children + i;
304
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);
309                 else
310                 {
311                         WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
312                         status = -1;
313                 }
314
315                 if (status != 0)
316                         break;
317         }
318
319         /* Add the new command to the linked list */
320         if (fs_commands_g == NULL)
321                 fs_commands_g = command;
322         else
323         {
324                 fs_command_t *prev;
325
326                 prev = fs_commands_g;
327                 while ((prev != NULL) && (prev->next != NULL))
328                         prev = prev->next;
329                 prev->next = command;
330         }
331
332         return (0);
333 } /* int fs_config_add_command */
334
335 static int fs_complex_config (oconfig_item_t *ci)
336 {
337         int success;
338         int errors;
339         int status;
340         int i;
341
342         DEBUG ("freeswitch plugin: reading config");
343
344         success = 0;
345         errors = 0;
346
347         for (i = 0; i < ci->children_num; i++)
348         {
349                 oconfig_item_t *child = ci->children + i;
350
351                 if (strcasecmp ("Host", child->key) == 0)
352                 {
353                         if (fs_host != NULL) free (fs_host);
354                         fs_host = strdup(child->values[0].value.string);
355                 }
356                 else if (strcasecmp ("Port", child->key) == 0)
357                 {
358                         if (fs_port != NULL) free (fs_port);
359                         fs_port = strdup(child->values[0].value.string);
360                 }
361                 else if (strcasecmp ("Pass", child->key) == 0)
362                 {
363                         if (fs_pass != NULL) free (fs_pass);
364                         fs_pass = strdup(child->values[0].value.string);
365                 }
366                 else if (strcasecmp ("Command", child->key) == 0)
367                 {
368                         status = fs_config_add_command(child);
369                         if (status == 0)
370                                 success++;
371                         else
372                                 errors++;
373                 }
374                 else
375                 {
376                         WARNING ("freeswitch plugin: Option '%s' not allowed here.", child->key);
377                         errors++;
378                 }
379         }
380
381         if ((success == 0) && (errors > 0))
382         {
383                 ERROR ("freeswitch plugin: All statements failed.");
384                 return (-1);
385         }
386
387         return (0);
388 } /* int fs_complex_config */
389
390 static void fs_submit (const fs_command_t *fc,
391         const fs_match_t *fm, const cu_match_value_t *fmv)
392 {
393         DEBUG ("freeswitch plugin: in fs_submit");
394
395         DEBUG ("fc->instance");
396         DEBUG (fc->instance);
397
398         DEBUG ("fm->type");
399         DEBUG (fm->type);
400
401         DEBUG ("fmv->value");
402         DEBUG (fmv->value);
403
404         value_t values[1];
405         value_list_t vl = VALUE_LIST_INIT;
406
407         values[0] = fmv->value;
408
409         vl.values = values;
410         vl.values_len = 1;
411         vl.time = time (NULL);
412
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));
418
419         plugin_dispatch_values (&vl);
420 } /* void fs_submit */
421
422 static int fs_read_command (fs_command_t *fc)
423 {
424         DEBUG ("freeswitch plugin: in fs_read_command");
425
426         fs_match_t *fm;
427         int status;
428
429         /* can't the following be done nicer ? */
430         char *line;
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);
434
435         fc->buffer_fill = 0;
436
437         if (esl_handle.last_sr_event && esl_handle.last_sr_event->body)
438         {
439                 DEBUG ("freeswitch plugin: output from esl (truncated):\n%s\n\n", esl_handle.last_sr_event->body);
440                 sfree(fc->buffer);
441                 fc->buffer = strdup(esl_handle.last_sr_event->body);
442                 fc->buffer_fill = 1; // ??
443         }
444
445         for (fm = fc->matches; fm != NULL; fm = fm->next)
446         {
447                 cu_match_value_t *mv;
448
449                 status = match_apply (fm->match, fc->buffer);
450                 if (status != 0)
451                 {
452                         WARNING ("freeswitch plugin: match_apply failed.");
453                         continue;
454                 }
455
456                 mv = match_get_user_data (fm->match);
457                 if (mv == NULL)
458                 {
459                         WARNING ("freeswitch plugin: match_get_user_data returned NULL.");
460                         continue;
461                 }
462
463                 fs_submit (fc, fm, mv);
464         } /* for (fm = fc->matches; fm != NULL; fm = fm->next) */
465
466         return (0);
467 } /* int fs_read_command */
468
469 static int fs_read (void)
470 {
471         fs_command_t *fc;
472
473         DEBUG ("freeswitch plugin: read poll");
474
475         for (fc = fs_commands_g; fc != NULL; fc = fc->next)
476                 fs_read_command (fc);
477
478         return (0);
479 } /* int fs_read */
480
481 /*
482 static void *msg_thread_run(esl_thread_t *me, void *obj)
483 {
484         esl_handle_t *esl_handle = (esl_handle_t *) obj;
485         thread_running = 1;
486
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)
491         {
492                 esl_status_t status = esl_recv_event_timed(esl_handle, 10, 1, NULL);
493                 if (status == ESL_FAIL)
494                 {
495                         //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
496                         DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
497                         thread_running = 0;
498                 }
499                 usleep(1000);
500         }
501
502         thread_running = 0;
503         return (NULL);
504 } */ /* void *msg_thread_run */
505
506 static int fs_init (void)
507 {
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;
512
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);
516
517         /* Start a seperate thread for incoming events here */
518         //esl_thread_create_detached(msg_thread_run, &esl_handle);
519
520         return(0);
521 } /* int fs_init */
522
523 static int fs_shutdown (void)
524 {
525         DEBUG ("freeswitch plugin: disconnecting");
526         esl_disconnect(&esl_handle);
527         return (0);
528 } /* int fs_shutdown */
529
530 void module_register (void)
531 {
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 */