-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp-error-catcher.c
More file actions
343 lines (316 loc) · 9.79 KB
/
Copy pathhttp-error-catcher.c
File metadata and controls
343 lines (316 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <microhttpd.h>
#include <riemann/event.h>
#include <riemann/message.h>
#include <riemann/client.h>
#include <riemann/attribute.h>
int listen_port = 0, riemann_port = 5555;
char *riemann_host = NULL;
char *maintainance_page = NULL;
const char *riemann_field_service = "http_error_catcher";
const char *riemann_field_host = NULL;
char *body = "OK\n";
int body_len = 3;
typedef struct attribute_list_s
{
riemann_attribute_pairs_t *attr;
struct attribute_list_s *next;
} attribute_list_t;
static attribute_list_t *
push_riemann_attribute_pair(attribute_list_t *list,
const char *key,
const char *value)
{
riemann_attribute_pairs_t *attr;
attribute_list_t *item;
item = calloc(1, sizeof(attribute_list_t));
if (item == NULL) {
return NULL;
}
attr = calloc(1, sizeof(riemann_attribute_pairs_t));
if (attr == NULL) {
free(item);
return NULL;
}
item->attr = attr;
attr->key = (char *)key;
attr->value = (char *)value;
if (list) {
list->next = item;
}
return item;
}
static int
headers_to_attributes(void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
{
attribute_list_t **cur = cls;
*cur = push_riemann_attribute_pair(*cur, key, value);
if (*cur == NULL) {
return MHD_NO;
}
(void)kind;
return MHD_YES;
}
static int
write_to_riemann(void *cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **ptr)
{
int marker, ret;
riemann_client_t *riemann_client = cls;
if (*ptr != &marker || *upload_data_size != 0) {
*ptr = ▮
*upload_data_size = 0;
return MHD_YES;
}
{
riemann_message_t msg = RIEMANN_MSG_INIT;
riemann_event_t event = RIEMANN_EVENT_INIT;
riemann_event_t *events[] = { NULL };
attribute_list_t *head, *cur;
int header_count = 2, i;
riemann_attribute_pairs_t *attributes;
events[0] = &event;
riemann_event_set_host(&event, riemann_field_host);
riemann_event_set_service(&event, riemann_field_service);
head = push_riemann_attribute_pair(NULL, "Method", method);
if (head == NULL) {
return MHD_NO;
}
cur = push_riemann_attribute_pair(head, "URL", url);
if (cur == NULL) {
free(head->attr);
free(head);
return MHD_NO;
}
header_count += MHD_get_connection_values(connection, MHD_HEADER_KIND,
headers_to_attributes, &cur);
attributes = calloc(header_count, sizeof(riemann_attribute_pairs_t));
if (attributes == NULL) {
cur = head;
while (cur) {
free(cur->attr);
head = cur;
free(cur);
cur = head->next;
}
return MHD_NO;
}
cur = head;
for (i = 0; i < header_count && cur; ++i) {
attributes[i].key = cur->attr->key;
attributes[i].value = cur->attr->value;
free(cur->attr);
head = cur->next;
free(cur);
cur = head;
}
riemann_event_set_attributes(&event, attributes, header_count);
riemann_message_set_events(&msg, events, 1);
ret = riemann_client_send_message(riemann_client, &msg, 0, NULL);
if (ret) {
fprintf(stderr, "Cannot send message: %s\n", strerror(errno));
}
fprintf(stderr, "Notfied riemann about '%s'\n", url);
}
{
struct MHD_Response *response;
response = MHD_create_response_from_buffer(body_len, (void *)body,
MHD_RESPMEM_PERSISTENT);
if (response) {
MHD_add_response_header(response, "Content-Type", "text/html");
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
}
}
(void)version;
(void)upload_data;
return ret;
}
void
print_help()
{
fprintf(stderr, "Usage: http-error-catcher -l 9000 -r localhost [-p 5555] -h example.com [-s http_error_catcher] [-m maintainance.html]\n");
}
void
wait()
{
pthread_mutex_t mutex;
pthread_cond_t cond;
int ret;
ret = pthread_mutex_init(&mutex, NULL);
if (ret != 0) {
fprintf(stderr, "Cannot initialize mutex\n");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&mutex);
ret = pthread_cond_init(&cond, NULL);
if (ret != 0) {
fprintf(stderr, "Cannot initialize condition variable\n");
exit(EXIT_FAILURE);
}
pthread_cond_wait(&cond, &mutex);
}
void
init_from_env()
{
char *str;
int num;
str = getenv("LISTEN_PORT");
if (str != NULL) {
num = atoi(str);
if (num > 0) {
listen_port = num;
}
}
str = getenv("RIEMANN_HOST");
if (str != NULL && str[0] != '\0') {
riemann_host = str;
}
str = getenv("RIEMANN_PORT");
if (str != NULL) {
num = atoi(str);
if (num > 0) {
riemann_port = num;
}
}
str = getenv("RIEMANN_FIELD_SERVICE");
if (str != NULL && str[0] != '\0') {
riemann_field_service = str;
}
str = getenv("RIEMANN_FIELD_HOST");
if (str != NULL && str[0] != '\0') {
riemann_field_host = str;
}
str = getenv("MAINTAINANCE_PAGE");
if (str != NULL && str[0] != '\0') {
maintainance_page = str;
}
}
int main(int argc, char ** argv)
{
int ret;
struct MHD_Daemon *daemon;
int opt;
riemann_client_t riemann_client;
struct sockaddr_in localhost_addr;
init_from_env();
while ((opt = getopt(argc, argv, "l:r:p:s:h:m:")) != -1) {
switch (opt) {
case 'l':
listen_port = atoi(optarg);
break;
case 'r':
riemann_host = strdup(optarg);
break;
case 'p':
riemann_port = atoi(optarg);
break;
case 's':
riemann_field_service = strdup(optarg);
break;
case 'h':
riemann_field_host = strdup(optarg);
break;
case 'm':
maintainance_page = strdup(optarg);
break;
default:
print_help();
exit(EXIT_FAILURE);
}
}
if (!listen_port || !riemann_host || !riemann_field_host) {
if (!listen_port) {
fprintf(stderr, "port to listen on is not specified (LISTEN_PORT or -l)\n");
}
if (!riemann_host) {
fprintf(stderr, "riemann host is not specified (RIEMANN_HOST or -r)\n");
}
if (!riemann_field_host) {
fprintf(stderr, "host field for riemann payload is not specified (RIEMANN_FIELD_HOST or -h)\n");
}
print_help();
exit(EXIT_FAILURE);
}
if (maintainance_page) {
int ret;
int fd;
struct stat stat;
fd = open(maintainance_page, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open maintainance page '%s': strerror(%s)\n",
maintainance_page, strerror(errno));
exit(EXIT_FAILURE);
}
ret = fstat(fd, &stat);
if (ret != 0) {
fprintf(stderr, "Cannot get size of maintainance page '%s': strerror(%s)\n",
maintainance_page, strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}
body_len = stat.st_size;
body = calloc(body_len, sizeof(char));
if (body == NULL) {
fprintf(stderr, "Cannot allocate enough memory for content of maintainance page '%s'\n",
maintainance_page);
close(fd);
exit(EXIT_FAILURE);
}
ret = read(fd, body, body_len);
if (ret != body_len) {
fprintf(stderr, "Error while reading maintainance page content '%s': strerror(%s)\n",
maintainance_page, strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}
}
ret = riemann_client_init(&riemann_client);
if (ret) {
fprintf(stderr, "Cannot initialize riemann_client: strerror(%s)\n", strerror(errno));
exit(EXIT_FAILURE);
}
ret = riemann_client_connect(&riemann_client, RIEMANN_TCP, riemann_host, riemann_port);
if (ret) {
fprintf(stderr, "Cannot connect: strerror(%s) gai_strerrror(%s)\n", strerror(errno), gai_strerror(ret));
exit(EXIT_FAILURE);
}
memset(&localhost_addr, 0, sizeof(struct sockaddr_in));
localhost_addr.sin_family = AF_INET;
localhost_addr.sin_port = htons(listen_port);
ret = inet_pton(AF_INET, "127.0.0.1", &localhost_addr.sin_addr);
if (ret != 1) {
fprintf(stderr, "Cannot construct address structure: strerror(%s)\n", strerror(errno));
exit(EXIT_FAILURE);
}
daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL,
listen_port, NULL, NULL,
&write_to_riemann, &riemann_client,
MHD_OPTION_SOCK_ADDR, &localhost_addr,
MHD_OPTION_END);
if (daemon == NULL) {
fprintf(stderr, "Cannot start HTTP daemon\n");
exit(EXIT_FAILURE);
}
wait();
MHD_stop_daemon(daemon);
return 0;
}