Replace all syslog-calls with one of the new logging-macros.
[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         int step = atoi (COLLECTD_STEP);
75
76         hl_this = hosts;
77         hl_prev = NULL;
78         while (hl_this != NULL)
79         {
80                 DEBUG ("host = %s, wait_left = %i, wait_time = %i, next = %p",
81                                 hl_this->host, hl_this->wait_left, 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 ("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                                 hl_this->wait_left = hl_this->wait_time;
100                                 hl_this->wait_time *= 2;
101                                 if (hl_this->wait_time > 86400)
102                                         hl_this->wait_time = 86400;
103                         }
104                 }
105                 else
106                 {
107                         hl_this->wait_left -= step;
108                 }
109
110                 if (hl_this != NULL)
111                 {
112                         hl_prev = hl_this;
113                         hl_this = hl_this->next;
114                 }
115         }
116 }
117
118 static int ping_init (void)
119 {
120         if (hosts != NULL)
121                 add_hosts ();
122
123         return (0);
124 }
125
126 static int ping_config (const char *key, const char *value)
127 {
128         if (pingobj == NULL)
129         {
130                 if ((pingobj = ping_construct ()) == NULL)
131                 {
132                         ERROR ("ping: `ping_construct' failed: %s",
133                                         ping_get_error (pingobj));
134                         return (1);
135                 }
136         }
137
138         if (strcasecmp (key, "host") == 0)
139         {
140                 hostlist_t *hl;
141                 char *host;
142                 int step = atoi (COLLECTD_STEP);
143
144                 if ((hl = (hostlist_t *) malloc (sizeof (hostlist_t))) == NULL)
145                 {
146                         ERROR ("ping plugin: malloc failed: %s",
147                                         strerror (errno));
148                         return (1);
149                 }
150                 if ((host = strdup (value)) == NULL)
151                 {
152                         free (hl);
153                         ERROR ("ping plugin: strdup failed: %s",
154                                         strerror (errno));
155                         return (1);
156                 }
157
158                 hl->host = host;
159                 hl->wait_time = 2 * step;
160                 hl->wait_left = 0;
161                 hl->next = hosts;
162                 hosts = hl;
163         }
164         else if (strcasecmp (key, "ttl") == 0)
165         {
166                 int ttl = atoi (value);
167                 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
168                 {
169                         WARNING ("ping: liboping did not accept the TTL value %i", ttl);
170                         return (1);
171                 }
172         }
173         else
174         {
175                 return (-1);
176         }
177
178         return (0);
179 }
180
181 static void ping_submit (char *host, double latency)
182 {
183         value_t values[1];
184         value_list_t vl = VALUE_LIST_INIT;
185
186         values[0].gauge = latency;
187
188         vl.values = values;
189         vl.values_len = 1;
190         vl.time = time (NULL);
191         strcpy (vl.host, hostname_g);
192         strcpy (vl.plugin, "ping");
193         strcpy (vl.plugin_instance, "");
194         strncpy (vl.type_instance, host, sizeof (vl.type_instance));
195
196         plugin_dispatch_values ("ping", &vl);
197 }
198
199 static int ping_read (void)
200 {
201         pingobj_iter_t *iter;
202
203         char   host[512];
204         double latency;
205         size_t buf_len;
206
207         if (pingobj == NULL)
208                 return (-1);
209
210         if (hosts != NULL)
211                 add_hosts ();
212
213         if (ping_send (pingobj) < 0)
214         {
215                 ERROR ("ping: `ping_send' failed: %s",
216                                 ping_get_error (pingobj));
217                 return (-1);
218         }
219
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;
228
229                 buf_len = sizeof (latency);
230                 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
231                                         &latency, &buf_len))
232                         continue;
233
234                 DEBUG ("host = %s, latency = %f", host, latency);
235                 ping_submit (host, latency);
236         }
237
238         return (0);
239 } /* int ping_read */
240
241 void module_register (void)
242 {
243         plugin_register_data_set (&ds);
244         plugin_register_init ("ping", ping_init);
245         plugin_register_read ("ping", ping_read);
246         plugin_register_config ("ping", ping_config, config_keys, config_keys_num);
247 } /* void module_register */