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