3ac7aaba0b5715e220bea8241e50e5e56d119f7a
1 /**
2 * collectd - src/ping.c
3 * Copyright (C) 2005,2006 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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
27 #include <netinet/in.h>
28 #include "liboping/oping.h"
30 /*
31 * Private data types
32 */
33 struct hostlist_s
34 {
35 char *host;
36 int wait_time;
37 int wait_left;
38 struct hostlist_s *next;
39 };
40 typedef struct hostlist_s hostlist_t;
42 /*
43 * Private variables
44 */
45 static pingobj_t *pingobj = NULL;
46 static hostlist_t *hosts = NULL;
48 static data_source_t dsrc[1] =
49 {
50 {"ping", DS_TYPE_GAUGE, 0, 65535.0},
51 };
53 static data_set_t ds =
54 {
55 "ping", 1, dsrc
56 };
58 static const char *config_keys[] =
59 {
60 "Host",
61 "TTL",
62 NULL
63 };
64 static int config_keys_num = 2;
66 /*
67 * Private functions
68 */
69 static void add_hosts (void)
70 {
71 hostlist_t *hl_this;
72 hostlist_t *hl_prev;
74 int step = atoi (COLLECTD_STEP);
76 hl_this = hosts;
77 hl_prev = NULL;
78 while (hl_this != NULL)
79 {
80 DEBUG ("host = %s, wait_left = %i, wait_time = %i, next = %p",
81 hl_this->host, hl_this->wait_left, hl_this->wait_time, (void *) hl_this->next);
83 if (hl_this->wait_left <= 0)
84 {
85 if (ping_host_add (pingobj, hl_this->host) == 0)
86 {
87 DEBUG ("Successfully added host %s", hl_this->host);
88 /* Remove the host from the linked list */
89 if (hl_prev != NULL)
90 hl_prev->next = hl_this->next;
91 else
92 hosts = hl_this->next;
93 free (hl_this->host);
94 free (hl_this);
95 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
96 }
97 else
98 {
99 hl_this->wait_left = hl_this->wait_time;
100 hl_this->wait_time *= 2;
101 if (hl_this->wait_time > 86400)
102 hl_this->wait_time = 86400;
103 }
104 }
105 else
106 {
107 hl_this->wait_left -= step;
108 }
110 if (hl_this != NULL)
111 {
112 hl_prev = hl_this;
113 hl_this = hl_this->next;
114 }
115 }
116 }
118 static int ping_init (void)
119 {
120 if (hosts != NULL)
121 add_hosts ();
123 return (0);
124 }
126 static int ping_config (const char *key, const char *value)
127 {
128 if (pingobj == NULL)
129 {
130 if ((pingobj = ping_construct ()) == NULL)
131 {
132 ERROR ("ping: `ping_construct' failed: %s",
133 ping_get_error (pingobj));
134 return (1);
135 }
136 }
138 if (strcasecmp (key, "host") == 0)
139 {
140 hostlist_t *hl;
141 char *host;
142 int step = atoi (COLLECTD_STEP);
144 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
145 {
146 char errbuf[1024];
147 ERROR ("ping plugin: malloc failed: %s",
148 sstrerror (errno, errbuf,
149 sizeof (errbuf)));
150 return (1);
151 }
152 if ((host = strdup (value)) == NULL)
153 {
154 char errbuf[1024];
155 free (hl);
156 ERROR ("ping plugin: strdup failed: %s",
157 sstrerror (errno, errbuf,
158 sizeof (errbuf)));
159 return (1);
160 }
162 hl->host = host;
163 hl->wait_time = 2 * step;
164 hl->wait_left = 0;
165 hl->next = hosts;
166 hosts = hl;
167 }
168 else if (strcasecmp (key, "ttl") == 0)
169 {
170 int ttl = atoi (value);
171 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
172 {
173 WARNING ("ping: liboping did not accept the TTL value %i", ttl);
174 return (1);
175 }
176 }
177 else
178 {
179 return (-1);
180 }
182 return (0);
183 }
185 static void ping_submit (char *host, double latency)
186 {
187 value_t values[1];
188 value_list_t vl = VALUE_LIST_INIT;
190 values[0].gauge = latency;
192 vl.values = values;
193 vl.values_len = 1;
194 vl.time = time (NULL);
195 strcpy (vl.host, hostname_g);
196 strcpy (vl.plugin, "ping");
197 strcpy (vl.plugin_instance, "");
198 strncpy (vl.type_instance, host, sizeof (vl.type_instance));
200 plugin_dispatch_values ("ping", &vl);
201 }
203 static int ping_read (void)
204 {
205 pingobj_iter_t *iter;
207 char host[512];
208 double latency;
209 size_t buf_len;
211 if (pingobj == NULL)
212 return (-1);
214 if (hosts != NULL)
215 add_hosts ();
217 if (ping_send (pingobj) < 0)
218 {
219 ERROR ("ping: `ping_send' failed: %s",
220 ping_get_error (pingobj));
221 return (-1);
222 }
224 for (iter = ping_iterator_get (pingobj);
225 iter != NULL;
226 iter = ping_iterator_next (iter))
227 {
228 buf_len = sizeof (host);
229 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
230 host, &buf_len))
231 continue;
233 buf_len = sizeof (latency);
234 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
235 &latency, &buf_len))
236 continue;
238 DEBUG ("host = %s, latency = %f", host, latency);
239 ping_submit (host, latency);
240 }
242 return (0);
243 } /* int ping_read */
245 void module_register (void)
246 {
247 plugin_register_data_set (&ds);
248 plugin_register_init ("ping", ping_init);
249 plugin_register_read ("ping", ping_read);
250 plugin_register_config ("ping", ping_config, config_keys, config_keys_num);
251 } /* void module_register */