ping plugin: Fail if no hosts was pinged.
[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                                 DEBUG ("ping plugin: Failed adding host `%s'", hl_this->host);
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;
104                         }
105                 }
106                 else
107                 {
108                         hl_this->wait_left -= interval_g;
109                 }
110
111                 if (hl_this != NULL)
112                 {
113                         hl_prev = hl_this;
114                         hl_this = hl_this->next;
115                 }
116         }
117 }
118
119 static int ping_init (void)
120 {
121         if (hosts != NULL)
122                 add_hosts ();
123
124         return (0);
125 }
126
127 static int ping_config (const char *key, const char *value)
128 {
129         if (pingobj == NULL)
130         {
131                 if ((pingobj = ping_construct ()) == NULL)
132                 {
133                         ERROR ("ping: `ping_construct' failed: %s",
134                                         ping_get_error (pingobj));
135                         return (1);
136                 }
137         }
138
139         if (strcasecmp (key, "host") == 0)
140         {
141                 hostlist_t *hl;
142                 char *host;
143
144                 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
145                 {
146                         char errbuf[1024];
147                         ERROR ("ping plugin: malloc failed: %s",
148                                         sstrerror (errno, errbuf,
149                                                 sizeof (errbuf)));
150                         return (1);
151                 }
152                 if ((host = strdup (value)) == NULL)
153                 {
154                         char errbuf[1024];
155                         free (hl);
156                         ERROR ("ping plugin: strdup failed: %s",
157                                         sstrerror (errno, errbuf,
158                                                 sizeof (errbuf)));
159                         return (1);
160                 }
161
162                 hl->host = host;
163                 hl->wait_time = 2 * interval_g;
164                 hl->wait_left = 0;
165                 hl->next = hosts;
166                 hosts = hl;
167         }
168         else if (strcasecmp (key, "ttl") == 0)
169         {
170                 int ttl = atoi (value);
171                 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
172                 {
173                         WARNING ("ping: liboping did not accept the TTL value %i", ttl);
174                         return (1);
175                 }
176         }
177         else
178         {
179                 return (-1);
180         }
181
182         return (0);
183 }
184
185 static void ping_submit (char *host, double latency)
186 {
187         value_t values[1];
188         value_list_t vl = VALUE_LIST_INIT;
189
190         values[0].gauge = latency;
191
192         vl.values = values;
193         vl.values_len = 1;
194         vl.time = time (NULL);
195         strcpy (vl.host, hostname_g);
196         strcpy (vl.plugin, "ping");
197         strcpy (vl.plugin_instance, "");
198         strncpy (vl.type_instance, host, sizeof (vl.type_instance));
199
200         plugin_dispatch_values ("ping", &vl);
201 }
202
203 static int ping_read (void)
204 {
205         pingobj_iter_t *iter;
206
207         char   host[512];
208         double latency;
209         size_t buf_len;
210         int    number_of_hosts;
211
212         if (pingobj == NULL)
213                 return (-1);
214
215         if (hosts != NULL)
216                 add_hosts ();
217
218         if (ping_send (pingobj) < 0)
219         {
220                 ERROR ("ping: `ping_send' failed: %s",
221                                 ping_get_error (pingobj));
222                 return (-1);
223         }
224
225         number_of_hosts = 0;
226         for (iter = ping_iterator_get (pingobj);
227                         iter != NULL;
228                         iter = ping_iterator_next (iter))
229         {
230                 buf_len = sizeof (host);
231                 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
232                                         host, &buf_len))
233                         continue;
234
235                 buf_len = sizeof (latency);
236                 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
237                                         &latency, &buf_len))
238                         continue;
239
240                 DEBUG ("ping plugin: host = %s, latency = %f", host, latency);
241                 ping_submit (host, latency);
242                 number_of_hosts++;
243         }
244
245         return (number_of_hosts == 0 ? -1 : 0);
246 } /* int ping_read */
247
248 void module_register (modreg_e load)
249 {
250         if (load & MR_DATASETS)
251                 plugin_register_data_set (&ds);
252
253         if (load & MR_READ)
254         {
255                 plugin_register_config ("ping", ping_config,
256                                 config_keys, config_keys_num);
257                 plugin_register_init ("ping", ping_init);
258                 plugin_register_read ("ping", ping_read);
259         }
260 } /* void module_register */