root/src/noit_check.c

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

oh dear, check creation?!

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