Add missing kstat definitions
[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/socket.h>
33 #include <sys/un.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36
37 #define ZOOKEEPER_DEF_HOST "127.0.0.1"
38 #define ZOOKEEPER_DEF_PORT "2181"
39
40 static char *zk_host = NULL;
41 static char *zk_port = NULL;
42
43 static const char *config_keys[] =
44 {
45         "Host",
46         "Port"
47 };
48 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
49
50 static int zookeeper_config(const char *key, const char *value)
51 {
52         if (strncmp(key, "Host", strlen("Host")) == 0)
53         {
54                 sfree (zk_host);
55                 zk_host = strdup (value);
56         }
57         else if (strncmp(key, "Port", strlen("Port")) == 0)
58         {
59                 sfree (zk_port);
60                 zk_port = strdup (value);
61         }
62         else
63         {
64                 return -1;
65         }
66         return 0;
67 }
68
69 static void zookeeper_submit_gauge (const char * type, const char * type_inst, gauge_t val)
70 {
71         value_t values[1];
72         value_list_t vl = VALUE_LIST_INIT;
73
74         values[0].gauge = val;
75
76         vl.values = values;
77         vl.values_len = 1;
78         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
79         sstrncpy (vl.plugin, "zookeeper", sizeof (vl.plugin));
80         sstrncpy (vl.type, type, sizeof (vl.type));
81         if (type_inst != NULL)
82                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
83
84         plugin_dispatch_values (&vl);
85 } /* zookeeper_submit_gauge */
86
87 static void zookeeper_submit_derive (const char * type, const char * type_inst, derive_t val)
88 {
89         value_t values[1];
90         value_list_t vl = VALUE_LIST_INIT;
91
92         values[0].derive = val;
93
94         vl.values = values;
95         vl.values_len = 1;
96         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
97         sstrncpy (vl.plugin, "zookeeper", sizeof (vl.plugin));
98         sstrncpy (vl.type, type, sizeof (vl.type));
99         if (type_inst != NULL)
100                 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
101
102         plugin_dispatch_values (&vl);
103 } /* zookeeper_submit_derive */
104
105 static int zookeeper_connect (void)
106 {
107         int sk = -1;
108         int status;
109         struct addrinfo ai_hints;
110         struct addrinfo *ai;
111         struct addrinfo *ai_list;
112         char *host;
113         char *port;
114
115         memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
116         ai_hints.ai_family   = AF_UNSPEC;
117         ai_hints.ai_socktype = SOCK_STREAM;
118
119         host = (zk_host != NULL) ? zk_host : ZOOKEEPER_DEF_HOST;
120         port = (zk_port != NULL) ? zk_port : ZOOKEEPER_DEF_PORT;
121         status = getaddrinfo (host, port, &ai_hints, &ai_list);
122         if (status != 0)
123         {
124                 char errbuf[1024];
125                 INFO ("getaddrinfo failed: %s",
126                           (status == EAI_SYSTEM)
127                           ? sstrerror (errno, errbuf, sizeof (errbuf))
128                           : gai_strerror (status));
129                 return (-1);
130         }
131
132         for (ai = ai_list; ai != NULL; ai = ai->ai_next)
133         {
134                 sk = socket (ai->ai_family, SOCK_STREAM, 0);
135                 if (sk < 0)
136                 {
137                         char errbuf[1024];
138                         WARNING ("zookeeper: socket(2) failed: %s",
139                                          sstrerror (errno, errbuf, sizeof(errbuf)));
140                         continue;
141                 }
142                 status = (int) connect (sk, ai->ai_addr, ai->ai_addrlen);
143                 if (status != 0)
144                 {
145                         char errbuf[1024];
146                         close (sk);
147                         sk = -1;
148                         WARNING ("zookeeper: connect(2) failed: %s",
149                                          sstrerror (errno, errbuf, sizeof(errbuf)));
150                         continue;
151                 }
152
153                 /* connected */
154                 break;
155         }
156
157         freeaddrinfo(ai_list);
158         return (sk);
159 } /* int zookeeper_connect */
160
161 static int zookeeper_query (char *buffer, size_t buffer_size)
162 {
163         int sk = -1;
164         int status;
165         size_t buffer_fill;
166
167         sk = zookeeper_connect();
168         if (sk < 0)
169         {
170                 ERROR ("zookeeper: Could not connect to daemon");
171                 return (-1);
172         }
173
174         status = (int) swrite (sk, "mntr\r\n", strlen("mntr\r\n"));
175         if (status != 0)
176         {
177                 char errbuf[1024];
178                 ERROR ("zookeeper: write(2) failed: %s",
179                            sstrerror (errno, errbuf, sizeof (errbuf)));
180                 close (sk);
181                 return (-1);
182         }
183
184         memset (buffer, 0, buffer_size);
185         buffer_fill = 0;
186
187         while ((status = (int) recv (sk, buffer + buffer_fill,
188           buffer_size - buffer_fill, /* flags = */ 0)) != 0)
189         {
190                 if (status < 0)
191                 {
192                         char errbuf[1024];
193                         if ((errno == EAGAIN) || (errno == EINTR))
194                                 continue;
195                         ERROR ("zookeeper: Error reading from socket: %s",
196                                    sstrerror (errno, errbuf, sizeof (errbuf)));
197                         close (sk);
198                         return (-1);
199                 }
200
201                 buffer_fill += (size_t) status;
202                 if (status == 0)
203                 {
204                         /* done reading from the socket */
205                         break;
206                 }
207         } /* while (recv) */
208
209         status = 0;
210         if (buffer_fill == 0)
211         {
212                 WARNING ("zookeeper: No data returned by MNTR command.");
213                 status = -1;
214         }
215
216         close(sk);
217         return (status);
218 } /* int zookeeper_query */
219
220
221 static int zookeeper_read (void) {
222         char buf[4096];
223         char *ptr;
224         char *save_ptr;
225         char *line;
226         char *fields[2];
227
228         if (zookeeper_query (buf, sizeof (buf)) < 0)
229         {
230                 return (-1);
231         }
232
233         ptr = buf;
234         save_ptr = NULL;
235         while ((line = strtok_r (ptr, "\n\r", &save_ptr)) != NULL)
236         {
237                 ptr = NULL;
238                 if (strsplit(line, fields, 2) != 2)
239                 {
240                         continue;
241                 }
242 #define FIELD_CHECK(check, expected) \
243         (strncmp (check, expected, strlen(expected)) == 0)
244
245                 if (FIELD_CHECK (fields[0], "zk_avg_latency"))
246                 {
247                         zookeeper_submit_gauge ("latency", "avg", atol(fields[1]));
248                 }
249                 else if (FIELD_CHECK(fields[0], "zk_min_latency"))
250                 {
251                         zookeeper_submit_gauge ("latency", "min", atol(fields[1]));
252                 }
253                 else if (FIELD_CHECK (fields[0], "zk_max_latency"))
254                 {
255                         zookeeper_submit_gauge ("latency", "max", atol(fields[1]));
256                 }
257                 else if (FIELD_CHECK (fields[0], "zk_packets_received"))
258                 {
259                         zookeeper_submit_derive ("packets", "received", atol(fields[1]));
260                 }
261                 else if (FIELD_CHECK (fields[0], "zk_packets_sent"))
262                 {
263                         zookeeper_submit_derive ("packets", "sent", atol(fields[1]));
264                 }
265                 else if (FIELD_CHECK (fields[0], "zk_num_alive_connections"))
266                 {
267                         zookeeper_submit_gauge ("current_connections", NULL, atol(fields[1]));
268                 }
269                 else if (FIELD_CHECK (fields[0], "zk_outstanding_requests"))
270                 {
271                         zookeeper_submit_gauge ("requests", "outstanding", atol(fields[1]));
272                 }
273                 else if (FIELD_CHECK (fields[0], "zk_znode_count"))
274                 {
275                         zookeeper_submit_gauge ("gauge", "znode", atol(fields[1]));
276                 }
277                 else if (FIELD_CHECK (fields[0], "zk_watch_count"))
278                 {
279                         zookeeper_submit_gauge ("gauge", "watch", 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_ephemerals_count"))
286                 {
287                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
288                 }
289                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
290                 {
291                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
292                 }
293                 else if (FIELD_CHECK (fields[0], "zk_approximate_data_size"))
294                 {
295                         zookeeper_submit_gauge ("bytes", "approximate_data_size", atol(fields[1]));
296                 }
297                 else if (FIELD_CHECK (fields[0], "zk_followers"))
298                 {
299                         zookeeper_submit_gauge ("count", "followers", atol(fields[1]));
300                 }
301                 else if (FIELD_CHECK (fields[0], "zk_synced_followers"))
302                 {
303                         zookeeper_submit_gauge ("count", "synced_followers", atol(fields[1]));
304                 }
305                 else if (FIELD_CHECK (fields[0], "zk_pending_syncs"))
306                 {
307                         zookeeper_submit_gauge ("count", "pending_syncs", atol(fields[1]));
308                 }
309                 else
310                 {
311                         DEBUG("Uncollected zookeeper MNTR field %s", fields[0]);
312                 }
313         }
314
315         return (0);
316 } /* zookeeper_read */
317
318 void module_register (void)
319 {
320         plugin_register_config ("zookeeper", zookeeper_config, config_keys, config_keys_num);
321         plugin_register_read ("zookeeper", zookeeper_read);
322 } /* void module_register */