root/src/noit_check_tools.c

Revision 41959b7938b249031e78a2d70002aeb6837ecfd8, 11.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

Now that we've made it survive the attempt to run a check twice,
we should just log a message that there could be a problem if
the check is non-transient and give running the check out best
effort.

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