root/src/noit_check_tools.c

Revision 14748d7c2d9bb7457d44bf98a24b5f03860e2b63, 10.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

crash bug on null values in JSON

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