freeswitch plugin: Commented the seperate thread for incoming messages, maybe use...
[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
27 /*
28 #include "utils_match.h"
29 */
30 #include <esl.h>
31
32 #define FREESWITCH_DEF_HOST "127.0.0.1"
33 #define FREESWITCH_DEF_PORT "8021"
34 #define FREESWITCH_DEF_PASSWORD "ClueCon"
35
36 static const char *config_keys[] = 
37 {
38         "Host",
39         "Port",
40         "Password"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43
44 typedef struct profilename
45 {
46         char *name;
47         struct profilename *next;
48 } profilename_t;
49
50 static esl_handle_t handle = {{0}};
51 // static int thread_running = 0;
52
53 static char *freeswitch_host = NULL;
54 static char freeswitch_port[16];
55 static char *freeswitch_password = NULL;
56
57 static void freeswitch_submit (const char *profile, const char *type, gauge_t inbound, gauge_t outbound)
58 {
59         value_t values[2];
60         value_list_t vl = VALUE_LIST_INIT;
61
62         values[0].gauge = inbound;
63         values[1].gauge = outbound;
64
65         vl.values = values;
66         vl.values_len = 2;
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));
71
72         plugin_dispatch_values (&vl);
73 } /* void freeswitch_submit */
74
75 static int freeswitch_read (void)
76 {
77         const char *host;
78         const char *port;
79         const char *password;
80
81         /* Set some default configuration variables */
82         host = freeswitch_host;
83         if (host == NULL)
84                 host = FREESWITCH_DEF_HOST;
85
86         port = freeswitch_port;
87         if (port == NULL)
88                 port = FREESWITCH_DEF_PORT;
89
90         password = freeswitch_password;
91         if (password == NULL)
92                 password = FREESWITCH_DEF_PASSWORD;
93
94 //
95
96         esl_send_recv(&handle, "api show channels\n\n");
97         if (handle.last_sr_event && handle.last_sr_event->body) {
98                 DEBUG ("OUTPUT FROM FREESWITCH:\n%s\n\n", handle.last_sr_event->body);
99         }
100
101         freeswitch_submit ("res-public", "fs_channels", 3, 5);
102
103         return (0);
104 } /* int freeswitch_read */
105
106 static int freeswitch_config (const char *key, const char *value)
107 {
108         if (strcasecmp ("Host", key) == 0)
109         {
110                 if (freeswitch_host != NULL)
111                         free (freeswitch_host);
112                 freeswitch_host = strdup (value);
113         }
114         else if (strcasecmp ("Port", key) == 0)
115         {
116                 int port = (int) (atof (value));
117                 if ((port > 0) && (port <= 65535))
118                         ssnprintf (freeswitch_port, sizeof (freeswitch_port),
119                                         "%i", port);
120                 else
121                         sstrncpy (freeswitch_port, value, sizeof (freeswitch_port));
122         }
123         else if (strcasecmp ("Password", key) == 0)
124         {
125                 if (freeswitch_password != NULL)
126                         free (freeswitch_password);
127                 freeswitch_password = strdup (value);
128         }
129         else
130         {
131                 return (-1);
132         }
133         return (0);
134 } /* int freeswitch_config */
135
136 /*
137 static void *msg_thread_run(esl_thread_t *me, void *obj)
138 {
139         esl_handle_t *handle = (esl_handle_t *) obj;
140         thread_running = 1;
141
142         // Maybe do some more in this loop later, like receive subscribed events,
143         // and create statistics of them
144         // see fs_cli.c function static void *msg_thread_run(), around line 198
145         while (thread_running && handle->connected)
146         {
147                 esl_status_t status = esl_recv_event_timed(handle, 10, 1, NULL);
148                 if (status == ESL_FAIL)
149                 {
150                         //DEBUG ("Disconnected [%s]\n", ESL_LOG_WARNING); // todo fixit
151                         DEBUG ("Disconnected [%s]\n", "ESL_LOG_WARNING");
152                         thread_running = 0;
153                 }
154                 usleep(1000);
155         }
156
157         thread_running = 0;
158         return (NULL);
159 } */ /* void *msg_thread_run */
160
161 static int freeswitch_init (void)
162 {
163         esl_connect(&handle, "172.16.235.98", 8021, "ClueCon");
164         //esl_thread_create_detached(msg_thread_run, &handle);
165         return(0);
166 } /* int freeswitch_init */
167
168 void module_register (void)
169 {
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 */