2 * collectd - src/ping.c
3 * Copyright (C) 2005,2006 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
27 #include <netinet/in.h>
28 #include "liboping/oping.h"
38 struct hostlist_s *next;
40 typedef struct hostlist_s hostlist_t;
45 static pingobj_t *pingobj = NULL;
46 static hostlist_t *hosts = NULL;
48 static const char *config_keys[] =
54 static int config_keys_num = 2;
59 static void add_hosts (void)
66 while (hl_this != NULL)
68 DEBUG ("ping plugin: host = %s, wait_left = %i, "
69 "wait_time = %i, next = %p",
70 hl_this->host, hl_this->wait_left,
71 hl_this->wait_time, (void *) hl_this->next);
73 if (hl_this->wait_left <= 0)
75 if (ping_host_add (pingobj, hl_this->host) == 0)
77 DEBUG ("ping plugin: Successfully added host %s", hl_this->host);
78 /* Remove the host from the linked list */
80 hl_prev->next = hl_this->next;
82 hosts = hl_this->next;
85 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
89 WARNING ("ping plugin: Failed adding host "
90 "`%s': %s", hl_this->host,
91 ping_get_error (pingobj));
92 hl_this->wait_left = hl_this->wait_time;
93 hl_this->wait_time *= 2;
94 if (hl_this->wait_time > 86400)
95 hl_this->wait_time = 86400;
100 hl_this->wait_left -= interval_g;
106 hl_this = hl_this->next;
109 } /* void add_hosts */
111 static int ping_init (void)
120 } /* int ping_init */
122 static int ping_config (const char *key, const char *value)
126 if ((pingobj = ping_construct ()) == NULL)
128 ERROR ("ping plugin: `ping_construct' failed.");
133 if (strcasecmp (key, "host") == 0)
138 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
141 ERROR ("ping plugin: malloc failed: %s",
142 sstrerror (errno, errbuf,
146 if ((host = strdup (value)) == NULL)
150 ERROR ("ping plugin: strdup failed: %s",
151 sstrerror (errno, errbuf,
157 hl->wait_time = 2 * interval_g;
162 else if (strcasecmp (key, "ttl") == 0)
164 int ttl = atoi (value);
165 if (ping_setopt (pingobj, PING_OPT_TTL, (void *) &ttl))
167 WARNING ("ping: liboping did not accept the TTL value %i", ttl);
179 static void ping_submit (char *host, double latency)
182 value_list_t vl = VALUE_LIST_INIT;
184 values[0].gauge = latency;
188 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
189 sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
190 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
191 sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
192 sstrncpy (vl.type, "ping", sizeof (vl.type));
194 plugin_dispatch_values (&vl);
197 static int ping_read (void)
199 pingobj_iter_t *iter;
212 if (ping_send (pingobj) < 0)
214 ERROR ("ping plugin: `ping_send' failed: %s",
215 ping_get_error (pingobj));
220 for (iter = ping_iterator_get (pingobj);
222 iter = ping_iterator_next (iter))
224 buf_len = sizeof (host);
225 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
228 WARNING ("ping plugin: ping_iterator_get_info "
229 "(PING_INFO_HOSTNAME) failed.");
233 buf_len = sizeof (latency);
234 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
237 WARNING ("ping plugin: ping_iterator_get_info (%s, "
238 "PING_INFO_LATENCY) failed.", host);
242 DEBUG ("ping plugin: host = %s, latency = %f", host, latency);
243 ping_submit (host, latency);
247 if ((number_of_hosts == 0) && (getuid () != 0))
249 ERROR ("ping plugin: All hosts failed. Try starting collectd as root.");
252 return (number_of_hosts == 0 ? -1 : 0);
253 } /* int ping_read */
255 void module_register (void)
257 plugin_register_config ("ping", ping_config,
258 config_keys, config_keys_num);
259 plugin_register_init ("ping", ping_init);
260 plugin_register_read ("ping", ping_read);
261 } /* void module_register */