root/src/noit_check.c

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

implements filter sets, fixes #24

  • 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 <ctype.h>
12 #include <assert.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15
16 #include "utils/noit_log.h"
17 #include "utils/noit_hash.h"
18 #include "utils/noit_skiplist.h"
19 #include "noit_conf.h"
20 #include "noit_check.h"
21 #include "noit_module.h"
22 #include "noit_console.h"
23 #include "eventer/eventer.h"
24
25 /* 60 seconds of possible stutter */
26 #define MAX_INITIAL_STUTTER 60
27
28 static noit_hash_table polls = NOIT_HASH_EMPTY;
29 static noit_skiplist polls_by_name = { 0 };
30 static u_int32_t __config_load_generation = 0;
31 struct uuid_dummy {
32   uuid_t foo;
33 };
34
35 static void register_console_check_commands();
36
37 #define UUID_SIZE sizeof(struct uuid_dummy)
38
39 static const char *
40 __noit_check_available_string(int16_t available) {
41   switch(available) {
42     case NP_AVAILABLE:    return "available";
43     case NP_UNAVAILABLE:  return "unavailable";
44     case NP_UNKNOWN:      return "unknown";
45   }
46   return "???";
47 }
48 static const char *
49 __noit_check_state_string(int16_t state) {
50   switch(state) {
51     case NP_GOOD:         return "good";
52     case NP_BAD:          return "bad";
53     case NP_UNKNOWN:      return "unknown";
54   }
55   return "???";
56 }
57 static int __check_name_compare(const void *a, const void *b) {
58   const noit_check_t *ac = a;
59   const noit_check_t *bc = b;
60   int rv;
61   if((rv = strcmp(ac->target, bc->target)) != 0) return rv;
62   if((rv = strcmp(ac->name, bc->name)) != 0) return rv;
63   return 0;
64 }
65 int
66 noit_check_max_initial_stutter() {
67   int stutter;
68   if(!noit_conf_get_int(NULL, "/noit/checks/@max_initial_stutter", &stutter))
69     stutter = MAX_INITIAL_STUTTER;
70   return stutter * 1000;
71 }
72 void
73 noit_check_fake_last_check(noit_check_t *check,
74                            struct timeval *lc, struct timeval *_now) {
75   struct timeval now, period;
76   double r;
77   int offset, max;
78
79   r = drand48();
80   max = noit_check_max_initial_stutter();
81   offset = r * (MIN(max, check->period));
82   period.tv_sec = (check->period - offset) / 1000;
83   period.tv_usec = ((check->period - offset) % 1000) * 1000;
84   if(!_now) {
85     gettimeofday(&now, NULL);
86     _now = &now;
87   }
88   sub_timeval(*_now, period, lc);
89 }
90 void
91 noit_poller_process_checks(const char *xpath) {
92   int i, flags, cnt = 0;
93   noit_conf_section_t *sec;
94   __config_load_generation++;
95   sec = noit_conf_get_sections(NULL, xpath, &cnt);
96   for(i=0; i<cnt; i++) {
97     noit_check_t *existing_check;
98     char uuid_str[37];
99     char target[256];
100     char module[256];
101     char name[256];
102     char filterset[256];
103     char oncheck[1024] = "";
104     int no_period = 0;
105     int no_oncheck = 0;
106     int period = 0, timeout = 0;
107     noit_conf_boolean disabled = noit_false, busted = noit_false;
108     uuid_t uuid, out_uuid;
109     noit_hash_table *options;
110
111 #define NEXT(...) noitL(noit_stderr, __VA_ARGS__); continue
112 #define MYATTR(type,a,...) noit_conf_get_##type(sec[i], "@" #a, __VA_ARGS__)
113 #define INHERIT(type,a,...) \
114   noit_conf_get_##type(sec[i], "ancestor-or-self::node()/@" #a, __VA_ARGS__)
115
116     if(!MYATTR(stringbuf, uuid, uuid_str, sizeof(uuid_str))) {
117       noitL(noit_stderr, "check %d has no uuid\n", i+1);
118       continue;
119     }
120
121     if(uuid_parse(uuid_str, uuid)) {
122       noitL(noit_stderr, "check uuid: '%s' is invalid\n", uuid_str);
123       continue;
124     }
125
126     if(!INHERIT(stringbuf, target, target, sizeof(target))) {
127       noitL(noit_stderr, "check uuid: '%s' has no target\n", uuid_str);
128       busted = noit_true;
129     }
130     if(!INHERIT(stringbuf, module, module, sizeof(module))) {
131       noitL(noit_stderr, "check uuid: '%s' has no module\n", uuid_str);
132       busted = noit_true;
133     }
134
135     if(!INHERIT(stringbuf, filterset, filterset, sizeof(filterset)))
136       filterset[0] = '\0';
137
138     if(!MYATTR(stringbuf, name, name, sizeof(name)))
139       strlcpy(name, module, sizeof(name));
140
141     if(!INHERIT(int, period, &period) || period == 0)
142       no_period = 1;
143
144     if(!INHERIT(stringbuf, oncheck, oncheck, sizeof(oncheck)) || !oncheck[0])
145       no_oncheck = 1;
146
147     if(no_period && no_oncheck) {
148       noitL(noit_stderr, "check uuid: '%s' has neither period nor oncheck\n",
149             uuid_str);
150       busted = noit_true;
151     }
152     if(!(no_period || no_oncheck)) {
153       noitL(noit_stderr, "check uuid: '%s' has oncheck and period.\n",
154             uuid_str);
155       busted = noit_true;
156     }
157     if(!INHERIT(int, timeout, &timeout)) {
158       noitL(noit_stderr, "check uuid: '%s' has no timeout\n", uuid_str);
159       busted = noit_true;
160     }
161     if(!no_period && timeout >= period) {
162       noitL(noit_stderr, "check uuid: '%s' timeout > period\n", uuid_str);
163       timeout = period/2;
164     }
165     options = noit_conf_get_hash(sec[i], "config");
166
167     INHERIT(boolean, disable, &disabled);
168     flags = 0;
169     if(busted) flags |= NP_UNCONFIG;
170     if(disabled) flags |= NP_DISABLED;
171
172     if(noit_hash_retrieve(&polls, (char *)uuid, UUID_SIZE,
173                           (void **)&existing_check)) {
174       /* Once set, we can never change it. */
175       assert(!existing_check->module || !existing_check->module[0] ||
176              !strcmp(existing_check->module, module));
177       /* Only set it if it is not yet set */
178       if(!existing_check->module || !existing_check->module[0]) {
179         if(existing_check->module) free(existing_check->module);
180         existing_check->module = strdup(module);
181       }
182       noit_check_update(existing_check, target, name, filterset, options,
183                            period, timeout, oncheck[0] ? oncheck : NULL,
184                            flags);
185       noitL(noit_debug, "reloaded uuid: %s\n", uuid_str);
186     }
187     else {
188       noit_poller_schedule(target, module, name, filterset, options,
189                            period, timeout, oncheck[0] ? oncheck : NULL,
190                            flags, uuid, out_uuid);
191       noitL(noit_debug, "loaded uuid: %s\n", uuid_str);
192     }
193
194     noit_hash_destroy(options, free, free);
195     free(options);
196   }
197   if(sec) free(sec);
198 }
199
200 void
201 noit_poller_initiate() {
202   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
203   uuid_t key_id;
204   int klen;
205   noit_check_t *check;
206   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
207                        (void **)&check)) {
208     noit_module_t *mod;
209     mod = noit_module_lookup(check->module);
210     if(mod) {
211       if(NOIT_CHECK_LIVE(check))
212         continue;
213       if((check->flags & NP_DISABLED) == 0) {
214         if(mod->initiate_check)
215           mod->initiate_check(mod, check, 0, NULL);
216       }
217       else
218         noitL(noit_debug, "Skipping %s`%s, disabled.\n",
219               check->target, check->name);
220     }
221     else {
222       noitL(noit_stderr, "Cannot find module '%s'\n", check->module);
223       check->flags |= NP_DISABLED;
224     }
225   }
226 }
227
228 void
229 noit_poller_flush_epoch(int oldest_allowed) {
230   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
231   uuid_t key_id;
232   int klen;
233   noit_check_t *check, *tofree = NULL;
234
235   /* Cleanup any previous causal map */
236   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
237                        (void **)&check)) {
238     /* We don't free the one we're looking at... we free it on the next
239      * pass.  This leaves out iterator in good shape.  We just need to
240      * remember to free it one last time outside the while loop, down...
241      */
242     if(tofree) {
243       noit_poller_deschedule(tofree->checkid);
244       tofree = NULL;
245     }
246     if(check->generation < oldest_allowed) {
247       tofree = check;
248     }
249   }
250   /* ... here */
251   if(tofree) noit_poller_deschedule(tofree->checkid);
252 }
253
254 void
255 noit_poller_make_causal_map() {
256   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
257   uuid_t key_id;
258   int klen;
259   noit_check_t *check, *parent;
260
261   /* Cleanup any previous causal map */
262   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
263                        (void **)&check)) {
264     dep_list_t *dep;
265     while((dep = check->causal_checks) != NULL) {
266       check->causal_checks = dep->next;
267       free(dep);
268     }
269   }
270
271   memset(&iter, 0, sizeof(iter));
272   /* Walk all checks and add check dependencies to their parents */
273   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
274                        (void **)&check)) {
275     if(check->oncheck) {
276       /* This service is causally triggered by another service */
277       char fullcheck[1024];
278       char *name = check->oncheck;
279       char *target = NULL;
280
281       noitL(noit_debug, "Searching for upstream trigger on %s\n", name);
282       if((target = strchr(check->oncheck, '`')) != NULL) {
283         strlcpy(fullcheck, check->oncheck, target - check->oncheck);
284         name = target + 1;
285         target = fullcheck;
286       }
287       else
288        target = check->target;
289
290       parent = noit_poller_lookup_by_name(target, name);
291       if(!parent) {
292         check->flags |= NP_DISABLED;
293         noitL(noit_stderr, "Disabling check %s`%s, can't find oncheck %s`%s\n",
294               check->target, check->name, target, name);
295       }
296       else {
297         dep_list_t *dep;
298         dep = malloc(sizeof(*dep));
299         dep->check = check;
300         dep->next = parent->causal_checks;
301         parent->causal_checks = dep;
302         noitL(noit_debug, "Causal map %s`%s --> %s`%s\n",
303               parent->target, parent->name, check->target, check->name);
304       }
305     }
306   }
307 }
308 void
309 noit_poller_reload(const char *xpath)
310 {
311   noit_poller_process_checks(xpath ? xpath : "/noit/checks//check");
312   if(!xpath) {
313     /* Full reload, we need to wipe old checks */
314     noit_poller_flush_epoch(__config_load_generation);
315   }
316   noit_poller_make_causal_map();
317   noit_poller_initiate();
318 }
319 void
320 noit_poller_init() {
321   noit_skiplist_init(&polls_by_name);
322   noit_skiplist_set_compare(&polls_by_name, __check_name_compare,
323                             __check_name_compare);
324   register_console_check_commands();
325   noit_poller_reload(NULL);
326 }
327
328 int
329 noit_check_update(noit_check_t *new_check,
330                   const char *target,
331                   const char *name,
332                   const char *filterset,
333                   noit_hash_table *config,
334                   u_int32_t period,
335                   u_int32_t timeout,
336                   const char *oncheck,
337                   int flags) {
338   int8_t family;
339   int rv;
340   int mask = NP_DISABLED | NP_UNCONFIG;
341   union {
342     struct in_addr addr4;
343     struct in6_addr addr6;
344   } a;
345
346
347   family = AF_INET;
348   rv = inet_pton(family, target, &a);
349   if(rv != 1) {
350     family = AF_INET6;
351     rv = inet_pton(family, target, &a);
352     if(rv != 1) {
353       noitL(noit_stderr, "Cannot translate '%s' to IP\n", target);
354       memset(&a, 0, sizeof(a));
355       flags |= (NP_UNCONFIG & NP_DISABLED);
356     }
357   }
358
359   new_check->generation = __config_load_generation;
360   new_check->target_family = family;
361   memcpy(&new_check->target_addr, &a, sizeof(a));
362   if(new_check->target) free(new_check->target);
363   new_check->target = strdup(target);
364   if(new_check->name) free(new_check->name);
365   new_check->name = name ? strdup(name): NULL;
366   if(new_check->filterset) free(new_check->filterset);
367   new_check->filterset = filterset ? strdup(filterset): NULL;
368
369   if(config != NULL) {
370     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
371     const char *k;
372     int klen;
373     void *data;
374     if(new_check->config) noit_hash_delete_all(new_check->config, free, free);
375     else new_check->config = calloc(1, sizeof(*new_check->config));
376     while(noit_hash_next(config, &iter, &k, &klen, &data)) {
377       noit_hash_store(new_check->config, strdup(k), klen, strdup((char *)data));
378     }
379   }
380   if(new_check->oncheck) free(new_check->oncheck);
381   new_check->oncheck = oncheck ? strdup(oncheck) : NULL;
382   new_check->period = period;
383   new_check->timeout = timeout;
384
385   /* Unset what could be set.. then set what should be set */
386   new_check->flags = (new_check->flags & ~mask) | flags;
387
388   /* This remove could fail -- no big deal */
389   noit_skiplist_remove(&polls_by_name, new_check, NULL);
390
391   /* This insert could fail.. which means we have a conflict on
392    * target`name.  That should result in the check being disabled. */
393   if(!noit_skiplist_insert(&polls_by_name, new_check)) {
394     noitL(noit_stderr, "Check %s`%s disabled due to naming conflict\n",
395           new_check->target, new_check->name);
396     new_check->flags |= NP_DISABLED;
397   }
398   noit_check_log_check(new_check);
399   return 0;
400 }
401 int
402 noit_poller_schedule(const char *target,
403                      const char *module,
404                      const char *name,
405                      const char *filterset,
406                      noit_hash_table *config,
407                      u_int32_t period,
408                      u_int32_t timeout,
409                      const char *oncheck,
410                      int flags,
411                      uuid_t in,
412                      uuid_t out) {
413   noit_check_t *new_check;
414   new_check = calloc(1, sizeof(*new_check));
415   if(!new_check) return -1;
416
417   /* The module and the UUID can never be changed */
418   new_check->module = strdup(module);
419   if(uuid_is_null(in))
420     uuid_generate(new_check->checkid);
421   else
422     uuid_copy(new_check->checkid, in);
423
424   noit_check_update(new_check, target, name, filterset, config,
425                     period, timeout, oncheck, flags);
426   assert(noit_hash_store(&polls,
427                          (char *)new_check->checkid, UUID_SIZE,
428                          new_check));
429   uuid_copy(out, new_check->checkid);
430
431   return 0;
432 }
433
434 int
435 noit_poller_deschedule(uuid_t in) {
436   noit_check_t *checker;
437   noit_module_t *mod;
438   if(noit_hash_retrieve(&polls,
439                         (char *)in, UUID_SIZE,
440                         (void **)&checker) == 0) {
441     return -1;
442   }
443   if(checker->flags & NP_RUNNING) {
444     checker->flags |= NP_KILLED;
445     return 0;
446   }
447   checker->flags |= NP_KILLED;
448
449   noit_skiplist_remove(&polls_by_name, checker, NULL);
450   noit_hash_delete(&polls, (char *)in, UUID_SIZE, NULL, NULL);
451
452   mod = noit_module_lookup(checker->module);
453   if(mod->cleanup) mod->cleanup(mod, checker);
454   if(checker->fire_event) {
455      eventer_remove(checker->fire_event);
456      free(checker->fire_event->closure);
457      eventer_free(checker->fire_event);
458      checker->fire_event = NULL;
459   }
460   if(checker->closure) free(checker->closure);
461   if(checker->target) free(checker->target);
462   if(checker->module) free(checker->module);
463   if(checker->name) free(checker->name);
464   if(checker->config) {
465     noit_hash_destroy(checker->config, free, free);
466     free(checker->config);
467     checker->config = NULL;
468   }
469   free(checker);
470   return 0;
471 }
472
473 noit_check_t *
474 noit_poller_lookup(uuid_t in) {
475   noit_check_t *check;
476   if(noit_hash_retrieve(&polls,
477                         (char *)in, UUID_SIZE,
478                         (void **)&check)) {
479     return check;
480   }
481   return NULL;
482 }
483 noit_check_t *
484 noit_poller_lookup_by_name(char *target, char *name) {
485   noit_check_t *check, *tmp_check;
486   tmp_check = calloc(1, sizeof(*tmp_check));
487   tmp_check->target = target;
488   tmp_check->name = name;
489   check = noit_skiplist_find(&polls_by_name, tmp_check, NULL);
490   free(tmp_check);
491   return check;
492 }
493
494 void
495 noit_check_stats_clear(stats_t *s) {
496   memset(s, 0, sizeof(*s));
497   s->state = NP_UNKNOWN;
498   s->available = NP_UNKNOWN;
499 }
500 static void
501 __free_metric(void *vm) {
502   metric_t *m = vm;
503   free(m->metric_name);
504   if(m->metric_value.i) free(m->metric_value.i);
505   free(m);
506 }
507
508 void
509 __stats_add_metric(stats_t *newstate, metric_t *m) {
510   noit_hash_replace(&newstate->metrics, m->metric_name, strlen(m->metric_name),
511                     m, NULL, __free_metric);
512 }
513
514 static size_t
515 noit_metric_sizes(metric_type_t type, void *value) {
516   switch(type) {
517     case METRIC_INT32:
518     case METRIC_UINT32:
519       return sizeof(int32_t);
520     case METRIC_INT64:
521     case METRIC_UINT64:
522       return sizeof(int64_t);
523     case METRIC_DOUBLE:
524       return sizeof(double);
525     case METRIC_STRING:
526       return strlen((char *)value) + 1;
527     case METRIC_GUESS:
528       break;
529   }
530   assert(type != type);
531   return 0;
532 }
533 static metric_type_t
534 noit_metric_guess_type(const char *s, void **replacement) {
535   char *copy, *cp, *trailer, *rpl;
536   int negative = 0, bigF = 0;
537   metric_type_t type = METRIC_STRING;
538
539   if(!s) return METRIC_GUESS;
540   copy = cp = strdup(s);
541
542   /* TRIM the string */
543   while(*cp && isspace(*cp)) cp++; /* ltrim */
544   s = cp; /* found a good starting point */
545   while(*cp) cp++; /* advance to \0 */
546   cp--; /* back up one */
547   while(cp > s && isspace(*cp)) *cp-- = '\0'; /* rtrim */
548
549   /* Find the first space */
550   cp = (char *)s;
551   while(*cp && !isspace(*cp)) cp++;
552   trailer = cp;
553   cp--; /* backup one */
554   if(cp > s && *cp == '%') *cp-- = '\0'; /* chop a last % is there is one */
555
556   while(*trailer && isspace(*trailer)) *trailer++ = '\0'; /* rtrim */
557
558   /* string was       '  -1.23e-01%  inodes used  ' */
559   /* copy is (~ = \0) '  -1.23e-01~  inodes used~~' */
560   /*                     ^           ^              */
561   /*                     s           trailer        */
562
563   /* So, the trailer must not contain numbers */
564   while(*trailer) { if(isdigit(*trailer)) goto notanumber; trailer++; }
565
566   /* And the 's' must be of the form:
567    *  0) may start with a sign [-+]?
568    *  1) [1-9][0-9]*
569    *  2) [0]?.[0-9]+
570    *  3) 0
571    *  4) [1-9][0-9]*.[0-9]+
572    *  5) all of the above ending with e[+-][0-9]+
573    */
574    rpl = (char *)s;
575    /* CASE 0 */
576    if(s[0] == '-' || s[0] == '+') {
577      if(s[0] == '-') negative = 1;
578      s++;
579    }
580
581    if(s[0] == '.') goto decimal; /* CASE 2 */
582    if(s[0] == '0') { /* CASE 2 & 3 */
583      s++;
584      if(!s[0]) goto scanint; /* CASE 3 */
585      if(s[0] == '.') goto decimal; /* CASE 2 */
586      goto notanumber;
587    }
588    if(s[0] >= '1' && s[0] <= '9') { /* CASE 1 & 4 */
589      s++;
590      while(isdigit(s[0])) s++; /* CASE 1 & 4 */
591      if(!s[0]) goto scanint; /* CASE 1 */
592      if(s[0] == '.') goto decimal; /* CASE 4 */
593      goto notanumber;
594    }
595    /* Not case 1,2,3,4 */
596    goto notanumber;
597
598   decimal:
599    s++;
600    if(!isdigit(s[0])) goto notanumber;
601    s++;
602    while(isdigit(s[0])) s++;
603    if(!s[0]) goto scandouble;
604    if(s[0] == 'e' || s[0] == 'E') goto exponent; /* CASE 5 */
605    goto notanumber;
606
607   exponent:
608    if(s[0] == 'E') bigF = 1; /* We want the caps variant */
609    s++;
610    if(s[0] != '-' && s[0] != '+') goto notanumber;
611    s++;
612    if(!isdigit(s[0])) goto notanumber;
613    s++;
614    while(isdigit(s[0])) s++;
615    if(!s[0]) goto scandouble;
616    goto notanumber;
617
618  scanint:
619    if(negative) {
620      int64_t *v;
621      v = calloc(1, sizeof(*v));
622      *v = strtoll(rpl, NULL, 10);
623      *replacement = v;
624      type = METRIC_INT64;
625      goto alldone;
626    }
627    else {
628      u_int64_t *v;
629      v = calloc(1, sizeof(*v));
630      *v = strtoull(rpl, NULL, 10);
631      *replacement = v;
632      type = METRIC_UINT64;
633      goto alldone;
634    }
635  scandouble:
636    {
637      double *v;
638      v = calloc(1, sizeof(*v));
639      *v = strtod(rpl, NULL);
640      *replacement = v;
641      type = METRIC_DOUBLE;
642      goto alldone;
643    }
644
645  alldone:
646  notanumber:
647   free(copy);
648   return type;
649 }
650 void
651 noit_stats_set_metric(stats_t *newstate, char *name, metric_type_t type,
652                       void *value) {
653   metric_t *m;
654   void *replacement = NULL;
655   if(type == METRIC_GUESS)
656     type = noit_metric_guess_type((char *)value, &replacement);
657   if(type == METRIC_GUESS) return;
658
659   m = calloc(1, sizeof(*m));
660   m->metric_name = strdup(name);
661   m->metric_type = type;
662   if(replacement)
663     m->metric_value.vp = replacement;
664   else if(value) {
665     size_t len;
666     len = noit_metric_sizes(type, value);
667     m->metric_value.vp = calloc(1, len);
668     memcpy(m->metric_value.vp, value, len);
669   }
670   __stats_add_metric(newstate, m);
671 }
672
673 void
674 noit_check_set_stats(struct _noit_module *module,
675                      noit_check_t *check, stats_t *newstate) {
676   int report_change = 0;
677   dep_list_t *dep;
678   if(check->stats.previous.status)
679     free(check->stats.previous.status);
680   noit_hash_destroy(&check->stats.previous.metrics, NULL, __free_metric);
681   memcpy(&check->stats.previous, &check->stats.current, sizeof(stats_t));
682   memcpy(&check->stats.current, newstate, sizeof(stats_t));
683   if(check->stats.current.status)
684     check->stats.current.status = strdup(check->stats.current.status);
685
686   /* check for state changes */
687   if(check->stats.current.available != NP_UNKNOWN &&
688      check->stats.previous.available != NP_UNKNOWN &&
689      check->stats.current.available != check->stats.previous.available)
690     report_change = 1;
691   if(check->stats.current.state != NP_UNKNOWN &&
692      check->stats.previous.state != NP_UNKNOWN &&
693      check->stats.current.state != check->stats.previous.state)
694     report_change = 1;
695
696   noitL(noit_error, "%s`%s <- [%s]\n", check->target, check->name,
697         check->stats.current.status);
698   if(report_change) {
699     noitL(noit_error, "%s`%s -> [%s:%s]\n",
700           check->target, check->name,
701           __noit_check_available_string(check->stats.current.available),
702           __noit_check_state_string(check->stats.current.state));
703   }
704
705   /* Write out our status */
706   noit_check_log_status(check);
707   /* Write out all metrics */
708   noit_check_log_metrics(check);
709
710   for(dep = check->causal_checks; dep; dep = dep->next) {
711     noit_module_t *mod;
712     mod = noit_module_lookup(dep->check->module);
713     assert(mod);
714     noitL(noit_debug, "Firing %s`%s in response to %s`%s\n",
715           dep->check->target, dep->check->name,
716           check->target, check->name);
717     mod->initiate_check(mod, dep->check, 1, check);
718   }
719 }
720
721 static void
722 nc_printf_check_brief(noit_console_closure_t ncct,
723                       noit_check_t *check) {
724   char out[512];
725   char uuid_str[37];
726   snprintf(out, sizeof(out), "%s`%s", check->target, check->name);
727   uuid_unparse_lower(check->checkid, uuid_str);
728   nc_printf(ncct, "%s %s\n", uuid_str, out);
729   if(check->stats.current.status)
730     nc_printf(ncct, "\t%s\n", check->stats.current.status);
731 }
732
733 static int
734 noit_console_show_checks(noit_console_closure_t ncct,
735                          int argc, char **argv,
736                          noit_console_state_t *dstate,
737                          void *closure) {
738   struct timeval _now;
739   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
740   uuid_t key_id;
741   int klen;
742   noit_check_t *check;
743
744   gettimeofday(&_now, NULL);
745   while(noit_hash_next(&polls, &iter, (const char **)key_id, &klen,
746                        (void **)&check)) {
747     nc_printf_check_brief(ncct, check);
748   }
749   return 0;
750 }
751
752 static void
753 register_console_check_commands() {
754   noit_console_state_t *tl;
755   cmd_info_t *showcmd;
756
757   tl = noit_console_state_initial();
758   showcmd = noit_console_state_get_cmd(tl, "show");
759   assert(showcmd && showcmd->dstate);
760
761   noit_console_state_add_cmd(showcmd->dstate,
762     NCSCMD("checks", noit_console_show_checks, NULL, NULL));
763 }
764
Note: See TracBrowser for help on using the browser.