ping plugin: Updated the ping plugin to work with liboping 0.2.2.
[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; 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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
28
29 #define MODULE_NAME "ping"
30
31 #include <netinet/in.h>
32 #include "liboping/oping.h"
33
34 static pingobj_t *pingobj = NULL;
35
36 static char *file_template = "ping-%s.rrd";
37
38 static char *ds_def[] = 
39 {
40         "DS:ping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
41         NULL
42 };
43 static int ds_num = 1;
44
45 static char *config_keys[] =
46 {
47         "Host",
48         "TTL",
49         NULL
50 };
51 static int config_keys_num = 2;
52
53 static void ping_init (void)
54 {
55         return;
56 }
57
58 static int ping_config (char *key, char *value)
59 {
60         if (pingobj == NULL)
61         {
62                 if ((pingobj = ping_construct ()) == NULL)
63                 {
64                         syslog (LOG_ERR, "ping: `ping_construct' failed: %s",
65                                         ping_get_error (pingobj));
66                         return (1);
67                 }
68         }
69
70         if (strcasecmp (key, "host") == 0)
71         {
72                 if (ping_host_add (pingobj, value) < 0)
73                 {
74                         syslog (LOG_WARNING, "ping: `ping_host_add' failed: %s",
75                                         ping_get_error (pingobj));
76                         return (1);
77                 }
78         }
79         else if (strcasecmp (key, "ttl") == 0)
80         {
81                 int ttl = atoi (value);
82                 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
83                 {
84                         syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
85                         return (1);
86                 }
87         }
88         else
89         {
90                 return (-1);
91         }
92
93         return (0);
94 }
95
96 static void ping_write (char *host, char *inst, char *val)
97 {
98         char file[512];
99         int status;
100
101         status = snprintf (file, 512, file_template, inst);
102         if (status < 1)
103                 return;
104         else if (status >= 512)
105                 return;
106
107         rrd_update_file (host, file, val, ds_def, ds_num);
108 }
109
110 #define BUFSIZE 256
111 static void ping_submit (char *host, double latency)
112 {
113         char buf[BUFSIZE];
114
115         if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE)
116                 return;
117
118         plugin_submit (MODULE_NAME, host, buf);
119 }
120 #undef BUFSIZE
121
122 static void ping_read (void)
123 {
124         pingobj_iter_t *iter;
125
126         char   host[512];
127         double latency;
128         size_t buf_len;
129
130         if (pingobj == NULL)
131                 return;
132
133         if (ping_send (pingobj) < 0)
134         {
135                 syslog (LOG_ERR, "ping: `ping_send' failed: %s",
136                                 ping_get_error (pingobj));
137                 return;
138         }
139
140         for (iter = ping_iterator_get (pingobj);
141                         iter != NULL;
142                         iter = ping_iterator_next (iter))
143         {
144                 buf_len = sizeof (host);
145                 if (ping_iterator_get_info (iter, PING_INFO_HOSTNAME,
146                                         host, &buf_len))
147                         continue;
148
149                 buf_len = sizeof (latency);
150                 if (ping_iterator_get_info (iter, PING_INFO_LATENCY,
151                                         &latency, &buf_len))
152                         continue;
153
154                 DBG ("host = %s, latency = %f", host, latency);
155                 ping_submit (host, latency);
156         }
157 }
158
159 void module_register (void)
160 {
161         plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
162         cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
163 }
164
165 #undef MODULE_NAME