root/src/noit_check_tools.c

Revision 02569455fdf31dd93db18514b0b4cda5d5181ff7, 8.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

work on #171, getting check info works

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