1 /**
2 * collectd - src/ping.c
3 * Copyright (C) 2005 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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
29 #define MODULE_NAME "ping"
31 #include <netinet/in.h>
32 #include "liboping/liboping.h"
34 static pingobj_t *pingobj = NULL;
36 static char *file_template = "ping-%s.rrd";
38 static char *ds_def[] =
39 {
40 "DS:ping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
41 NULL
42 };
43 static int ds_num = 1;
45 static char *config_keys[] =
46 {
47 "Host",
48 "TTL",
49 NULL
50 };
51 static int config_keys_num = 2;
53 static void ping_init (void)
54 {
55 return;
56 }
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.\n");
65 return (1);
66 }
67 }
69 if (strcasecmp (key, "host") == 0)
70 {
71 if (ping_host_add (pingobj, value) < 0)
72 {
73 syslog (LOG_WARNING, "ping: `ping_host_add' failed.");
74 return (1);
75 }
76 }
77 else if (strcasecmp (key, "ttl") == 0)
78 {
79 int ttl = atoi (value);
80 if (ping_setopt (pingobj, PING_DEF_TIMEOUT, (void *) &ttl))
81 {
82 syslog (LOG_WARNING, "ping: liboping did not accept the TTL value %i", ttl);
83 return (1);
84 }
85 }
86 else
87 {
88 return (-1);
89 }
91 return (0);
92 }
94 static void ping_write (char *host, char *inst, char *val)
95 {
96 char file[512];
97 int status;
99 status = snprintf (file, 512, file_template, inst);
100 if (status < 1)
101 return;
102 else if (status >= 512)
103 return;
105 rrd_update_file (host, file, val, ds_def, ds_num);
106 }
108 #define BUFSIZE 256
109 static void ping_submit (char *host, double latency)
110 {
111 char buf[BUFSIZE];
113 if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE)
114 return;
116 plugin_submit (MODULE_NAME, host, buf);
117 }
118 #undef BUFSIZE
120 static void ping_read (void)
121 {
122 pingobj_iter_t *iter;
124 char *host;
125 double latency;
127 if (pingobj == NULL)
128 return;
130 if (ping_send (pingobj) < 0)
131 {
132 syslog (LOG_ERR, "ping: `ping_send' failed.");
133 return;
134 }
136 for (iter = ping_iterator_get (pingobj); iter != NULL; iter = ping_iterator_next (iter))
137 {
138 const char *tmp;
140 if ((tmp = ping_iterator_get_host (iter)) == NULL)
141 continue;
142 if ((host = strdup (tmp)) == NULL)
143 continue;
145 latency = ping_iterator_get_latency (iter);
147 DBG ("host = %s, latency = %f", host, latency);
148 ping_submit (host, latency);
150 free (host); host = NULL;
151 }
152 }
154 void module_register (void)
155 {
156 plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
157 cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
158 }
160 #undef MODULE_NAME