2 * collectd - src/ping.c
3 * Copyright (C) 2005 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
26 #define MODULE_NAME "ping"
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[] =
43 "DS:ping:GAUGE:25:0:65535",
46 static int ds_num = 1;
48 static char *config_keys[] =
53 static int config_keys_num = 1;
59 for (i = 0; i < num_pinghosts; i++)
65 int ping_config (char *key, char *value)
67 if (strcasecmp (key, "host"))
71 else if (num_pinghosts >= MAX_PINGHOSTS)
75 else if ((pinghosts[num_pinghosts] = strdup (value)) == NULL)
81 pingerrors[num_pinghosts] = 0;
87 void ping_write (char *host, char *inst, char *val)
92 status = snprintf (file, 512, file_template, inst);
95 else if (status >= 512)
98 rrd_update_file (host, file, val, ds_def, ds_num);
102 void ping_submit (int ping_time, char *host)
106 if (snprintf (buf, BUFSIZE, "%u:%u", (unsigned int) curtime, ping_time) >= BUFSIZE)
109 plugin_submit (MODULE_NAME, host, buf);
113 void ping_read (void)
118 for (i = 0; i < num_pinghosts; i++)
120 if (pingerrors[i] & 0x30)
123 ping = tpinghost (pinghosts[i]);
128 if (!(pingerrors[i] & 0x01))
129 syslog (LOG_WARNING, "ping %s: Connection timed out.", pinghosts[i]);
130 pingerrors[i] |= 0x01;
134 if (!(pingerrors[i] & 0x02))
135 syslog (LOG_WARNING, "ping %s: Host or service is not reachable.", pinghosts[i]);
136 pingerrors[i] |= 0x02;
140 syslog (LOG_ERR, "ping %s: Socket error. Ping will be disabled.", pinghosts[i]);
141 pingerrors[i] |= 0x10;
145 if (!(pingerrors[i] & 0x04))
146 syslog (LOG_WARNING, "ping %s: Connection refused.", pinghosts[i]);
147 pingerrors[i] |= 0x04;
151 if (pingerrors[i] != 0x00)
152 syslog (LOG_NOTICE, "ping %s: Back to normal: %ims.", pinghosts[i], ping);
153 pingerrors[i] = 0x00;
154 ping_submit (ping, pinghosts[i]);
155 } /* switch (ping) */
156 } /* for (i = 0; i < num_pinghosts; i++) */
159 void module_register (void)
161 plugin_register (MODULE_NAME, ping_init, ping_read, ping_write);
162 cf_register (MODULE_NAME, ping_config, config_keys, config_keys_num);
166 #endif /* COLLECT_PING */