root/src/noit_check_tools.c

Revision d38990d06dbc481418b2317122b36d481acdae86, 8.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

fixes #213 w/ examples

  • 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 "utils/noit_str.h"
36
37 #include <assert.h>
38
39 typedef struct {
40   noit_module_t *self;
41   noit_check_t *check;
42   dispatch_func_t dispatch;
43 } recur_closure_t;
44
45 static noit_hash_table interpolation_operators = NOIT_HASH_EMPTY;
46
47 static int
48 interpolate_oper_copy(char *buff, int len, const char *replacement) {
49   strlcpy(buff, replacement, len);
50   return strlen(buff);
51 }
52 static int
53 interpolate_oper_ccns(char *buff, int len, const char *replacement) {
54   char *start;
55   start = strstr(replacement, "::");
56   return interpolate_oper_copy(buff, len, start ? (start + 2) : replacement);
57 }
58
59 void
60 noit_check_tools_init() {
61   noit_check_interpolate_register_oper_fn("copy", interpolate_oper_copy);
62   noit_check_interpolate_register_oper_fn("ccns", interpolate_oper_ccns);
63 }
64
65 int
66 noit_check_interpolate_register_oper_fn(const char *name,
67                                         intperpolate_oper_fn f) {
68   noit_hash_replace(&interpolation_operators,
69                     strdup(name), strlen(name),
70                     (void *)f,
71                     free, NULL);
72   return 0;
73 }
74
75 int
76 noit_check_interpolate(char *buff, int len, const char *fmt,
77                        noit_hash_table *attrs,
78                        noit_hash_table *config) {
79   char *copy = NULL;
80   char closer;
81   const char *fmte, *key;
82   int replaced_something = 1;
83   int iterations = 3;
84
85   while(replaced_something && iterations > 0) {
86     char *cp = buff, * const end = buff + len;
87     iterations--;
88     replaced_something = 0;
89     while(*fmt && cp < end) {
90       switch(*fmt) {
91         case '%':
92           if(fmt[1] == '{' || fmt[1] == '[') {
93             closer = (fmt[1] == '{') ? '}' : ']';
94             fmte = fmt + 2;
95             key = fmte;
96             while(*fmte && *fmte != closer) fmte++;
97             if(*fmte == closer) {
98               /* We have a full key here */
99               const char *replacement, *oper, *nkey;
100               intperpolate_oper_fn oper_sprint;
101
102               /* keys can be of the form: :operator:key */
103               oper = key;
104               if(*oper == ':' &&
105                  (nkey = strnstrn(":", 1, oper + 1, fmte - key - 1)) != NULL) {
106                 void *voper;
107                 oper++;
108                 /* find oper, nkey-oper */
109                 if(!noit_hash_retrieve(&interpolation_operators,
110                                        oper, nkey - oper,
111                                        &voper)) {
112                   /* else oper <- copy */
113                   oper_sprint = interpolate_oper_copy;
114                 }
115                 else
116                   oper_sprint = (intperpolate_oper_fn)voper;
117                 nkey++;
118               }
119               else {
120                 oper_sprint = interpolate_oper_copy;
121                 nkey = key;
122               }
123               if(!noit_hash_retr_str((closer == '}') ?  config : attrs,
124                                      nkey, fmte - nkey, &replacement))
125                 replacement = "";
126               fmt = fmte + 1; /* Format points just after the end of the key */
127               cp += oper_sprint(cp, end-cp, replacement);
128               *(end-1) = '\0'; /* In case the oper_sprint didn't teminate */
129               replaced_something = 1;
130               break;
131             }
132           }
133         default:
134           *cp++ = *fmt++;
135       }
136     }
137     *cp = '\0';
138     if(copy) free(copy);
139     if(replaced_something)
140       copy = strdup(buff);
141     fmt = copy;
142   }
143   return strlen(buff);
144 }
145
146 static int
147 noit_check_recur_handler(eventer_t e, int mask, void *closure,
148                               struct timeval *now) {
149   recur_closure_t *rcl = closure;
150   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
151   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
152                            rcl->dispatch);
153   rcl->dispatch(rcl->self, rcl->check);
154   free(rcl);
155   return 0;
156 }
157
158 int
159 noit_check_schedule_next(noit_module_t *self,
160                          struct timeval *last_check, noit_check_t *check,
161                          struct timeval *now, dispatch_func_t dispatch) {
162   eventer_t newe;
163   struct timeval period, earliest;
164   recur_closure_t *rcl;
165
166   assert(check->fire_event == NULL);
167   if(check->period == 0) return 0;
168   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) return 0;
169
170   /* If we have an event, we know when we intended it to fire.  This means
171    * we should schedule that point + period.
172    */
173   if(now)
174     memcpy(&earliest, now, sizeof(earliest));
175   else
176     gettimeofday(&earliest, NULL);
177   period.tv_sec = check->period / 1000;
178   period.tv_usec = (check->period % 1000) * 1000;
179
180   newe = eventer_alloc();
181   memcpy(&newe->whence, last_check, sizeof(*last_check));
182   add_timeval(newe->whence, period, &newe->whence);
183   if(compare_timeval(newe->whence, earliest) < 0)
184     memcpy(&newe->whence, &earliest, sizeof(earliest));
185   newe->mask = EVENTER_TIMER;
186   newe->callback = noit_check_recur_handler;
187   rcl = calloc(1, sizeof(*rcl));
188   rcl->self = self;
189   rcl->check = check;
190   rcl->dispatch = dispatch;
191   newe->closure = rcl;
192
193   eventer_add(newe);
194   check->fire_event = newe;
195   return 0;
196 }
197
198 void
199 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
200   struct timeval __now, p_int;
201   eventer_t e;
202   e = eventer_alloc();
203   e->fd = -1;
204   e->mask = EVENTER_ASYNCH;
205   gettimeofday(&__now, NULL);
206   memcpy(&e->whence, &__now, sizeof(__now));
207   p_int.tv_sec = check->timeout / 1000;
208   p_int.tv_usec = (check->timeout % 1000) * 1000;
209   add_timeval(e->whence, p_int, &e->whence);
210   e->callback = callback;
211   e->closure =  check->closure;
212   eventer_add(e);
213 }
214
215 void
216 noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs) {
217 #define CA_STORE(a,b) noit_hash_store(attrs, a, strlen(a), b)
218   CA_STORE("target", check->target);
219   CA_STORE("name", check->name);
220   CA_STORE("module", check->module);
221 }
222 void
223 noit_check_release_attrs(noit_hash_table *attrs) {
224   noit_hash_destroy(attrs, NULL, NULL);
225 }
226
227 int
228 noit_check_xpath(char *xpath, int len,
229                  const char *base, const char *arg) {
230   uuid_t checkid;
231   int base_trailing_slash;
232   char argcopy[1024], *target, *module, *name;
233
234   base_trailing_slash = (base[strlen(base)-1] == '/');
235   xpath[0] = '\0';
236   argcopy[0] = '\0';
237   if(arg) strlcpy(argcopy, arg, sizeof(argcopy));
238
239   if(uuid_parse(argcopy, checkid) == 0) {
240     /* If they kill by uuid, we'll seek and destroy -- find it anywhere */
241     snprintf(xpath, len, "/noit/checks%s%s/check[@uuid=\"%s\"]",
242              base, base_trailing_slash ? "" : "/", argcopy);
243   }
244   else if((module = strchr(argcopy, '`')) != NULL) {
245     noit_check_t *check;
246     char uuid_str[37];
247     target = argcopy;
248     *module++ = '\0';
249     if((name = strchr(module+1, '`')) == NULL)
250       name = module;
251     else
252       name++;
253     check = noit_poller_lookup_by_name(target, name);
254     if(!check) {
255       return -1;
256     }
257     uuid_unparse_lower(check->checkid, uuid_str);
258     snprintf(xpath, len, "/noit/checks%s%s/check[@uuid=\"%s\"]",
259              base, base_trailing_slash ? "" : "/", uuid_str);
260   }
261   return strlen(xpath);
262 }
Note: See TracBrowser for help on using the browser.