root/src/noit_check_tools.c

Revision 70c30ef22f6d1312375e0392164d755db5824cb5, 13.1 kB (checked in by Phil Maddox <philip.maddox@circonus.com>, 3 days ago)

Use mtevAssert and mtevFatal Instead Of assert() and abort()

Use libmtev calls to safely flush logs and abort rather than calling
the assert and abort calls directly.

  • 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 MTEV_HOOK_IMPL(check_preflight,
44   (noit_module_t *self, noit_check_t *check, noit_check_t *cause),
45   void *, closure,
46   (void *closure, noit_module_t *self, noit_check_t *check, noit_check_t *cause),
47   (closure,self,check,cause))
48 MTEV_HOOK_IMPL(check_postflight,
49   (noit_module_t *self, noit_check_t *check, noit_check_t *cause),
50   void *, closure,
51   (void *closure, noit_module_t *self, noit_check_t *check, noit_check_t *cause),
52   (closure,self,check,cause))
53
54 typedef struct {
55   noit_module_t *self;
56   noit_check_t *check;
57   noit_check_t *cause;
58   dispatch_func_t dispatch;
59 } recur_closure_t;
60
61 static void
62 noit_check_recur_name_details(char *buf, int buflen,
63                               eventer_t e, void *closure) {
64   char id_str[UUID_STR_LEN+1];
65   recur_closure_t *rcl = e ? e->closure : NULL;
66   if(!e) {
67     snprintf(buf, buflen, "noit_check_recur_handler");
68     return;
69   }
70   uuid_unparse_lower(rcl->check->checkid, id_str);
71   snprintf(buf, buflen, "fire(%s)", id_str);
72   return;
73 }
74 static int
75 noit_check_recur_handler(eventer_t e, int mask, void *closure,
76                               struct timeval *now) {
77   recur_closure_t *rcl = closure;
78   int ms;
79
80   if(e != rcl->check->fire_event) return 0;
81
82   noit_check_resolve(rcl->check);
83   ms = noit_check_schedule_next(rcl->self, NULL, rcl->check, now,
84                                 rcl->dispatch, NULL);
85   if(ms == 0)
86     rcl->check->fire_event = NULL; /* This is us, we get free post-return */
87   if(NOIT_CHECK_RESOLVED(rcl->check)) {
88     if(MTEV_HOOK_CONTINUE ==
89        check_preflight_hook_invoke(rcl->self, rcl->check, rcl->cause)) {
90       if(NOIT_CHECK_DISPATCH_ENABLED()) {
91         char id[UUID_STR_LEN+1];
92         uuid_unparse_lower(rcl->check->checkid, id);
93         NOIT_CHECK_DISPATCH(id, rcl->check->module, rcl->check->name,
94                             rcl->check->target);
95       }
96       if(ms < rcl->check->timeout && !(rcl->check->flags & NP_TRANSIENT))
97         mtevL(noit_error, "%s might not finish in %dms (timeout %dms)\n",
98               rcl->check->name, ms, rcl->check->timeout);
99       rcl->dispatch(rcl->self, rcl->check, rcl->cause);
100     }
101     check_postflight_hook_invoke(rcl->self, rcl->check, rcl->cause);
102   }
103   else
104     mtevL(noit_debug, "skipping %s`%s`%s, unresolved\n",
105           rcl->check->target, rcl->check->module, rcl->check->name);
106   free(rcl);
107   return 0;
108 }
109
110 int
111 noit_check_schedule_next(noit_module_t *self,
112                          struct timeval *last_check, noit_check_t *check,
113                          struct timeval *now, dispatch_func_t dispatch,
114                          noit_check_t *cause) {
115   eventer_t newe;
116   struct timeval period, earliest, diff;
117   int64_t diffms, periodms, offsetms;
118   recur_closure_t *rcl;
119   int initial = last_check ? 1 : 0;
120
121   mtevAssert(cause == NULL);
122   if(check->period == 0) return 0;
123
124   /* if last_check is not passed, we use the initial_schedule_time
125    * otherwise, we set the initial_schedule_time
126    */
127   if(!last_check) last_check = &check->initial_schedule_time;
128   else memcpy(&check->initial_schedule_time, last_check, sizeof(*last_check));
129
130   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) {
131     if(!(check->flags & NP_TRANSIENT)) check_slots_dec_tv(last_check);
132     memset(&check->initial_schedule_time, 0, sizeof(struct timeval));
133     return 0;
134   }
135
136   /* If we have an event, we know when we intended it to fire.  This means
137    * we should schedule that point + period.
138    */
139   if(now)
140     memcpy(&earliest, now, sizeof(earliest));
141   else
142     gettimeofday(&earliest, NULL);
143
144   /* If the check is unconfigured and needs resolving, we'll set the
145    * period down a bit lower so we can pick up the resolution quickly.
146    * The one exception is if this is the initial run.
147    */
148   if(!initial &&
149      !NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
150      check->period > 1000) {
151     period.tv_sec = 1;
152     period.tv_usec = 0;
153   }
154   else {
155     period.tv_sec = check->period / 1000;
156     period.tv_usec = (check->period % 1000) * 1000;
157   }
158   periodms = period.tv_sec * 1000 + period.tv_usec / 1000;
159
160   newe = eventer_alloc();
161   /* calculate the differnet between the initial schedule time and "now" */
162   if(compare_timeval(earliest, *last_check) >= 0) {
163     sub_timeval(earliest, *last_check, &diff);
164     diffms = (int64_t)diff.tv_sec * 1000 + diff.tv_usec / 1000;
165   }
166   else {
167     mtevFatal(noit_error, "time is going backwards. abort.\n");
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   mtevAssert(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                                           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, 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, 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, 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, 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, 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, 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, prefix, METRIC_INT32, &val);
326         count++;
327       }
328       break;
329     case json_type_null:
330       if(prefix) {
331         noit_stats_set_metric(check, 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, 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, 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, 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, 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,
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, root, NULL);
376   if(tok) json_tokener_free(tok);
377   if(root) json_object_put(root);
378   return rv;
379 }
380
381 void
382 noit_check_make_attrs(noit_check_t *check, mtev_hash_table *attrs) {
383 #define CA_STORE(a,b) mtev_hash_store(attrs, a, strlen(a), b)
384   CA_STORE("target", check->target);
385   CA_STORE("target_ip", check->target_ip);
386   CA_STORE("name", check->name);
387   CA_STORE("module", check->module);
388 }
Note: See TracBrowser for help on using the browser.