root/src/noit_check_tools.c

Revision 278ca955d564824e83664c1eebbfbb36c0ef1b0c, 9.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 2 years ago)

remove trailing ;, empty declaration error in sun studio

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