root/src/noit_check_tools.c

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

Merge branch 'unclump'

This merge obsoletes the initial stutter and "random"
distribution across a 60 second window and replaces it
with a metered distribution across 20ms slots in a one
minute time window.

Conflicts:

src/noit_check.c

  • 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   switch(json_object_get_type(o)) {
185     /* sub callers */
186     case json_type_array:
187     {
188       int i, alen = json_object_array_length(o);
189       for(i=0;i<alen;i++) {
190         struct json_object *item = json_object_array_get_idx(o, i);
191         MKKEY("%d", i);
192         count += populate_stats_from_resmon_formatted_json(s, item, keybuff);
193       }
194     }
195     break;
196     case json_type_object:
197     {
198       struct lh_table *lh;
199       struct lh_entry *el;
200       struct json_object *has_type = NULL, *has_value = NULL;
201       lh = json_object_get_object(o);
202       lh_foreach(lh, el) {
203         if(!strcmp(el->k, "_type")) has_type = (struct json_object *)el->v;
204         else if(!strcmp(el->k, "_value")) has_value = (struct json_object *)el->v;
205         else {
206           struct json_object *item = (struct json_object *)el->v;
207           MKKEY("%s", (const char *)el->k);
208           count += populate_stats_from_resmon_formatted_json(s, item, keybuff);
209         }
210       }
211       if(prefix && has_type && has_value &&
212          json_object_is_type(has_type, json_type_string) &&
213          json_object_is_type(has_value, json_type_string)) {
214         const char *type_str = json_object_get_string(has_type);
215         const char *value_str = json_object_get_string(has_value);
216         switch(*type_str) {
217           case METRIC_INT32:
218           case METRIC_UINT32:
219           case METRIC_INT64:
220           case METRIC_UINT64:
221           case METRIC_DOUBLE:
222           case METRIC_STRING:
223             noit_stats_set_metric_coerce(s, prefix,
224                                          (metric_type_t)*type_str, value_str);
225             count++;
226           default:
227             break;
228         }
229       }
230       break;
231     }
232
233     /* directs */
234     case json_type_string:
235       if(prefix) {
236         noit_stats_set_metric(s, prefix, METRIC_GUESS,
237                               (char *)json_object_get_string(o));
238         count++;
239       }
240       break;
241     case json_type_boolean:
242       if(prefix) {
243         int val = json_object_get_boolean(o) ? 1 : 0;
244         noit_stats_set_metric(s, prefix, METRIC_INT32, &val);
245         count++;
246       }
247       break;
248     case json_type_null:
249       if(prefix) {
250         noit_stats_set_metric(s, prefix, METRIC_STRING, NULL);
251         count++;
252       }
253       break;
254     case json_type_double:
255       if(prefix) {
256         double val = json_object_get_double(o);
257         noit_stats_set_metric(s, prefix, METRIC_DOUBLE, &val);
258         count++;
259       }
260       break;
261     case json_type_int:
262       if(prefix) {
263         int64_t i64;
264         uint64_t u64;
265         switch(json_object_get_int_overflow(o)) {
266           case json_overflow_int:
267             i64 = json_object_get_int(o);
268             noit_stats_set_metric(s, prefix, METRIC_INT64, &i64);
269             count++;
270             break;
271           case json_overflow_int64:
272             i64 = json_object_get_int64(o);
273             noit_stats_set_metric(s, prefix, METRIC_INT64, &i64);
274             count++;
275             break;
276           case json_overflow_uint64:
277             u64 = json_object_get_uint64(o);
278             noit_stats_set_metric(s, prefix, METRIC_UINT64, &u64);
279             count++;
280             break;
281         }
282       }
283   }
284   return count;
285 }
286 int
287 noit_check_stats_from_json_str(stats_t *s, const char *json_str, int len) {
288   int rv = -1;
289   struct json_tokener *tok = NULL;
290   struct json_object *root = NULL;
291   tok = json_tokener_new();
292   root = json_tokener_parse_ex(tok, json_str, len);
293   if(root) rv = populate_stats_from_resmon_formatted_json(s, root, NULL);
294   if(tok) json_tokener_free(tok);
295   if(root) json_object_put(root);
296   return rv;
297 }
Note: See TracBrowser for help on using the browser.