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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "configfile.h"
27 #include "utils_debug.h"
29 #define MODULE_NAME "ping"
31 #include <netinet/in.h>
32 #include "liboping/oping.h"
39 struct hostlist_s *next;
41 typedef struct hostlist_s hostlist_t;
43 static pingobj_t *pingobj = NULL;
44 static hostlist_t *hosts = NULL;
46 static char *file_template = "ping-%s.rrd";
48 static char *ds_def[] =
50 "DS:ping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
53 static int ds_num = 1;
55 static char *config_keys[] =
61 static int config_keys_num = 2;
63 static void add_hosts (void)
68 int step = atoi (COLLECTD_STEP);
72 while (hl_this != NULL)
74 DBG ("host = %s, wait_left = %i, wait_time = %i, next = %p",
75 hl_this->host, hl_this->wait_left, hl_this->wait_time, (void *) hl_this->next);
77 if (hl_this->wait_left <= 0)
79 if (ping_host_add (pingobj, hl_this->host) == 0)
81 DBG ("Successfully added host %s", hl_this->host);
82 /* Remove the host from the linked list */
84 hl_prev->next = hl_this->next;
86 hosts = hl_this->next;
89 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
93 hl_this->wait_left = hl_this->wait_time;
94 hl_this->wait_time *= 2;
95 if (hl_this->wait_time > 86400)
96 hl_this->wait_time = 86400;
101 hl_this->wait_left -= step;
107 hl_this = hl_this->next;
112 static void ping_init (void)
118 static int ping_config (char *key, char *value)
122 if ((pingobj = ping_construct ()) == NULL)
124 syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
125 ping_get_error (pingobj));
130 if (strcasecmp (key, "host") == 0)
134 int step = atoi (COLLECTD_STEP);
136 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
138 syslog (LOG_ERR, "ping plugin: malloc failed: %s",
142 if ((host = strdup (value)) == NULL)
145 syslog (LOG_ERR, "ping plugin: strdup failed: %s",
151 hl->wait_time = 2 * step;
156 else if (strcasecmp (key, "ttl") == 0)
158 int ttl = atoi (value);
159 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
161 syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
173 static void ping_write (char *host, char *inst, char *val)
178 status = snprintf (file, 512, file_template, inst);
181 else if (status >= 512)
184 rrd_update_file (host, file, val, ds_def, ds_num);
188 static void ping_submit (char *host, double latency)
192 if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE)
195 plugin_submit (MODULE_NAME, host, buf);
199 static void ping_read (void)
201 pingobj_iter_t *iter;
213 if (ping_send (pingobj) < 0)
215 syslog (LOG_ERR, "ping: `ping_send' failed: %s",
216 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,
229 buf_len = sizeof (latency);
230 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
234 DBG ("host = %s, latency = %f", host, latency);
235 ping_submit (host, latency);
239 void module_register (void)
241 plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
242 cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);