Changed backport of `ping.c' so it uses global variables again.
[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 "ping.h"
24
25 #if COLLECT_PING
26 #define MODULE_NAME "ping"
27
28 #include "plugin.h"
29 #include "common.h"
30
31 #include <netinet/in.h>
32 #include "libping/ping.h"
33
34 extern char *pinghosts[MAX_PINGHOSTS];
35 extern int   num_pinghosts;
36 static int   hosts_flags[MAX_PINGHOSTS];
37 static int   hosts_disable[MAX_PINGHOSTS];
38 static int   hosts_backoff[MAX_PINGHOSTS];
39
40 static char *file_template = "ping-%s.rrd";
41
42 static char *ds_def[] = 
43 {
44         "DS:ping:GAUGE:25:0:65535",
45         NULL
46 };
47 static int ds_num = 1;
48
49 void ping_init (void)
50 {
51         int i;
52
53         for (i = 0; i < MAX_PINGHOSTS; i++)
54         {
55                 hosts_flags[i] = 0;
56                 hosts_disable[i] = 0;
57                 hosts_backoff[i] = 1;
58         }
59
60         return;
61 }
62
63 void ping_write (char *host, char *inst, char *val)
64 {
65         char file[512];
66         int status;
67
68         status = snprintf (file, 512, file_template, inst);
69         if (status < 1)
70                 return;
71         else if (status >= 512)
72                 return;
73
74         rrd_update_file (host, file, val, ds_def, ds_num);
75 }
76
77 #define BUFSIZE 256
78 void ping_submit (int ping_time, char *host)
79 {
80         char buf[BUFSIZE];
81
82         if (snprintf (buf, BUFSIZE, "%u:%u", (unsigned int) curtime, ping_time) >= BUFSIZE)
83                 return;
84
85         plugin_submit (MODULE_NAME, host, buf);
86 }
87 #undef BUFSIZE
88
89 void ping_read (void)
90 {
91         int ping;
92         int i;
93
94         for (i = 0; i < num_pinghosts; i++)
95         {
96                 if (hosts_disable[i] > 0)
97                 {
98                         hosts_disable[i]--;
99                         continue;
100                 }
101                 
102                 ping = tpinghost (pinghosts[i]);
103
104                 switch (ping)
105                 {
106                         case 0:
107                                 if (!(hosts_flags[i] & 0x01))
108                                         syslog (LOG_WARNING, "ping %s: Connection timed out.", pinghosts[i]);
109                                 hosts_flags[i] |= 0x01;
110                                 break;
111
112                         case -1:
113                                 if (!(hosts_flags[i] & 0x02))
114                                         syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", pinghosts[i]);
115                                 hosts_flags[i] |= 0x02;
116                                 break;
117
118                         case -2:
119                                 syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled for %i iteration(s).",
120                                                 pinghosts[i], hosts_backoff[i]);
121                                 hosts_disable[i] = hosts_backoff[i];
122                                 if (hosts_backoff[i] < 8192) /* 22 3/4 hours */
123                                         hosts_backoff[i] *= 2;
124                                 hosts_flags[i] |= 0x10;
125                                 break;
126
127                         case -3:
128                                 if (!(hosts_flags[i] & 0x04))
129                                         syslog (LOG_WARNING, "ping %s: Connection refused.", pinghosts[i]);
130                                 hosts_flags[i] |= 0x04;
131                                 break;
132
133                         default:
134                                 if (hosts_flags[i] != 0x00)
135                                         syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", pinghosts[i], ping);
136                                 hosts_flags[i] = 0x00;
137                                 hosts_backoff[i] = 1;
138                                 ping_submit (ping, pinghosts[i]);
139                 } /* switch (ping) */
140         } /* for (i = 0; i < num_pinghosts; i++) */
141 }
142
143 void module_register (void)
144 {
145         plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
146 }
147
148 #undef MODULE_NAME
149 #endif /* COLLECT_PING */