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