removed
[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   pingerrors[MAX_PINGHOSTS];
37
38 static char *file_template = "ping-%s.rrd";
39
40 static char *ds_def[] = 
41 {
42         "DS:ping:GAUGE:25:0:65535",
43         NULL
44 };
45 static int ds_num = 1;
46
47 void ping_init (void)
48 {
49         int i;
50
51         for (i = 0; i < num_pinghosts; i++)
52                 pingerrors[i] = 0;
53
54         return;
55 }
56
57 void ping_write (char *host, char *inst, char *val)
58 {
59         char file[512];
60         int status;
61
62         status = snprintf (file, 512, file_template, inst);
63         if (status < 1)
64                 return;
65         else if (status >= 512)
66                 return;
67
68         rrd_update_file (host, file, val, ds_def, ds_num);
69 }
70
71 #define BUFSIZE 256
72 void ping_submit (int ping_time, char *host)
73 {
74         char buf[BUFSIZE];
75
76         if (snprintf (buf, BUFSIZE, "%u:%u", (unsigned int) curtime, ping_time) >= BUFSIZE)
77                 return;
78
79         plugin_submit (MODULE_NAME, host, buf);
80 }
81 #undef BUFSIZE
82
83 void ping_read (void)
84 {
85         int ping;
86         int i;
87
88         for (i = 0; i < num_pinghosts; i++)
89         {
90                 if (pingerrors[i] & 0x30)
91                         continue;
92                 
93                 ping = tpinghost (pinghosts[i]);
94
95                 switch (ping)
96                 {
97                         case 0:
98                                 if (!(pingerrors[i] & 0x01))
99                                         syslog (LOG_WARNING, "ping %s: Connection timed out.", pinghosts[i]);
100                                 pingerrors[i] |= 0x01;
101                                 break;
102
103                         case -1:
104                                 if (!(pingerrors[i] & 0x02))
105                                         syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", pinghosts[i]);
106                                 pingerrors[i] |= 0x02;
107                                 break;
108
109                         case -2:
110                                 syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled.", pinghosts[i]);
111                                 pingerrors[i] |= 0x10;
112                                 break;
113
114                         case -3:
115                                 if (!(pingerrors[i] & 0x04))
116                                         syslog (LOG_WARNING, "ping %s: Connection refused.", pinghosts[i]);
117                                 pingerrors[i] |= 0x04;
118                                 break;
119
120                         default:
121                                 if (pingerrors[i] != 0x00)
122                                         syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", pinghosts[i], ping);
123                                 pingerrors[i] = 0x00;
124                                 ping_submit (ping, pinghosts[i]);
125                 } /* switch (ping) */
126         } /* for (i = 0; i < num_pinghosts; i++) */
127 }
128
129 void module_register (void)
130 {
131         plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
132 }
133
134 #undef MODULE_NAME
135 #endif /* COLLECT_PING */