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"
26 #include "utils_debug.h"
28 #include <netinet/in.h>
29 #include "liboping/oping.h"
39 struct hostlist_s *next;
41 typedef struct hostlist_s hostlist_t;
46 static pingobj_t *pingobj = NULL;
47 static hostlist_t *hosts = NULL;
49 static data_source_t dsrc[1] =
51 {"ping", DS_TYPE_GAUGE, 0, 65535.0},
54 static data_set_t ds =
59 static const char *config_keys[] =
65 static int config_keys_num = 2;
70 static void add_hosts (void)
75 int step = atoi (COLLECTD_STEP);
79 while (hl_this != NULL)
81 DBG ("host = %s, wait_left = %i, wait_time = %i, next = %p",
82 hl_this->host, hl_this->wait_left, hl_this->wait_time, (void *) hl_this->next);
84 if (hl_this->wait_left <= 0)
86 if (ping_host_add (pingobj, hl_this->host) == 0)
88 DBG ("Successfully added host %s", hl_this->host);
89 /* Remove the host from the linked list */
91 hl_prev->next = hl_this->next;
93 hosts = hl_this->next;
96 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
100 hl_this->wait_left = hl_this->wait_time;
101 hl_this->wait_time *= 2;
102 if (hl_this->wait_time > 86400)
103 hl_this->wait_time = 86400;
108 hl_this->wait_left -= step;
114 hl_this = hl_this->next;
119 static int ping_init (void)
127 static int ping_config (const char *key, const char *value)
131 if ((pingobj = ping_construct ()) == NULL)
133 syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
134 ping_get_error (pingobj));
139 if (strcasecmp (key, "host") == 0)
143 int step = atoi (COLLECTD_STEP);
145 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
147 syslog (LOG_ERR, "ping plugin: malloc failed: %s",
151 if ((host = strdup (value)) == NULL)
154 syslog (LOG_ERR, "ping plugin: strdup failed: %s",
160 hl->wait_time = 2 * step;
165 else if (strcasecmp (key, "ttl") == 0)
167 int ttl = atoi (value);
168 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
170 syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
182 static void ping_submit (char *host, double latency)
185 value_list_t vl = VALUE_LIST_INIT;
187 values[0].gauge = latency;
191 vl.time = time (NULL);
192 strcpy (vl.host, hostname);
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);
200 static int ping_read (void)
202 pingobj_iter_t *iter;
214 if (ping_send (pingobj) < 0)
216 syslog (LOG_ERR, "ping: `ping_send' failed: %s",
217 ping_get_error (pingobj));
221 for (iter = ping_iterator_get (pingobj);
223 iter = ping_iterator_next (iter))
225 buf_len = sizeof (host);
226 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
230 buf_len = sizeof (latency);
231 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
235 DBG ("host = %s, latency = %f", host, latency);
236 ping_submit (host, latency);
240 } /* int ping_read */
242 void module_register (void)
244 plugin_register_data_set (&ds);
245 plugin_register_init ("ping", ping_init);
246 plugin_register_read ("ping", ping_read);
247 plugin_register_config ("ping", ping_config, config_keys, config_keys_num);
248 } /* void module_register */