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 hl_this = hosts;
75 hl_prev = NULL;
76 while (hl_this != NULL)
77 {
78 DEBUG ("host = %s, wait_left = %i, wait_time = %i, next = %p",
79 hl_this->host, hl_this->wait_left, hl_this->wait_time, (void *) hl_this->next);
81 if (hl_this->wait_left <= 0)
82 {
83 if (ping_host_add (pingobj, hl_this->host) == 0)
84 {
85 DEBUG ("Successfully added host %s", hl_this->host);
86 /* Remove the host from the linked list */
87 if (hl_prev != NULL)
88 hl_prev->next = hl_this->next;
89 else
90 hosts = hl_this->next;
91 free (hl_this->host);
92 free (hl_this);
93 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
94 }
95 else
96 {
97 hl_this->wait_left = hl_this->wait_time;
98 hl_this->wait_time *= 2;
99 if (hl_this->wait_time > 86400)
100 hl_this->wait_time = 86400;
101 }
102 }
103 else
104 {
105 hl_this->wait_left -= interval_g;
106 }
108 if (hl_this != NULL)
109 {
110 hl_prev = hl_this;
111 hl_this = hl_this->next;
112 }
113 }
114 }
116 static int ping_init (void)
117 {
118 if (hosts != NULL)
119 add_hosts ();
121 return (0);
122 }
124 static int ping_config (const char *key, const char *value)
125 {
126 if (pingobj == NULL)
127 {
128 if ((pingobj = ping_construct ()) == NULL)
129 {
130 ERROR ("ping: `ping_construct' failed: %s",
131 ping_get_error (pingobj));
132 return (1);
133 }
134 }
136 if (strcasecmp (key, "host") == 0)
137 {
138 hostlist_t *hl;
139 char *host;
141 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
142 {
143 char errbuf[1024];
144 ERROR ("ping plugin: malloc failed: %s",
145 sstrerror (errno, errbuf,
146 sizeof (errbuf)));
147 return (1);
148 }
149 if ((host = strdup (value)) == NULL)
150 {
151 char errbuf[1024];
152 free (hl);
153 ERROR ("ping plugin: strdup failed: %s",
154 sstrerror (errno, errbuf,
155 sizeof (errbuf)));
156 return (1);
157 }
159 hl->host = host;
160 hl->wait_time = 2 * interval_g;
161 hl->wait_left = 0;
162 hl->next = hosts;
163 hosts = hl;
164 }
165 else if (strcasecmp (key, "ttl") == 0)
166 {
167 int ttl = atoi (value);
168 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
169 {
170 WARNING ("ping: liboping did not accept the TTL value %i", ttl);
171 return (1);
172 }
173 }
174 else
175 {
176 return (-1);
177 }
179 return (0);
180 }
182 static void ping_submit (char *host, double latency)
183 {
184 value_t values[1];
185 value_list_t vl = VALUE_LIST_INIT;
187 values[0].gauge = latency;
189 vl.values = values;
190 vl.values_len = 1;
191 vl.time = time (NULL);
192 strcpy (vl.host, hostname_g);
193 strcpy (vl.plugin, "ping");
194 strcpy (vl.plugin_instance, "");
195 strncpy (vl.type_instance, host, sizeof (vl.type_instance));
197 plugin_dispatch_values ("ping", &vl);
198 }
200 static int ping_read (void)
201 {
202 pingobj_iter_t *iter;
204 char host[512];
205 double latency;
206 size_t buf_len;
208 if (pingobj == NULL)
209 return (-1);
211 if (hosts != NULL)
212 add_hosts ();
214 if (ping_send (pingobj) < 0)
215 {
216 ERROR ("ping: `ping_send' failed: %s",
217 ping_get_error (pingobj));
218 return (-1);
219 }
221 for (iter = ping_iterator_get (pingobj);
222 iter != NULL;
223 iter = ping_iterator_next (iter))
224 {
225 buf_len = sizeof (host);
226 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
227 host, &buf_len))
228 continue;
230 buf_len = sizeof (latency);
231 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
232 &latency, &buf_len))
233 continue;
235 DEBUG ("host = %s, latency = %f", host, latency);
236 ping_submit (host, latency);
237 }
239 return (0);
240 } /* int ping_read */
242 void module_register (modreg_e load)
243 {
244 if (load & MR_DATASETS)
245 plugin_register_data_set (&ds);
247 if (load & MR_READ)
248 {
249 plugin_register_config ("ping", ping_config,
250 config_keys, config_keys_num);
251 plugin_register_init ("ping", ping_init);
252 plugin_register_read ("ping", ping_read);
253 }
254 } /* void module_register */