root/src/noit_check_tools.c

Revision 46b9cf0be63c479e256c74b79f413f940c8f0563, 10.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 2 years ago)

make setting of metrcis hookable

  • 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   rcl->check->fire_event = NULL; /* This is us, we get free post-return */
65   noit_check_resolve(rcl->check);
66   noit_check_schedule_next(rcl->self, &e->whence, rcl->check, now,
67                            rcl->dispatch, NULL);
68   if(NOIT_CHECK_RESOLVED(rcl->check)) {
69     if(NOIT_HOOK_CONTINUE ==
70        check_preflight_hook_invoke(rcl->self, rcl->check, rcl->cause)) {
71       if(NOIT_CHECK_DISPATCH_ENABLED()) {
72         char id[UUID_STR_LEN+1];
73         uuid_unparse_lower(rcl->check->checkid, id);
74         NOIT_CHECK_DISPATCH(id, rcl->check->module, rcl->check->name,
75                             rcl->check->target);
76       }
77       rcl->dispatch(rcl->self, rcl->check, rcl->cause);
78     }
79     check_postflight_hook_invoke(rcl->self, rcl->check, rcl->cause);
80   }
81   else
82     noitL(noit_debug, "skipping %s`%s`%s, unresolved\n",
83           rcl->check->target, rcl->check->module, rcl->check->name);
84   free(rcl);
85   return 0;
86 }
87
88 int
89 noit_check_schedule_next(noit_module_t *self,
90                          struct timeval *last_check, noit_check_t *check,
91                          struct timeval *now, dispatch_func_t dispatch,
92                          noit_check_t *cause) {
93   eventer_t newe;
94   struct timeval period, earliest;
95   recur_closure_t *rcl;
96
97   assert(cause == NULL);
98   assert(check->fire_event == NULL);
99   if(check->period == 0) return 0;
100   if(NOIT_CHECK_DISABLED(check) || NOIT_CHECK_KILLED(check)) {
101     if(!(check->flags & NP_TRANSIENT)) check_slots_dec_tv(last_check);
102     return 0;
103   }
104
105   /* If we have an event, we know when we intended it to fire.  This means
106    * we should schedule that point + period.
107    */
108   if(now)
109     memcpy(&earliest, now, sizeof(earliest));
110   else
111     gettimeofday(&earliest, NULL);
112
113   /* If the check is unconfigured and needs resolving, we'll set the
114    * period down a bit lower so we can pick up the resolution quickly.
115    */
116   if(!NOIT_CHECK_RESOLVED(check) && NOIT_CHECK_SHOULD_RESOLVE(check) &&
117       check->period > 1000) {
118     period.tv_sec = 1;
119     period.tv_usec = 0;
120   }
121   else {
122     period.tv_sec = check->period / 1000;
123     period.tv_usec = (check->period % 1000) * 1000;
124   }
125
126   newe = eventer_alloc();
127   memcpy(&newe->whence, last_check, sizeof(*last_check));
128   add_timeval(newe->whence, period, &newe->whence);
129   if(compare_timeval(newe->whence, earliest) < 0)
130     memcpy(&newe->whence, &earliest, sizeof(earliest));
131   newe->mask = EVENTER_TIMER;
132   newe->callback = noit_check_recur_handler;
133   rcl = calloc(1, sizeof(*rcl));
134   rcl->self = self;
135   rcl->check = check;
136   rcl->cause = cause;
137   rcl->dispatch = dispatch;
138   newe->closure = rcl;
139
140   eventer_add(newe);
141   check->fire_event = newe;
142   return 0;
143 }
144
145 void
146 noit_check_run_full_asynch_opts(noit_check_t *check, eventer_func_t callback,
147                                 int mask) {
148   struct timeval __now, p_int;
149   eventer_t e;
150   e = eventer_alloc();
151   e->fd = -1;
152   e->mask = EVENTER_ASYNCH | mask;
153   gettimeofday(&__now, NULL);
154   memcpy(&e->whence, &__now, sizeof(__now));
155   p_int.tv_sec = check->timeout / 1000;
156   p_int.tv_usec = (check->timeout % 1000) * 1000;
157   add_timeval(e->whence, p_int, &e->whence);
158   e->callback = callback;
159   e->closure =  check->closure;
160   eventer_add(e);
161 }
162 void
163 noit_check_run_full_asynch(noit_check_t *check, eventer_func_t callback) {
164   noit_check_run_full_asynch_opts(check, callback,
165                                   EVENTER_DEFAULT_ASYNCH_ABORT);
166 }
167
168 void
169 noit_check_tools_init() {
170   noit_check_tools_shared_init();
171   eventer_name_callback("noit_check_recur_handler", noit_check_recur_handler);
172 }
173
174 static int
175 populate_stats_from_resmon_formatted_json(noit_check_t *check,
176                                           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(check, 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(check, 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(check, 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(check, 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(check, 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(check, s, prefix, METRIC_INT32, &val);
252         count++;
253       }
254       break;
255     case json_type_null:
256       if(prefix) {
257         noit_stats_set_metric(check, 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(check, 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(check, 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(check, 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(check, 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(noit_check_t *check, stats_t *s,
295                                const char *json_str, int len) {
296   int rv = -1;
297   struct json_tokener *tok = NULL;
298   struct json_object *root = NULL;
299   tok = json_tokener_new();
300   root = json_tokener_parse_ex(tok, json_str, len);
301   if(root) rv = populate_stats_from_resmon_formatted_json(check, s, root, NULL);
302   if(tok) json_tokener_free(tok);
303   if(root) json_object_put(root);
304   return rv;
305 }
Note: See TracBrowser for help on using the browser.