root/src/modules/http.c

Revision 7b927621aa1d1fb7e9e915e74ab2a86ce90f73ac, 28.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

make the metrics stuff more flexible, setup check/status/metrics logging.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <assert.h>
12 #include <math.h>
13
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #include <libxml/xpath.h>
17
18 #include "noit_module.h"
19 #include "noit_check.h"
20 #include "utils/noit_log.h"
21 #include "utils/noit_hash.h"
22
23 #include <apr_uri.h>
24 #include <apr_atomic.h>
25 #include <apr_strings.h>
26 #include "serf.h"
27
28 #define NOIT_HTTP_VERSION_STRING "0.1"
29
30 typedef struct {
31   noit_hash_table *options;
32   void (*results)(noit_module_t *, noit_check_t *);
33 } serf_module_conf_t;
34
35 typedef struct {
36   int using_ssl;
37   serf_ssl_context_t *ssl_ctx;
38   serf_bucket_alloc_t *bkt_alloc;
39 } app_baton_t;
40
41 typedef struct {
42   serf_response_acceptor_t acceptor;
43   app_baton_t *acceptor_baton;
44
45   serf_response_handler_t handler;
46   const char *host;
47   const char *method;
48   const char *path;
49   const char *authn;
50
51   noit_module_t *self;
52   noit_check_t *check;
53 } handler_baton_t;
54
55 typedef struct buf_t {
56   char *b;
57   int32_t l;
58 } buf_t;
59
60 typedef struct {
61   apr_pool_t *pool;
62   apr_sockaddr_t *address;
63   serf_context_t *context;
64   serf_connection_t *connection;
65   serf_request_t *request;
66   app_baton_t app_ctx;
67   handler_baton_t handler_ctx;
68   apr_uri_t url;
69   int timed_out;
70
71   serf_status_line status;
72   buf_t headers;
73   buf_t body;
74
75   struct timeval finish_time;
76   eventer_t fd_event;
77   eventer_t timeout_event;
78 } serf_check_info_t;
79
80 typedef struct {
81   serf_check_info_t serf;
82   struct timeval xml_doc_time;
83   char *xpathexpr;
84   xmlDocPtr xml_doc;
85   char *resmod;
86   char *resserv;
87 } resmon_check_info_t;
88
89 typedef struct {
90   noit_module_t *self;
91   noit_check_t *check;
92   void *serf_baton;
93   apr_socket_t *skt;
94 } serf_closure_t;
95
96 static noit_log_stream_t nlerr = NULL;
97 static noit_log_stream_t nldeb = NULL;
98 static int serf_handler(eventer_t e, int mask, void *closure,
99                         struct timeval *now);
100 static int serf_recur_handler(eventer_t e, int mask, void *closure,
101                               struct timeval *now);
102 static void serf_log_results(noit_module_t *self, noit_check_t *check);
103 static void resmon_log_results(noit_module_t *self, noit_check_t *check);
104 static void resmon_part_log_results(noit_module_t *self, noit_check_t *check,
105                                     noit_check_t *parent);
106
107 static int serf_config(noit_module_t *self, noit_hash_table *options) {
108   serf_module_conf_t *conf;
109   conf = noit_module_get_userdata(self);
110   if(conf) {
111     if(conf->options) {
112       noit_hash_destroy(conf->options, free, free);
113       free(conf->options);
114     }
115   }
116   else
117     conf = calloc(1, sizeof(*conf));
118   conf->options = options;
119   conf->results = serf_log_results;
120   noit_module_set_userdata(self, conf);
121   return 0;
122 }
123 static int resmon_config(noit_module_t *self, noit_hash_table *options) {
124   serf_module_conf_t *conf;
125   conf = noit_module_get_userdata(self);
126   if(conf) {
127     if(conf->options) {
128       noit_hash_destroy(conf->options, free, free);
129       free(conf->options);
130     }
131   }
132   else
133     conf = calloc(1, sizeof(*conf));
134   conf->options = options;
135   if(!conf->options) conf->options = calloc(1, sizeof(*conf->options));
136   noit_hash_store(conf->options, strdup("url"), strlen("url"),
137                   strdup("http://localhost:81/"));
138   conf->results = resmon_log_results;
139   noit_module_set_userdata(self, conf);
140   return 0;
141 }
142 static void generic_log_results(noit_module_t *self, noit_check_t *check) {
143   serf_module_conf_t *module_conf;
144   module_conf = noit_module_get_userdata(self);
145   module_conf->results(self, check);
146 }
147 static void serf_log_results(noit_module_t *self, noit_check_t *check) {
148   serf_check_info_t *ci = check->closure;
149   struct timeval duration;
150   stats_t current;
151   int expect_code = 200;
152   char *code_str;
153   char human_buffer[256], code[4], rt[14];
154
155   noit_check_stats_clear(&current);
156
157   if(noit_hash_retrieve(check->config, "code", strlen("code"),
158                         (void **)&code_str))
159     expect_code = atoi(code_str);
160
161   sub_timeval(ci->finish_time, check->last_fire_time, &duration);
162
163   snprintf(code, sizeof(code), "%3d", ci->status.code);
164   snprintf(rt, sizeof(rt), "%.3fs",
165            (float)duration.tv_sec + (float)duration.tv_usec / 1000000.0);
166   snprintf(human_buffer, sizeof(human_buffer),
167            "code=%s,rt=%s,bytes=%d",
168            ci->status.code ? code : "undefined",
169            ci->timed_out ? "timeout" : rt,
170            ci->body.l);
171   noitL(nldeb, "http(%s) [%s]\n", check->target, human_buffer);
172
173   memcpy(&current.whence, &ci->finish_time, sizeof(current.whence));
174   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
175   current.available = (ci->timed_out || !ci->status.code) ? NP_UNAVAILABLE : NP_AVAILABLE;
176   current.state = (ci->status.code != 200) ? NP_BAD : NP_GOOD;
177   current.status = human_buffer;
178   if(current.available == NP_AVAILABLE) {
179     noit_stats_set_metric(&current, "code",
180                           METRIC_STRING, ci->status.code?code:NULL);
181     noit_stats_set_metric(&current, "bytes",
182                           METRIC_INT32, &ci->body.l);
183   }
184   else {
185     noit_stats_set_metric(&current, "code", METRIC_STRING, NULL);
186     noit_stats_set_metric(&current, "bytes", METRIC_INT32, NULL);
187   }
188   noit_check_set_stats(self, check, &current);
189 }
190 static void resmon_part_log_results_xml(noit_module_t *self,
191                                         noit_check_t *check,
192                                         xmlDocPtr xml) {
193   resmon_check_info_t *rci = check->closure;
194   xmlXPathContextPtr xpath_ctxt = NULL;
195   stats_t current;
196
197   noit_check_stats_clear(&current);
198   current.available = NP_UNAVAILABLE;
199   current.state = NP_BAD;
200
201   if(xml && rci->xpathexpr) {
202     current.available = NP_AVAILABLE;
203     xpath_ctxt = xmlXPathNewContext(xml);
204     if(xpath_ctxt) {
205       xmlXPathObjectPtr pobj;
206       pobj = xmlXPathEval((xmlChar *)rci->xpathexpr, xpath_ctxt);
207       if(pobj) {
208         int i, cnt;
209         cnt = xmlXPathNodeSetGetLength(pobj->nodesetval);
210         for(i=0; i<cnt; i++) {
211           xmlNodePtr node;
212           char *value;
213           node = xmlXPathNodeSetItem(pobj->nodesetval, i);
214           value = (char *)xmlXPathCastNodeToString(node);
215           if(!strcmp((char *)node->name,"last_runtime_seconds")) {
216             float duration = atof(value) * 1000;
217             current.duration = (int) duration;
218           }
219           else if(!strcmp((char *)node->name, "message")) {
220             current.status = strdup(value);
221           }
222           else if(!strcmp((char *)node->name, "state")) {
223             current.state = strcmp(value,"OK") ? NP_BAD : NP_GOOD;
224           }
225         }
226       }
227     }
228   }
229   memcpy(&current.whence, &rci->serf.finish_time, sizeof(current.whence));
230   current.status = current.status ? current.status : strdup("unknown");
231   noitL(nldeb, "resmon_part(%s/%s/%s) [%s]\n", check->target,
232         rci->resmod, rci->resserv, current.status);
233   noit_check_set_stats(self, check, &current);
234 }
235 static void resmon_part_log_results(noit_module_t *self, noit_check_t *check,
236                                     noit_check_t *parent) {
237   resmon_check_info_t *rci = parent->closure;
238   resmon_part_log_results_xml(self, check, rci->xml_doc);
239 }
240 static void resmon_log_results(noit_module_t *self, noit_check_t *check) {
241   serf_check_info_t *ci = check->closure;
242   resmon_check_info_t *rci = check->closure;
243   struct timeval duration;
244   stats_t current;
245   int32_t services = 0;
246   char human_buffer[256], rt[14];
247   xmlDocPtr resmon_results = NULL;
248   xmlXPathContextPtr xpath_ctxt = NULL;
249   xmlXPathObjectPtr pobj = NULL;
250
251   noit_check_stats_clear(&current);
252
253   if(ci->body.b) resmon_results = xmlParseMemory(ci->body.b, ci->body.l);
254   if(resmon_results) {
255     xpath_ctxt = xmlXPathNewContext(resmon_results);
256     pobj = xmlXPathEval((xmlChar *)"/ResmonResults/ResmonResult", xpath_ctxt);
257     if(pobj)
258       if(pobj->type == XPATH_NODESET)
259         services = xmlXPathNodeSetGetLength(pobj->nodesetval);
260   } else {
261     if(ci->body.l)
262       noitL(nlerr, "Error in resmon doc: %s\n", ci->body.b);
263   }
264
265   /* Save our results for future dependent checks */
266   memcpy(&current.whence, &ci->finish_time, sizeof(current.whence));
267   memcpy(&rci->xml_doc_time, &ci->finish_time, sizeof(ci->finish_time));
268   if(rci->xml_doc) xmlFreeDoc(rci->xml_doc);
269   rci->xml_doc = resmon_results;
270
271   if(rci->xpathexpr) {
272     /* This is actually a part check... we had to do all the work as
273      * it isn't being used as a causal firing from a generic resmon check
274      */
275     resmon_part_log_results_xml(self, check, rci->xml_doc);
276     goto out;
277   }
278
279   sub_timeval(ci->finish_time, check->last_fire_time, &duration);
280   snprintf(rt, sizeof(rt), "%.3fs",
281            (float)duration.tv_sec + (float)duration.tv_usec / 1000000.0);
282   snprintf(human_buffer, sizeof(human_buffer),
283            "services=%d,rt=%s",
284            services,
285            ci->timed_out ? "timeout" : rt);
286   noitL(nldeb, "resmon(%s) [%s]\n", check->target, human_buffer);
287
288   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
289   current.available = (ci->timed_out || ci->status.code != 200) ?
290                           NP_UNAVAILABLE : NP_AVAILABLE;
291   current.state = services ? NP_GOOD : NP_BAD;
292   current.status = human_buffer;
293
294   noit_stats_set_metric(&current, "services", METRIC_INT32, &services);
295   if(services) {
296     int i;
297     for(i=0; i<services; i++) {
298       xmlNodePtr node, attrnode;
299       node = xmlXPathNodeSetItem(pobj->nodesetval, i);
300       if(node) {
301         int a;
302         char *attrs[3] = { "last_runtime_seconds", "state", "message" };
303         char *resmod = NULL, *resserv = NULL, *value = NULL;
304         char attr[1024];
305         xmlXPathObjectPtr sobj;
306
307         xpath_ctxt->node = node;
308         sobj = xmlXPathEval((xmlChar *)"@module", xpath_ctxt);
309         resmod = (char *)xmlXPathCastNodeSetToString(sobj->nodesetval);
310         sobj = xmlXPathEval((xmlChar *)"@service", xpath_ctxt);
311         resserv = (char *)xmlXPathCastNodeSetToString(sobj->nodesetval);
312         if(!resmod && !resserv) continue;
313
314         for(a=0; a<3; a++) {
315           int32_t intval;
316           sobj = xmlXPathEval((xmlChar *)attrs[a], xpath_ctxt);
317           attrnode = xmlXPathNodeSetItem(sobj->nodesetval, 0);
318           value = (char *)xmlXPathCastNodeToString(attrnode);
319           snprintf(attr, sizeof(attr), "%s`%s`%s",
320                    resmod, resserv, (char *)attrnode->name);
321           switch(a) {
322             case 0:
323               /* The first is integer */
324               intval = (int)(atof(value) * 1000.0);
325               noit_stats_set_metric(&current, attr, METRIC_INT32, &intval);
326               break;
327             case 1:
328               noit_stats_set_metric(&current, attr, METRIC_STRING, value);
329               break;
330             case 2:
331               noit_stats_set_metric(&current, attr, METRIC_GUESS, value);
332               break;
333           }
334         }
335       }
336     }
337   }
338
339   noit_check_set_stats(self, check, &current);
340
341  out:
342   if(pobj) xmlXPathFreeObject(pobj);
343   if(xpath_ctxt) xmlXPathFreeContext(xpath_ctxt);
344 }
345 static void serf_check_info_cleanup(serf_check_info_t *ci) {
346   if(ci->connection) {
347     serf_connection_close(ci->connection);
348     ci->connection = NULL;
349   }
350   if(ci->fd_event) {
351     eventer_remove_fd(ci->fd_event->fd);
352     eventer_free(ci->fd_event);
353     ci->fd_event = NULL;
354   }
355   ci->timeout_event = NULL;
356   if(ci->pool) apr_pool_destroy(ci->pool);
357   memset(ci, 0, sizeof(*ci));
358 }
359 static int serf_complete(eventer_t e, int mask,
360                          void *closure, struct timeval *now) {
361   serf_closure_t *ccl = (serf_closure_t *)closure;
362   serf_check_info_t *ci = (serf_check_info_t *)ccl->check->closure;
363
364   noitLT(nldeb, now, "serf_complete(%s)\n", ccl->check->target);
365   if(!NOIT_CHECK_DISABLED(ccl->check) && !NOIT_CHECK_KILLED(ccl->check)) {
366     generic_log_results(ccl->self, ccl->check);
367   }
368   serf_check_info_cleanup(ci);
369   ccl->check->flags &= ~NP_RUNNING;
370   free(ccl);
371   return 0;
372 }
373
374 static int serf_handler(eventer_t e, int mask,
375                         void *closure, struct timeval *now) {
376   apr_pollfd_t desc = { 0 };
377   serf_closure_t *sct = closure;
378   serf_check_info_t *ci = sct->check->closure;
379
380   desc.desc_type = APR_POLL_SOCKET;
381   desc.desc.s = sct->skt;
382
383   desc.rtnevents = 0;
384   if(mask & EVENTER_READ) desc.rtnevents |= APR_POLLIN;
385   if(mask & EVENTER_WRITE) desc.rtnevents |= APR_POLLOUT;
386   if(mask & EVENTER_EXCEPTION) desc.rtnevents |= APR_POLLERR;
387   serf_event_trigger(ci->context, sct->serf_baton, &desc);
388   serf_context_prerun(ci->context);
389
390   /* We're about to deschedule and free the event, drop our reference */
391   if(!e->mask)
392     ci->fd_event = NULL;
393
394   return e->mask;
395 }
396
397 static int serf_init(noit_module_t *self) {
398   return 0;
399 }
400 static void closed_connection(serf_connection_t *conn,
401                               void *closed_baton,
402                               apr_status_t why,
403                               apr_pool_t *pool) {
404 }
405
406 static serf_bucket_t* conn_setup(apr_socket_t *skt,
407                                 void *setup_baton,
408                                 apr_pool_t *pool) {
409   serf_bucket_t *c;
410   app_baton_t *ctx = setup_baton;
411
412   c = serf_bucket_socket_create(skt, ctx->bkt_alloc);
413   if (ctx->using_ssl) {
414       c = serf_bucket_ssl_decrypt_create(c, ctx->ssl_ctx, ctx->bkt_alloc);
415       if (!ctx->ssl_ctx) {
416           ctx->ssl_ctx = serf_bucket_ssl_decrypt_context_get(c);
417       }
418   }
419
420   return c;
421 }
422
423 static serf_bucket_t* accept_response(serf_request_t *request,
424                                       serf_bucket_t *stream,
425                                       void *acceptor_baton,
426                                       apr_pool_t *pool) {
427   serf_bucket_t *c;
428   serf_bucket_alloc_t *bkt_alloc;
429
430   /* get the per-request bucket allocator */
431   bkt_alloc = serf_request_get_alloc(request);
432
433   /* Create a barrier so the response doesn't eat us! */
434   c = serf_bucket_barrier_create(stream, bkt_alloc);
435
436   return serf_bucket_response_create(c, bkt_alloc);
437 }
438
439 static void append_buf(apr_pool_t *p, buf_t *b,
440                        const char *data, int len) {
441   char *n;
442   n = apr_palloc(p, b->l + len + 1);
443   if(b->l == 0)
444     b->b = n;
445   else {
446     memcpy(n, b->b, b->l);
447     b->b = n;
448   }
449   memcpy(b->b + b->l, data, len);
450   b->l += len;
451   b->b[b->l] = '\0';
452 }
453
454 static apr_status_t handle_response(serf_request_t *request,
455                                     serf_bucket_t *response,
456                                     void *handler_baton,
457                                     apr_pool_t *pool) {
458   const char *data;
459   apr_size_t len;
460   apr_status_t status;
461   handler_baton_t *ctx = handler_baton;
462   serf_check_info_t *ci = ctx->check->closure;
463
464   if(response == NULL) {
465     /* We were cancelled. */
466     goto finish;
467   }
468   status = serf_bucket_response_status(response, &ci->status);
469   if (status) {
470     if (APR_STATUS_IS_EAGAIN(status)) {
471       return status;
472     }
473     goto finish;
474   }
475
476   while (1) {
477     status = serf_bucket_read(response, 1024*32, &data, &len);
478     if (SERF_BUCKET_READ_ERROR(status))
479       return status;
480
481     append_buf(ci->pool, &ci->body, data, len);
482
483     /* are we done yet? */
484     if (APR_STATUS_IS_EOF(status)) {
485       serf_bucket_t *hdrs;
486       hdrs = serf_bucket_response_get_headers(response);
487       while (1) {
488         status = serf_bucket_read(hdrs, 2048, &data, &len);
489         if (SERF_BUCKET_READ_ERROR(status))
490           return status;
491
492         append_buf(ci->pool, &ci->headers, data, len);
493         if (APR_STATUS_IS_EOF(status)) {
494           break;
495         }
496       }
497
498       goto finish;
499     }
500
501     /* have we drained the response so far? */
502     if (APR_STATUS_IS_EAGAIN(status))
503       return status;
504
505     /* loop to read some more. */
506   }
507  finish:
508   gettimeofday(&ci->finish_time, NULL);
509   if(ci->timeout_event) {
510     eventer_remove(ci->timeout_event);
511     ci->timed_out = 0;
512     memcpy(&ci->timeout_event->whence, &ci->finish_time,
513            sizeof(&ci->finish_time));
514     eventer_add(ci->timeout_event);
515   }
516   return APR_EOF;
517 }
518
519 static apr_status_t setup_request(serf_request_t *request,
520                                   void *setup_baton,
521                                   serf_bucket_t **req_bkt,
522                                   serf_response_acceptor_t *acceptor,                                             void **acceptor_baton,
523                                   serf_response_handler_t *handler,
524                                   void **handler_baton,
525                                   apr_pool_t *pool) {
526   handler_baton_t *ctx = setup_baton;
527   serf_bucket_t *hdrs_bkt;
528   serf_bucket_t *body_bkt;
529
530   body_bkt = NULL;
531
532   *req_bkt = serf_bucket_request_create(ctx->method, ctx->path, body_bkt,
533                                         serf_request_get_alloc(request));
534
535   hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
536
537   serf_bucket_headers_setn(hdrs_bkt, "Host", ctx->host);
538   serf_bucket_headers_setn(hdrs_bkt, "User-Agent",
539                            "Noit/" NOIT_HTTP_VERSION_STRING);
540   /* Shouldn't serf do this for us? */
541   serf_bucket_headers_setn(hdrs_bkt, "Accept-Encoding", "gzip");
542
543   if (ctx->authn != NULL) {
544     serf_bucket_headers_setn(hdrs_bkt, "Authorization", ctx->authn);
545   }
546
547   if (ctx->acceptor_baton->using_ssl) {
548     serf_bucket_alloc_t *req_alloc;
549     app_baton_t *app_ctx = ctx->acceptor_baton;
550
551     req_alloc = serf_request_get_alloc(request);
552
553     if (app_ctx->ssl_ctx == NULL) {
554       *req_bkt =
555         serf_bucket_ssl_encrypt_create(*req_bkt, NULL,
556                                        app_ctx->bkt_alloc);
557       app_ctx->ssl_ctx =
558         serf_bucket_ssl_encrypt_context_get(*req_bkt);
559     }
560     else {
561       *req_bkt =
562         serf_bucket_ssl_encrypt_create(*req_bkt, app_ctx->ssl_ctx,
563                                        app_ctx->bkt_alloc);
564     }
565   }
566
567   *acceptor = ctx->acceptor;
568   *acceptor_baton = ctx->acceptor_baton;
569   *handler = ctx->handler;
570   *handler_baton = ctx;
571
572   return APR_SUCCESS;
573 }
574
575 struct __unix_apr_socket_t {
576   apr_pool_t *pool;
577   int socketdes;
578 };
579
580 static apr_status_t serf_eventer_add(void *user_baton,
581                                      apr_pollfd_t *pfd,
582                                      void *serf_baton) {
583   eventer_t e, newe = NULL;
584   serf_closure_t *sct = user_baton, *newsct;
585   assert(pfd->desc_type == APR_POLL_SOCKET);
586   struct __unix_apr_socket_t *hack = (struct __unix_apr_socket_t *)pfd->desc.s;
587
588   noitL(nldeb, "serf_eventer_add() => %d\n", hack->socketdes);
589   e = eventer_find_fd(hack->socketdes);
590   if(!e) {
591     newe = e = eventer_alloc();
592     e->fd = hack->socketdes;
593     e->callback = serf_handler;
594     e->closure = calloc(1, sizeof(serf_closure_t));
595   }
596   newsct = e->closure;
597   newsct->self = sct->self;
598   newsct->check = sct->check;
599   newsct->serf_baton = serf_baton;
600   newsct->skt = pfd->desc.s;
601   e->mask = 0;
602   if(pfd->reqevents & APR_POLLIN) e->mask |= EVENTER_READ;
603   if(pfd->reqevents & APR_POLLOUT) e->mask |= EVENTER_WRITE;
604   if(pfd->reqevents & APR_POLLERR) e->mask |= EVENTER_EXCEPTION;
605   if(newe) {
606     serf_check_info_t *ci = sct->check->closure;
607     eventer_add(newe);
608     ci->fd_event = newe;
609   }
610 /* ** Unneeded as this is called recursively **
611   else
612     eventer_update(e);
613 */
614   return APR_SUCCESS;
615 }
616 static apr_status_t serf_eventer_remove(void *user_baton,
617                                         apr_pollfd_t *pfd,
618                                         void *serf_baton) {
619   serf_closure_t *sct = user_baton;
620   serf_check_info_t *ci;
621   eventer_t e;
622
623   ci = sct->check->closure;
624   assert(pfd->desc_type == APR_POLL_SOCKET);
625   struct __unix_apr_socket_t *hack = (struct __unix_apr_socket_t *)pfd->desc.s;
626
627   noitL(nldeb, "serf_eventer_remove() => %d\n", hack->socketdes);
628   e = eventer_find_fd(hack->socketdes);
629   if(e) e->mask = 0;
630   return 0;
631 }
632
633 static int serf_initiate(noit_module_t *self, noit_check_t *check) {
634   serf_closure_t *ccl;
635   serf_check_info_t *ci;
636   struct timeval when, p_int;
637   apr_status_t status;
638   eventer_t newe;
639   serf_module_conf_t *mod_config;
640   char *config_url;
641
642   mod_config = noit_module_get_userdata(self);
643   ci = (serf_check_info_t *)check->closure;
644   /* We cannot be running */
645   assert(!(check->flags & NP_RUNNING));
646   check->flags |= NP_RUNNING;
647   noitL(nldeb, "serf_initiate(%p,%s)\n",
648         self, check->target);
649
650   /* remove a timeout if we still have one -- we should unless someone
651    * has set a lower timeout than the period.
652    */
653   ci->timed_out = 1;
654   if(ci->timeout_event) {
655     eventer_remove(ci->timeout_event);
656     free(ci->timeout_event->closure);
657     eventer_free(ci->timeout_event);
658     ci->timeout_event = NULL;
659   }
660   assert(!ci->pool);
661   apr_pool_create(&ci->pool, NULL);
662   apr_atomic_init(ci->pool);
663
664   gettimeofday(&when, NULL);
665   memcpy(&check->last_fire_time, &when, sizeof(when));
666
667   ccl = apr_pcalloc(ci->pool, sizeof(*ccl));
668   ccl->self = self;
669   ccl->check = check;
670
671   if(!noit_hash_retrieve(check->config, "url", strlen("url"),
672                         (void **)&config_url))
673     if(!mod_config->options ||
674        !noit_hash_retrieve(mod_config->options, "url", strlen("url"),
675                            (void **)&config_url))
676       config_url = "http://localhost/";
677   apr_uri_parse(ci->pool, config_url, &ci->url);
678
679   if (!ci->url.port) {
680     ci->url.port = apr_uri_port_of_scheme(ci->url.scheme);
681   }
682   if (!ci->url.path) {
683     ci->url.path = "/";
684   }
685
686   if (strcasecmp(ci->url.scheme, "https") == 0) {
687     ci->app_ctx.using_ssl = 1;
688   }
689   else {
690     ci->app_ctx.using_ssl = 0;
691   }
692
693   status = apr_sockaddr_info_get(&ci->address,
694                                  check->target, APR_UNSPEC, ci->url.port, 0,
695                                  ci->pool);
696   if (status) {
697     /* Handle error -- log failure */
698     apr_pool_destroy(ci->pool);
699     memset(ci, 0, sizeof(*ci));
700     check->flags &= ~NP_RUNNING;
701     return 0;
702   }
703
704   ci->context = serf_context_create_ex(ccl, serf_eventer_add,
705                                        serf_eventer_remove, ci->pool);
706
707   ci->app_ctx.bkt_alloc = serf_bucket_allocator_create(ci->pool, NULL, NULL);
708   ci->app_ctx.ssl_ctx = NULL;
709
710   ci->connection = serf_connection_create(ci->context, ci->address,
711                                           conn_setup, &ci->app_ctx,
712                                           closed_connection, &ci->app_ctx,
713                                           ci->pool);
714
715   ci->handler_ctx.method = apr_pstrdup(ci->pool, "GET");
716   ci->handler_ctx.host = apr_pstrdup(ci->pool, ci->url.hostname);
717   ci->handler_ctx.path = ci->url.path;
718   ci->handler_ctx.authn = NULL;
719
720   ci->handler_ctx.acceptor = accept_response;
721   ci->handler_ctx.acceptor_baton = &ci->app_ctx;
722   ci->handler_ctx.handler = handle_response;
723   ci->handler_ctx.self = self;
724   ci->handler_ctx.check = check;
725
726   ci->request = serf_connection_request_create(ci->connection, setup_request,
727                                                &ci->handler_ctx);
728   serf_context_prerun(ci->context);
729
730   newe = eventer_alloc();
731   newe->mask = EVENTER_TIMER;
732   gettimeofday(&when, NULL);
733   p_int.tv_sec = check->timeout / 1000;
734   p_int.tv_usec = (check->timeout % 1000) * 1000;
735   add_timeval(when, p_int, &newe->whence);
736   ccl = calloc(1, sizeof(*ccl));
737   ccl->self = self;
738   ccl->check = check;
739   newe->closure = ccl;
740   newe->callback = serf_complete;
741   eventer_add(newe);
742   ci->timeout_event = newe;
743   return 0;
744 }
745 static int serf_schedule_next(noit_module_t *self,
746                               struct timeval *last_check, noit_check_t *check,
747                               struct timeval *now) {
748   eventer_t newe;
749   struct timeval period, earliest;
750   serf_closure_t *ccl;
751
752   if(check->period == 0) return 0;
753
754   /* If we have an event, we know when we intended it to fire.  This means
755    * we should schedule that point + period.
756    */
757   if(now)
758     memcpy(&earliest, now, sizeof(earliest));
759   else
760     gettimeofday(&earliest, NULL);
761   period.tv_sec = check->period / 1000;
762   period.tv_usec = (check->period % 1000) * 1000;
763
764   newe = eventer_alloc();
765   memcpy(&newe->whence, last_check, sizeof(*last_check));
766   add_timeval(newe->whence, period, &newe->whence);
767   if(compare_timeval(newe->whence, earliest) < 0)
768     memcpy(&newe->whence, &earliest, sizeof(earliest));
769   newe->mask = EVENTER_TIMER;
770   newe->callback = serf_recur_handler;
771   ccl = calloc(1, sizeof(*ccl));
772   ccl->self = self;
773   ccl->check = check;
774   newe->closure = ccl;
775
776   eventer_add(newe);
777   check->fire_event = newe;
778   return 0;
779 }
780 static int serf_recur_handler(eventer_t e, int mask, void *closure,
781                               struct timeval *now) {
782   serf_closure_t *cl = (serf_closure_t *)closure;
783   serf_schedule_next(cl->self, &e->whence, cl->check, now);
784   serf_initiate(cl->self, cl->check);
785   free(cl);
786   return 0;
787 }
788 static void serf_cleanup(noit_module_t *self, noit_check_t *check) {
789   serf_check_info_t *sci;
790   if(check->fire_event) {
791     eventer_remove(check->fire_event);
792     free(check->fire_event->closure);
793     eventer_free(check->fire_event);
794     check->fire_event = NULL;
795   }
796   sci = check->closure;
797   if(sci) {
798     serf_check_info_cleanup(sci);
799     free(sci);
800   }
801 }
802 static int serf_initiate_check(noit_module_t *self, noit_check_t *check,
803                                int once, noit_check_t *cause) {
804   if(!check->closure) check->closure = calloc(1, sizeof(serf_check_info_t));
805   if(once) {
806     serf_initiate(self, check);
807     return 0;
808   }
809   /* If check->fire_event, we're already scheduled... */
810   if(!check->fire_event) {
811     struct timeval epoch = { 0L, 0L };
812     noit_check_fake_last_check(check, &epoch, NULL);
813     serf_schedule_next(self, &epoch, check, NULL);
814   }
815   return 0;
816 }
817 static int resmon_initiate_check(noit_module_t *self, noit_check_t *check,
818                                  int once, noit_check_t *parent) {
819   /* resmon_check_info_t gives us a bit more space */
820   if(!check->closure) check->closure = calloc(1, sizeof(resmon_check_info_t));
821   if(once) {
822     serf_initiate(self, check);
823     return 0;
824   }
825   if(!check->fire_event) {
826     struct timeval epoch = { 0L, 0L };
827     noit_check_fake_last_check(check, &epoch, NULL);
828     serf_schedule_next(self, &epoch, check, NULL);
829   }
830   return 0;
831 }
832
833 static void resmon_cleanup(noit_module_t *self, noit_check_t *check) {
834   resmon_check_info_t *rci;
835   if(check->fire_event) {
836     eventer_remove(check->fire_event);
837     free(check->fire_event->closure);
838     eventer_free(check->fire_event);
839     check->fire_event = NULL;
840   }
841   rci = check->closure;
842   if(rci) {
843     if(rci->xpathexpr) free(rci->xpathexpr);
844     if(rci->resmod) free(rci->resmod);
845     if(rci->resserv) free(rci->resserv);
846     if(rci->xml_doc) xmlFreeDoc(rci->xml_doc);
847     serf_check_info_cleanup(&rci->serf);
848     free(rci);
849   }
850 }
851 static int resmon_part_initiate_check(noit_module_t *self, noit_check_t *check,
852                                       int once, noit_check_t *parent) {
853   char xpathexpr[1024];
854   const char *resmod, *resserv;
855   resmon_check_info_t *rci;
856
857   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
858
859   if(!check->closure) check->closure = calloc(1, sizeof(resmon_check_info_t));
860   rci = check->closure;
861   if(!rci->xpathexpr) {
862     if(!noit_hash_retrieve(check->config,
863                            "resmon_module", strlen("resmon_module"),
864                            (void **)&resmod)) {
865       resmod = "DUMMY_MODULE";
866     }
867     if(!noit_hash_retrieve(check->config,
868                            "resmon_service", strlen("resmon_service"),
869                            (void **)&resserv)) {
870       resserv = "DUMMY_SERVICE";
871     }
872     snprintf(xpathexpr, sizeof(xpathexpr),
873              "//ResmonResult[@module=\"%s\" and @service=\"%s\"]/*",
874              resmod, resserv);
875     rci->xpathexpr = strdup(xpathexpr);
876     rci->resmod = strdup(resmod);
877     rci->resserv = strdup(resserv);
878   }
879
880   if(parent && !strcmp(parent->module, "resmon")) {
881     /* Content is cached in the parent */
882     resmon_part_log_results(self, check, parent);
883     return 0;
884   }
885   if(once) {
886     serf_initiate(self, check);
887     return 0;
888   }
889   if(!check->fire_event)
890     serf_schedule_next(self, NULL, check, NULL);
891   return 0;
892 }
893
894
895
896 static int serf_onload(noit_module_t *self) {
897   apr_initialize();
898   atexit(apr_terminate);
899
900   nlerr = noit_log_stream_find("error/serf");
901   nldeb = noit_log_stream_find("debug/serf");
902   if(!nlerr) nlerr = noit_stderr;
903   if(!nldeb) nldeb = noit_debug;
904
905   eventer_name_callback("http/serf_handler", serf_handler);
906   eventer_name_callback("http/serf_complete", serf_complete);
907   eventer_name_callback("http/serf_recur_handler", serf_recur_handler);
908   return 0;
909 }
910 noit_module_t http = {
911   NOIT_MODULE_MAGIC,
912   NOIT_MODULE_ABI_VERSION,
913   "http",
914   "libserf-based HTTP and HTTPS resource checker",
915   serf_onload,
916   serf_config,
917   serf_init,
918   serf_initiate_check,
919   serf_cleanup
920 };
921
922 noit_module_t resmon = {
923   NOIT_MODULE_MAGIC,
924   NOIT_MODULE_ABI_VERSION,
925   "resmon",
926   "libserf-based resmon resource checker",
927   serf_onload,
928   resmon_config,
929   serf_init,
930   resmon_initiate_check,
931   resmon_cleanup
932 };
933
934 noit_module_t resmon_part = {
935   NOIT_MODULE_MAGIC,
936   NOIT_MODULE_ABI_VERSION,
937   "resmon_part",
938   "resmon part resource checker",
939   serf_onload,
940   resmon_config,
941   serf_init,
942   resmon_part_initiate_check,
943   resmon_cleanup
944 };
945
Note: See TracBrowser for help on using the browser.