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; either version 2 of the License, or (at your
8 * option) any later version.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
29 #define MODULE_NAME "ping"
31 #include <netinet/in.h>
32 #include "liboping/oping.h"
34 struct hostlist_s
35 {
36 char *host;
37 int wait_time;
38 int wait_left;
39 struct hostlist_s *next;
40 };
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[] =
49 {
50 "DS:ping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
51 NULL
52 };
53 static int ds_num = 1;
55 static char *config_keys[] =
56 {
57 "Host",
58 "TTL",
59 NULL
60 };
61 static int config_keys_num = 2;
63 static void add_hosts (void)
64 {
65 hostlist_t *hl_this;
66 hostlist_t *hl_prev;
68 int step = atoi (COLLECTD_STEP);
70 hl_this = hosts;
71 hl_prev = NULL;
72 while (hl_this != NULL)
73 {
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)
78 {
79 if (ping_host_add (pingobj, hl_this->host) == 0)
80 {
81 DBG ("Successfully added host %s", hl_this->host);
82 /* Remove the host from the linked list */
83 if (hl_prev != NULL)
84 hl_prev->next = hl_this->next;
85 else
86 hosts = hl_this->next;
87 free (hl_this->host);
88 free (hl_this);
89 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
90 }
91 else
92 {
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;
97 }
98 }
99 else
100 {
101 hl_this->wait_left -= step;
102 }
104 if (hl_this != NULL)
105 {
106 hl_prev = hl_this;
107 hl_this = hl_this->next;
108 }
109 }
110 }
112 static void ping_init (void)
113 {
114 if (hosts != NULL)
115 add_hosts ();
116 }
118 static int ping_config (char *key, char *value)
119 {
120 if (pingobj == NULL)
121 {
122 if ((pingobj = ping_construct ()) == NULL)
123 {
124 syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
125 ping_get_error (pingobj));
126 return (1);
127 }
128 }
130 if (strcasecmp (key, "host") == 0)
131 {
132 hostlist_t *hl;
133 char *host;
134 int step = atoi (COLLECTD_STEP);
136 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
137 {
138 syslog (LOG_ERR, "ping plugin: malloc failed: %s",
139 strerror (errno));
140 return (1);
141 }
142 if ((host = strdup (value)) == NULL)
143 {
144 free (hl);
145 syslog (LOG_ERR, "ping plugin: strdup failed: %s",
146 strerror (errno));
147 return (1);
148 }
150 hl->host = host;
151 hl->wait_time = 2 * step;
152 hl->wait_left = 0;
153 hl->next = hosts;
154 hosts = hl;
155 }
156 else if (strcasecmp (key, "ttl") == 0)
157 {
158 int ttl = atoi (value);
159 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
160 {
161 syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
162 return (1);
163 }
164 }
165 else
166 {
167 return (-1);
168 }
170 return (0);
171 }
173 static void ping_write (char *host, char *inst, char *val)
174 {
175 char file[512];
176 int status;
178 status = snprintf (file, 512, file_template, inst);
179 if (status < 1)
180 return;
181 else if (status >= 512)
182 return;
184 rrd_update_file (host, file, val, ds_def, ds_num);
185 }
187 #define BUFSIZE 256
188 static void ping_submit (char *host, double latency)
189 {
190 char buf[BUFSIZE];
192 if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE)
193 return;
195 plugin_submit (MODULE_NAME, host, buf);
196 }
197 #undef BUFSIZE
199 static void ping_read (void)
200 {
201 pingobj_iter_t *iter;
203 char host[512];
204 double latency;
205 size_t buf_len;
207 if (pingobj == NULL)
208 return;
210 if (hosts != NULL)
211 add_hosts ();
213 if (ping_send (pingobj) < 0)
214 {
215 syslog (LOG_ERR, "ping: `ping_send' failed: %s",
216 ping_get_error (pingobj));
217 return;
218 }
220 for (iter = ping_iterator_get (pingobj);
221 iter != NULL;
222 iter = ping_iterator_next (iter))
223 {
224 buf_len = sizeof (host);
225 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
226 host, &buf_len))
227 continue;
229 buf_len = sizeof (latency);
230 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
231 &latency, &buf_len))
232 continue;
234 DBG ("host = %s, latency = %f", host, latency);
235 ping_submit (host, latency);
236 }
237 }
239 void module_register (void)
240 {
241 plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
242 cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
243 }
245 #undef MODULE_NAME