root/src/noit_check.c

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

support 'no check' on console to delete checks as well as 'reload' on console (currently only reloads checks)

  • 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, 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;
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       disabled = 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       disabled = 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       disabled = 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       disabled = noit_true;
147     }
148     if(!INHERIT(int, timeout, &timeout)) {
149       noitL(noit_stderr, "check uuid: '%s' has no timeout\n", uuid_str);
150       disabled = 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     if(noit_hash_retrieve(&polls, (char *)uuid, UUID_SIZE,
159                           (void **)&existing_check)) {
160       noit_check_update(existing_check, target, name, options,
161                            period, timeout, oncheck[0] ? oncheck : NULL,
162                            disabled);
163       noitL(noit_debug, "reloaded uuid: %s\n", uuid_str);
164     }
165     else {
166       noit_poller_schedule(target, module, name, options,
167                            period, timeout, oncheck[0] ? oncheck : NULL,
168                            disabled, uuid, out_uuid);
169       noitL(noit_debug, "loaded uuid: %s\n", uuid_str);
170     }
171   }
172 }
173
174 void
175 noit_poller_initiate() {
176   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
177   uuid_t key_id;
178   int klen;
179   noit_check_t *check;
180   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
181                        (void **)&check)) {
182     noit_module_t *mod;
183     mod = noit_module_lookup(check->module);
184     if(mod) {
185       if(NOIT_CHECK_LIVE(check))
186         continue;
187       if((check->flags & NP_DISABLED) == 0)
188         mod->initiate_check(mod, check, 0, NULL);
189       else
190         noitL(noit_debug, "Skipping %s`%s, disabled.\n",
191               check->target, check->name);
192     }
193     else {
194       noitL(noit_stderr, "Cannot find module '%s'\n", check->module);
195       check->flags |= NP_DISABLED;
196     }
197   }
198 }
199
200 void
201 noit_poller_make_causal_map() {
202   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
203   uuid_t key_id;
204   int klen;
205   noit_check_t *check, *parent;
206
207   /* Cleanup any previous causal map */
208   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
209                        (void **)&check)) {
210     dep_list_t *dep;
211     while((dep = check->causal_checks) != NULL) {
212       check->causal_checks = dep->next;
213       free(dep);
214     }
215   }
216
217   /* Walk all checks and add check dependencies to their parents */
218   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
219                        (void **)&check)) {
220     if(check->oncheck) {
221       /* This service is causally triggered by another service */
222       char fullcheck[1024];
223       char *name = check->oncheck;
224       char *target = NULL;
225
226       if((target = strchr(check->oncheck, '`')) != NULL) {
227         strlcpy(fullcheck, check->oncheck, target - check->oncheck);
228         name = target + 1;
229         target = fullcheck;
230       }
231       else
232        target = check->target;
233
234       parent = noit_poller_lookup_by_name(target, name);
235       if(!parent) {
236         check->flags |= NP_DISABLED;
237         noitL(noit_stderr, "Disabling check %s`%s, can't find oncheck %s`%s\n",
238               check->target, check->name, target, name);
239       }
240       else {
241         dep_list_t *dep;
242         dep = malloc(sizeof(*dep));
243         dep->check = check;
244         dep->next = parent->causal_checks;
245         parent->causal_checks = dep;
246         noitL(noit_debug, "Causal map %s`%s --> %s`%s\n",
247               parent->target, parent->name, check->target, check->name);
248       }
249     }
250   }
251 }
252 void
253 noit_poller_reload(const char *xpath)
254 {
255   noit_poller_process_checks(xpath ? xpath : "/noit/checks//check");
256   noit_poller_make_causal_map();
257   noit_poller_initiate();
258 }
259 void
260 noit_poller_init() {
261   noit_skiplist_init(&polls_by_name);
262   noit_skiplist_set_compare(&polls_by_name, __check_name_compare,
263                             __check_name_compare);
264   register_console_check_commands();
265   noit_poller_reload(NULL);
266 }
267
268 int
269 noit_check_update(noit_check_t *new_check,
270                   const char *target,
271                   const char *name,
272                   noit_hash_table *config,
273                   u_int32_t period,
274                   u_int32_t timeout,
275                   const char *oncheck,
276                   noit_conf_boolean disabled) {
277   int8_t family;
278   int rv;
279   union {
280     struct in_addr addr4;
281     struct in6_addr addr6;
282   } a;
283
284
285   family = AF_INET;
286   rv = inet_pton(family, target, &a);
287   if(rv != 1) {
288     family = AF_INET6;
289     rv = inet_pton(family, target, &a);
290     if(rv != 1) {
291       noitL(noit_stderr, "Cannot translate '%s' to IP\n", target);
292       memset(&a, 0, sizeof(a));
293       disabled = noit_true;
294     }
295   }
296
297   new_check->generation = __config_load_generation;
298   new_check->target_family = family;
299   memcpy(&new_check->target_addr, &a, sizeof(a));
300   if(new_check->target) free(new_check->target);
301   new_check->target = strdup(target);
302   if(new_check->name) free(new_check->name);
303   new_check->name = name ? strdup(name): NULL;
304
305   if(config != NULL) {
306     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
307     const char *k;
308     int klen;
309     void *data;
310     if(new_check->config) noit_hash_delete_all(new_check->config, free, free);
311     else new_check->config = calloc(1, sizeof(*new_check->config));
312     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
313       noit_hash_store(new_check->config, strdup(k), klen, strdup((char *)data));
314     }
315   }
316   if(new_check->oncheck) free(new_check->oncheck);
317   new_check->oncheck = oncheck ? strdup(oncheck) : NULL;
318   new_check->period = period;
319   new_check->timeout = timeout;
320
321   if(disabled) new_check->flags |= NP_DISABLED;
322
323   /* This remove could fail -- no big deal */
324   noit_skiplist_remove(&polls_by_name, new_check, NULL);
325
326   /* This insert could fail.. which means we have a conflict on
327    * target`name.  That should result in the check being disabled. */
328   if(!noit_skiplist_insert(&polls_by_name, new_check)) {
329     noitL(noit_stderr, "Check %s`%s disabled due to naming conflict\n",
330           new_check->target, new_check->name);
331     new_check->flags |= NP_DISABLED;
332   }
333   return 0;
334 }
335 int
336 noit_poller_schedule(const char *target,
337                      const char *module,
338                      const char *name,
339                      noit_hash_table *config,
340                      u_int32_t period,
341                      u_int32_t timeout,
342                      const char *oncheck,
343                      noit_conf_boolean disabled,
344                      uuid_t in,
345                      uuid_t out) {
346   noit_check_t *new_check;
347   new_check = calloc(1, sizeof(*new_check));
348   if(!new_check) return -1;
349
350   /* The module and the UUID can never be changed */
351   new_check->module = strdup(module);
352   if(uuid_is_null(in))
353     uuid_generate(new_check->checkid);
354   else
355     uuid_copy(new_check->checkid, in);
356
357   noit_check_update(new_check, target, name, config,
358                     period, timeout, oncheck, disabled);
359   assert(noit_hash_store(&polls,
360                          (char *)new_check->checkid, UUID_SIZE,
361                          new_check));
362   uuid_copy(out, new_check->checkid);
363
364   return 0;
365 }
366
367 int
368 noit_poller_deschedule(uuid_t in) {
369   noit_check_t *checker;
370   noit_module_t *mod;
371   if(noit_hash_retrieve(&polls,
372                         (char *)in, UUID_SIZE,
373                         (void **)&checker) == 0) {
374     return -1;
375   }
376   if(checker->flags & NP_RUNNING) {
377     checker->flags |= NP_KILLED;
378     return 0;
379   }
380   checker->flags |= NP_KILLED;
381
382   noit_skiplist_remove(&polls_by_name, checker, NULL);
383   noit_hash_delete(&polls, (char *)in, UUID_SIZE, NULL, NULL);
384
385   mod = noit_module_lookup(checker->module);
386   mod->cleanup(mod, checker);
387   if(checker->fire_event) {
388      eventer_remove(checker->fire_event);
389      eventer_free(checker->fire_event);
390      checker->fire_event = NULL;
391   }
392
393   if(checker->target) free(checker->target);
394   if(checker->module) free(checker->module);
395   if(checker->name) free(checker->name);
396   if(checker->config) {
397     noit_hash_destroy(checker->config, free, free);
398     free(checker->config);
399     checker->config = NULL;
400   }
401   free(checker);
402   return 0;
403 }
404
405 noit_check_t *
406 noit_poller_lookup(uuid_t in) {
407   noit_check_t *check;
408   if(noit_hash_retrieve(&polls,
409                         (char *)in, UUID_SIZE,
410                         (void **)&check)) {
411     return check;
412   }
413   return NULL;
414 }
415 noit_check_t *
416 noit_poller_lookup_by_name(char *target, char *name) {
417   noit_check_t *check, *tmp_check;
418   tmp_check = calloc(1, sizeof(*tmp_check));
419   tmp_check->target = target;
420   tmp_check->name = name;
421   check = noit_skiplist_find(&polls_by_name, tmp_check, NULL);
422   free(tmp_check);
423   return check;
424 }
425
426 static void
427 __free_metric(void *vm) {
428   metric_t *m = vm;
429   free(m->metric_name);
430   if(m->metric_value.i) free(m->metric_value.i);
431 }
432
433 void
434 __stats_add_metric(stats_t *newstate, metric_t *m) {
435   noit_hash_replace(&newstate->metrics, m->metric_name, strlen(m->metric_name),
436                     m, NULL, __free_metric);
437 }
438
439 void
440 noit_stats_set_metric_int(stats_t *newstate, char *name, int *value) {
441   metric_t *m = calloc(1, sizeof(*m));
442   m->metric_name = strdup(name);
443   m->metric_type = METRIC_INT;
444   if(value) {
445     m->metric_value.i = malloc(sizeof(*value));
446     *(m->metric_value.i) = *value;
447   }
448   __stats_add_metric(newstate, m);
449 }
450
451 void
452 noit_stats_set_metric_float(stats_t *newstate, char *name, float *value) {
453   metric_t *m = calloc(1, sizeof(*m));
454   m->metric_name = strdup(name);
455   m->metric_type = METRIC_FLOAT;
456   if(value) {
457     m->metric_value.f = malloc(sizeof(*value));
458     *(m->metric_value.f) = *value;
459   }
460   __stats_add_metric(newstate, m);
461 }
462
463 void
464 noit_stats_set_metric_string(stats_t *newstate, char *name, char *value) {
465   metric_t *m = calloc(1, sizeof(*m));
466   m->metric_name = strdup(name);
467   m->metric_type = METRIC_STRING;
468   m->metric_value.s = value ? strdup(value) : NULL;
469   __stats_add_metric(newstate, m);
470 }
471
472 void
473 noit_check_set_stats(struct _noit_module *module,
474                      noit_check_t *check, stats_t *newstate) {
475   int report_change = 0;
476   dep_list_t *dep;
477   if(check->stats.previous.status)
478     free(check->stats.previous.status);
479   noit_hash_destroy(&check->stats.previous.metrics, NULL, __free_metric);
480   memcpy(&check->stats.previous, &check->stats.current, sizeof(stats_t));
481   memcpy(&check->stats.current, newstate, sizeof(stats_t));
482   if(check->stats.current.status)
483     check->stats.current.status = strdup(check->stats.current.status);
484
485   /* check for state changes */
486   if(check->stats.current.available != 0 &&
487      check->stats.previous.available != 0 &&
488      check->stats.current.available != check->stats.previous.available)
489     report_change = 1;
490   if(check->stats.current.state != 0 &&
491      check->stats.previous.state != 0 &&
492      check->stats.current.state != check->stats.previous.state)
493     report_change = 1;
494
495   noitL(noit_error, "%s`%s <- [%s]\n", check->target, check->name,
496         check->stats.current.status);
497   if(report_change) {
498     noitL(noit_error, "%s`%s -> [%s:%s]\n",
499           check->target, check->name,
500           __noit_check_available_string(check->stats.current.available),
501           __noit_check_state_string(check->stats.current.state));
502   }
503   for(dep = check->causal_checks; dep; dep = dep->next) {
504     noit_module_t *mod;
505     mod = noit_module_lookup(dep->check->module);
506     assert(mod);
507     noitL(noit_debug, "Firing %s`%s in response to %s`%s\n",
508           dep->check->target, dep->check->name,
509           check->target, check->name);
510     mod->initiate_check(mod, dep->check, 1, check);
511   }
512 }
513
514 static void
515 nc_printf_check_brief(noit_console_closure_t ncct,
516                       noit_check_t *check) {
517   char out[512];
518   char uuid_str[37];
519   snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
520   uuid_unparse_lower(check->checkid, uuid_str);
521   nc_printf(ncct, "%s %s\n", uuid_str, out);
522   if(check->stats.current.status)
523     nc_printf(ncct, "\t%s\n", check->stats.current.status);
524 }
525
526 static int
527 noit_console_show_checks(noit_console_closure_t ncct,
528                          int argc, char **argv,
529                          noit_console_state_t *dstate,
530                          void *closure) {
531   struct timeval _now;
532   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
533   uuid_t key_id;
534   int klen;
535   noit_check_t *check;
536
537   gettimeofday(&_now, NULL);
538   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
539                        (void **)&check)) {
540     nc_printf_check_brief(ncct, check);
541   }
542   return 0;
543 }
544
545 static void
546 register_console_check_commands() {
547   noit_console_state_t *tl;
548   cmd_info_t *showcmd;
549
550   tl = noit_console_state_initial();
551   showcmd = noit_console_state_get_cmd(tl, "show");
552   assert(showcmd && showcmd->dstate);
553
554   noit_console_state_add_cmd(showcmd->dstate,
555     NCSCMD("checks", noit_console_show_checks, NULL, NULL));
556 }
557
Note: See TracBrowser for help on using the browser.