a236c8ae66404bd5173c0be7e2537246e34511f4
[collectd.git] / src / zookeeper.c
1 /**
2  * collectd - src/zookeeper.c
3  * Copyright (C) 2014       Google, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Jeremy Katz <jeremy at katzbox.net>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30
31 #include <netdb.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35
36 #define ZOOKEEPER_DEF_HOST "127.0.0.1"
37 #define ZOOKEEPER_DEF_PORT "2181"
38
39 static char *zk_host = NULL;
40 static char *zk_port = NULL;
41
42 static const char *config_keys[] =
43 {
44         "Host",
45         "Port"
46 };
47 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
48
49 static int zookeeper_config(const char *key, const char *value)
50 {
51         if (strncmp(key, "Host", strlen("Host")) == 0)
52         {
53                 sfree (zk_host);
54                 zk_host = strdup (value);
55         }
56         else if (strncmp(key, "Port", strlen("Port")) == 0)
57         {
58                 sfree (zk_port);
59                 zk_port = strdup (value);
60         }
61         else
62         {
63                 return -1;
64         }
65         return 0;
66 }
67
68 static void zookeeper_submit_gauge (const char * type, const char * type_inst, gauge_t val)
69 {
70         value_t values[1];
71         value_list_t vl = VALUE_LIST_INIT;
72
73         values[0].gauge = val;
74
75         vl.values = values;
76         vl.values_len = 1;
77         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
78         sstrncpy (vl.plugin, "zookeeper", sizeof (vl.plugin));
79         sstrncpy (vl.type, type, sizeof (vl.type));
80         if (type_inst != NULL)
81                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
82
83         plugin_dispatch_values (&vl);
84 } /* zookeeper_submit_gauge */
85
86 static void zookeeper_submit_derive (const char * type, const char * type_inst, derive_t val)
87 {
88         value_t values[1];
89         value_list_t vl = VALUE_LIST_INIT;
90
91         values[0].derive = val;
92
93         vl.values = values;
94         vl.values_len = 1;
95         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
96         sstrncpy (vl.plugin, "zookeeper", sizeof (vl.plugin));
97         sstrncpy (vl.type, type, sizeof (vl.type));
98         if (type_inst != NULL)
99                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
100
101         plugin_dispatch_values (&vl);
102 } /* zookeeper_submit_derive */
103
104 static int zookeeper_connect (void)
105 {
106         int sk = -1;
107         int status;
108         struct addrinfo ai_hints = { 0 };
109         struct addrinfo *ai;
110         struct addrinfo *ai_list;
111         const char *host;
112         const char *port;
113
114         ai_hints.ai_family   = AF_UNSPEC;
115         ai_hints.ai_socktype = SOCK_STREAM;
116
117         host = (zk_host != NULL) ? zk_host : ZOOKEEPER_DEF_HOST;
118         port = (zk_port != NULL) ? zk_port : ZOOKEEPER_DEF_PORT;
119         status = getaddrinfo (host, port, &ai_hints, &ai_list);
120         if (status != 0)
121         {
122                 char errbuf[1024];
123                 INFO ("getaddrinfo failed: %s",
124                           (status == EAI_SYSTEM)
125                           ? sstrerror (errno, errbuf, sizeof (errbuf))
126                           : gai_strerror (status));
127                 return (-1);
128         }
129
130         for (ai = ai_list; ai != NULL; ai = ai->ai_next)
131         {
132                 sk = socket (ai->ai_family, SOCK_STREAM, 0);
133                 if (sk < 0)
134                 {
135                         char errbuf[1024];
136                         WARNING ("zookeeper: socket(2) failed: %s",
137                                          sstrerror (errno, errbuf, sizeof(errbuf)));
138                         continue;
139                 }
140                 status = (int) connect (sk, ai->ai_addr, ai->ai_addrlen);
141                 if (status != 0)
142                 {
143                         char errbuf[1024];
144                         close (sk);
145                         sk = -1;
146                         WARNING ("zookeeper: connect(2) failed: %s",
147                                          sstrerror (errno, errbuf, sizeof(errbuf)));
148                         continue;
149                 }
150
151                 /* connected */
152                 break;
153         }
154
155         freeaddrinfo(ai_list);
156         return (sk);
157 } /* int zookeeper_connect */
158
159 static int zookeeper_query (char *buffer, size_t buffer_size)
160 {
161         int sk, status;
162         size_t buffer_fill;
163
164         sk = zookeeper_connect();
165         if (sk < 0)
166         {
167                 ERROR ("zookeeper: Could not connect to daemon");
168                 return (-1);
169         }
170
171         status = (int) swrite (sk, "mntr\r\n", strlen("mntr\r\n"));
172         if (status != 0)
173         {
174                 char errbuf[1024];
175                 ERROR ("zookeeper: write(2) failed: %s",
176                            sstrerror (errno, errbuf, sizeof (errbuf)));
177                 close (sk);
178                 return (-1);
179         }
180
181         memset (buffer, 0, buffer_size);
182         buffer_fill = 0;
183
184         while ((status = (int) recv (sk, buffer + buffer_fill,
185           buffer_size - buffer_fill, /* flags = */ 0)) != 0)
186         {
187                 if (status < 0)
188                 {
189                         char errbuf[1024];
190                         if ((errno == EAGAIN) || (errno == EINTR))
191                                 continue;
192                         ERROR ("zookeeper: Error reading from socket: %s",
193                                    sstrerror (errno, errbuf, sizeof (errbuf)));
194                         close (sk);
195                         return (-1);
196                 }
197
198                 buffer_fill += (size_t) status;
199         } /* while (recv) */
200
201         status = 0;
202         if (buffer_fill == 0)
203         {
204                 WARNING ("zookeeper: No data returned by MNTR command.");
205                 status = -1;
206         }
207
208         close(sk);
209         return (status);
210 } /* int zookeeper_query */
211
212
213 static int zookeeper_read (void) {
214         char buf[4096];
215         char *ptr;
216         char *save_ptr;
217         char *line;
218         char *fields[2];
219
220         if (zookeeper_query (buf, sizeof (buf)) < 0)
221         {
222                 return (-1);
223         }
224
225         ptr = buf;
226         save_ptr = NULL;
227         while ((line = strtok_r (ptr, "\n\r", &save_ptr)) != NULL)
228         {
229                 ptr = NULL;
230                 if (strsplit(line, fields, 2) != 2)
231                 {
232                         continue;
233                 }
234 #define FIELD_CHECK(check, expected) \
235         (strncmp (check, expected, strlen(expected)) == 0)
236
237                 if (FIELD_CHECK (fields[0], "zk_avg_latency"))
238                 {
239                         zookeeper_submit_gauge ("latency", "avg", atol(fields[1]));
240                 }
241                 else if (FIELD_CHECK(fields[0], "zk_min_latency"))
242                 {
243                         zookeeper_submit_gauge ("latency", "min", atol(fields[1]));
244                 }
245                 else if (FIELD_CHECK (fields[0], "zk_max_latency"))
246                 {
247                         zookeeper_submit_gauge ("latency", "max", atol(fields[1]));
248                 }
249                 else if (FIELD_CHECK (fields[0], "zk_packets_received"))
250                 {
251                         zookeeper_submit_derive ("packets", "received", atol(fields[1]));
252                 }
253                 else if (FIELD_CHECK (fields[0], "zk_packets_sent"))
254                 {
255                         zookeeper_submit_derive ("packets", "sent", atol(fields[1]));
256                 }
257                 else if (FIELD_CHECK (fields[0], "zk_num_alive_connections"))
258                 {
259                         zookeeper_submit_gauge ("current_connections", NULL, atol(fields[1]));
260                 }
261                 else if (FIELD_CHECK (fields[0], "zk_outstanding_requests"))
262                 {
263                         zookeeper_submit_gauge ("requests", "outstanding", atol(fields[1]));
264                 }
265                 else if (FIELD_CHECK (fields[0], "zk_znode_count"))
266                 {
267                         zookeeper_submit_gauge ("gauge", "znode", atol(fields[1]));
268                 }
269                 else if (FIELD_CHECK (fields[0], "zk_watch_count"))
270                 {
271                         zookeeper_submit_gauge ("gauge", "watch", atol(fields[1]));
272                 }
273                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
274                 {
275                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
276                 }
277                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
278                 {
279                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
280                 }
281                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
282                 {
283                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
284                 }
285                 else if (FIELD_CHECK (fields[0], "zk_approximate_data_size"))
286                 {
287                         zookeeper_submit_gauge ("bytes", "approximate_data_size", atol(fields[1]));
288                 }
289                 else if (FIELD_CHECK (fields[0], "zk_followers"))
290                 {
291                         zookeeper_submit_gauge ("count", "followers", atol(fields[1]));
292                 }
293                 else if (FIELD_CHECK (fields[0], "zk_synced_followers"))
294                 {
295                         zookeeper_submit_gauge ("count", "synced_followers", atol(fields[1]));
296                 }
297                 else if (FIELD_CHECK (fields[0], "zk_pending_syncs"))
298                 {
299                         zookeeper_submit_gauge ("count", "pending_syncs", atol(fields[1]));
300                 }
301                 else
302                 {
303                         DEBUG("Uncollected zookeeper MNTR field %s", fields[0]);
304                 }
305         }
306
307         return (0);
308 } /* zookeeper_read */
309
310 void module_register (void)
311 {
312         plugin_register_config ("zookeeper", zookeeper_config, config_keys, config_keys_num);
313         plugin_register_read ("zookeeper", zookeeper_read);
314 } /* void module_register */