root/src/noit_check_tools.c

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

refs #340

This is a rather major change. Targets can now be hostnames in addition
to IP addresses so long as //checks/@resolve_targets is not false.

If a target is entered that does not look like an IP (inet_pton fails)
then the check is marked as needing resolution NP_RESOLVE.

A passive look-aside dns cache has been implemented in noit_check_resolver.c
that is used to power the whole system and some reasonably simply console
command have been provided:

show dns_cache [fqdn1 [fqdn2]] -- shows the state
dns_cache <fqdn> [fqdn2 [fqdn3]] -- submits for lookup
no dns_cache <fqdn> [fqdn2 [fqdn3]] -- purges from cache

The big change is that modules that relied on check->target to be an IP
address are going to explode when names are provided. Instead, modules
should now use target for the provided target (possibly a FQDN) and use
target_ip (check->target_ip or check.target_ip) for a resolved IP address
and also check for the case of empty string: (check->target_ip[0] == '\0')
for the case that resolution has failed. In lua, the target_ip will be
presented as nil in the case of failed name resolution.

I believe I've updated all necessary components of the system for this to
"just work" but people that have implemented their own check should update
them before they elect to use non-IP addresses as targets.

The dns subsystem supports both IPv4 and IPv6, but currently prefers IPv4
addresses if any are present.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "noit_check_tools.h"
35 #include "noit_check_tools_shared.h"
36 #include "utils/noit_str.h"
37
38 #include <assert.h>
39
40 typedef struct {
41   noit_module_t *self;
42   noit_check_t *check;
43   dispatch_func_t dispatch;
44 } recur_closure_t;
45
46 static int
47 noit_check_recur_handler(eventer_t e, int mask, void *closure,
48                               struct timeval *now) {
49   recur_closure_t *rcl = closure;
50   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
51   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
52                            rcl->dispatch);
53   noit_check_resolve(rcl->check);
54   if(NOIT_CHECK_RESOLVED(rcl->check))
55     rcl->dispatch(rcl->self, rcl->check);
56   else
57     noitL(noit_debug, "skipping %s`%s`%s, unresolved\n",
58           rcl->check->target, rcl->check->module, rcl->check->name);
59   free(rcl);
60   return 0;
61 }
62
63 int
64 noit_check_schedule_next(noit_module_t *self,
65                          struct timeval *last_check, noit_check_t *check,
66                          struct timeval *now, dispatch_func_t dispatch) {
67   eventer_t newe;
68   struct timeval period, earliest;
69   recur_closure_t *rcl;
70
71   assert(check->fire_event == NULL);
72   if(check->period == 0) return 0;
73   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
74
75   /* If we have an event, we know when we intended it to fire.  This means
76    * we should schedule that point + period.
77    */
78   if(now)
79     memcpy(&earliest, now, sizeof(earliest));
80   else
81     gettimeofday(&earliest, NULL);
82
83   /* If the check is unconfigured and needs resolving, we'll set the
84    * period down a bit lower so we can pick up the resolution quickly.
85    */
86   if(!NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
87       check->period > 1000) {
88     period.tv_sec = 1;
89     period.tv_usec = 0;
90   }
91   else {
92     period.tv_sec = check->period / 1000;
93     period.tv_usec = (check->period % 1000) * 1000;
94   }
95
96   newe = eventer_alloc();
97   memcpy(&newe->whence, last_check, sizeof(*last_check));
98   add_timeval(newe->whence, period, &newe->whence);
99   if(compare_timeval(newe->whence, earliest) < 0)
100     memcpy(&newe->whence, &earliest, sizeof(earliest));
101   newe->mask = EVENTER_TIMER;
102   newe->callback = noit_check_recur_handler;
103   rcl = calloc(1, sizeof(*rcl));
104   rcl->self = self;
105   rcl->check = check;
106   rcl->dispatch = dispatch;
107   newe->closure = rcl;
108
109   eventer_add(newe);
110   check->fire_event = newe;
111   return 0;
112 }
113
114 void
115 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
116   struct timeval __now, p_int;
117   eventer_t e;
118   e = eventer_alloc();
119   e->fd = -1;
120   e->mask = EVENTER_ASYNCH;
121   gettimeofday(&__now, NULL);
122   memcpy(&e->whence, &__now, sizeof(__now));
123   p_int.tv_sec = check->timeout / 1000;
124   p_int.tv_usec = (check->timeout % 1000) * 1000;
125   add_timeval(e->whence, p_int, &e->whence);
126   e->callback = callback;
127   e->closure =  check->closure;
128   eventer_add(e);
129 }
130
131 void
132 noit_check_tools_init() {
133   noit_check_tools_shared_init();
134   eventer_name_callback("noit_check_recur_handler", noit_check_recur_handler);
135 }
136
Note: See TracBrowser for help on using the browser.