root/src/noit_check_tools.c

Revision ffa4a996b745fa225e55f9cce5b5a13f21223025, 12.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 2 weeks ago)

last commit was half baked, we need the other side of this fire_event to be checked correctly.

  • 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 "dtrace_probes.h"
35 #include "noit_check_tools.h"
36 #include "noit_check_tools_shared.h"
37 #include "utils/noit_str.h"
38 #include "json-lib/json.h"
39
40 #include <assert.h>
41
42 NOIT_HOOK_IMPL(check_preflight,
43   (noit_module_t *self, noit_check_t *check, noit_check_t *cause),
44   void *, closure,
45   (void *closure, noit_module_t *self, noit_check_t *check, noit_check_t *cause),
46   (closure,self,check,cause))
47 NOIT_HOOK_IMPL(check_postflight,
48   (noit_module_t *self, noit_check_t *check, noit_check_t *cause),
49   void *, closure,
50   (void *closure, noit_module_t *self, noit_check_t *check, noit_check_t *cause),
51   (closure,self,check,cause))
52
53 typedef struct {
54   noit_module_t *self;
55   noit_check_t *check;
56   noit_check_t *cause;
57   dispatch_func_t dispatch;
58 } recur_closure_t;
59
60 static void
61 noit_check_recur_name_details(char *buf, int buflen,
62                               eventer_t e, void *closure) {
63   char id_str[UUID_STR_LEN+1];
64   recur_closure_t *rcl = e ? e->closure : NULL;
65   if(!e) {
66     snprintf(buf, buflen, "noit_check_recur_handler");
67     return;
68   }
69   uuid_unparse_lower(rcl->check->checkid, id_str);
70   snprintf(buf, buflen, "fire(%s)", id_str);
71   return;
72 }
73 static int
74 noit_check_recur_handler(eventer_t e, int mask, void *closure,
75                               struct timeval *now) {
76   recur_closure_t *rcl = closure;
77   int ms;
78
79   if(e != rcl->check->fire_event) return 0;
80
81   noit_check_resolve(rcl->check);
82   ms = noit_check_schedule_next(rcl->self, NULL, rcl->check, now,
83                                 rcl->dispatch, NULL);
84   if(ms == 0)
85     rcl->check->fire_event = NULL; /* This is us, we get free post-return */
86   if(NOIT_CHECK_RESOLVED(rcl->check)) {
87     if(NOIT_HOOK_CONTINUE ==
88        check_preflight_hook_invoke(rcl->self, rcl->check, rcl->cause)) {
89       if(NOIT_CHECK_DISPATCH_ENABLED()) {
90         char id[UUID_STR_LEN+1];
91         uuid_unparse_lower(rcl->check->checkid, id);
92         NOIT_CHECK_DISPATCH(id, rcl->check->module, rcl->check->name,
93                             rcl->check->target);
94       }
95       if(ms < rcl->check->timeout && !(rcl->check->flags & NP_TRANSIENT))
96         noitL(noit_error, "%s might not finish in %dms (timeout %dms)\n",
97               rcl->check->name, ms, rcl->check->timeout);
98       rcl->dispatch(rcl->self, rcl->check, rcl->cause);
99     }
100     check_postflight_hook_invoke(rcl->self, rcl->check, rcl->cause);
101   }
102   else
103     noitL(noit_debug, "skipping %s`%s`%s, unresolved\n",
104           rcl->check->target, rcl->check->module, rcl->check->name);
105   free(rcl);
106   return 0;
107 }
108
109 int
110 noit_check_schedule_next(noit_module_t *self,
111                          struct timeval *last_check, noit_check_t *check,
112                          struct timeval *now, dispatch_func_t dispatch,
113                          noit_check_t *cause) {
114   eventer_t newe;
115   struct timeval period, earliest, diff;
116   int64_t diffms, periodms, offsetms;
117   recur_closure_t *rcl;
118   int initial = last_check ? 1 : 0;
119
120   assert(cause == NULL);
121   if(check->period == 0) return 0;
122
123   /* if last_check is not passed, we use the initial_schedule_time
124    * otherwise, we set the initial_schedule_time
125    */
126   if(!last_check) last_check = &check->initial_schedule_time;
127   else memcpy(&check->initial_schedule_time, last_check, sizeof(*last_check));
128
129   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) {
130     if(!(check->flags & NP_TRANSIENT)) check_slots_dec_tv(last_check);
131     memset(&check->initial_schedule_time, 0, sizeof(struct timeval));
132     return 0;
133   }
134
135   /* If we have an event, we know when we intended it to fire.  This means
136    * we should schedule that point + period.
137    */
138   if(now)
139     memcpy(&earliest, now, sizeof(earliest));
140   else
141     gettimeofday(&earliest, NULL);
142
143   /* If the check is unconfigured and needs resolving, we'll set the
144    * period down a bit lower so we can pick up the resolution quickly.
145    * The one exception is if this is the initial run.
146    */
147   if(!initial &&
148      !NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
149      check->period > 1000) {
150     period.tv_sec = 1;
151     period.tv_usec = 0;
152   }
153   else {
154     period.tv_sec = check->period / 1000;
155     period.tv_usec = (check->period % 1000) * 1000;
156   }
157   periodms = period.tv_sec * 1000 + period.tv_usec / 1000;
158
159   newe = eventer_alloc();
160   /* calculate the differnet between the initial schedule time and "now" */
161   if(compare_timeval(earliest, *last_check) >= 0) {
162     sub_timeval(earliest, *last_check, &diff);
163     diffms = (int64_t)diff.tv_sec * 1000 + diff.tv_usec / 1000;
164   }
165   else {
166     noitL(noit_error, "time is going backwards. abort.\n");
167     abort();
168   }
169   /* determine the offset from initial schedule time that would place
170    * us at the next period-aligned point past "now" */
171   offsetms = ((diffms / periodms) + 1) * periodms;
172   diff.tv_sec = offsetms / 1000;
173   diff.tv_usec = (offsetms % 1000) * 1000;
174
175   memcpy(&newe->whence, last_check, sizeof(*last_check));
176   add_timeval(newe->whence, diff, &newe->whence);
177
178   sub_timeval(newe->whence, earliest, &diff);
179   diffms = (int64_t)diff.tv_sec * 1000 + (int)diff.tv_usec / 1000;
180   assert(compare_timeval(newe->whence, earliest) > 0);
181   newe->mask = EVENTER_TIMER;
182   newe->callback = noit_check_recur_handler;
183   rcl = calloc(1, sizeof(*rcl));
184   rcl->self = self;
185   rcl->check = check;
186   rcl->cause = cause;
187   rcl->dispatch = dispatch;
188   newe->closure = rcl;
189
190   /* knuth's golden ratio approach */
191   if(!self->thread_unsafe) {
192     newe->thr_owner = CHOOSE_EVENTER_THREAD_FOR_CHECK(check);
193   }
194   check->fire_event = newe;
195   eventer_add(newe);
196   return diffms;
197 }
198
199 void
200 noit_check_run_full_asynch_opts(noit_check_t *check, eventer_func_t callback,
201                                 int mask) {
202   struct timeval __now, p_int;
203   eventer_t e;
204   e = eventer_alloc();
205   e->fd = -1;
206   e->mask = EVENTER_ASYNCH | mask;
207   gettimeofday(&__now, NULL);
208   memcpy(&e->whence, &__now, sizeof(__now));
209   p_int.tv_sec = check->timeout / 1000;
210   p_int.tv_usec = (check->timeout % 1000) * 1000;
211   add_timeval(e->whence, p_int, &e->whence);
212   e->callback = callback;
213   e->closure =  check->closure;
214   eventer_add(e);
215 }
216 void
217 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
218   noit_check_run_full_asynch_opts(check, callback,
219                                   EVENTER_DEFAULT_ASYNCH_ABORT);
220 }
221
222 void
223 noit_check_tools_init() {
224   noit_check_tools_shared_init();
225   eventer_name_callback_ext("noit_check_recur_handler",
226                             noit_check_recur_handler,
227                             noit_check_recur_name_details, NULL);
228 }
229
230 static int
231 populate_stats_from_resmon_formatted_json(noit_check_t *check,
232                                           stats_t *s, struct json_object *o,
233                                           const char *prefix) {
234   int count = 0;
235   char keybuff[256];
236 #define MKKEY(fmt, arg) do { \
237   if(prefix) snprintf(keybuff, sizeof(keybuff), "%s`" fmt, prefix, arg); \
238   else snprintf(keybuff, sizeof(keybuff), fmt, arg); \
239 } while(0)
240   if(o == NULL) {
241     if(prefix) {
242       noit_stats_set_metric(check, s, prefix, METRIC_STRING, NULL);
243       count++;
244     }
245     return count;
246   }
247   switch(json_object_get_type(o)) {
248     /* sub callers */
249     case json_type_array:
250     {
251       int i, alen = json_object_array_length(o);
252       for(i=0;i<alen;i++) {
253         struct json_object *item = json_object_array_get_idx(o, i);
254         MKKEY("%d", i);
255         count += populate_stats_from_resmon_formatted_json(check, s, item, keybuff);
256       }
257     }
258     break;
259     case json_type_object:
260     {
261       struct jl_lh_table *lh;
262       struct jl_lh_entry *el;
263       struct json_object *has_type = NULL, *has_value = NULL;
264       lh = json_object_get_object(o);
265       jl_lh_foreach(lh, el) {
266         if(!strcmp(el->k, "_type")) has_type = (struct json_object *)el->v;
267         else if(!strcmp(el->k, "_value")) has_value = (struct json_object *)el->v;
268         else {
269           struct json_object *item = (struct json_object *)el->v;
270           MKKEY("%s", (const char *)el->k);
271           count += populate_stats_from_resmon_formatted_json(check, s, item, keybuff);
272         }
273       }
274       if(prefix && has_type &&
275          json_object_is_type(has_type, json_type_string)) {
276         const char *type_str = json_object_get_string(has_type);
277
278 #define COERCE_JSON_OBJECT(type, item) do { \
279   const char *value_str = NULL; \
280   if(json_object_is_type(item, json_type_string)) \
281     value_str = json_object_get_string(item); \
282   else if(!json_object_is_type(item, json_type_null)) \
283     value_str = json_object_to_json_string(item); \
284   switch(type) { \
285     case METRIC_INT32: case METRIC_UINT32: case METRIC_INT64: \
286     case METRIC_UINT64: case METRIC_DOUBLE: case METRIC_STRING: \
287       noit_stats_set_metric_coerce(check, s, prefix, \
288                                    (metric_type_t)type, value_str); \
289       count++; \
290     default: \
291       break; \
292   } \
293 } while(0)
294
295
296         if (has_value == NULL) {
297           noit_stats_set_metric_coerce(check, s, prefix, (metric_type_t)*type_str, NULL);
298           count++;
299         }
300         else if(json_object_is_type(has_value, json_type_array)) {
301           int i, alen = json_object_array_length(has_value);
302           for(i=0;i<alen;i++) {
303             struct json_object *item = json_object_array_get_idx(has_value, i);
304             COERCE_JSON_OBJECT(*type_str, item);
305           }
306         }
307         else {
308           COERCE_JSON_OBJECT(*type_str, has_value);
309         }
310       }
311       break;
312     }
313
314     /* directs */
315     case json_type_string:
316       if(prefix) {
317         noit_stats_set_metric(check, s, prefix, METRIC_GUESS,
318                               (char *)json_object_get_string(o));
319         count++;
320       }
321       break;
322     case json_type_boolean:
323       if(prefix) {
324         int val = json_object_get_boolean(o) ? 1 : 0;
325         noit_stats_set_metric(check, s, prefix, METRIC_INT32, &val);
326         count++;
327       }
328       break;
329     case json_type_null:
330       if(prefix) {
331         noit_stats_set_metric(check, s, prefix, METRIC_STRING, NULL);
332         count++;
333       }
334       break;
335     case json_type_double:
336       if(prefix) {
337         double val = json_object_get_double(o);
338         noit_stats_set_metric(check, s, prefix, METRIC_DOUBLE, &val);
339         count++;
340       }
341       break;
342     case json_type_int:
343       if(prefix) {
344         int64_t i64;
345         uint64_t u64;
346         switch(json_object_get_int_overflow(o)) {
347           case json_overflow_int:
348             i64 = json_object_get_int(o);
349             noit_stats_set_metric(check, s, prefix, METRIC_INT64, &i64);
350             count++;
351             break;
352           case json_overflow_int64:
353             i64 = json_object_get_int64(o);
354             noit_stats_set_metric(check, s, prefix, METRIC_INT64, &i64);
355             count++;
356             break;
357           case json_overflow_uint64:
358             u64 = json_object_get_uint64(o);
359             noit_stats_set_metric(check, s, prefix, METRIC_UINT64, &u64);
360             count++;
361             break;
362         }
363       }
364   }
365   return count;
366 }
367 int
368 noit_check_stats_from_json_str(noit_check_t *check, stats_t *s,
369                                const char *json_str, int len) {
370   int rv = -1;
371   struct json_tokener *tok = NULL;
372   struct json_object *root = NULL;
373   tok = json_tokener_new();
374   root = json_tokener_parse_ex(tok, json_str, len);
375   if(root) rv = populate_stats_from_resmon_formatted_json(check, s, root, NULL);
376   if(tok) json_tokener_free(tok);
377   if(root) json_object_put(root);
378   return rv;
379 }
Note: See TracBrowser for help on using the browser.