Changed the ping-module so it doesn't give up on socket-errors.
[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
28 #define MODULE_NAME "ping"
29
30 #include <netinet/in.h>
31 #include "libping/ping.h"
32
33 #define MAX_PINGHOSTS 32
34
35 static char *hosts[MAX_PINGHOSTS];
36 static int   hosts_flags[MAX_PINGHOSTS];
37 static int   hosts_disable[MAX_PINGHOSTS];
38 static int   hosts_backoff[MAX_PINGHOSTS];
39 static int   num_pinghosts;
40
41 static char *file_template = "ping-%s.rrd";
42
43 static char *ds_def[] = 
44 {
45         "DS:ping:GAUGE:25:0:65535",
46         NULL
47 };
48 static int ds_num = 1;
49
50 static char *config_keys[] =
51 {
52         "Host",
53         NULL
54 };
55 static int config_keys_num = 1;
56
57 static void ping_init (void)
58 {
59         int i;
60
61         for (i = 0; i < MAX_PINGHOSTS; i++)
62         {
63                 hosts_flags[i] = 0;
64                 hosts_disable[i] = 0;
65                 hosts_backoff[i] = 1;
66         }
67
68         return;
69 }
70
71 static int ping_config (char *key, char *value)
72 {
73         if (strcasecmp (key, "host"))
74         {
75                 return (-1);
76         }
77         else if (num_pinghosts >= MAX_PINGHOSTS)
78         {
79                 return (1);
80         }
81         else if ((hosts[num_pinghosts] = strdup (value)) == NULL)
82         {
83                 return (2);
84         }
85         else
86         {
87                 hosts_flags[num_pinghosts] = 0;
88                 num_pinghosts++;
89                 return (0);
90         }
91 }
92
93 static void ping_write (char *host, char *inst, char *val)
94 {
95         char file[512];
96         int status;
97
98         status = snprintf (file, 512, file_template, inst);
99         if (status < 1)
100                 return;
101         else if (status >= 512)
102                 return;
103
104         rrd_update_file (host, file, val, ds_def, ds_num);
105 }
106
107 #define BUFSIZE 256
108 static void ping_submit (int ping_time, char *host)
109 {
110         char buf[BUFSIZE];
111
112         if (snprintf (buf, BUFSIZE, "%u:%u", (unsigned int) curtime, ping_time) >= BUFSIZE)
113                 return;
114
115         plugin_submit (MODULE_NAME, host, buf);
116 }
117 #undef BUFSIZE
118
119 static void ping_read (void)
120 {
121         int ping;
122         int i;
123
124         for (i = 0; i < num_pinghosts; i++)
125         {
126                 if (hosts_disable[i] > 0)
127                 {
128                         hosts_disable[i]--;
129                         continue;
130                 }
131                 
132                 ping = tpinghost (hosts[i]);
133
134                 switch (ping)
135                 {
136                         case 0:
137                                 if (!(hosts_flags[i] & 0x01))
138                                         syslog (LOG_WARNING, "ping %s: Connection timed out.", hosts[i]);
139                                 hosts_flags[i] |= 0x01;
140                                 break;
141
142                         case -1:
143                                 if (!(hosts_flags[i] & 0x02))
144                                         syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", hosts[i]);
145                                 hosts_flags[i] |= 0x02;
146                                 break;
147
148                         case -2:
149                                 syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled for %i iteration(s).",
150                                                 hosts[i], hosts_backoff[i]);
151                                 hosts_disable[i] = hosts_backoff[i];
152                                 if (hosts_backoff[i] < 8192) /* 22 3/4 hours */
153                                         hosts_backoff[i] *= 2;
154                                 hosts_flags[i] |= 0x10;
155                                 break;
156
157                         case -3:
158                                 if (!(hosts_flags[i] & 0x04))
159                                         syslog (LOG_WARNING, "ping %s: Connection refused.", hosts[i]);
160                                 hosts_flags[i] |= 0x04;
161                                 break;
162
163                         default:
164                                 if (hosts_flags[i] != 0x00)
165                                         syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", hosts[i], ping);
166                                 hosts_flags[i] = 0x00;
167                                 hosts_backoff[i] = 1;
168                                 ping_submit (ping, hosts[i]);
169                 } /* switch (ping) */
170         } /* for (i = 0; i < num_pinghosts; i++) */
171 }
172
173 void module_register (void)
174 {
175         plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
176         cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
177 }
178
179 #undef MODULE_NAME