9c318ceecf98178130b8a2bf45194e1bfd23b823
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 "ping.h"
25 #if COLLECT_PING
26 #define MODULE_NAME "ping"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
32 #include <netinet/in.h>
33 #include "libping/ping.h"
35 extern char *pinghosts[MAX_PINGHOSTS];
36 extern int num_pinghosts;
37 static int pingerrors[MAX_PINGHOSTS];
39 static char *file_template = "ping-%s.rrd";
41 static char *ds_def[] =
42 {
43 "DS:ping:GAUGE:25:0:65535",
44 NULL
45 };
46 static int ds_num = 1;
48 static char *config_keys[] =
49 {
50 "Host",
51 NULL
52 };
53 static int config_keys_num = 1;
55 extern time_t curtime;
57 void ping_init (void)
58 {
59 int i;
61 for (i = 0; i < num_pinghosts; i++)
62 pingerrors[i] = 0;
64 return;
65 }
67 int ping_config (char *key, char *value)
68 {
69 if (strcasecmp (key, "host"))
70 {
71 return (-1);
72 }
73 else if (num_pinghosts >= MAX_PINGHOSTS)
74 {
75 return (1);
76 }
77 else if ((pinghosts[num_pinghosts] = strdup (value)) == NULL)
78 {
79 return (2);
80 }
81 else
82 {
83 pingerrors[num_pinghosts] = 0;
84 num_pinghosts++;
85 return (0);
86 }
87 }
89 void ping_write (char *host, char *inst, char *val)
90 {
91 char file[512];
92 int status;
94 status = snprintf (file, 512, file_template, inst);
95 if (status < 1)
96 return;
97 else if (status >= 512)
98 return;
100 rrd_update_file (host, file, val, ds_def, ds_num);
101 }
103 #define BUFSIZE 256
104 void ping_submit (int ping_time, char *host)
105 {
106 char buf[BUFSIZE];
108 if (snprintf (buf, BUFSIZE, "%u:%u", (unsigned int) curtime, ping_time) >= BUFSIZE)
109 return;
111 plugin_submit (MODULE_NAME, host, buf);
112 }
113 #undef BUFSIZE
115 void ping_read (void)
116 {
117 int ping;
118 int i;
120 for (i = 0; i < num_pinghosts; i++)
121 {
122 if (pingerrors[i] & 0x30)
123 continue;
125 ping = tpinghost (pinghosts[i]);
127 switch (ping)
128 {
129 case 0:
130 if (!(pingerrors[i] & 0x01))
131 syslog (LOG_WARNING, "ping %s: Connection timed out.", pinghosts[i]);
132 pingerrors[i] |= 0x01;
133 break;
135 case -1:
136 if (!(pingerrors[i] & 0x02))
137 syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", pinghosts[i]);
138 pingerrors[i] |= 0x02;
139 break;
141 case -2:
142 syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled.", pinghosts[i]);
143 pingerrors[i] |= 0x10;
144 break;
146 case -3:
147 if (!(pingerrors[i] & 0x04))
148 syslog (LOG_WARNING, "ping %s: Connection refused.", pinghosts[i]);
149 pingerrors[i] |= 0x04;
150 break;
152 default:
153 if (pingerrors[i] != 0x00)
154 syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", pinghosts[i], ping);
155 pingerrors[i] = 0x00;
156 ping_submit (ping, pinghosts[i]);
157 } /* switch (ping) */
158 } /* for (i = 0; i < num_pinghosts; i++) */
159 }
161 void module_register (void)
162 {
163 plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
164 cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
165 }
167 #undef MODULE_NAME
168 #endif /* COLLECT_PING */