root/src/noit_check.c

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

crazy guessing of string types and autoconversion to signed/unsigned int64s and doubles

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