c666e8bc2ef9fd63b4f94ef9289b987c70bcaff8
[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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 /*
27 #include "utils_match.h"
28 #include <esl.h>
29 */
30
31 #define FREESWITCH_DEF_HOST "127.0.0.1"
32 #define FREESWITCH_DEF_PORT "8021"
33 #define FREESWITCH_DEF_PASSWORD "ClueCon"
34
35 static const char *config_keys[] = 
36 {
37         "Host",
38         "Port",
39         "Password"
40 };
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42
43 typedef struct profilename
44 {
45         char *name;
46         struct profilename *next;
47 } profilename_t;
48
49 // static profilename_t *first_profilename = NULL;
50 static char *freeswitch_host = NULL;
51 static char freeswitch_port[16];
52 static char *freeswitch_password = NULL;
53
54 static void freeswitch_submit (const char *profile, const char *type, gauge_t inbound, gauge_t outbound)
55 {
56         value_t values[2];
57         value_list_t vl = VALUE_LIST_INIT;
58
59         values[0].gauge = inbound;
60         values[1].gauge = outbound;
61
62         vl.values = values;
63         vl.values_len = 2;
64         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
65         sstrncpy (vl.plugin, "freeswitch", sizeof (vl.plugin));
66         sstrncpy (vl.type, type, sizeof (vl.type));
67         sstrncpy (vl.type_instance, profile, sizeof (vl.type_instance));
68
69         plugin_dispatch_values (&vl);
70 } /* void freeswitch_submit */
71
72 static int freeswitch_read (void)
73 {
74         const char *host;
75         const char *port;
76         const char *password;
77
78         host = freeswitch_host;
79         if (host == NULL)
80                 host = FREESWITCH_DEF_HOST;
81
82         port = freeswitch_port;
83         if (port == NULL)
84                 port = FREESWITCH_DEF_PORT;
85
86         password = freeswitch_password;
87         if (password == NULL)
88                 password = FREESWITCH_DEF_PASSWORD;
89
90 /*
91         esl_handle_t handle = {{0}};
92         esl_connect(&handle, host, port, password);
93
94         esl_send_recv(&handle, "api show channels\n\n");
95 */
96         
97         // DO YOUR THING HERE TO PARSE &handle
98         
99 /*
100         esl_disconnect(&handle);
101 */
102
103 DEBUG ("FreeSWITCH SUBMIT: res-public fs_channels 3 5");
104
105         freeswitch_submit ("res-public", "fs_channels", 3, 5);
106
107         return (0);
108 } /* int freeswitch_read */
109
110 static int freeswitch_config (const char *key, const char *value)
111 {
112         if (strcasecmp ("Host", key) == 0)
113         {
114                 if (freeswitch_host != NULL)
115                         free (freeswitch_host);
116                 freeswitch_host = strdup (value);
117         }
118         else if (strcasecmp ("Port", key) == 0)
119         {
120                 int port = (int) (atof (value));
121                 if ((port > 0) && (port <= 65535))
122                         ssnprintf (freeswitch_port, sizeof (freeswitch_port),
123                                         "%i", port);
124                 else
125                         sstrncpy (freeswitch_port, value, sizeof (freeswitch_port));
126         }
127         else if (strcasecmp ("Password", key) == 0)
128         {
129                 if (freeswitch_password != NULL)
130                         free (freeswitch_password);
131                 freeswitch_password = strdup (value);
132         }
133         else
134         {
135                 return (-1);
136         }
137         return (0);
138 } /* int freeswitch_config */
139
140 void module_register (void)
141 {
142         plugin_register_config ("freeswitch", freeswitch_config,
143                         config_keys, config_keys_num);
144         plugin_register_read ("freeswitch", freeswitch_read);
145 } /* void module_register */