root/src/noit_check.c

Revision e01b3ad015472c9a83ced96738b07da543511a40, 16.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 10 years ago)

check, check, 1, 2, 3

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <assert.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
14
15 #include "utils/noit_log.h"
16 #include "utils/noit_hash.h"
17 #include "utils/noit_skiplist.h"
18 #include "noit_conf.h"
19 #include "noit_check.h"
20 #include "noit_module.h"
21 #include "noit_console.h"
22 #include "eventer/eventer.h"
23
24 /* 60 seconds of possible stutter */
25 #define MAX_INITIAL_STUTTER (60*1000)
26
27 static noit_hash_table polls = NOIT_HASH_EMPTY;
28 static noit_skiplist polls_by_name = { 0 };
29 static u_int32_t __config_load_generation = 0;
30 struct uuid_dummy {
31   uuid_t foo;
32 };
33
34 static void register_console_check_commands();
35
36 #define UUID_SIZE sizeof(struct uuid_dummy)
37
38 static const char *
39 __noit_check_available_string(int16_t available) {
40   switch(available) {
41     case NP_AVAILABLE:    return "available";
42     case NP_UNAVAILABLE:  return "unavailable";
43     case NP_UNKNOWN:      return "unknown";
44   }
45   return "???";
46 }
47 static const char *
48 __noit_check_state_string(int16_t state) {
49   switch(state) {
50     case NP_GOOD:         return "good";
51     case NP_BAD:          return "bad";
52     case NP_UNKNOWN:      return "unknown";
53   }
54   return "???";
55 }
56 static int __check_name_compare(void *a, void *b) {
57   noit_check_t *ac = a;
58   noit_check_t *bc = b;
59   int rv;
60   if((rv = strcmp(ac->target, bc->target)) != 0) return rv;
61   if((rv = strcmp(ac->name, bc->name)) != 0) return rv;
62   return 0;
63 }
64 int
65 noit_check_max_initial_stutter() {
66   return MAX_INITIAL_STUTTER;
67 }
68 void
69 noit_check_fake_last_check(noit_check_t *check,
70                            struct timeval *lc, struct timeval *_now) {
71   struct timeval now, period;
72   double r;
73   int offset;
74
75   r = drand48();
76   offset = r * (MIN(MAX_INITIAL_STUTTER, check->period));
77   period.tv_sec = (check->period - offset) / 1000;
78   period.tv_usec = ((check->period - offset) % 1000) * 1000;
79   if(!_now) {
80     gettimeofday(&now, NULL);
81     _now = &now;
82   }
83   sub_timeval(*_now, period, lc);
84 }
85 void
86 noit_poller_process_checks(const char *xpath) {
87   int i, flags, cnt = 0;
88   noit_conf_section_t *sec;
89   __config_load_generation++;
90   sec = noit_conf_get_sections(NULL, xpath, &cnt);
91   for(i=0; i<cnt; i++) {
92     noit_check_t *existing_check;
93     char uuid_str[37];
94     char target[256];
95     char module[256];
96     char name[256];
97     char oncheck[1024] = "";
98     int no_period = 0;
99     int no_oncheck = 0;
100     int period = 0, timeout = 0;
101     noit_conf_boolean disabled = noit_false, busted = noit_false;
102     uuid_t uuid, out_uuid;
103     noit_hash_table *options;
104
105 #define NEXT(...) noitL(noit_stderr, __VA_ARGS__); continue
106 #define MYATTR(type,a,...) noit_conf_get_##type(sec[i], "@" #a, __VA_ARGS__)
107 #define INHERIT(type,a,...) \
108   noit_conf_get_##type(sec[i], "ancestor-or-self::node()/@" #a, __VA_ARGS__)
109
110     if(!MYATTR(stringbuf, uuid, uuid_str, sizeof(uuid_str))) {
111       noitL(noit_stderr, "check %d has no uuid\n", i+1);
112       continue;
113     }
114
115     if(uuid_parse(uuid_str, uuid)) {
116       noitL(noit_stderr, "check uuid: '%s' is invalid\n", uuid_str);
117       continue;
118     }
119
120     if(!INHERIT(stringbuf, target, target, sizeof(target))) {
121       noitL(noit_stderr, "check uuid: '%s' has no target\n", uuid_str);
122       busted = noit_true;
123     }
124     if(!INHERIT(stringbuf, module, module, sizeof(module))) {
125       noitL(noit_stderr, "check uuid: '%s' has no module\n", uuid_str);
126       busted = noit_true;
127     }
128
129     if(!MYATTR(stringbuf, name, name, sizeof(name)))
130       strlcpy(name, module, sizeof(name));
131
132     if(!INHERIT(int, period, &period) || period == 0)
133       no_period = 1;
134
135     if(!INHERIT(stringbuf, oncheck, oncheck, sizeof(oncheck)) || !oncheck[0])
136       no_oncheck = 1;
137
138     if(no_period && no_oncheck) {
139       noitL(noit_stderr, "check uuid: '%s' has neither period nor oncheck\n",
140             uuid_str);
141       busted = noit_true;
142     }
143     if(!(no_period || no_oncheck)) {
144       noitL(noit_stderr, "check uuid: '%s' has oncheck and period.\n",
145             uuid_str);
146       busted = noit_true;
147     }
148     if(!INHERIT(int, timeout, &timeout)) {
149       noitL(noit_stderr, "check uuid: '%s' has no timeout\n", uuid_str);
150       busted = noit_true;
151     }
152     if(!no_period && timeout >= period) {
153       noitL(noit_stderr, "check uuid: '%s' timeout > period\n", uuid_str);
154       timeout = period/2;
155     }
156     options = noit_conf_get_hash(sec[i], "ancestor-or-self::node()/config/*");
157
158     flags = 0;
159     if(busted) flags |= NP_UNCONFIG;
160     if(disabled) flags |= NP_DISABLED;
161
162     if(noit_hash_retrieve(&polls, (char *)uuid, UUID_SIZE,
163                           (void **)&existing_check)) {
164       noit_check_update(existing_check, target, name, options,
165                            period, timeout, oncheck[0] ? oncheck : NULL,
166                            flags);
167       noitL(noit_debug, "reloaded uuid: %s\n", uuid_str);
168     }
169     else {
170       noit_poller_schedule(target, module, name, options,
171                            period, timeout, oncheck[0] ? oncheck : NULL,
172                            flags, uuid, out_uuid);
173       noitL(noit_debug, "loaded uuid: %s\n", uuid_str);
174     }
175   }
176 }
177
178 void
179 noit_poller_initiate() {
180   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
181   uuid_t key_id;
182   int klen;
183   noit_check_t *check;
184   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
185                        (void **)&check)) {
186     noit_module_t *mod;
187     mod = noit_module_lookup(check->module);
188     if(mod) {
189       if(NOIT_CHECK_LIVE(check))
190         continue;
191       if((check->flags & NP_DISABLED) == 0)
192         mod->initiate_check(mod, check, 0, NULL);
193       else
194         noitL(noit_debug, "Skipping %s`%s, disabled.\n",
195               check->target, check->name);
196     }
197     else {
198       noitL(noit_stderr, "Cannot find module '%s'\n", check->module);
199       check->flags |= NP_DISABLED;
200     }
201   }
202 }
203
204 void
205 noit_poller_make_causal_map() {
206   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
207   uuid_t key_id;
208   int klen;
209   noit_check_t *check, *parent;
210
211   /* Cleanup any previous causal map */
212   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
213                        (void **)&check)) {
214     dep_list_t *dep;
215     while((dep = check->causal_checks) != NULL) {
216       check->causal_checks = dep->next;
217       free(dep);
218     }
219   }
220
221   /* Walk all checks and add check dependencies to their parents */
222   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
223                        (void **)&check)) {
224     if(check->oncheck) {
225       /* This service is causally triggered by another service */
226       char fullcheck[1024];
227       char *name = check->oncheck;
228       char *target = NULL;
229
230       if((target = strchr(check->oncheck, '`')) != NULL) {
231         strlcpy(fullcheck, check->oncheck, target - check->oncheck);
232         name = target + 1;
233         target = fullcheck;
234       }
235       else
236        target = check->target;
237
238       parent = noit_poller_lookup_by_name(target, name);
239       if(!parent) {
240         check->flags |= NP_DISABLED;
241         noitL(noit_stderr, "Disabling check %s`%s, can't find oncheck %s`%s\n",
242               check->target, check->name, target, name);
243       }
244       else {
245         dep_list_t *dep;
246         dep = malloc(sizeof(*dep));
247         dep->check = check;
248         dep->next = parent->causal_checks;
249         parent->causal_checks = dep;
250         noitL(noit_debug, "Causal map %s`%s --> %s`%s\n",
251               parent->target, parent->name, check->target, check->name);
252       }
253     }
254   }
255 }
256 void
257 noit_poller_reload(const char *xpath)
258 {
259   noit_poller_process_checks(xpath ? xpath : "/noit/checks//check");
260   noit_poller_make_causal_map();
261   noit_poller_initiate();
262 }
263 void
264 noit_poller_init() {
265   noit_skiplist_init(&polls_by_name);
266   noit_skiplist_set_compare(&polls_by_name, __check_name_compare,
267                             __check_name_compare);
268   register_console_check_commands();
269   noit_poller_reload(NULL);
270 }
271
272 int
273 noit_check_update(noit_check_t *new_check,
274                   const char *target,
275                   const char *name,
276                   noit_hash_table *config,
277                   u_int32_t period,
278                   u_int32_t timeout,
279                   const char *oncheck,
280                   int flags) {
281   int8_t family;
282   int rv;
283   int mask = NP_DISABLED | NP_UNCONFIG;
284   union {
285     struct in_addr addr4;
286     struct in6_addr addr6;
287   } a;
288
289
290   family = AF_INET;
291   rv = inet_pton(family, target, &a);
292   if(rv != 1) {
293     family = AF_INET6;
294     rv = inet_pton(family, target, &a);
295     if(rv != 1) {
296       noitL(noit_stderr, "Cannot translate '%s' to IP\n", target);
297       memset(&a, 0, sizeof(a));
298       flags |= (NP_UNCONFIG & NP_DISABLED);
299     }
300   }
301
302   new_check->generation = __config_load_generation;
303   new_check->target_family = family;
304   memcpy(&new_check->target_addr, &a, sizeof(a));
305   if(new_check->target) free(new_check->target);
306   new_check->target = strdup(target);
307   if(new_check->name) free(new_check->name);
308   new_check->name = name ? strdup(name): NULL;
309
310   if(config != NULL) {
311     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
312     const char *k;
313     int klen;
314     void *data;
315     if(new_check->config) noit_hash_delete_all(new_check->config, free, free);
316     else new_check->config = calloc(1, sizeof(*new_check->config));
317     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
318       noit_hash_store(new_check->config, strdup(k), klen, strdup((char *)data));
319     }
320   }
321   if(new_check->oncheck) free(new_check->oncheck);
322   new_check->oncheck = oncheck ? strdup(oncheck) : NULL;
323   new_check->period = period;
324   new_check->timeout = timeout;
325
326   /* Unset what could be set.. then set what should be set */
327   new_check->flags = (new_check->flags & ~mask) | flags;
328
329   /* This remove could fail -- no big deal */
330   noit_skiplist_remove(&polls_by_name, new_check, NULL);
331
332   /* This insert could fail.. which means we have a conflict on
333    * target`name.  That should result in the check being disabled. */
334   if(!noit_skiplist_insert(&polls_by_name, new_check)) {
335     noitL(noit_stderr, "Check %s`%s disabled due to naming conflict\n",
336           new_check->target, new_check->name);
337     new_check->flags |= NP_DISABLED;
338   }
339   return 0;
340 }
341 int
342 noit_poller_schedule(const char *target,
343                      const char *module,
344                      const char *name,
345                      noit_hash_table *config,
346                      u_int32_t period,
347                      u_int32_t timeout,
348                      const char *oncheck,
349                      int flags,
350                      uuid_t in,
351                      uuid_t out) {
352   noit_check_t *new_check;
353   new_check = calloc(1, sizeof(*new_check));
354   if(!new_check) return -1;
355
356   /* The module and the UUID can never be changed */
357   new_check->module = strdup(module);
358   if(uuid_is_null(in))
359     uuid_generate(new_check->checkid);
360   else
361     uuid_copy(new_check->checkid, in);
362
363   noit_check_update(new_check, target, name, config,
364                     period, timeout, oncheck, flags);
365   assert(noit_hash_store(&polls,
366                          (char *)new_check->checkid, UUID_SIZE,
367                          new_check));
368   uuid_copy(out, new_check->checkid);
369
370   return 0;
371 }
372
373 int
374 noit_poller_deschedule(uuid_t in) {
375   noit_check_t *checker;
376   noit_module_t *mod;
377   if(noit_hash_retrieve(&polls,
378                         (char *)in, UUID_SIZE,
379                         (void **)&checker) == 0) {
380     return -1;
381   }
382   if(checker->flags & NP_RUNNING) {
383     checker->flags |= NP_KILLED;
384     return 0;
385   }
386   checker->flags |= NP_KILLED;
387
388   noit_skiplist_remove(&polls_by_name, checker, NULL);
389   noit_hash_delete(&polls, (char *)in, UUID_SIZE, NULL, NULL);
390
391   mod = noit_module_lookup(checker->module);
392   mod->cleanup(mod, checker);
393   if(checker->fire_event) {
394      eventer_remove(checker->fire_event);
395      eventer_free(checker->fire_event);
396      checker->fire_event = NULL;
397   }
398
399   if(checker->target) free(checker->target);
400   if(checker->module) free(checker->module);
401   if(checker->name) free(checker->name);
402   if(checker->config) {
403     noit_hash_destroy(checker->config, free, free);
404     free(checker->config);
405     checker->config = NULL;
406   }
407   free(checker);
408   return 0;
409 }
410
411 noit_check_t *
412 noit_poller_lookup(uuid_t in) {
413   noit_check_t *check;
414   if(noit_hash_retrieve(&polls,
415                         (char *)in, UUID_SIZE,
416                         (void **)&check)) {
417     return check;
418   }
419   return NULL;
420 }
421 noit_check_t *
422 noit_poller_lookup_by_name(char *target, char *name) {
423   noit_check_t *check, *tmp_check;
424   tmp_check = calloc(1, sizeof(*tmp_check));
425   tmp_check->target = target;
426   tmp_check->name = name;
427   check = noit_skiplist_find(&polls_by_name, tmp_check, NULL);
428   free(tmp_check);
429   return check;
430 }
431
432 static void
433 __free_metric(void *vm) {
434   metric_t *m = vm;
435   free(m->metric_name);
436   if(m->metric_value.i) free(m->metric_value.i);
437 }
438
439 void
440 __stats_add_metric(stats_t *newstate, metric_t *m) {
441   noit_hash_replace(&newstate->metrics, m->metric_name, strlen(m->metric_name),
442                     m, NULL, __free_metric);
443 }
444
445 void
446 noit_stats_set_metric_int(stats_t *newstate, char *name, int *value) {
447   metric_t *m = calloc(1, sizeof(*m));
448   m->metric_name = strdup(name);
449   m->metric_type = METRIC_INT;
450   if(value) {
451     m->metric_value.i = malloc(sizeof(*value));
452     *(m->metric_value.i) = *value;
453   }
454   __stats_add_metric(newstate, m);
455 }
456
457 void
458 noit_stats_set_metric_float(stats_t *newstate, char *name, float *value) {
459   metric_t *m = calloc(1, sizeof(*m));
460   m->metric_name = strdup(name);
461   m->metric_type = METRIC_FLOAT;
462   if(value) {
463     m->metric_value.f = malloc(sizeof(*value));
464     *(m->metric_value.f) = *value;
465   }
466   __stats_add_metric(newstate, m);
467 }
468
469 void
470 noit_stats_set_metric_string(stats_t *newstate, char *name, char *value) {
471   metric_t *m = calloc(1, sizeof(*m));
472   m->metric_name = strdup(name);
473   m->metric_type = METRIC_STRING;
474   m->metric_value.s = value ? strdup(value) : NULL;
475   __stats_add_metric(newstate, m);
476 }
477
478 void
479 noit_check_set_stats(struct _noit_module *module,
480                      noit_check_t *check, stats_t *newstate) {
481   int report_change = 0;
482   dep_list_t *dep;
483   if(check->stats.previous.status)
484     free(check->stats.previous.status);
485   noit_hash_destroy(&check->stats.previous.metrics, NULL, __free_metric);
486   memcpy(&check->stats.previous, &check->stats.current, sizeof(stats_t));
487   memcpy(&check->stats.current, newstate, sizeof(stats_t));
488   if(check->stats.current.status)
489     check->stats.current.status = strdup(check->stats.current.status);
490
491   /* check for state changes */
492   if(check->stats.current.available != 0 &&
493      check->stats.previous.available != 0 &&
494      check->stats.current.available != check->stats.previous.available)
495     report_change = 1;
496   if(check->stats.current.state != 0 &&
497      check->stats.previous.state != 0 &&
498      check->stats.current.state != check->stats.previous.state)
499     report_change = 1;
500
501   noitL(noit_error, "%s`%s <- [%s]\n", check->target, check->name,
502         check->stats.current.status);
503   if(report_change) {
504     noitL(noit_error, "%s`%s -> [%s:%s]\n",
505           check->target, check->name,
506           __noit_check_available_string(check->stats.current.available),
507           __noit_check_state_string(check->stats.current.state));
508   }
509   for(dep = check->causal_checks; dep; dep = dep->next) {
510     noit_module_t *mod;
511     mod = noit_module_lookup(dep->check->module);
512     assert(mod);
513     noitL(noit_debug, "Firing %s`%s in response to %s`%s\n",
514           dep->check->target, dep->check->name,
515           check->target, check->name);
516     mod->initiate_check(mod, dep->check, 1, check);
517   }
518 }
519
520 static void
521 nc_printf_check_brief(noit_console_closure_t ncct,
522                       noit_check_t *check) {
523   char out[512];
524   char uuid_str[37];
525   snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
526   uuid_unparse_lower(check->checkid, uuid_str);
527   nc_printf(ncct, "%s %s\n", uuid_str, out);
528   if(check->stats.current.status)
529     nc_printf(ncct, "\t%s\n", check->stats.current.status);
530 }
531
532 static int
533 noit_console_show_checks(noit_console_closure_t ncct,
534                          int argc, char **argv,
535                          noit_console_state_t *dstate,
536                          void *closure) {
537   struct timeval _now;
538   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
539   uuid_t key_id;
540   int klen;
541   noit_check_t *check;
542
543   gettimeofday(&_now, NULL);
544   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
545                        (void **)&check)) {
546     nc_printf_check_brief(ncct, check);
547   }
548   return 0;
549 }
550
551 static void
552 register_console_check_commands() {
553   noit_console_state_t *tl;
554   cmd_info_t *showcmd;
555
556   tl = noit_console_state_initial();
557   showcmd = noit_console_state_get_cmd(tl, "show");
558   assert(showcmd && showcmd->dstate);
559
560   noit_console_state_add_cmd(showcmd->dstate,
561     NCSCMD("checks", noit_console_show_checks, NULL, NULL));
562 }
563
Note: See TracBrowser for help on using the browser.