Code

sensors plugin: malloc + memset -> calloc
[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  **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #include <netdb.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
36 #define ZOOKEEPER_DEF_HOST "127.0.0.1"
37 #define ZOOKEEPER_DEF_PORT "2181"
39 static char *zk_host = NULL;
40 static char *zk_port = NULL;
42 static const char *config_keys[] =
43 {
44         "Host",
45         "Port"
46 };
47 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
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 }
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;
73         values[0].gauge = val;
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));
83         plugin_dispatch_values (&vl);
84 } /* zookeeper_submit_gauge */
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;
91         values[0].derive = val;
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));
101         plugin_dispatch_values (&vl);
102 } /* zookeeper_submit_derive */
104 static int zookeeper_connect (void)
106         int sk = -1;
107         int status;
108         struct addrinfo ai_hints;
109         struct addrinfo *ai;
110         struct addrinfo *ai_list;
111         const char *host;
112         const char *port;
114         memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
115         ai_hints.ai_family   = AF_UNSPEC;
116         ai_hints.ai_socktype = SOCK_STREAM;
118         host = (zk_host != NULL) ? zk_host : ZOOKEEPER_DEF_HOST;
119         port = (zk_port != NULL) ? zk_port : ZOOKEEPER_DEF_PORT;
120         status = getaddrinfo (host, port, &ai_hints, &ai_list);
121         if (status != 0)
122         {
123                 char errbuf[1024];
124                 INFO ("getaddrinfo failed: %s",
125                           (status == EAI_SYSTEM)
126                           ? sstrerror (errno, errbuf, sizeof (errbuf))
127                           : gai_strerror (status));
128                 return (-1);
129         }
131         for (ai = ai_list; ai != NULL; ai = ai->ai_next)
132         {
133                 sk = socket (ai->ai_family, SOCK_STREAM, 0);
134                 if (sk < 0)
135                 {
136                         char errbuf[1024];
137                         WARNING ("zookeeper: socket(2) failed: %s",
138                                          sstrerror (errno, errbuf, sizeof(errbuf)));
139                         continue;
140                 }
141                 status = (int) connect (sk, ai->ai_addr, ai->ai_addrlen);
142                 if (status != 0)
143                 {
144                         char errbuf[1024];
145                         close (sk);
146                         sk = -1;
147                         WARNING ("zookeeper: connect(2) failed: %s",
148                                          sstrerror (errno, errbuf, sizeof(errbuf)));
149                         continue;
150                 }
152                 /* connected */
153                 break;
154         }
156         freeaddrinfo(ai_list);
157         return (sk);
158 } /* int zookeeper_connect */
160 static int zookeeper_query (char *buffer, size_t buffer_size)
162         int sk = -1;
163         int status;
164         size_t buffer_fill;
166         sk = zookeeper_connect();
167         if (sk < 0)
168         {
169                 ERROR ("zookeeper: Could not connect to daemon");
170                 return (-1);
171         }
173         status = (int) swrite (sk, "mntr\r\n", strlen("mntr\r\n"));
174         if (status != 0)
175         {
176                 char errbuf[1024];
177                 ERROR ("zookeeper: write(2) failed: %s",
178                            sstrerror (errno, errbuf, sizeof (errbuf)));
179                 close (sk);
180                 return (-1);
181         }
183         memset (buffer, 0, buffer_size);
184         buffer_fill = 0;
186         while ((status = (int) recv (sk, buffer + buffer_fill,
187           buffer_size - buffer_fill, /* flags = */ 0)) != 0)
188         {
189                 if (status < 0)
190                 {
191                         char errbuf[1024];
192                         if ((errno == EAGAIN) || (errno == EINTR))
193                                 continue;
194                         ERROR ("zookeeper: Error reading from socket: %s",
195                                    sstrerror (errno, errbuf, sizeof (errbuf)));
196                         close (sk);
197                         return (-1);
198                 }
200                 buffer_fill += (size_t) status;
201         } /* while (recv) */
203         status = 0;
204         if (buffer_fill == 0)
205         {
206                 WARNING ("zookeeper: No data returned by MNTR command.");
207                 status = -1;
208         }
210         close(sk);
211         return (status);
212 } /* int zookeeper_query */
215 static int zookeeper_read (void) {
216         char buf[4096];
217         char *ptr;
218         char *save_ptr;
219         char *line;
220         char *fields[2];
222         if (zookeeper_query (buf, sizeof (buf)) < 0)
223         {
224                 return (-1);
225         }
227         ptr = buf;
228         save_ptr = NULL;
229         while ((line = strtok_r (ptr, "\n\r", &save_ptr)) != NULL)
230         {
231                 ptr = NULL;
232                 if (strsplit(line, fields, 2) != 2)
233                 {
234                         continue;
235                 }
236 #define FIELD_CHECK(check, expected) \
237         (strncmp (check, expected, strlen(expected)) == 0)
239                 if (FIELD_CHECK (fields[0], "zk_avg_latency"))
240                 {
241                         zookeeper_submit_gauge ("latency", "avg", atol(fields[1]));
242                 }
243                 else if (FIELD_CHECK(fields[0], "zk_min_latency"))
244                 {
245                         zookeeper_submit_gauge ("latency", "min", atol(fields[1]));
246                 }
247                 else if (FIELD_CHECK (fields[0], "zk_max_latency"))
248                 {
249                         zookeeper_submit_gauge ("latency", "max", atol(fields[1]));
250                 }
251                 else if (FIELD_CHECK (fields[0], "zk_packets_received"))
252                 {
253                         zookeeper_submit_derive ("packets", "received", atol(fields[1]));
254                 }
255                 else if (FIELD_CHECK (fields[0], "zk_packets_sent"))
256                 {
257                         zookeeper_submit_derive ("packets", "sent", atol(fields[1]));
258                 }
259                 else if (FIELD_CHECK (fields[0], "zk_num_alive_connections"))
260                 {
261                         zookeeper_submit_gauge ("current_connections", NULL, atol(fields[1]));
262                 }
263                 else if (FIELD_CHECK (fields[0], "zk_outstanding_requests"))
264                 {
265                         zookeeper_submit_gauge ("requests", "outstanding", atol(fields[1]));
266                 }
267                 else if (FIELD_CHECK (fields[0], "zk_znode_count"))
268                 {
269                         zookeeper_submit_gauge ("gauge", "znode", atol(fields[1]));
270                 }
271                 else if (FIELD_CHECK (fields[0], "zk_watch_count"))
272                 {
273                         zookeeper_submit_gauge ("gauge", "watch", atol(fields[1]));
274                 }
275                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
276                 {
277                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
278                 }
279                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
280                 {
281                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
282                 }
283                 else if (FIELD_CHECK (fields[0], "zk_ephemerals_count"))
284                 {
285                         zookeeper_submit_gauge ("gauge", "ephemerals", atol(fields[1]));
286                 }
287                 else if (FIELD_CHECK (fields[0], "zk_approximate_data_size"))
288                 {
289                         zookeeper_submit_gauge ("bytes", "approximate_data_size", atol(fields[1]));
290                 }
291                 else if (FIELD_CHECK (fields[0], "zk_followers"))
292                 {
293                         zookeeper_submit_gauge ("count", "followers", atol(fields[1]));
294                 }
295                 else if (FIELD_CHECK (fields[0], "zk_synced_followers"))
296                 {
297                         zookeeper_submit_gauge ("count", "synced_followers", atol(fields[1]));
298                 }
299                 else if (FIELD_CHECK (fields[0], "zk_pending_syncs"))
300                 {
301                         zookeeper_submit_gauge ("count", "pending_syncs", atol(fields[1]));
302                 }
303                 else
304                 {
305                         DEBUG("Uncollected zookeeper MNTR field %s", fields[0]);
306                 }
307         }
309         return (0);
310 } /* zookeeper_read */
312 void module_register (void)
314         plugin_register_config ("zookeeper", zookeeper_config, config_keys, config_keys_num);
315         plugin_register_read ("zookeeper", zookeeper_read);
316 } /* void module_register */