root/src/noit_check_tools.c

Revision 304ec80b8cf842fc0abe5f9029790908b6455957, 13.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 1 week ago)

Convert to libmtev.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  * Copyright (c) 2015, Circonus, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials provided
15  *       with the distribution.
16  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *       of its contributors may be used to endorse or promote products
18  *       derived from this software without specific prior written
19  *       permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <mtev_defines.h>
35 #include <mtev_str.h>
36 #include <mtev_json.h>
37
38 #include "noit_mtev_bridge.h"
39 #include "noit_dtrace_probes.h"
40 #include "noit_check_tools.h"
41 #include "noit_check_tools_shared.h"
42
43 #include <assert.h>
44
45 MTEV_HOOK_IMPL(check_preflight,
46   (noit_module_t *self, noit_check_t *check, noit_check_t *cause),
47   void *, closure,
48   (void *closure, noit_module_t *self, noit_check_t *check, noit_check_t *cause),
49   (closure,self,check,cause))
50 MTEV_HOOK_IMPL(check_postflight,
51   (noit_module_t *self, noit_check_t *check, noit_check_t *cause),
52   void *, closure,
53   (void *closure, noit_module_t *self, noit_check_t *check, noit_check_t *cause),
54   (closure,self,check,cause))
55
56 typedef struct {
57   noit_module_t *self;
58   noit_check_t *check;
59   noit_check_t *cause;
60   dispatch_func_t dispatch;
61 } recur_closure_t;
62
63 static void
64 noit_check_recur_name_details(char *buf, int buflen,
65                               eventer_t e, void *closure) {
66   char id_str[UUID_STR_LEN+1];
67   recur_closure_t *rcl = e ? e->closure : NULL;
68   if(!e) {
69     snprintf(buf, buflen, "noit_check_recur_handler");
70     return;
71   }
72   uuid_unparse_lower(rcl->check->checkid, id_str);
73   snprintf(buf, buflen, "fire(%s)", id_str);
74   return;
75 }
76 static int
77 noit_check_recur_handler(eventer_t e, int mask, void *closure,
78                               struct timeval *now) {
79   recur_closure_t *rcl = closure;
80   int ms;
81
82   if(e != rcl->check->fire_event) return 0;
83
84   noit_check_resolve(rcl->check);
85   ms = noit_check_schedule_next(rcl->self, NULL, rcl->check, now,
86                                 rcl->dispatch, NULL);
87   if(ms == 0)
88     rcl->check->fire_event = NULL; /* This is us, we get free post-return */
89   if(NOIT_CHECK_RESOLVED(rcl->check)) {
90     if(MTEV_HOOK_CONTINUE ==
91        check_preflight_hook_invoke(rcl->self, rcl->check, rcl->cause)) {
92       if(NOIT_CHECK_DISPATCH_ENABLED()) {
93         char id[UUID_STR_LEN+1];
94         uuid_unparse_lower(rcl->check->checkid, id);
95         NOIT_CHECK_DISPATCH(id, rcl->check->module, rcl->check->name,
96                             rcl->check->target);
97       }
98       if(ms < rcl->check->timeout && !(rcl->check->flags & NP_TRANSIENT))
99         mtevL(noit_error, "%s might not finish in %dms (timeout %dms)\n",
100               rcl->check->name, ms, rcl->check->timeout);
101       rcl->dispatch(rcl->self, rcl->check, rcl->cause);
102     }
103     check_postflight_hook_invoke(rcl->self, rcl->check, rcl->cause);
104   }
105   else
106     mtevL(noit_debug, "skipping %s`%s`%s, unresolved\n",
107           rcl->check->target, rcl->check->module, rcl->check->name);
108   free(rcl);
109   return 0;
110 }
111
112 int
113 noit_check_schedule_next(noit_module_t *self,
114                          struct timeval *last_check, noit_check_t *check,
115                          struct timeval *now, dispatch_func_t dispatch,
116                          noit_check_t *cause) {
117   eventer_t newe;
118   struct timeval period, earliest, diff;
119   int64_t diffms, periodms, offsetms;
120   recur_closure_t *rcl;
121   int initial = last_check ? 1 : 0;
122
123   assert(cause == NULL);
124   if(check->period == 0) return 0;
125
126   /* if last_check is not passed, we use the initial_schedule_time
127    * otherwise, we set the initial_schedule_time
128    */
129   if(!last_check) last_check = &check->initial_schedule_time;
130   else memcpy(&check->initial_schedule_time, last_check, sizeof(*last_check));
131
132   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) {
133     if(!(check->flags & NP_TRANSIENT)) check_slots_dec_tv(last_check);
134     memset(&check->initial_schedule_time, 0, sizeof(struct timeval));
135     return 0;
136   }
137
138   /* If we have an event, we know when we intended it to fire.  This means
139    * we should schedule that point + period.
140    */
141   if(now)
142     memcpy(&earliest, now, sizeof(earliest));
143   else
144     gettimeofday(&earliest, NULL);
145
146   /* If the check is unconfigured and needs resolving, we'll set the
147    * period down a bit lower so we can pick up the resolution quickly.
148    * The one exception is if this is the initial run.
149    */
150   if(!initial &&
151      !NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
152      check->period > 1000) {
153     period.tv_sec = 1;
154     period.tv_usec = 0;
155   }
156   else {
157     period.tv_sec = check->period / 1000;
158     period.tv_usec = (check->period % 1000) * 1000;
159   }
160   periodms = period.tv_sec * 1000 + period.tv_usec / 1000;
161
162   newe = eventer_alloc();
163   /* calculate the differnet between the initial schedule time and "now" */
164   if(compare_timeval(earliest, *last_check) >= 0) {
165     sub_timeval(earliest, *last_check, &diff);
166     diffms = (int64_t)diff.tv_sec * 1000 + diff.tv_usec / 1000;
167   }
168   else {
169     mtevL(noit_error, "time is going backwards. abort.\n");
170     abort();
171   }
172   /* determine the offset from initial schedule time that would place
173    * us at the next period-aligned point past "now" */
174   offsetms = ((diffms / periodms) + 1) * periodms;
175   diff.tv_sec = offsetms / 1000;
176   diff.tv_usec = (offsetms % 1000) * 1000;
177
178   memcpy(&newe->whence, last_check, sizeof(*last_check));
179   add_timeval(newe->whence, diff, &newe->whence);
180
181   sub_timeval(newe->whence, earliest, &diff);
182   diffms = (int64_t)diff.tv_sec * 1000 + (int)diff.tv_usec / 1000;
183   assert(compare_timeval(newe->whence, earliest) > 0);
184   newe->mask = EVENTER_TIMER;
185   newe->callback = noit_check_recur_handler;
186   rcl = calloc(1, sizeof(*rcl));
187   rcl->self = self;
188   rcl->check = check;
189   rcl->cause = cause;
190   rcl->dispatch = dispatch;
191   newe->closure = rcl;
192
193   /* knuth's golden ratio approach */
194   if(!self->thread_unsafe) {
195     newe->thr_owner = CHOOSE_EVENTER_THREAD_FOR_CHECK(check);
196   }
197   check->fire_event = newe;
198   eventer_add(newe);
199   return diffms;
200 }
201
202 void
203 noit_check_run_full_asynch_opts(noit_check_t *check, eventer_func_t callback,
204                                 int mask) {
205   struct timeval __now, p_int;
206   eventer_t e;
207   e = eventer_alloc();
208   e->fd = -1;
209   e->mask = EVENTER_ASYNCH | mask;
210   gettimeofday(&__now, NULL);
211   memcpy(&e->whence, &__now, sizeof(__now));
212   p_int.tv_sec = check->timeout / 1000;
213   p_int.tv_usec = (check->timeout % 1000) * 1000;
214   add_timeval(e->whence, p_int, &e->whence);
215   e->callback = callback;
216   e->closure =  check->closure;
217   eventer_add(e);
218 }
219 void
220 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
221   noit_check_run_full_asynch_opts(check, callback,
222                                   EVENTER_DEFAULT_ASYNCH_ABORT);
223 }
224
225 void
226 noit_check_tools_init() {
227   noit_check_tools_shared_init();
228   eventer_name_callback_ext("noit_check_recur_handler",
229                             noit_check_recur_handler,
230                             noit_check_recur_name_details, NULL);
231 }
232
233 static int
234 populate_stats_from_resmon_formatted_json(noit_check_t *check,
235                                           stats_t *s, struct json_object *o,
236                                           const char *prefix) {
237   int count = 0;
238   char keybuff[256];
239 #define MKKEY(fmt, arg) do { \
240   if(prefix) snprintf(keybuff, sizeof(keybuff), "%s`" fmt, prefix, arg); \
241   else snprintf(keybuff, sizeof(keybuff), fmt, arg); \
242 } while(0)
243   if(o == NULL) {
244     if(prefix) {
245       noit_stats_set_metric(check, s, prefix, METRIC_STRING, NULL);
246       count++;
247     }
248     return count;
249   }
250   switch(json_object_get_type(o)) {
251     /* sub callers */
252     case json_type_array:
253     {
254       int i, alen = json_object_array_length(o);
255       for(i=0;i<alen;i++) {
256         struct json_object *item = json_object_array_get_idx(o, i);
257         MKKEY("%d", i);
258         count += populate_stats_from_resmon_formatted_json(check, s, item, keybuff);
259       }
260     }
261     break;
262     case json_type_object:
263     {
264       struct jl_lh_table *lh;
265       struct jl_lh_entry *el;
266       struct json_object *has_type = NULL, *has_value = NULL;
267       lh = json_object_get_object(o);
268       jl_lh_foreach(lh, el) {
269         if(!strcmp(el->k, "_type")) has_type = (struct json_object *)el->v;
270         else if(!strcmp(el->k, "_value")) has_value = (struct json_object *)el->v;
271         else {
272           struct json_object *item = (struct json_object *)el->v;
273           MKKEY("%s", (const char *)el->k);
274           count += populate_stats_from_resmon_formatted_json(check, s, item, keybuff);
275         }
276       }
277       if(prefix && has_type &&
278          json_object_is_type(has_type, json_type_string)) {
279         const char *type_str = json_object_get_string(has_type);
280
281 #define COERCE_JSON_OBJECT(type, item) do { \
282   const char *value_str = NULL; \
283   if(json_object_is_type(item, json_type_string)) \
284     value_str = json_object_get_string(item); \
285   else if(!json_object_is_type(item, json_type_null)) \
286     value_str = json_object_to_json_string(item); \
287   switch(type) { \
288     case METRIC_INT32: case METRIC_UINT32: case METRIC_INT64: \
289     case METRIC_UINT64: case METRIC_DOUBLE: case METRIC_STRING: \
290       noit_stats_set_metric_coerce(check, s, prefix, \
291                                    (metric_type_t)type, value_str); \
292       count++; \
293     default: \
294       break; \
295   } \
296 } while(0)
297
298
299         if (has_value == NULL) {
300           noit_stats_set_metric_coerce(check, s, prefix, (metric_type_t)*type_str, NULL);
301           count++;
302         }
303         else if(json_object_is_type(has_value, json_type_array)) {
304           int i, alen = json_object_array_length(has_value);
305           for(i=0;i<alen;i++) {
306             struct json_object *item = json_object_array_get_idx(has_value, i);
307             COERCE_JSON_OBJECT(*type_str, item);
308           }
309         }
310         else {
311           COERCE_JSON_OBJECT(*type_str, has_value);
312         }
313       }
314       break;
315     }
316
317     /* directs */
318     case json_type_string:
319       if(prefix) {
320         noit_stats_set_metric(check, s, prefix, METRIC_GUESS,
321                               (char *)json_object_get_string(o));
322         count++;
323       }
324       break;
325     case json_type_boolean:
326       if(prefix) {
327         int val = json_object_get_boolean(o) ? 1 : 0;
328         noit_stats_set_metric(check, s, prefix, METRIC_INT32, &val);
329         count++;
330       }
331       break;
332     case json_type_null:
333       if(prefix) {
334         noit_stats_set_metric(check, s, prefix, METRIC_STRING, NULL);
335         count++;
336       }
337       break;
338     case json_type_double:
339       if(prefix) {
340         double val = json_object_get_double(o);
341         noit_stats_set_metric(check, s, prefix, METRIC_DOUBLE, &val);
342         count++;
343       }
344       break;
345     case json_type_int:
346       if(prefix) {
347         int64_t i64;
348         uint64_t u64;
349         switch(json_object_get_int_overflow(o)) {
350           case json_overflow_int:
351             i64 = json_object_get_int(o);
352             noit_stats_set_metric(check, s, prefix, METRIC_INT64, &i64);
353             count++;
354             break;
355           case json_overflow_int64:
356             i64 = json_object_get_int64(o);
357             noit_stats_set_metric(check, s, prefix, METRIC_INT64, &i64);
358             count++;
359             break;
360           case json_overflow_uint64:
361             u64 = json_object_get_uint64(o);
362             noit_stats_set_metric(check, s, prefix, METRIC_UINT64, &u64);
363             count++;
364             break;
365         }
366       }
367   }
368   return count;
369 }
370 int
371 noit_check_stats_from_json_str(noit_check_t *check, stats_t *s,
372                                const char *json_str, int len) {
373   int rv = -1;
374   struct json_tokener *tok = NULL;
375   struct json_object *root = NULL;
376   tok = json_tokener_new();
377   root = json_tokener_parse_ex(tok, json_str, len);
378   if(root) rv = populate_stats_from_resmon_formatted_json(check, s, root, NULL);
379   if(tok) json_tokener_free(tok);
380   if(root) json_object_put(root);
381   return rv;
382 }
383
384 void
385 noit_check_make_attrs(noit_check_t *check, mtev_hash_table *attrs) {
386 #define CA_STORE(a,b) mtev_hash_store(attrs, a, strlen(a), b)
387   CA_STORE("target", check->target);
388   CA_STORE("target_ip", check->target_ip);
389   CA_STORE("name", check->name);
390   CA_STORE("module", check->module);
391 }
Note: See TracBrowser for help on using the browser.