ping plugin: Improved error and debug messages.
[collectd.git] / src / ping.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 #include <netinet/in.h>
28 #include "liboping/oping.h"
29
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;
41
42 /*
43  * Private variables
44  */
45 static pingobj_t *pingobj = NULL;
46 static hostlist_t *hosts = NULL;
47
48 static data_source_t dsrc[1] =
49 {
50         {"ping", DS_TYPE_GAUGE, 0, 65535.0},
51 };
52
53 static data_set_t ds =
54 {
55         "ping", 1, dsrc
56 };
57
58 static const char *config_keys[] =
59 {
60         "Host",
61         "TTL",
62         NULL
63 };
64 static int config_keys_num = 2;
65
66 /*
67  * Private functions
68  */
69 static void add_hosts (void)
70 {
71         hostlist_t *hl_this;
72         hostlist_t *hl_prev;
73
74         hl_this = hosts;
75         hl_prev = NULL;
76         while (hl_this != NULL)
77         {
78                 DEBUG ("ping plugin: host = %s, wait_left = %i, "
79                                 "wait_time = %i, next = %p",
80                                 hl_this->host, hl_this->wait_left,
81                                 hl_this->wait_time, (void *) hl_this->next);
82
83                 if (hl_this->wait_left <= 0)
84                 {
85                         if (ping_host_add (pingobj, hl_this->host) == 0)
86                         {
87                                 DEBUG ("ping plugin: Successfully added host %s", hl_this->host);
88                                 /* Remove the host from the linked list */
89                                 if (hl_prev != NULL)
90                                         hl_prev->next = hl_this->next;
91                                 else
92                                         hosts = hl_this->next;
93                                 free (hl_this->host);
94                                 free (hl_this);
95                                 hl_this = (hl_prev != NULL) ? hl_prev : hosts;
96                         }
97                         else
98                         {
99                                 WARNING ("ping plugin: Failed adding host "
100                                                 "`%s': %s", hl_this->host,
101                                                 ping_get_error (pingobj));
102                                 hl_this->wait_left = hl_this->wait_time;
103                                 hl_this->wait_time *= 2;
104                                 if (hl_this->wait_time > 86400)
105                                         hl_this->wait_time = 86400;
106                         }
107                 }
108                 else
109                 {
110                         hl_this->wait_left -= interval_g;
111                 }
112
113                 if (hl_this != NULL)
114                 {
115                         hl_prev = hl_this;
116                         hl_this = hl_this->next;
117                 }
118         }
119 }
120
121 static int ping_init (void)
122 {
123         if (hosts != NULL)
124                 add_hosts ();
125
126         return (0);
127 }
128
129 static int ping_config (const char *key, const char *value)
130 {
131         if (pingobj == NULL)
132         {
133                 if ((pingobj = ping_construct ()) == NULL)
134                 {
135                         ERROR ("ping plugin: `ping_construct' failed.");
136                         return (1);
137                 }
138         }
139
140         if (strcasecmp (key, "host") == 0)
141         {
142                 hostlist_t *hl;
143                 char *host;
144
145                 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
146                 {
147                         char errbuf[1024];
148                         ERROR ("ping plugin: malloc failed: %s",
149                                         sstrerror (errno, errbuf,
150                                                 sizeof (errbuf)));
151                         return (1);
152                 }
153                 if ((host = strdup (value)) == NULL)
154                 {
155                         char errbuf[1024];
156                         free (hl);
157                         ERROR ("ping plugin: strdup failed: %s",
158                                         sstrerror (errno, errbuf,
159                                                 sizeof (errbuf)));
160                         return (1);
161                 }
162
163                 hl->host = host;
164                 hl->wait_time = 2 * interval_g;
165                 hl->wait_left = 0;
166                 hl->next = hosts;
167                 hosts = hl;
168         }
169         else if (strcasecmp (key, "ttl") == 0)
170         {
171                 int ttl = atoi (value);
172                 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
173                 {
174                         WARNING ("ping: liboping did not accept the TTL value %i", ttl);
175                         return (1);
176                 }
177         }
178         else
179         {
180                 return (-1);
181         }
182
183         return (0);
184 }
185
186 static void ping_submit (char *host, double latency)
187 {
188         value_t values[1];
189         value_list_t vl = VALUE_LIST_INIT;
190
191         values[0].gauge = latency;
192
193         vl.values = values;
194         vl.values_len = 1;
195         vl.time = time (NULL);
196         strcpy (vl.host, hostname_g);
197         strcpy (vl.plugin, "ping");
198         strcpy (vl.plugin_instance, "");
199         strncpy (vl.type_instance, host, sizeof (vl.type_instance));
200
201         plugin_dispatch_values ("ping", &vl);
202 }
203
204 static int ping_read (void)
205 {
206         pingobj_iter_t *iter;
207
208         char   host[512];
209         double latency;
210         size_t buf_len;
211         int    number_of_hosts;
212
213         if (pingobj == NULL)
214                 return (-1);
215
216         if (hosts != NULL)
217                 add_hosts ();
218
219         if (ping_send (pingobj) < 0)
220         {
221                 ERROR ("ping plugin: `ping_send' failed: %s",
222                                 ping_get_error (pingobj));
223                 return (-1);
224         }
225
226         number_of_hosts = 0;
227         for (iter = ping_iterator_get (pingobj);
228                         iter != NULL;
229                         iter = ping_iterator_next (iter))
230         {
231                 buf_len = sizeof (host);
232                 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
233                                         host, &buf_len))
234                 {
235                         WARNING ("ping plugin: ping_iterator_get_info "
236                                         "(PING_INFO_HOSTNAME) failed.");
237                         continue;
238                 }
239
240                 buf_len = sizeof (latency);
241                 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
242                                         &latency, &buf_len))
243                 {
244                         WARNING ("ping plugin: ping_iterator_get_info (%s, "
245                                         "PING_INFO_LATENCY) failed.", host);
246                         continue;
247                 }
248
249                 DEBUG ("ping plugin: host = %s, latency = %f", host, latency);
250                 ping_submit (host, latency);
251                 number_of_hosts++;
252         }
253
254         if ((number_of_hosts == 0) && (getuid != 0))
255         {
256                 ERROR ("ping plugin: All hosts failed. Try starting collectd as root.");
257         }
258
259         return (number_of_hosts == 0 ? -1 : 0);
260 } /* int ping_read */
261
262 void module_register (modreg_e load)
263 {
264         if (load & MR_DATASETS)
265                 plugin_register_data_set (&ds);
266
267         if (load & MR_READ)
268         {
269                 plugin_register_config ("ping", ping_config,
270                                 config_keys, config_keys_num);
271                 plugin_register_init ("ping", ping_init);
272                 plugin_register_read ("ping", ping_read);
273         }
274 } /* void module_register */