Deleted liboping/Makefile because it gets generated.
[collectd.git] / src / ping.c
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  **/
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/liboping.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:25:0:65535",
41         NULL
42 };
43 static int ds_num = 1;
44
45 static char *config_keys[] =
46 {
47         "Host",
48         NULL
49 };
50 static int config_keys_num = 1;
51
52 static void ping_init (void)
53 {
54         return;
55 }
56
57 static int ping_config (char *key, char *value)
58 {
59         if (strcasecmp (key, "host"))
60         {
61                 return (-1);
62         }
63
64         if (pingobj == NULL)
65         {
66                 if ((pingobj = ping_construct ()) == NULL)
67                 {
68                         syslog (LOG_ERR, "ping: `ping_construct' failed.\n");
69                         return (-1);
70                 }
71         }
72
73         if (ping_host_add (pingobj, value) < 0)
74         {
75                 syslog (LOG_ERR, "ping: `ping_host_add' failed.\n");
76                 return (-1);
77         }
78
79         return (0);
80 }
81
82 static void ping_write (char *host, char *inst, char *val)
83 {
84         char file[512];
85         int status;
86
87         status = snprintf (file, 512, file_template, inst);
88         if (status < 1)
89                 return;
90         else if (status >= 512)
91                 return;
92
93         rrd_update_file (host, file, val, ds_def, ds_num);
94 }
95
96 #define BUFSIZE 256
97 static void ping_submit (char *host, double latency)
98 {
99         char buf[BUFSIZE];
100
101         if (snprintf (buf, BUFSIZE, "%u:%f", (unsigned int) curtime, latency) >= BUFSIZE)
102                 return;
103
104         plugin_submit (MODULE_NAME, host, buf);
105 }
106 #undef BUFSIZE
107
108 static void ping_read (void)
109 {
110         pingobj_iter_t *iter;
111
112         char   *host;
113         double  latency;
114
115         if (pingobj == NULL)
116                 return;
117
118         if (ping_send (pingobj) < 0)
119         {
120                 syslog (LOG_ERR, "ping: `ping_send' failed.");
121                 return;
122         }
123
124         for (iter = ping_iterator_get (pingobj); iter != NULL; iter = ping_iterator_next (iter))
125         {
126                 const char *tmp;
127
128                 if ((tmp = ping_iterator_get_host (iter)) == NULL)
129                         continue;
130                 if ((host = strdup (tmp)) == NULL)
131                         continue;
132
133                 latency = ping_iterator_get_latency (iter);
134
135                 DBG ("host = %s, latency = %f", host, latency);
136                 ping_submit (host, latency);
137
138                 free (host); host = NULL;
139         }
140 }
141
142 void module_register (void)
143 {
144         plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
145         cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
146 }
147
148 #undef MODULE_NAME